From 0ca9e773b26ef2f1863c5812a34ab0808f294a05 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 18 Apr 2021 07:55:48 +0200 Subject: [PATCH] Refined WidgetsContainer interface and some names --- radio/src/gui/colorlcd/gui.h | 2 +- radio/src/gui/colorlcd/layout.cpp | 6 +- radio/src/gui/colorlcd/layout.h | 8 +-- .../colorlcd/layouts/layout_factory_impl.cpp | 13 ++-- .../colorlcd/layouts/layout_factory_impl.h | 9 +-- .../src/gui/colorlcd/layouts/topbar_impl.cpp | 2 +- radio/src/gui/colorlcd/layouts/topbar_impl.h | 9 +-- radio/src/gui/colorlcd/screen_setup.cpp | 66 +++++++++++-------- radio/src/gui/colorlcd/screen_setup.h | 2 +- radio/src/gui/colorlcd/view_main.cpp | 6 +- .../colorlcd/widgets/widgets_container_impl.h | 17 ++--- radio/src/gui/colorlcd/widgets_container.h | 7 +- 12 files changed, 66 insertions(+), 81 deletions(-) diff --git a/radio/src/gui/colorlcd/gui.h b/radio/src/gui/colorlcd/gui.h index 4b38183cc..f99867164 100644 --- a/radio/src/gui/colorlcd/gui.h +++ b/radio/src/gui/colorlcd/gui.h @@ -47,7 +47,7 @@ void drawCurveHorizontalScale(); void drawCurveCoord(int x, int y, const char * text, bool active=false); void drawCurvePoint(int x, int y, LcdFlags color); -extern WidgetsContainerInterface * customScreens[MAX_CUSTOM_SCREENS]; +extern WidgetsContainer * customScreens[MAX_CUSTOM_SCREENS]; void drawAlertBox(const char * title, const char * text, const char * action); void showAlertBox(const char * title, const char * text, const char * action, uint8_t sound); diff --git a/radio/src/gui/colorlcd/layout.cpp b/radio/src/gui/colorlcd/layout.cpp index a9441b309..6e64c7222 100644 --- a/radio/src/gui/colorlcd/layout.cpp +++ b/radio/src/gui/colorlcd/layout.cpp @@ -21,7 +21,7 @@ #include "opentx.h" #include "view_main.h" -WidgetsContainerInterface * customScreens[MAX_CUSTOM_SCREENS] = {}; +WidgetsContainer * customScreens[MAX_CUSTOM_SCREENS] = {}; std::list & getRegisteredLayouts() { @@ -49,7 +49,7 @@ const LayoutFactory * getLayoutFactory(const char * name) // // Loads a layout, but does not attach it to any window // -WidgetsContainerInterface * +WidgetsContainer * loadLayout(const char * name, LayoutPersistentData * persistentData) { const LayoutFactory * factory = getLayoutFactory(name); @@ -137,7 +137,7 @@ void loadCustomScreens() // - new screen is configured into g_model // - the new screen is returned (not attached) // -WidgetsContainerInterface * +WidgetsContainer * createCustomScreen(const LayoutFactory* factory, unsigned customScreenIndex) { if (!factory || (customScreenIndex >= MAX_CUSTOM_SCREENS)) diff --git a/radio/src/gui/colorlcd/layout.h b/radio/src/gui/colorlcd/layout.h index 54d9d1b58..f611dc034 100644 --- a/radio/src/gui/colorlcd/layout.h +++ b/radio/src/gui/colorlcd/layout.h @@ -42,15 +42,15 @@ class LayoutFactory const char * getName() const { return name; } virtual void drawThumb(BitmapBuffer * dc, uint16_t x, uint16_t y, LcdFlags flags) const = 0; virtual const ZoneOption * getOptions() const = 0; - virtual WidgetsContainerInterface * create(LayoutPersistentData * persistentData) const = 0; - virtual WidgetsContainerInterface * load(LayoutPersistentData * persistentData) const = 0; + virtual WidgetsContainer * create(LayoutPersistentData * persistentData) const = 0; + virtual WidgetsContainer * load(LayoutPersistentData * persistentData) const = 0; protected: const char * id; const char * name; }; -WidgetsContainerInterface * loadLayout(const char * name, LayoutPersistentData * persistentData); +WidgetsContainer * loadLayout(const char * name, LayoutPersistentData * persistentData); // intented for new models void loadDefaultLayout(); @@ -61,7 +61,7 @@ void loadCustomScreens(); // delete all custom screens from memory void deleteCustomScreens(); -WidgetsContainerInterface * +WidgetsContainer * createCustomScreen(const LayoutFactory* factory, unsigned customScreenIndex); // Remove custom screen from the model diff --git a/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp b/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp index f9440b443..c3a93ca1b 100644 --- a/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/layout_factory_impl.cpp @@ -29,7 +29,7 @@ Layout::Layout(const LayoutFactory * factory, PersistentData * persistentData): factory(factory), decoration(new ViewMainDecoration(this, getRect())) { - decorate(); + adjustLayout(); } void Layout::create() @@ -54,7 +54,7 @@ void Layout::paint(BitmapBuffer * dc) void Layout::checkEvents() { LayoutBase::checkEvents(); - decorate(); + adjustLayout(); uint32_t now = RTOS_GET_MS(); if (now - lastRefresh >= LAYOUT_REFRESH) { @@ -82,12 +82,7 @@ void Layout::setFlightModeVisible(bool visible) decoration->setFlightModeVisible(visible); } -void Layout::adjustDecoration() -{ - decoration->adjustDecoration(); -} - -void Layout::decorate() +void Layout::adjustLayout() { // Check if deco setting are still up-to-date uint8_t checkSettings = @@ -110,7 +105,7 @@ void Layout::decorate() setFlightModeVisible(hasFlightMode()); // Re-compute positions - adjustDecoration(); + decoration->adjustDecoration(); // and update relevant windows updateZones(); diff --git a/radio/src/gui/colorlcd/layouts/layout_factory_impl.h b/radio/src/gui/colorlcd/layouts/layout_factory_impl.h index a70562804..e858aee51 100644 --- a/radio/src/gui/colorlcd/layouts/layout_factory_impl.h +++ b/radio/src/gui/colorlcd/layouts/layout_factory_impl.h @@ -40,7 +40,7 @@ class ViewMainDecoration; -typedef WidgetsContainer LayoutBase; +typedef WidgetsContainerImpl LayoutBase; class Layout: public LayoutBase { @@ -61,7 +61,7 @@ class Layout: public LayoutBase void create() override; - const LayoutFactory * getFactory() const override + const LayoutFactory * getFactory() const { return factory; } @@ -93,11 +93,8 @@ class Layout: public LayoutBase void setSlidersVisible(bool visible); void setFlightModeVisible(bool visible); - // Re-calculate positions - void adjustDecoration(); - // Updates settings for trims, sliders, pots, etc... - void decorate() override; + void adjustLayout() override; protected: const LayoutFactory * factory = nullptr; diff --git a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp index 39d0631a0..14fe2cdb6 100644 --- a/radio/src/gui/colorlcd/layouts/topbar_impl.cpp +++ b/radio/src/gui/colorlcd/layouts/topbar_impl.cpp @@ -25,7 +25,7 @@ const char * const STR_MONTHS[] = TR_MONTHS; constexpr uint32_t TOPBAR_REFRESH = 1000 / 2; // 2 Hz TopbarImpl::TopbarImpl(Window * parent) : - WidgetsContainer({0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData) + TopbarImplBase({0, 0, LCD_W, MENU_HEADER_HEIGHT}, &g_model.topbarData) { attach(parent); } diff --git a/radio/src/gui/colorlcd/layouts/topbar_impl.h b/radio/src/gui/colorlcd/layouts/topbar_impl.h index 8147aa151..3315e8b3f 100644 --- a/radio/src/gui/colorlcd/layouts/topbar_impl.h +++ b/radio/src/gui/colorlcd/layouts/topbar_impl.h @@ -23,7 +23,7 @@ #include "topbar.h" #include "layouts/layout_factory_impl.h" -typedef WidgetsContainer TopbarImplBase; +typedef WidgetsContainerImpl TopbarImplBase; class TopbarImpl: public TopbarImplBase, @@ -54,13 +54,6 @@ class TopbarImpl: void paint(BitmapBuffer * dc) override; void checkEvents() override; - - // Fake implementation to satisfy WidgetsContainerInterface - // this should probably be get rid of (might be tricky - const LayoutFactory * getFactory() const override - { - return nullptr; - } protected: uint32_t lastRefresh = 0; diff --git a/radio/src/gui/colorlcd/screen_setup.cpp b/radio/src/gui/colorlcd/screen_setup.cpp index 0faa8ab82..77bc9ef68 100644 --- a/radio/src/gui/colorlcd/screen_setup.cpp +++ b/radio/src/gui/colorlcd/screen_setup.cpp @@ -25,6 +25,7 @@ #include "widget_settings.h" #include "topbar.h" #include "libopenui.h" +#include "layouts/layout_factory_impl.h" #define SET_DIRTY() storageDirty(EE_MODEL) @@ -108,7 +109,7 @@ class LayoutChoice: public FormField std::function setValue; }; -SetupWidgetsPageSlot::SetupWidgetsPageSlot(FormGroup * parent, const rect_t & rect, WidgetsContainerInterface* container, uint8_t slotIndex): +SetupWidgetsPageSlot::SetupWidgetsPageSlot(FormGroup * parent, const rect_t & rect, WidgetsContainer* container, uint8_t slotIndex): Button(parent, rect) { setPressHandler([parent, container, slotIndex]() -> uint8_t { @@ -353,32 +354,39 @@ static void updateLayoutOptions(FormGroup* optionsWindow, unsigned customScreenI // Layout options... int index = 0; - auto factory = customScreens[customScreenIndex]->getFactory(); - if (factory) { - for (auto * option = factory->getOptions(); option->name; option++, index++) { - - auto layoutData = g_model.screenData[customScreenIndex].layoutData; - ZoneOptionValue * value = &layoutData.options[index].value; - - // Option label - new StaticText(optionsWindow, grid.getLabelSlot(false), option->name); - - // Option value - switch (option->type) { - case ZoneOption::Bool: - new CheckBox(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->boolValue)); - break; - - case ZoneOption::Color: - new ColorEdit(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->unsignedValue)); - break; - - default: - break; - } - grid.nextLine(); - } + auto layout = dynamic_cast(customScreens[customScreenIndex]); + if (!layout) { + return; } + auto factory = layout->getFactory(); + if (!factory) { + return; + } + + for (auto * option = factory->getOptions(); option->name; option++, index++) { + + auto layoutData = g_model.screenData[customScreenIndex].layoutData; + ZoneOptionValue * value = &layoutData.options[index].value; + + // Option label + new StaticText(optionsWindow, grid.getLabelSlot(false), option->name); + + // Option value + switch (option->type) { + case ZoneOption::Bool: + new CheckBox(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->boolValue)); + break; + + case ZoneOption::Color: + new ColorEdit(optionsWindow, grid.getFieldSlot(), GET_SET_DEFAULT(value->unsignedValue)); + break; + + default: + break; + } + grid.nextLine(); + } + optionsWindow->adjustHeight(); } @@ -398,7 +406,11 @@ void ScreenSetupPage::build(FormWindow * window) rect_t optRect = {0, grid.getWindowHeight(), window->width(), 0}; auto optionsWindow = new FormGroup(window, optRect, FORWARD_SCROLL | FORM_FORWARD_FOCUS); - LayoutChoice::LayoutFactoryGetter getFactory = GET_VALUE(customScreens[idx]->getFactory()); + LayoutChoice::LayoutFactoryGetter getFactory = [idx] () -> const LayoutFactory * { + auto layout = dynamic_cast(customScreens[idx]); + if (!layout) return nullptr; + return layout->getFactory(); + }; LayoutChoice::LayoutFactorySetter setLayout = [idx, optionsWindow](const LayoutFactory *factory) { // delete any options potentially accessing // the old custom screen diff --git a/radio/src/gui/colorlcd/screen_setup.h b/radio/src/gui/colorlcd/screen_setup.h index 50767f3f3..051aa70de 100644 --- a/radio/src/gui/colorlcd/screen_setup.h +++ b/radio/src/gui/colorlcd/screen_setup.h @@ -58,7 +58,7 @@ class ScreenSetupPage: public PageTab { class SetupWidgetsPageSlot: public Button { public: - SetupWidgetsPageSlot(FormGroup * parent, const rect_t & rect, WidgetsContainerInterface* container, uint8_t slotIndex); + SetupWidgetsPageSlot(FormGroup * parent, const rect_t & rect, WidgetsContainer* container, uint8_t slotIndex); void paint(BitmapBuffer * dc) override; }; diff --git a/radio/src/gui/colorlcd/view_main.cpp b/radio/src/gui/colorlcd/view_main.cpp index 849294d8b..bbd6d2420 100644 --- a/radio/src/gui/colorlcd/view_main.cpp +++ b/radio/src/gui/colorlcd/view_main.cpp @@ -164,7 +164,7 @@ void ViewMain::updateTopbarVisibility() int leftScroll = getScrollPositionX() % pageWidth; if (leftScroll == 0) { setTopbarVisible(hasTopbar(g_model.view)); - customScreens[g_model.view]->decorate(); + customScreens[g_model.view]->adjustLayout(); } else { int leftIdx = getScrollPositionX() / pageWidth; @@ -185,8 +185,8 @@ void ViewMain::updateTopbarVisibility() } setTopbarVisible(ratio); - customScreens[leftIdx]->decorate(); - customScreens[leftIdx+1]->decorate(); + customScreens[leftIdx]->adjustLayout(); + customScreens[leftIdx+1]->adjustLayout(); } } } diff --git a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h index e0592227b..c91cee3a6 100644 --- a/radio/src/gui/colorlcd/widgets/widgets_container_impl.h +++ b/radio/src/gui/colorlcd/widgets/widgets_container_impl.h @@ -24,22 +24,13 @@ #include "widgets_container.h" template -class WidgetsContainer: public WidgetsContainerInterface +class WidgetsContainerImpl: public WidgetsContainer { public: - // struct ZonePersistentData { - // char widgetName[WIDGET_NAME_LEN]; - // Widget::PersistentData widgetData; - // }; - - // struct PersistentData { - // ZonePersistentData zones[N]; - // ZoneOptionValueTyped options[O]; - // }; typedef WidgetsContainerPersistentData PersistentData; - WidgetsContainer(const rect_t & rect, PersistentData * persistentData): - WidgetsContainerInterface(nullptr, rect, FORM_FORWARD_FOCUS), + WidgetsContainerImpl(const rect_t & rect, PersistentData * persistentData): + WidgetsContainer(nullptr, rect, FORM_FORWARD_FOCUS), persistentData(persistentData) { } @@ -146,7 +137,7 @@ class WidgetsContainer: public WidgetsContainerInterface } } - void decorate() override + void adjustLayout() override { } diff --git a/radio/src/gui/colorlcd/widgets_container.h b/radio/src/gui/colorlcd/widgets_container.h index 7ba7e1219..d02237b87 100644 --- a/radio/src/gui/colorlcd/widgets_container.h +++ b/radio/src/gui/colorlcd/widgets_container.h @@ -64,7 +64,7 @@ struct WidgetsContainerPersistentData { typedef WidgetsContainerPersistentData TopBarPersistentData; -class WidgetsContainerInterface: public FormGroup +class WidgetsContainer: public FormGroup { public: using FormGroup::FormGroup; @@ -74,10 +74,7 @@ class WidgetsContainerInterface: public FormGroup virtual Widget * createWidget(unsigned int index, const WidgetFactory * factory) = 0; virtual Widget * getWidget(unsigned int index) = 0; virtual void removeWidget(unsigned int index) = 0; - - // this should go away... - virtual const LayoutFactory * getFactory() const = 0; - virtual void decorate() = 0; + virtual void adjustLayout() = 0; };