[Horus] Widgets can be written in Lua

This commit is contained in:
Bertrand Songis
2016-02-18 22:10:27 +01:00
parent 01ef176173
commit d1be1b4ea0
12 changed files with 354 additions and 186 deletions
+7 -5
View File
@@ -31,10 +31,12 @@ void Layout::load()
{
unsigned int count = getZonesCount();
for (unsigned int i=0; i<count; i++) {
char name[sizeof(persistentData->zones[i].widgetName)+1];
memset(name, 0, sizeof(name));
strncpy(name, persistentData->zones[i].widgetName, sizeof(persistentData->zones[i].widgetName));
widgets[i] = loadWidget(name, getZone(i), &persistentData->zones[i].widgetData);
if (persistentData->zones[i].widgetName[0]) {
char name[sizeof(persistentData->zones[i].widgetName)+1];
memset(name, 0, sizeof(name));
strncpy(name, persistentData->zones[i].widgetName, sizeof(persistentData->zones[i].widgetName));
widgets[i] = loadWidget(name, getZone(i), &persistentData->zones[i].widgetData);
}
}
}
@@ -88,4 +90,4 @@ void loadCustomScreens()
if (customScreens[0] == NULL) {
customScreens[0] = registeredLayouts[0]->create(&g_model.screenData[0].layoutData);
}
}
}
+8
View File
@@ -50,6 +50,14 @@ class Layout
memset(widgets, 0, sizeof(widgets));
}
virtual ~Layout()
{
for (uint8_t i=0; i<MAX_LAYOUT_ZONES; i++) {
delete widgets[i];
}
memset(widgets, 0, sizeof(widgets));
}
const LayoutFactory * getFactory() const
{
return factory;
+2 -1
View File
@@ -355,6 +355,7 @@ bool menuScreenSetup(evt_t event)
lcdDrawText(MENUS_MARGIN_LEFT, y + FH / 2, "Layout");
const LayoutFactory * factory = editThemeChoice<const LayoutFactory>(SCREENS_SETUP_2ND_COLUMN, y, registeredLayouts, countRegisteredLayouts, currentScreen->getFactory(), needsOffsetCheck, attr, event);
if (factory) {
delete customScreens[T];
customScreens[T] = factory->create(&g_model.screenData[T].layoutData);
strncpy(g_model.screenData[T].layoutName, factory->getName(), sizeof(g_model.screenData[T].layoutName));
storageDirty(EE_MODEL);
@@ -375,7 +376,7 @@ bool menuScreenSetup(evt_t event)
default:
if (k < linesCount) {
uint8_t index = k - 3;
const ZoneOption *option = &options[index];
const ZoneOption * option = &options[index];
ZoneOptionValue value = currentScreen->getOptionValue(index);
value = editZoneOption(y, option, value, attr, event);
if (attr) {
+42 -39
View File
@@ -30,23 +30,6 @@ struct Zone
uint16_t x, y, w, h;
};
struct ZoneOption
{
enum Type {
Bool,
Integer,
String,
TextSize,
Timer,
Source,
Switch,
Color
};
const char * name;
Type type;
};
union ZoneOptionValue
{
bool boolValue;
@@ -55,6 +38,24 @@ union ZoneOptionValue
char stringValue[8];
};
struct ZoneOption
{
enum Type {
Integer,
Source,
Bool,
String,
TextSize,
Timer,
Switch,
Color
};
const char * name;
Type type;
ZoneOptionValue deflt;
};
class WidgetFactory;
class Widget
{
@@ -74,11 +75,6 @@ class Widget
{
}
virtual void init()
{
memset(persistentData, 0, sizeof(PersistentData));
}
const WidgetFactory * getFactory() const
{
return factory;
@@ -107,23 +103,39 @@ void registerWidget(const WidgetFactory * factory);
class WidgetFactory
{
public:
WidgetFactory(const char * name):
name(name)
WidgetFactory(const char * name, const ZoneOption * options):
name(name),
options(options)
{
registerWidget(this);
}
const char * getName() const
inline const char * getName() const
{
return name;
}
virtual const ZoneOption * getOptions() const = 0;
inline const ZoneOption * getOptions() const
{
return options;
}
void initPersistentData(Widget::PersistentData * persistentData) const
{
memset(persistentData, 0, sizeof(Widget::PersistentData));
if (options) {
int i = 0;
for (const ZoneOption * option = options; option->name; option++) {
persistentData->options[i++] = option->deflt;
}
}
}
virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData, bool init=true) const = 0;
protected:
const char * name;
const ZoneOption * options;
};
template<class T>
@@ -131,27 +143,18 @@ class BaseWidgetFactory: public WidgetFactory
{
public:
BaseWidgetFactory(const char * name, const ZoneOption * options):
WidgetFactory(name),
options(options)
WidgetFactory(name, options)
{
}
virtual const ZoneOption * getOptions() const
{
return options;
}
virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData, bool init=true) const
{
Widget * widget = new T(this, zone, persistentData);
if (init) {
widget->init();
initPersistentData(persistentData);
}
return widget;
}
protected:
const ZoneOption * options;
return new T(this, zone, persistentData);
}
};
#define MAX_REGISTERED_WIDGETS 10
+4 -12
View File
@@ -28,24 +28,16 @@ class GaugeWidget: public Widget
{
}
virtual void init()
{
persistentData->options[0].unsignedValue = 1;
persistentData->options[1].signedValue = -RESX;
persistentData->options[2].signedValue = RESX;
persistentData->options[3].unsignedValue = RED;
}
virtual void refresh();
static const ZoneOption options[];
};
const ZoneOption GaugeWidget::options[] = {
{ "Source", ZoneOption::Source },
{ "Min", ZoneOption::Integer },
{ "Max", ZoneOption::Integer },
{ "Color", ZoneOption::Color },
{ "Source", ZoneOption::Source, { .unsignedValue = 1 } },
{ "Min", ZoneOption::Integer, { .signedValue = -RESX } },
{ "Max", ZoneOption::Integer, { .signedValue = RESX } },
{ "Color", ZoneOption::Color, { .unsignedValue = RED } },
{ NULL, ZoneOption::Bool }
};
+3 -10
View File
@@ -28,22 +28,15 @@ class TextWidget: public Widget
{
}
virtual void init()
{
str2zchar(persistentData->options[0].stringValue, "Label", sizeof(persistentData->options[0].stringValue));
persistentData->options[1].unsignedValue = RED;
persistentData->options[2].unsignedValue = 0;
}
virtual void refresh();
static const ZoneOption options[];
};
const ZoneOption TextWidget::options[] = {
{ "Text", ZoneOption::String },
{ "Color", ZoneOption::Color },
{ "Size", ZoneOption::TextSize },
{ "Text", ZoneOption::String, { .stringValue = { '\015', '\347', '\0', '\14', '\377', '\376', '\373', '\364' } } },
{ "Color", ZoneOption::Color, { .unsignedValue = RED } },
{ "Size", ZoneOption::TextSize, { .unsignedValue = 0 } },
{ NULL, ZoneOption::Bool }
};
+1 -1
View File
@@ -34,7 +34,7 @@ class TimerWidget: public Widget
};
const ZoneOption TimerWidget::options[] = {
{ "Timer source", ZoneOption::Timer },
{ "Timer source", ZoneOption::Timer, { .unsignedValue = 0 } },
{ NULL, ZoneOption::Bool }
};
+1 -7
View File
@@ -28,19 +28,13 @@ class ValueWidget: public Widget
{
}
virtual void init()
{
persistentData->options[0].unsignedValue = 1;
persistentData->options[3].unsignedValue = RED;
}
virtual void refresh();
static const ZoneOption options[];
};
const ZoneOption ValueWidget::options[] = {
{ "Source", ZoneOption::Source },
{ "Source", ZoneOption::Source, { .unsignedValue = MIXSRC_Rud } },
{ NULL, ZoneOption::Bool }
};