diff --git a/CMakeLists.txt b/CMakeLists.txt index f9598f5..1c9177c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(headers src/checkboxicon.h src/colorinterface.h src/display.h + src/displaywithtitle.h src/fontinterface.h src/icon.h src/iconinterface.h @@ -38,6 +39,8 @@ set(sources src/changevaluedisplay_bool.cpp src/changevaluedisplay_daylightsavingmode.cpp src/changevaluedisplay_sntp_sync_mode_t.cpp + src/display.cpp + src/displaywithtitle.cpp src/menudisplay.cpp src/screenmanager.cpp src/richtextrenderer.cpp diff --git a/src/changevaluedisplay.cpp b/src/changevaluedisplay.cpp index cb4df1c..0660d78 100644 --- a/src/changevaluedisplay.cpp +++ b/src/changevaluedisplay.cpp @@ -1,13 +1,12 @@ #include "changevaluedisplay.h" +// 3rdparty lib includes +#include + namespace espgui { void ChangeValueDisplayInterface::initScreen() { - tft.fillScreen(TFT_BLACK); - - m_titleLabel.start(); - - tft.fillRect(0, 33, tft.width(), 3, TFT_WHITE); + Base::initScreen(); tft.drawRect(25, 75, 190, 65, TFT_WHITE); m_valueLabel.start(); @@ -23,9 +22,7 @@ void ChangeValueDisplayInterface::initScreen() template<> void ChangeValueDisplay::redraw() { - tft.setTextFont(4); - tft.setTextColor(TFT_YELLOW); - m_titleLabel.redraw(text()); + Base::redraw(); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextFont(7); diff --git a/src/changevaluedisplay.h b/src/changevaluedisplay.h index a735c7a..4410f84 100644 --- a/src/changevaluedisplay.h +++ b/src/changevaluedisplay.h @@ -1,10 +1,7 @@ #pragma once -// 3rdparty lib includes -#include - // local includes -#include "display.h" +#include "displaywithtitle.h" #include "textinterface.h" #include "actioninterface.h" #include "accessorinterface.h" @@ -13,16 +10,13 @@ namespace espgui { class ChangeValueDisplayInterface : - public Display, - public virtual TextInterface, + public DisplayWithTitle, public virtual ActionInterface { + using Base = DisplayWithTitle; public: void initScreen() override; - TextInterface *asTextInterface() override { return this; } - const TextInterface *asTextInterface() const override { return this; } - ChangeValueDisplayInterface *asChangeValueDisplayInterface() override { return this; } const ChangeValueDisplayInterface *asChangeValueDisplayInterface() const override { return this; } @@ -30,7 +24,6 @@ public: virtual void setShownValue(int value) = 0; protected: - Label m_titleLabel{5, 5}; // 230, 25 Label m_valueLabel{26, 81}; // 188, 53 }; @@ -86,6 +79,8 @@ void ChangeValueDisplay::start() template void ChangeValueDisplay::update() { + Base::update(); + if (!m_pressed) { const auto rotateOffset = m_rotateOffset; @@ -103,9 +98,7 @@ void ChangeValueDisplay::update() template void ChangeValueDisplay::redraw() { - tft.setTextFont(4); - tft.setTextColor(TFT_YELLOW); - m_titleLabel.redraw(text()); + Base::redraw(); tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextFont(7); diff --git a/src/display.cpp b/src/display.cpp new file mode 100644 index 0000000..b0a63fb --- /dev/null +++ b/src/display.cpp @@ -0,0 +1,13 @@ +#include "display.h" + +// local includes +#include "tftinstance.h" + +namespace espgui { + +void Display::initScreen() +{ + tft.fillScreen(TFT_BLACK); +} + +} // namespace espgui diff --git a/src/display.h b/src/display.h index b0e7c21..b32cb7b 100644 --- a/src/display.h +++ b/src/display.h @@ -64,14 +64,17 @@ public: void back() override {} }; -class Display : public virtual ConfirmInterface, public virtual BackInterface { +class Display : + public virtual ConfirmInterface, + public virtual BackInterface +{ public: virtual ~Display() = default; - virtual void start() {}; - virtual void initScreen() {}; - virtual void update() {}; - virtual void redraw() {}; + virtual void start() {} + virtual void initScreen(); + virtual void update() {} + virtual void redraw() {} virtual void stop() {} virtual void rotate(int offset) {} diff --git a/src/displaywithtitle.cpp b/src/displaywithtitle.cpp new file mode 100644 index 0000000..e162b40 --- /dev/null +++ b/src/displaywithtitle.cpp @@ -0,0 +1,25 @@ +#include "displaywithtitle.h" + +// local includes +#include "tftinstance.h" + +namespace espgui { + +void DisplayWithTitle::initScreen() +{ + Base::initScreen(); + + m_titleLabel.start(); + tft.fillRect(0, 33, tft.width(), 3, TFT_WHITE); +} + +void DisplayWithTitle::redraw() +{ + Base::redraw(); + + tft.setTextFont(4); + tft.setTextColor(TFT_YELLOW, TFT_BLACK); + m_titleLabel.redraw(text()); +} + +} // namespace espgui diff --git a/src/displaywithtitle.h b/src/displaywithtitle.h new file mode 100644 index 0000000..bba9c06 --- /dev/null +++ b/src/displaywithtitle.h @@ -0,0 +1,27 @@ +#pragma once + +// local includes +#include "display.h" +#include "textinterface.h" +#include "widgets/label.h" + +namespace espgui { + +class DisplayWithTitle : + public Display, + public virtual TextInterface +{ + using Base = Display; + +public: + TextInterface *asTextInterface() override { return this; } + const TextInterface *asTextInterface() const override { return this; } + + void initScreen() override; + void redraw() override; + +private: + Label m_titleLabel{5, 5}; // 230, 25 +}; + +} // namespace espgui diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index e4ea8da..41c1bd9 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -6,6 +6,8 @@ namespace espgui { void MenuDisplay::start() { + Base::start(); + m_selectedIndex = 0; m_scrollOffset = 0; @@ -15,10 +17,7 @@ void MenuDisplay::start() void MenuDisplay::initScreen() { - tft.fillScreen(TFT_BLACK); - - m_titleLabel.start(); - tft.fillRect(0, 33, tft.width(), 3, TFT_WHITE); + Base::initScreen(); for (auto &label : m_labels) label.start(); @@ -34,6 +33,8 @@ void MenuDisplay::initScreen() void MenuDisplay::update() { + Base::update(); + if (!m_pressed) { const auto offset = m_rotateOffset; @@ -78,9 +79,10 @@ void MenuDisplay::update() void MenuDisplay::redraw() { + Base::redraw(); + tft.setTextFont(4); tft.setTextColor(TFT_YELLOW, TFT_BLACK); - m_titleLabel.redraw(text()); int i{0}; @@ -163,6 +165,8 @@ void MenuDisplay::redraw() void MenuDisplay::stop() { + Base::stop(); + runForEveryMenuItem([](MenuItem &item){ item.stop(); }); @@ -170,11 +174,13 @@ void MenuDisplay::stop() void MenuDisplay::rotate(int offset) { + Base::rotate(offset); m_rotateOffset += offset; } void MenuDisplay::confirm() { + //Base::confirm(); m_pressed = true; } } // namespace espgui diff --git a/src/menudisplay.h b/src/menudisplay.h index bd6147f..c1b04e8 100644 --- a/src/menudisplay.h +++ b/src/menudisplay.h @@ -10,14 +10,16 @@ #include // local includes -#include "display.h" +#include "displaywithtitle.h" #include "textinterface.h" #include "widgets/label.h" #include "menuitem.h" namespace espgui { -class MenuDisplay : public Display, public virtual TextInterface +class MenuDisplay : public DisplayWithTitle { + using Base = DisplayWithTitle; + public: void start() override; void initScreen() override; @@ -28,9 +30,6 @@ public: void rotate(int offset) override; void confirm() override; - TextInterface *asTextInterface() override { return this; } - const TextInterface *asTextInterface() const override { return this; } - MenuDisplay *asMenuDisplay() override { return this; } const MenuDisplay *asMenuDisplay() const override { return this; } @@ -94,8 +93,6 @@ protected: void setSelectedIndex(int selectedIndex) { m_selectedIndex = selectedIndex; } private: - Label m_titleLabel{5, 5}; // 230, 25 - static constexpr size_t rowCount = CONFIG_ESPGUI_MENUDISPLAY_ROWS; static constexpr auto iconWidth = 25; static constexpr auto horizontalSpacing = 10;