diff --git a/Platformio/OmoteUI/core/UIElement.cpp b/Platformio/OmoteUI/core/UIElement.cpp index c6d89d1..bec4812 100644 --- a/Platformio/OmoteUI/core/UIElement.cpp +++ b/Platformio/OmoteUI/core/UIElement.cpp @@ -6,6 +6,12 @@ UIElement::UIElement(lv_obj_t *aLvglSelf, ID aId) mLvglSelf->user_data = this; } +UIElement::~UIElement() { + if (lv_obj_is_valid(mLvglSelf)) { + lv_obj_del(mLvglSelf); + } +} + void UIElement::AddElement(UIElement *anUIElement) { lv_obj_set_parent(anUIElement->mLvglSelf, mLvglSelf); } diff --git a/Platformio/OmoteUI/core/UIElement.hpp b/Platformio/OmoteUI/core/UIElement.hpp index 50728e4..05da87a 100644 --- a/Platformio/OmoteUI/core/UIElement.hpp +++ b/Platformio/OmoteUI/core/UIElement.hpp @@ -1,7 +1,7 @@ #pragma once #include "UIElementIds.hpp" -#include "lvgl.h" +#include #include "KeyPressAbstract.hpp" @@ -11,6 +11,7 @@ class UIElement { public: UIElement(lv_obj_t *aLvglSelf, const ID aId = ID()); + virtual ~UIElement(); virtual void SetBgColor(lv_color_t value, lv_style_selector_t selector = LV_PART_MAIN); diff --git a/Platformio/OmoteUI/core/page/PageBase.cpp b/Platformio/OmoteUI/core/page/PageBase.cpp index c70d79f..b9bf568 100644 --- a/Platformio/OmoteUI/core/page/PageBase.cpp +++ b/Platformio/OmoteUI/core/page/PageBase.cpp @@ -1,5 +1,5 @@ -#include "BackgroundScreen.hpp" #include "PageBase.hpp" +#include "BackgroundScreen.hpp" using namespace UI::Page; @@ -11,8 +11,37 @@ Base::Base(lv_obj_t *aLvglSelf, ID aID) : UIElement(aLvglSelf, aID) { SetWidth(lv_pct(100)); lv_obj_set_align(aLvglSelf, LV_ALIGN_CENTER); } - -void Base::AddWidget(Widget::Base::Ptr aWidget) { +// Return non owning refrence to widget +UI::Widget::Base *Base::AddWidget(Widget::Base::Ptr aWidget) { AddElement(aWidget.get()); mWidgets.push_back(std::move(aWidget)); -} \ No newline at end of file + return mWidgets[mWidgets.size() - 1].get(); +} + +UI::Widget::Base::Ptr Base::RemoveWidget(Widget::Base *aWidgetRefrence) { + auto widgetToRemoveIter = std::find_if( + mWidgets.begin(), mWidgets.end(), [aWidgetRefrence](auto &aWidget) { + return aWidget.get() == aWidgetRefrence; + }); + if (widgetToRemoveIter != mWidgets.end()) { + auto widget = std::move(*widgetToRemoveIter); + mWidgets.erase(widgetToRemoveIter); + return widget; + } + return nullptr; +} + +bool Base::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + if (OnKeyEvent(aKeyEvent)) { + return true; + } + for (auto &widget : mWidgets) { + auto used = widget->KeyEvent(aKeyEvent); + if (used) { + return true; + } + } + return false; +}; + +bool Base::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { return false; }; \ No newline at end of file diff --git a/Platformio/OmoteUI/core/page/PageBase.hpp b/Platformio/OmoteUI/core/page/PageBase.hpp index 9f70be2..fc9ee6e 100644 --- a/Platformio/OmoteUI/core/page/PageBase.hpp +++ b/Platformio/OmoteUI/core/page/PageBase.hpp @@ -4,8 +4,10 @@ #include namespace UI::Page { +class Tab; class TabView; class Base : public UIElement { + friend Tab; // Allow Tab to Forward all Key Events to its page friend TabView; // Allow Tab view to call OnShow and OnHide Since it can show // and Hide pages by swiping public: @@ -13,16 +15,18 @@ public: Base(ID aID); Base(lv_obj_t *aLvglSelf, ID aID); + virtual ~Base() = default; - void AddWidget(Widget::Base::Ptr aWidget); + Widget::Base *AddWidget(Widget::Base::Ptr aWidget); + Widget::Base::Ptr RemoveWidget(Widget::Base *aWidgetRefrence); size_t GetNumWidgets() { return mWidgets.size(); } protected: void OnShow() override{}; void OnHide() override{}; - bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override { - return false; - }; + + bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; private: std::vector mWidgets; diff --git a/Platformio/OmoteUI/core/page/SettingsPage.cpp b/Platformio/OmoteUI/core/page/SettingsPage.cpp index e5cbfba..7ae354c 100644 --- a/Platformio/OmoteUI/core/page/SettingsPage.cpp +++ b/Platformio/OmoteUI/core/page/SettingsPage.cpp @@ -1,5 +1,5 @@ -#include "BackgroundScreen.hpp" #include "SettingsPage.hpp" +#include "BackgroundScreen.hpp" using namespace UI::Page; @@ -7,7 +7,9 @@ SettingsPage::SettingsPage() : Base(ID::Pages::Settings) { SetBgColor(lv_color_make(255, 0, 0)); } -void SettingsPage::OnShow() { +void SettingsPage::OnShow() {} + +void SettingsPage::AddSlider() { auto fakeSlider = std::make_unique( lv_slider_create(UI::Screen::BackgroundScreen::getLvInstance())); fakeSlider->SetHeight(lv_pct(10)); @@ -16,5 +18,36 @@ void SettingsPage::OnShow() { auto sliderHeight = fakeSlider->GetHeight(); fakeSlider->SetY(sliderHeight * GetNumWidgets()); - AddWidget(std::move(fakeSlider)); + sliders.push_back(AddWidget(std::move(fakeSlider))); +} + +bool SettingsPage::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + using id = KeyPressAbstract::KeyId; + using eventType = KeyPressAbstract::KeyEvent::Type; + bool used = true; + switch (aKeyEvent.mId) { + case id::Aux1: + if (aKeyEvent.mType == eventType::Press) { + AddSlider(); + } + break; + case id::Aux2: + if (aKeyEvent.mType == eventType::Release) { + if (sliders.size() > 0) { + auto widget = RemoveWidget(sliders[0]); + sliders.erase( + sliders.begin()); // sliders is non owning so after removing delete + // it from non owning array + } + } + break; + case id::Aux3: + break; + case id::Aux4: + break; + default: + used = false; + break; + } + return used; } \ No newline at end of file diff --git a/Platformio/OmoteUI/core/page/SettingsPage.hpp b/Platformio/OmoteUI/core/page/SettingsPage.hpp index 5f8af77..c2d5f7b 100644 --- a/Platformio/OmoteUI/core/page/SettingsPage.hpp +++ b/Platformio/OmoteUI/core/page/SettingsPage.hpp @@ -5,7 +5,13 @@ class SettingsPage : public Base { public: SettingsPage(); + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + + void AddSlider(); + protected: void OnShow() override; + + std::vector sliders; }; } // namespace UI::Page diff --git a/Platformio/OmoteUI/core/page/TabView.cpp b/Platformio/OmoteUI/core/page/TabView.cpp index 70334ef..2bec6a3 100644 --- a/Platformio/OmoteUI/core/page/TabView.cpp +++ b/Platformio/OmoteUI/core/page/TabView.cpp @@ -1,5 +1,5 @@ -#include "BackgroundScreen.hpp" #include "TabView.hpp" +#include "BackgroundScreen.hpp" #include using namespace UI::Page; @@ -13,6 +13,16 @@ void Tab::GiveContent(Page::Base::Ptr aContent) { Base::Ptr Tab::TakeContent() { return std::move(mContent); } +bool Tab::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + return mContent->OnKeyEvent(aKeyEvent); +} + +bool Tab::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + return mContent->KeyEvent(aKeyEvent); +} + +/////////////////////TabView///////////////////////////////////// + TabView::TabView(ID aId) : Base(lv_tabview_create(Screen::BackgroundScreen::getLvInstance(), LV_DIR_TOP, 0), @@ -59,4 +69,11 @@ void TabView::HandleTabChangeImpl(lv_event_t *aTabChangeEvent) { if (self) { self->HandleTabChange(); } -} \ No newline at end of file +} + +bool TabView::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + if (OnKeyEvent(aKeyEvent)) { + return true; + } + return mTabs[GetCurrentTabIdx()]->KeyEvent(aKeyEvent); +}; diff --git a/Platformio/OmoteUI/core/page/TabView.hpp b/Platformio/OmoteUI/core/page/TabView.hpp index be88e7e..a977af2 100644 --- a/Platformio/OmoteUI/core/page/TabView.hpp +++ b/Platformio/OmoteUI/core/page/TabView.hpp @@ -2,8 +2,11 @@ #include namespace UI::Page { +class TabView; class Tab : public Base { + friend TabView; + public: typedef std::unique_ptr Ptr; @@ -12,6 +15,9 @@ public: void GiveContent(Page::Base::Ptr aContent); [[nodiscard]] Base::Ptr TakeContent(); + bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + private: Base::Ptr mContent; }; @@ -25,6 +31,8 @@ public: void SetCurrentTabIdx(uint16_t aTabToSetActive, lv_anim_enable_t aIsDoAnimation = LV_ANIM_ON); + bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + protected: void OnShow() {} void OnHide() {} diff --git a/Platformio/OmoteUI/core/screen/HomeScreen.cpp b/Platformio/OmoteUI/core/screen/HomeScreen.cpp index 1cf314d..d8a43c2 100644 --- a/Platformio/OmoteUI/core/screen/HomeScreen.cpp +++ b/Platformio/OmoteUI/core/screen/HomeScreen.cpp @@ -18,4 +18,15 @@ HomeScreen::HomeScreen() void HomeScreen::SetBgColor(lv_color_t value, lv_style_selector_t selector) { mTabView.SetBgColor(value, selector); UI::UIElement::SetBgColor(value, selector); -} \ No newline at end of file +} + +bool HomeScreen::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + return false; +}; + +bool HomeScreen::KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { + if (OnKeyEvent(aKeyEvent)) { + return true; + } + return mTabView.KeyEvent(aKeyEvent); +}; diff --git a/Platformio/OmoteUI/core/screen/HomeScreen.hpp b/Platformio/OmoteUI/core/screen/HomeScreen.hpp index 68bfd5b..b211d14 100644 --- a/Platformio/OmoteUI/core/screen/HomeScreen.hpp +++ b/Platformio/OmoteUI/core/screen/HomeScreen.hpp @@ -12,6 +12,11 @@ public: void SetBgColor(lv_color_t value, lv_style_selector_t selector = LV_PART_MAIN) override; + bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + +protected: + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; + private: Page::TabView mTabView; }; diff --git a/Platformio/OmoteUI/core/screen/ScreenBase.cpp b/Platformio/OmoteUI/core/screen/ScreenBase.cpp index d186f48..b159dcc 100644 --- a/Platformio/OmoteUI/core/screen/ScreenBase.cpp +++ b/Platformio/OmoteUI/core/screen/ScreenBase.cpp @@ -4,7 +4,6 @@ using namespace UI::Screen; Base::Base(ID aId) : UIElement(lv_obj_create(NULL), aId) {} - void Base::Show() { lv_scr_load_anim(LvglSelf(), mPushAnimation, 1000, 1000, false); UIElement::Show(); @@ -12,4 +11,6 @@ void Base::Show() { void Base::SetPushAnimation(lv_scr_load_anim_t aShowAnimation) { mPushAnimation = aShowAnimation; -} \ No newline at end of file +} + +bool Base::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { return false; }; \ No newline at end of file diff --git a/Platformio/OmoteUI/core/screen/ScreenBase.hpp b/Platformio/OmoteUI/core/screen/ScreenBase.hpp index 352d592..7a3db70 100644 --- a/Platformio/OmoteUI/core/screen/ScreenBase.hpp +++ b/Platformio/OmoteUI/core/screen/ScreenBase.hpp @@ -23,9 +23,7 @@ protected: void Show() override; void OnShow() override{}; void OnHide() override{}; - bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override { - return false; - }; + bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override; private: lv_scr_load_anim_t mPushAnimation = LV_SCR_LOAD_ANIM_NONE; diff --git a/Platformio/OmoteUI/core/widget/WidgetBase.hpp b/Platformio/OmoteUI/core/widget/WidgetBase.hpp index 357a4dc..a7eeb8d 100644 --- a/Platformio/OmoteUI/core/widget/WidgetBase.hpp +++ b/Platformio/OmoteUI/core/widget/WidgetBase.hpp @@ -2,13 +2,22 @@ #include "UIElement.hpp" #include + +namespace UI::Page { +class Base; +} + namespace UI::Widget { class Base : public UIElement { + + friend class UI::Page::Base; + public: typedef std::unique_ptr Ptr; Base(lv_obj_t *aLvglSelf); + virtual ~Base() override = default; protected: void OnShow() override{};