diff --git a/CMakeLists.txt b/CMakeLists.txt index ebdb301..6eba973 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,14 @@ set(headers src/accessorinterface.h src/actioninterface.h + src/backinterface.h src/changevaluedisplay.h src/changevaluedisplay_bool.h src/changevaluedisplay_daylightsavingmode.h src/changevaluedisplay_sntp_sync_mode_t.h src/checkboxicon.h src/colorinterface.h + src/confirminterface.h src/display.h src/displaywithtitle.h src/fontinterface.h diff --git a/src/backinterface.h b/src/backinterface.h new file mode 100644 index 0000000..f92731b --- /dev/null +++ b/src/backinterface.h @@ -0,0 +1,24 @@ +#pragma once + +namespace espgui { + +class BackInterface +{ +public: + virtual void back() = 0; +}; + +template +class BackActionInterface : public virtual BackInterface +{ +public: + void back() override { T{}.triggered(); } +}; + +class DummyBack : public virtual BackInterface +{ +public: + void back() override {} +}; + +} // namespace espgui diff --git a/src/changevaluedisplay.h b/src/changevaluedisplay.h index 4410f84..1d99680 100644 --- a/src/changevaluedisplay.h +++ b/src/changevaluedisplay.h @@ -7,6 +7,7 @@ #include "accessorinterface.h" #include "widgets/label.h" #include "tftinstance.h" +#include "backinterface.h" namespace espgui { class ChangeValueDisplayInterface : @@ -14,6 +15,7 @@ class ChangeValueDisplayInterface : public virtual ActionInterface { using Base = DisplayWithTitle; + public: void initScreen() override; @@ -45,7 +47,8 @@ template class ChangeValueDisplay : public ChangeValueDisplayInterface, public virtual AccessorInterface, - public virtual ChangeValueDisplaySettingsInterface + public virtual ChangeValueDisplaySettingsInterface, + public virtual BackInterface { using Base = ChangeValueDisplayInterface; @@ -54,8 +57,7 @@ public: void update() override; void redraw() override; - void rotate(int offset) override; - void confirm() override; + void buttonPressed(Button button) override; int shownValue() const { return m_value; } void setShownValue(int value) { m_value = value; } @@ -70,6 +72,8 @@ private: template void ChangeValueDisplay::start() { + Base::start(); + m_value = static_cast*>(this)->getValue(); m_rotateOffset = 0; @@ -109,14 +113,16 @@ template<> void ChangeValueDisplay::redraw(); template -void ChangeValueDisplay::rotate(int offset) +void ChangeValueDisplay::buttonPressed(Button button) { - m_rotateOffset += offset; -} + Base::buttonPressed(button); -template -void ChangeValueDisplay::confirm() -{ - m_pressed = true; + switch (button) + { + case Button::Left: this->back(); break; + case Button::Right: m_pressed = true; break; + case Button::Up: m_rotateOffset--; break; + case Button::Down: m_rotateOffset++; break; + } } } // namespace espgui diff --git a/src/confirminterface.h b/src/confirminterface.h new file mode 100644 index 0000000..f72bb0f --- /dev/null +++ b/src/confirminterface.h @@ -0,0 +1,24 @@ +#pragma once + +namespace espgui { + +class ConfirmInterface +{ +public: + virtual void confirm() = 0; +}; + +template +class ConfirmActionInterface : public virtual ConfirmInterface +{ +public: + void confirm() override { T{}.triggered(); } +}; + +class DummyConfirm : public virtual ConfirmInterface +{ +public: + void confirm() override {} +}; + +} // namespace espgui diff --git a/src/display.h b/src/display.h index b32cb7b..65557b6 100644 --- a/src/display.h +++ b/src/display.h @@ -2,6 +2,7 @@ // system includes #include +#include // forward declares namespace espgui { @@ -26,58 +27,40 @@ public: } }; -class ConfirmInterface +enum Button { -public: - virtual void confirm() = 0; + Left, + Right, + Up, + Down, + ButtonMax = Down }; -class BackInterface -{ -public: - virtual void back() = 0; -}; - -template -class ConfirmActionInterface : public virtual ConfirmInterface -{ -public: - void confirm() override { T{}.triggered(); } -}; - -class DummyConfirm : public virtual ConfirmInterface -{ -public: - void confirm() override {} -}; - -template -class BackActionInterface : public virtual BackInterface -{ -public: - void back() override { T{}.triggered(); } -}; - -class DummyBack : public virtual BackInterface -{ -public: - void back() override {} -}; - -class Display : - public virtual ConfirmInterface, - public virtual BackInterface +class Display { public: virtual ~Display() = default; + //! Display comes into existance, is shown virtual void start() {} + + //! Display needs to fully initialize screen virtual void initScreen(); + + //! Display can do work needed to update correctly virtual void update() {} + + //! Display can update screen incrementally virtual void redraw() {} + + //! Display goes out of existance, is not shown anymore virtual void stop() {} - virtual void rotate(int offset) {} + virtual void rawButtonPressed(uint8_t button) = 0; + virtual void rawButtonReleased(uint8_t button) = 0; + + virtual void buttonPressed(Button button) = 0; + virtual void buttonReleased(Button button) = 0; virtual TextInterface *asTextInterface() { return nullptr; } virtual const TextInterface *asTextInterface() const { return nullptr; } diff --git a/src/menudisplay.cpp b/src/menudisplay.cpp index 41c1bd9..25a540e 100644 --- a/src/menudisplay.cpp +++ b/src/menudisplay.cpp @@ -172,15 +172,16 @@ void MenuDisplay::stop() }); } -void MenuDisplay::rotate(int offset) +void MenuDisplay::buttonPressed(Button button) { - Base::rotate(offset); - m_rotateOffset += offset; -} + Base::buttonPressed(button); -void MenuDisplay::confirm() -{ - //Base::confirm(); - m_pressed = true; + switch (button) + { + case Button::Left: this->back(); break; + case Button::Right: m_pressed = true; break; + case Button::Up: m_rotateOffset--; break; + case Button::Down: m_rotateOffset++; break; + } } } // namespace espgui diff --git a/src/menudisplay.h b/src/menudisplay.h index c1b04e8..fa3a3fa 100644 --- a/src/menudisplay.h +++ b/src/menudisplay.h @@ -14,9 +14,10 @@ #include "textinterface.h" #include "widgets/label.h" #include "menuitem.h" +#include "backinterface.h" namespace espgui { -class MenuDisplay : public DisplayWithTitle +class MenuDisplay : public DisplayWithTitle, public virtual BackInterface { using Base = DisplayWithTitle; @@ -27,8 +28,7 @@ public: void redraw() override; void stop() override; - void rotate(int offset) override; - void confirm() override; + void buttonPressed(Button button) override; MenuDisplay *asMenuDisplay() override { return this; } const MenuDisplay *asMenuDisplay() const override { return this; }