half working widgets setup

This commit is contained in:
Raphael Coeffic
2021-04-04 12:08:22 +02:00
parent 618e679631
commit 5895abc3c0
13 changed files with 143 additions and 48 deletions
-2
View File
@@ -130,6 +130,4 @@ void loadDefaultLayout();
// intended for existing models // intended for existing models
void loadCustomScreens(); void loadCustomScreens();
void drawTrimsAndSliders(Layout::PersistentData * persistentData);
std::list<const LayoutFactory *> & getRegisteredLayouts(); std::list<const LayoutFactory *> & getRegisteredLayouts();
@@ -73,7 +73,6 @@ class Layout2P1: public Layout
Layout::decorate(HAS_TOPBAR(), HAS_SLIDERS(), HAS_TRIMS(), HAS_FM()); Layout::decorate(HAS_TOPBAR(), HAS_SLIDERS(), HAS_TRIMS(), HAS_FM());
} }
unsigned int getZonesCount() const override unsigned int getZonesCount() const override
{ {
return DIM(ZONES_LAYOUT_2P1); return DIM(ZONES_LAYOUT_2P1);
@@ -92,7 +91,6 @@ class Layout2P1: public Layout
if (value != newValue) { if (value != newValue) {
value = newValue; value = newValue;
// TODO call this from the Layout config window // TODO call this from the Layout config window
this->clear();
decorate(); decorate();
} }
} }
+104 -4
View File
@@ -21,6 +21,7 @@
#include <algorithm> #include <algorithm>
#include "screen_setup.h" #include "screen_setup.h"
#include "opentx.h" #include "opentx.h"
#include "view_main.h"
#define SET_DIRTY() storageDirty(EE_MODEL) #define SET_DIRTY() storageDirty(EE_MODEL)
@@ -133,6 +134,108 @@ ScreenSetupPage::ScreenSetupPage(ScreenMenu * menu, uint8_t pageIndex):
setTitle(title); setTitle(title);
} }
class SetupWidgetsPageSlot: public Button
{
public:
SetupWidgetsPageSlot(FormGroup * parent, const rect_t & rect, uint8_t pageIndex, uint8_t slotIndex):
Button(parent, rect),
pageIndex(pageIndex),
slotIndex(slotIndex)
{
setPressHandler([=](){
Menu * menu = new Menu(this);
menu->addLine("Select a widget", [=]() {
Menu * menu = new Menu(this);
for (auto factory: getRegisteredWidgets()) {
menu->addLine(factory->getName(), [=]() {
if (widget) {
widget->deleteLater();
}
// widget = factory->create(this, {1, 1, width() - 2, height() - 2}, &g_model.screenData[pageIndex].layoutData.zones[slotIndex].widgetData, true);
customScreens[pageIndex]->createWidget(slotIndex, factory);
return 0;
});
}
return 0;
});
if (widget /*g_model.screenData[pageIndex].layoutData.zones[slotIndex].widgetName[0]*/) {
menu->addLine(STR_DELETE, [=]() {
widget->deleteLater();
widget = nullptr;
return 0;
});
}
return 0;
});
}
void paint(BitmapBuffer * dc) override
{
if (hasFocus()) {
dc->drawSolidRect(0, 0, width(), height(), 1, DEFAULT_COLOR);
}
else {
dc->drawRect(0, 0, width(), height(), 1, DOTTED, DEFAULT_COLOR);
}
}
protected:
uint8_t pageIndex;
uint8_t slotIndex;
Window * widget = nullptr;
};
class SetupWidgetsPage: public FormWindow
{
public:
SetupWidgetsPage(uint8_t pageIndex):
FormWindow(MainWindow::instance(), {0, 0, LCD_W, LCD_H}, OPAQUE | FORM_FORWARD_FOCUS),
pageIndex(pageIndex)
{
Layer::push(this);
clearFocus();
auto & customScreen = customScreens[pageIndex];
for (unsigned i = 0; i < customScreen->getZonesCount(); i++) {
auto rect = customScreen->getZone(i);
new SetupWidgetsPageSlot(this, {rect.x - 1, rect.y - 1, rect.w + 2, rect.h + 2}, pageIndex, i);
}
}
void deleteLater(bool detach = true, bool trash = true) override
{
Layer::pop(this);
#if defined(HARDWARE_TOUCH)
Keyboard::hide();
#endif
Window::deleteLater(detach, trash);
}
#if defined(HARDWARE_KEYS)
void onEvent(event_t event) override
{
TRACE_WINDOWS("%s received event 0x%X", getWindowDebugString().c_str(), event);
if (event == EVT_KEY_LONG(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_EXIT)) {
killEvents(event);
deleteLater();
SET_DIRTY();
}
}
#endif
void paint(BitmapBuffer * dc) override
{
dc->clear(DEFAULT_BGCOLOR);
}
protected:
uint8_t pageIndex;
};
void ScreenSetupPage::build(FormWindow * window) void ScreenSetupPage::build(FormWindow * window)
{ {
FormGridLayout grid; FormGridLayout grid;
@@ -154,10 +257,7 @@ void ScreenSetupPage::build(FormWindow * window)
// Setup widgets button... // Setup widgets button...
setupWidgetsButton = new TextButton(window, grid.getFieldSlot(), STR_SETUP_WIDGETS); setupWidgetsButton = new TextButton(window, grid.getFieldSlot(), STR_SETUP_WIDGETS);
setupWidgetsButton->setPressHandler([=]() { setupWidgetsButton->setPressHandler([=]() {
// auto setupWidgetsPage = new SetupWidgetsPage(); new SetupWidgetsPage(pageIndex);
// setupWidgetsPage->setCloseHandler([=]() {
// setupWidgetsButton->setFocus();
// });
return 0; return 0;
}); });
grid.nextLine(); grid.nextLine();
+1
View File
@@ -242,5 +242,6 @@ MenuWindowContent * createMenuWindow(Menu * menu)
DialogWindowContent * createDialogWindow(Dialog * dialog, const rect_t & rect) DialogWindowContent * createDialogWindow(Dialog * dialog, const rect_t & rect)
{ {
TRACE("createDialogWindow [%d, %d, %d, %d]", rect.x, rect.y, rect.w, rect.h);
return new DialogWindowContent(dialog, rect); return new DialogWindowContent(dialog, rect);
} }
+1 -1
View File
@@ -43,7 +43,7 @@ const WidgetFactory * getWidgetFactory(const char * name)
return nullptr; return nullptr;
} }
Widget * loadWidget(const char * name, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData) Widget * loadWidget(const char * name, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData)
{ {
const WidgetFactory * factory = getWidgetFactory(name); const WidgetFactory * factory = getWidgetFactory(name);
if (factory) { if (factory) {
+8 -10
View File
@@ -18,11 +18,11 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
#ifndef _WIDGET_H_ #pragma once
#define _WIDGET_H_
#include <list> #include <list>
#include <string.h> #include <string.h>
#include "form.h"
#include "zone.h" #include "zone.h"
#include "debug.h" #include "debug.h"
@@ -34,15 +34,15 @@
#endif #endif
class WidgetFactory; class WidgetFactory;
class Widget : public Window class Widget : public FormField
{ {
public: public:
struct PersistentData { struct PersistentData {
ZoneOptionValueTyped options[MAX_WIDGET_OPTIONS] USE_IDX; ZoneOptionValueTyped options[MAX_WIDGET_OPTIONS] USE_IDX;
}; };
Widget(const WidgetFactory * factory, Window * parent, const rect_t & rect, PersistentData * persistentData): Widget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, PersistentData * persistentData):
Window(parent, rect), FormField(parent, rect),
factory(factory), factory(factory),
persistentData(persistentData) persistentData(persistentData)
{ {
@@ -116,7 +116,7 @@ class WidgetFactory
} }
} }
virtual Widget * create(Window * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const = 0; virtual Widget * create(FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const = 0;
protected: protected:
const char * name; const char * name;
@@ -132,7 +132,7 @@ class BaseWidgetFactory: public WidgetFactory
{ {
} }
Widget * create(Window * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const override Widget * create(FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData, bool init = true) const override
{ {
if (init) { if (init) {
initPersistentData(persistentData); initPersistentData(persistentData);
@@ -147,8 +147,6 @@ inline const ZoneOption * Widget::getOptions() const
return getFactory()->getOptions(); return getFactory()->getOptions();
} }
Widget * loadWidget(const char * name, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData); Widget * loadWidget(const char * name, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData);
std::list<const WidgetFactory *> & getRegisteredWidgets(); std::list<const WidgetFactory *> & getRegisteredWidgets();
#endif // _WIDGET_H_
+12 -1
View File
@@ -23,7 +23,7 @@
class GaugeWidget: public Widget class GaugeWidget: public Widget
{ {
public: public:
GaugeWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): GaugeWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData) Widget(factory, parent, rect, persistentData)
{ {
} }
@@ -55,15 +55,26 @@ class GaugeWidget: public Widget
dc->drawSolidFilledRect(0, 16, width(), 16, FOCUS_COLOR); dc->drawSolidFilledRect(0, 16, width(), 16, FOCUS_COLOR);
dc->drawNumber(0+width()/2, 17, percent, FONT(XS) | CUSTOM_COLOR | CENTERED, 0, nullptr, "%"); dc->drawNumber(0+width()/2, 17, percent, FONT(XS) | CUSTOM_COLOR | CENTERED, 0, nullptr, "%");
dc->invertRect(w, 16, width() - w, 16, CUSTOM_COLOR); dc->invertRect(w, 16, width() - w, 16, CUSTOM_COLOR);
if (hasFocus()) {
dc->clear(HIGHLIGHT_COLOR);
}
} }
void checkEvents() override void checkEvents() override
{ {
Widget::checkEvents();
auto newValue = getValue(persistentData->options[0].value.unsignedValue); auto newValue = getValue(persistentData->options[0].value.unsignedValue);
if (lastValue != newValue) { if (lastValue != newValue) {
lastValue = newValue; lastValue = newValue;
invalidate(); invalidate();
} }
if (!hasFocus() && g_tmr10ms < 100) {
TRACE("ICI");
setFocus(SET_FOCUS_FIRST);
}
} }
static const ZoneOption options[]; static const ZoneOption options[];
+3 -1
View File
@@ -23,7 +23,7 @@
class ModelBitmapWidget: public Widget class ModelBitmapWidget: public Widget
{ {
public: public:
ModelBitmapWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): ModelBitmapWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData), Widget(factory, parent, rect, persistentData),
buffer(nullptr), buffer(nullptr),
deps_hash(0) deps_hash(0)
@@ -65,6 +65,8 @@ class ModelBitmapWidget: public Widget
void checkEvents() override void checkEvents() override
{ {
Widget::checkEvents();
uint32_t new_hash = hash(g_model.header.bitmap, sizeof(g_model.header.bitmap)); uint32_t new_hash = hash(g_model.header.bitmap, sizeof(g_model.header.bitmap));
new_hash ^= hash(g_model.header.name, sizeof(g_model.header.name)); new_hash ^= hash(g_model.header.name, sizeof(g_model.header.name));
new_hash ^= hash(g_eeGeneral.themeName, sizeof(g_eeGeneral.themeName)); new_hash ^= hash(g_eeGeneral.themeName, sizeof(g_eeGeneral.themeName));
+2 -1
View File
@@ -28,7 +28,7 @@
class OutputsWidget: public Widget class OutputsWidget: public Widget
{ {
public: public:
OutputsWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): OutputsWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData) Widget(factory, parent, rect, persistentData)
{ {
} }
@@ -99,6 +99,7 @@ class OutputsWidget: public Widget
void checkEvents() override void checkEvents() override
{ {
Widget::checkEvents();
invalidate(); invalidate();
} }
+1 -1
View File
@@ -25,7 +25,7 @@
class TextWidget: public Widget class TextWidget: public Widget
{ {
public: public:
TextWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): TextWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData) Widget(factory, parent, rect, persistentData)
{ {
} }
+2 -1
View File
@@ -23,7 +23,7 @@
class TimerWidget: public Widget class TimerWidget: public Widget
{ {
public: public:
TimerWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): TimerWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData) Widget(factory, parent, rect, persistentData)
{ {
} }
@@ -88,6 +88,7 @@ class TimerWidget: public Widget
void checkEvents() override void checkEvents() override
{ {
Widget::checkEvents();
auto newValue = timersStates[persistentData->options[0].value.unsignedValue].val; auto newValue = timersStates[persistentData->options[0].value.unsignedValue].val;
if (lastValue != newValue) { if (lastValue != newValue) {
lastValue = newValue; lastValue = newValue;
+3 -1
View File
@@ -25,7 +25,7 @@ const coord_t NUMBERS_PADDING = 4;
class ValueWidget: public Widget class ValueWidget: public Widget
{ {
public: public:
ValueWidget(const WidgetFactory * factory, Window * parent, const rect_t & rect, Widget::PersistentData * persistentData): ValueWidget(const WidgetFactory * factory, FormGroup * parent, const rect_t & rect, Widget::PersistentData * persistentData):
Widget(factory, parent, rect, persistentData) Widget(factory, parent, rect, persistentData)
{ {
} }
@@ -112,6 +112,8 @@ class ValueWidget: public Widget
void checkEvents() override void checkEvents() override
{ {
Widget::checkEvents();
auto newValue = getValue(persistentData->options[0].value.unsignedValue); auto newValue = getValue(persistentData->options[0].value.unsignedValue);
if (lastValue != newValue) { if (lastValue != newValue) {
lastValue = newValue; lastValue = newValue;
+6 -23
View File
@@ -29,29 +29,14 @@ class WidgetsContainerInterface
{ {
public: public:
virtual unsigned int getZonesCount() const = 0; virtual unsigned int getZonesCount() const = 0;
virtual rect_t getZone(unsigned int index) const = 0; virtual rect_t getZone(unsigned int index) const = 0;
inline Widget * getWidget(unsigned int index)
{
return widgets[index];
}
inline void setWidget(unsigned int index, Widget * widget)
{
widgets[index] = widget;
}
virtual void createWidget(unsigned int index, const WidgetFactory * factory) = 0; virtual void createWidget(unsigned int index, const WidgetFactory * factory) = 0;
protected:
Widget ** widgets;
}; };
#define WIDGET_NAME_LEN 10 #define WIDGET_NAME_LEN 10
template<int N, int O> template<int N, int O>
class WidgetsContainer: public Window, public WidgetsContainerInterface class WidgetsContainer: public FormGroup, public WidgetsContainerInterface
{ {
public: public:
struct ZonePersistentData { struct ZonePersistentData {
@@ -65,7 +50,7 @@ class WidgetsContainer: public Window, public WidgetsContainerInterface
}; };
WidgetsContainer(const rect_t & rect, PersistentData * persistentData): WidgetsContainer(const rect_t & rect, PersistentData * persistentData):
Window(nullptr, rect), FormGroup(nullptr, rect, FORM_FORWARD_FOCUS),
persistentData(persistentData) persistentData(persistentData)
{ {
} }
@@ -96,7 +81,7 @@ class WidgetsContainer: public Window, public WidgetsContainerInterface
char name[WIDGET_NAME_LEN + 1]; char name[WIDGET_NAME_LEN + 1];
memset(name, 0, sizeof(name)); memset(name, 0, sizeof(name));
strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN); strncpy(name, persistentData->zones[i].widgetName, WIDGET_NAME_LEN);
// TODO widgets[i] = loadWidget(name, getZone(i), &persistentData->zones[i].widgetData); widgets[i] = loadWidget(name, this, getZone(i), &persistentData->zones[i].widgetData);
} }
else { else {
widgets[i] = nullptr; widgets[i] = nullptr;
@@ -115,11 +100,9 @@ class WidgetsContainer: public Window, public WidgetsContainerInterface
virtual void background() virtual void background()
{ {
if (widgets) { for (int i = 0; i < N; i++) {
for (int i = 0; i < N; i++) { if (widgets[i]) {
if (widgets[i]) { widgets[i]->background();
widgets[i]->background();
}
} }
} }
} }