diff --git a/Platformio/OmoteUI/core/Colors.hpp b/Platformio/OmoteUI/core/Colors.hpp index 9915650..772e04c 100644 --- a/Platformio/OmoteUI/core/Colors.hpp +++ b/Platformio/OmoteUI/core/Colors.hpp @@ -11,4 +11,6 @@ const auto BLUE = lv_color_make(0, 0, 255); const auto PURPLE = lv_color_make(128, 0, 128); const auto LILAC = lv_color_make(231, 209, 255); +const auto GREY = lv_color_make(105, 105, 105); + } // namespace UI::Color \ No newline at end of file diff --git a/Platformio/OmoteUI/core/TextStyle.hpp b/Platformio/OmoteUI/core/TextStyle.hpp new file mode 100644 index 0000000..ea80964 --- /dev/null +++ b/Platformio/OmoteUI/core/TextStyle.hpp @@ -0,0 +1,52 @@ +#pragma once +#include "Colors.hpp" +#include + +namespace UI { +class UIElement; + +class TextStyle { + friend UIElement; + +public: + TextStyle() = default; + + TextStyle &Opacity(lv_opa_t aOpacity) { + opacity = aOpacity; + return *this; + } + TextStyle &Color(lv_color_t aColor) { + color = aColor; + return *this; + } + TextStyle &Decor(lv_text_decor_t aDecor) { + decor = aDecor; + return *this; + } + TextStyle &Align(lv_text_align_t aAlignment) { + align = aAlignment; + return *this; + } + TextStyle &LetterSpacing(lv_coord_t aLetterSpacing) { + letter_space = aLetterSpacing; + return *this; + } + TextStyle &LineSpacing(lv_coord_t aLineSpacing) { + line_space = aLineSpacing; + return *this; + } + TextStyle &Font(const lv_font_t *aFont) { + font = aFont; + return *this; + } + +private: + lv_opa_t opacity = 255; + lv_color_t color = Color::WHITE; + lv_text_decor_t decor = LV_TEXT_DECOR_NONE; + lv_text_align_t align = LV_TEXT_ALIGN_CENTER; + lv_coord_t letter_space = 0; + lv_coord_t line_space = 0; + const lv_font_t *font = lv_font_default(); +}; +} // namespace UI \ No newline at end of file diff --git a/Platformio/OmoteUI/core/UIElement.cpp b/Platformio/OmoteUI/core/UIElement.cpp index 01da9a0..e79b966 100644 --- a/Platformio/OmoteUI/core/UIElement.cpp +++ b/Platformio/OmoteUI/core/UIElement.cpp @@ -68,10 +68,12 @@ void UIElement::SetContentHeight(lv_coord_t aHeight) { lv_coord_t UIElement::GetContentWidth() { auto lock = LvglResourceManger::GetInstance().scopeLock(); + lv_obj_update_layout(mLvglSelf); return lv_obj_get_content_width(mLvglSelf); } lv_coord_t UIElement::GetContentHeight() { auto lock = LvglResourceManger::GetInstance().scopeLock(); + lv_obj_update_layout(mLvglSelf); return lv_obj_get_content_height(mLvglSelf); } @@ -162,6 +164,33 @@ Padding UIElement::GetPadding(lv_style_selector_t aStyle) { .Column(lv_obj_get_style_pad_column(mLvglSelf, aStyle)); }; +void UIElement::SetTextStyle(TextStyle aNewTextStyle, + lv_style_selector_t aStyle) { + LvglResourceManger::GetInstance().AttemptNow([this, aNewTextStyle, aStyle] { + lv_obj_set_style_text_align(mLvglSelf, aNewTextStyle.align, aStyle); + lv_obj_set_style_text_color(mLvglSelf, aNewTextStyle.color, aStyle); + lv_obj_set_style_text_decor(mLvglSelf, aNewTextStyle.decor, aStyle); + lv_obj_set_style_text_font(mLvglSelf, aNewTextStyle.font, aStyle); + lv_obj_set_style_text_letter_space(mLvglSelf, aNewTextStyle.letter_space, + aStyle); + lv_obj_set_style_text_line_space(mLvglSelf, aNewTextStyle.line_space, + aStyle); + lv_obj_set_style_text_opa(mLvglSelf, aNewTextStyle.opacity, aStyle); + }); +} + +TextStyle UIElement::GetTextStyle(lv_style_selector_t aStyle) { + auto lock = LvglResourceManger::GetInstance().scopeLock(); + return TextStyle() + .Align(lv_obj_get_style_text_align(mLvglSelf, aStyle)) + .Color(lv_obj_get_style_text_color(mLvglSelf, aStyle)) + .Decor(lv_obj_get_style_text_decor(mLvglSelf, aStyle)) + .Font(lv_obj_get_style_text_font(mLvglSelf, aStyle)) + .LetterSpacing(lv_obj_get_style_text_letter_space(mLvglSelf, aStyle)) + .LineSpacing(lv_obj_get_style_text_line_space(mLvglSelf, aStyle)) + .Opacity(lv_obj_get_style_text_opa(mLvglSelf, aStyle)); +} + void UIElement::AlignTo(UIElement *anElementToAlignTo, lv_align_t anAlignment, lv_coord_t aXoffset, lv_coord_t aYOffset) { LvglResourceManger::GetInstance().AttemptNow([=] { diff --git a/Platformio/OmoteUI/core/UIElement.hpp b/Platformio/OmoteUI/core/UIElement.hpp index c31f832..20b93be 100644 --- a/Platformio/OmoteUI/core/UIElement.hpp +++ b/Platformio/OmoteUI/core/UIElement.hpp @@ -1,6 +1,7 @@ #pragma once #include "BorderOutlinePadding.hpp" +#include "TextStyle.hpp" #include "UIElementIds.hpp" #include @@ -56,6 +57,10 @@ public: lv_style_selector_t aStyle = LV_PART_MAIN); Padding GetPadding(lv_style_selector_t aStyle = LV_PART_MAIN); + virtual void SetTextStyle(TextStyle aNewStyle, + lv_style_selector_t aStyle = LV_PART_MAIN); + TextStyle GetTextStyle(lv_style_selector_t aStyle = LV_PART_MAIN); + virtual void AddElement(UIElement *anElement); ID GetID() { return mId; }; diff --git a/Platformio/OmoteUI/core/page/DisplaySettings.cpp b/Platformio/OmoteUI/core/page/DisplaySettings.cpp index bbef7cc..7d0dfa6 100644 --- a/Platformio/OmoteUI/core/page/DisplaySettings.cpp +++ b/Platformio/OmoteUI/core/page/DisplaySettings.cpp @@ -4,12 +4,25 @@ using namespace UI::Page; DisplaySettings::DisplaySettings(std::shared_ptr aDisplay) - : Base(UI::ID::Pages::DisplaySettings), mDisplay(aDisplay) { - auto slider = std::make_unique( - [this](auto aNewBrightness) { mDisplay->setBrightness(aNewBrightness); }, - 0, 255); - slider->AlignTo(this, LV_ALIGN_CENTER); - slider->SetWidth(GetContentWidth()); - slider->SetHeight(lv_pct(10)); - AddWidget(std::move(slider)); -} \ No newline at end of file + : Base(UI::ID::Pages::DisplaySettings), mDisplay(aDisplay), + mBrightnessSlider( + AddWidget(std::make_unique( + [this](auto aNewBrightness) { + mDisplay->setBrightness(aNewBrightness); + }, + 0, 255))) { + SetBgColor(Color::GREY); + auto usableWidth = GetContentWidth(); + + mBrightnessSlider->SetWidth(usableWidth - (usableWidth * 0.20f)); + mBrightnessSlider->SetHeight(lv_pct(10)); + mBrightnessSlider->AlignTo(this, LV_ALIGN_TOP_MID); +} + +void DisplaySettings::OnShow() { + mBrightnessSlider->SetValue(mDisplay->getBrightness()); +}; + +void DisplaySettings::OnHide(){ + +}; \ No newline at end of file diff --git a/Platformio/OmoteUI/core/page/DisplaySettings.hpp b/Platformio/OmoteUI/core/page/DisplaySettings.hpp index 0310bcb..7d73962 100644 --- a/Platformio/OmoteUI/core/page/DisplaySettings.hpp +++ b/Platformio/OmoteUI/core/page/DisplaySettings.hpp @@ -2,15 +2,22 @@ #include "DisplayAbstract.h" #include "PageBase.hpp" +namespace UI::Widget { +class Slider; +} + namespace UI::Page { class DisplaySettings : public Base { public: DisplaySettings(std::shared_ptr aDisplay); - void OnShow() override{}; - void OnHide() override{}; + void OnShow() override; + void OnHide() override; + + std::string GetTitle() override { return "Display Settings"; }; private: std::shared_ptr mDisplay; + Widget::Slider *mBrightnessSlider; }; } // namespace UI::Page diff --git a/Platformio/OmoteUI/core/page/PageBase.hpp b/Platformio/OmoteUI/core/page/PageBase.hpp index 7167ec3..ad86d7d 100644 --- a/Platformio/OmoteUI/core/page/PageBase.hpp +++ b/Platformio/OmoteUI/core/page/PageBase.hpp @@ -2,6 +2,7 @@ #include "UIElement.hpp" #include "WidgetBase.hpp" +#include #include namespace UI::Screen { class PopUpScreen; @@ -23,10 +24,15 @@ public: Base(lv_obj_t *aLvglSelf, ID aID); virtual ~Base() = default; + template ElementTy *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(); } + // Override to have a title associated with your page. + virtual std::string GetTitle() { return ""; }; + protected: void OnShow() override{}; void OnHide() override{}; @@ -37,4 +43,10 @@ protected: private: std::vector mWidgets; }; + +template +ElementTy *Base::AddWidget(Widget::Base::Ptr aWidget) { + return static_cast(AddWidget(std::move(aWidget))); +} + } // namespace UI::Page diff --git a/Platformio/OmoteUI/core/screen/PopUpScreen.cpp b/Platformio/OmoteUI/core/screen/PopUpScreen.cpp index d335dc6..ff0b94d 100644 --- a/Platformio/OmoteUI/core/screen/PopUpScreen.cpp +++ b/Platformio/OmoteUI/core/screen/PopUpScreen.cpp @@ -8,15 +8,25 @@ using namespace UI::Screen; PopUpScreen::PopUpScreen(Page::Base::Ptr aPage) : Screen::Base(UI::ID::Screens::PopUp), mContentPage(std::move(aPage)), mExitButton(std::make_unique( - [this] { UI::Screen::Manager::getInstance().popScreen(this); })) { + [this] { UI::Screen::Manager::getInstance().popScreen(this); })), + mTitle(std::make_unique(mContentPage->GetTitle())) { - mExitButton->SetWidth(30); - mExitButton->SetHeight(30); + mExitButton->SetWidth(lv_pct(10)); + mExitButton->SetHeight(mExitButton->GetWidth()); mExitButton->SetBgColor(Color::RED); mExitButton->AlignTo(this, LV_ALIGN_TOP_RIGHT, -5, 5); + mTitle->SetWidth(mExitButton->GetX()); + mTitle->SetHeight(mExitButton->GetHeight()); + mTitle->AlignTo(mExitButton.get(), LV_ALIGN_OUT_LEFT_BOTTOM); + mTitle->SetTextStyle(mTitle->GetTextStyle().Align(LV_TEXT_ALIGN_CENTER)); + + mContentPage->SetHeight(GetHeight() - mExitButton->GetBottom() - 5); + mContentPage->SetY(mExitButton->GetBottom() + 5); + AddElement(mContentPage.get()); AddElement(mExitButton.get()); + AddElement(mTitle.get()); } bool PopUpScreen::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) { diff --git a/Platformio/OmoteUI/core/screen/PopUpScreen.hpp b/Platformio/OmoteUI/core/screen/PopUpScreen.hpp index 973eee5..069b345 100644 --- a/Platformio/OmoteUI/core/screen/PopUpScreen.hpp +++ b/Platformio/OmoteUI/core/screen/PopUpScreen.hpp @@ -1,5 +1,6 @@ #pragma once #include "Button.hpp" +#include "Label.hpp" #include "PageBase.hpp" #include "ScreenBase.hpp" @@ -20,6 +21,7 @@ protected: private: UI::Page::Base::Ptr mContentPage; std::unique_ptr mExitButton; + std::unique_ptr mTitle; }; } // namespace UI::Screen \ No newline at end of file diff --git a/Platformio/OmoteUI/core/widget/Label.cpp b/Platformio/OmoteUI/core/widget/Label.cpp new file mode 100644 index 0000000..15021dd --- /dev/null +++ b/Platformio/OmoteUI/core/widget/Label.cpp @@ -0,0 +1,10 @@ +#include "Label.hpp" +#include "BackgroundScreen.hpp" +#include "Colors.hpp" + +using namespace UI::Widget; + +Label::Label(std::string aText) + : Base(lv_label_create(UI::Screen::BackgroundScreen::getLvInstance())) { + lv_label_set_text(LvglSelf(), aText.c_str()); +} \ No newline at end of file diff --git a/Platformio/OmoteUI/core/widget/Label.hpp b/Platformio/OmoteUI/core/widget/Label.hpp new file mode 100644 index 0000000..2698ba2 --- /dev/null +++ b/Platformio/OmoteUI/core/widget/Label.hpp @@ -0,0 +1,12 @@ +#pragma once +#include "WidgetBase.hpp" + +#include + +namespace UI::Widget { +class Label : public Base { +public: + Label(std::string aText); +}; + +} // namespace UI::Widget diff --git a/Platformio/OmoteUI/core/widget/WidgetBase.cpp b/Platformio/OmoteUI/core/widget/WidgetBase.cpp index e6728ef..4a69ed4 100644 --- a/Platformio/OmoteUI/core/widget/WidgetBase.cpp +++ b/Platformio/OmoteUI/core/widget/WidgetBase.cpp @@ -2,4 +2,7 @@ using namespace UI::Widget; -Base::Base(lv_obj_t *aLvglSelf) : UIElement(aLvglSelf) {} +Base::Base(lv_obj_t *aLvglSelf) : UIElement(aLvglSelf) { + SetWidth(lv_pct(100)); + SetHeight(lv_pct(100)); +}