From dfbfd7a6a87ed7ea7063bfc653c20c6920a672f3 Mon Sep 17 00:00:00 2001 From: MatthewColvin Date: Sat, 16 Sep 2023 21:43:07 -0500 Subject: [PATCH] Add visiblity API for UIElements Add ability for Screen Manager to display screens and retain others underneath in a stack fasion. --- Platformio/OmoteUI/core/ScreenManager.cpp | 11 ++++++-- Platformio/OmoteUI/core/ScreenManager.hpp | 7 ++++-- Platformio/OmoteUI/core/UIElement.cpp | 25 ++++++++++++++++++- Platformio/OmoteUI/core/UIElement.hpp | 22 +++++++++++++++- Platformio/OmoteUI/core/screen/HomeScreen.cpp | 4 +-- Platformio/OmoteUI/core/screen/ScreenBase.cpp | 9 +++++++ Platformio/OmoteUI/core/screen/ScreenBase.hpp | 12 ++++++++- Platformio/OmoteUI/core/widget/WidgetBase.hpp | 4 +++ Platformio/src/main.cpp | 6 ++--- 9 files changed, 88 insertions(+), 12 deletions(-) diff --git a/Platformio/OmoteUI/core/ScreenManager.cpp b/Platformio/OmoteUI/core/ScreenManager.cpp index 1c29861..511abc0 100644 --- a/Platformio/OmoteUI/core/ScreenManager.cpp +++ b/Platformio/OmoteUI/core/ScreenManager.cpp @@ -8,6 +8,13 @@ Manager &Manager::getInstance() { return mManager; } Manager::Manager() {} -void Manager::pushScreen(std::unique_ptr aPage) { - pages.push(std::move(aPage)); +void Manager::pushScreen(Screen::Base::Ptr aScreen, + lv_scr_load_anim_t aPushAnimation) { + aScreen->SetPushAnimation(aPushAnimation); + pushScreen(std::move(aScreen)); +} + +void Manager::pushScreen(Screen::Base::Ptr aScreen) { + mScreens.push(std::move(aScreen)); + mScreens.top()->Show(); } diff --git a/Platformio/OmoteUI/core/ScreenManager.hpp b/Platformio/OmoteUI/core/ScreenManager.hpp index c2dec2b..37cac93 100644 --- a/Platformio/OmoteUI/core/ScreenManager.hpp +++ b/Platformio/OmoteUI/core/ScreenManager.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "ScreenBase.hpp" #include #include @@ -8,13 +10,14 @@ class Manager { public: static Manager &getInstance(); - void pushScreen(std::unique_ptr aPage); + void pushScreen(Screen::Base::Ptr aScreen); + void pushScreen(Screen::Base::Ptr aScreen, lv_scr_load_anim_t aPushAnimation); private: Manager(); static Manager mManager; - std::stack> pages; + std::stack mScreens; }; } // namespace UI::Screen \ No newline at end of file diff --git a/Platformio/OmoteUI/core/UIElement.cpp b/Platformio/OmoteUI/core/UIElement.cpp index 96000ef..ccb3379 100644 --- a/Platformio/OmoteUI/core/UIElement.cpp +++ b/Platformio/OmoteUI/core/UIElement.cpp @@ -6,8 +6,31 @@ UIElement::UIElement(lv_obj_t *aLvglSelf, ID aId) mLvglSelf->user_data = this; } -UIElement::SetBgColor(lv_color_t aColor, lv_style_selector_t aStyle) { +bool UIElement::IsVisible() { return lv_obj_is_visible(mLvglSelf); } + +void UIElement::SetVisiblity(bool aVisible) { + if (aVisible == IsVisible()) { + return; + } + if (aVisible) { + Show(); + } else { + Hide(); + } +} + +void UIElement::SetBgColor(lv_color_t aColor, lv_style_selector_t aStyle) { lv_obj_set_style_bg_color(mLvglSelf, aColor, aStyle); }; +void UIElement::Show() { + lv_obj_clear_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN); + OnShow(); +} + +void UIElement::Hide() { + lv_obj_add_flag(mLvglSelf, LV_OBJ_FLAG_HIDDEN); + OnHide(); +} + } // namespace UI diff --git a/Platformio/OmoteUI/core/UIElement.hpp b/Platformio/OmoteUI/core/UIElement.hpp index 0d397f5..7f9df3d 100644 --- a/Platformio/OmoteUI/core/UIElement.hpp +++ b/Platformio/OmoteUI/core/UIElement.hpp @@ -10,9 +10,29 @@ class UIElement { public: UIElement(lv_obj_t *aLvglSelf, const ID aId = ID()); - SetBgColor(lv_color_t value, lv_style_selector_t selector = LV_PART_MAIN); + void SetBgColor(lv_color_t value, + lv_style_selector_t selector = LV_PART_MAIN); + + void SetVisiblity(bool aVisibility); + bool IsVisible(); + + lv_obj_t *operator()() { return mLvglSelf; } protected: + /// @brief get Lvgl object refernce to use in LVGL APIs + /// @return lvgl object a + lv_obj_t *LvglSelf() { return mLvglSelf; } + + /// @brief Show Element + virtual void Show(); + /// @brief Hide Element + virtual void Hide(); + /// @brief Override in child class to run something after element is shown + virtual void OnShow() = 0; + /// @brief Override in child class to run something after element is hidden + virtual void OnHide() = 0; + +private: lv_obj_t *mLvglSelf; const ID mId; }; diff --git a/Platformio/OmoteUI/core/screen/HomeScreen.cpp b/Platformio/OmoteUI/core/screen/HomeScreen.cpp index 7b414c3..9ac3a54 100644 --- a/Platformio/OmoteUI/core/screen/HomeScreen.cpp +++ b/Platformio/OmoteUI/core/screen/HomeScreen.cpp @@ -3,6 +3,6 @@ using namespace UI::Screen; HomeScreen::HomeScreen() : Base(UI::ID::Screens::Home) { - - + SetBgColor(lv_color_white()); + SetPushAnimation(LV_SCR_LOAD_ANIM_FADE_IN); } \ No newline at end of file diff --git a/Platformio/OmoteUI/core/screen/ScreenBase.cpp b/Platformio/OmoteUI/core/screen/ScreenBase.cpp index e53d778..f3d2b21 100644 --- a/Platformio/OmoteUI/core/screen/ScreenBase.cpp +++ b/Platformio/OmoteUI/core/screen/ScreenBase.cpp @@ -7,3 +7,12 @@ Base::Base(ID aId) : UIElement(lv_obj_create(NULL), aId) {} void Base::AddWidget(Widget::Base::Ptr aWidget) { mWidgets.push_back(std::move(aWidget)); } + +void Base::Show() { + lv_scr_load_anim(LvglSelf(), mPushAnimation, 1000, 1000, false); + UIElement::Show(); +} + +void Base::SetPushAnimation(lv_scr_load_anim_t aShowAnimation) { + mPushAnimation = aShowAnimation; +} \ No newline at end of file diff --git a/Platformio/OmoteUI/core/screen/ScreenBase.hpp b/Platformio/OmoteUI/core/screen/ScreenBase.hpp index 533b2d3..6eca8a7 100644 --- a/Platformio/OmoteUI/core/screen/ScreenBase.hpp +++ b/Platformio/OmoteUI/core/screen/ScreenBase.hpp @@ -7,17 +7,27 @@ #include namespace UI::Screen { +class Manager; + +class Base : public UIElement { + friend Manager; -class Base : UIElement { public: typedef std::unique_ptr Ptr; Base(ID aId); void AddWidget(Widget::Base::Ptr aWidget); + void SetPushAnimation(lv_scr_load_anim_t aPushAnimation); + +protected: + void Show() override; + void OnShow() override{}; + void OnHide() override{}; private: std::vector mWidgets; + lv_scr_load_anim_t mPushAnimation = LV_SCR_LOAD_ANIM_NONE; }; } // namespace UI::Screen \ No newline at end of file diff --git a/Platformio/OmoteUI/core/widget/WidgetBase.hpp b/Platformio/OmoteUI/core/widget/WidgetBase.hpp index b9756bc..803325f 100644 --- a/Platformio/OmoteUI/core/widget/WidgetBase.hpp +++ b/Platformio/OmoteUI/core/widget/WidgetBase.hpp @@ -10,6 +10,10 @@ public: Base(lv_obj_t *aLvglSelf); +protected: + void OnShow() override{}; + void OnHide() override{}; + private: }; diff --git a/Platformio/src/main.cpp b/Platformio/src/main.cpp index baf4d6c..ff6aa36 100644 --- a/Platformio/src/main.cpp +++ b/Platformio/src/main.cpp @@ -1,10 +1,10 @@ // OMOTE firmware for ESP32 // 2023 Maximilian Kern -#include #include "HardwareRevX.hpp" #include "OmoteUI.hpp" #include "omoteconfig.h" +#include std::shared_ptr hal = nullptr; @@ -12,7 +12,7 @@ void setup() { hal = HardwareRevX::getInstance(); hal->init(); - auto ui = OmoteUI::getInstance(hal); + auto ui = UI::Basic::OmoteUI::getInstance(hal); ui->layout_UI(); lv_timer_handler(); // Run the LVGL UI once before the loop takes over @@ -20,5 +20,5 @@ void setup() { void loop() { HardwareRevX::getInstance()->loopHandler(); - OmoteUI::getInstance()->loopHandler(); + UI::Basic::OmoteUI::getInstance()->loopHandler(); } \ No newline at end of file