diff --git a/CMakeLists.txt b/CMakeLists.txt index 17c2210..0372e2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ set(headers src/iconinterface.h src/menudisplay.h src/menuitem.h + src/messagepopupdisplay.h src/popupdisplay.h src/richtextrenderer.h src/screenmanager.h @@ -63,6 +64,7 @@ set(sources src/displaywithtitle.cpp src/graphdisplay.cpp src/menudisplay.cpp + src/messagepopupdisplay.cpp src/screenmanager.cpp src/splitgraphdisplay.cpp src/popupdisplay.cpp diff --git a/src/messagepopupdisplay.cpp b/src/messagepopupdisplay.cpp new file mode 100644 index 0000000..c847f6a --- /dev/null +++ b/src/messagepopupdisplay.cpp @@ -0,0 +1,96 @@ +#include "messagepopupdisplay.h" + +// 3rdparty lib includes +#include +#include +#include + +namespace espgui { + +MessagePopupDisplay::MessagePopupDisplay(std::string &&message, std::unique_ptr &&lastDisplay) : + PopupDisplay{std::move(lastDisplay)}, + m_message{std::move(message)} +{ +} + +void MessagePopupDisplay::buttonPressed(Button button) +{ + //Base::buttonPressed(button); + + switch (button) + { + using espgui::Button; + case Button::Left: + case Button::Right: + closeOverlay(); + break; + default:; + } +} + +void MessagePopupDisplay::initOverlay() +{ + const auto leftMargin = 20; + const auto rightMargin = leftMargin; + const auto topMargin = tft.height() > 300 ? 50 : 20; + const auto bottomMargin = topMargin; + + const auto width = tft.width() - leftMargin - rightMargin; + const auto height = tft.height() - topMargin - bottomMargin; + const auto right = tft.width() - rightMargin; + const auto bottom = tft.height() - bottomMargin; + + CPP_UNUSED(right) + + tft.drawSunkenRect(leftMargin, topMargin, width, height, + color565(240, 240, 240), + color565(100, 100, 100), + color565(30, 30, 30)); + + tft.setTextColor(TFT_WHITE, color565(30, 30, 30)); + + int x = leftMargin + 5; + int y = topMargin + 5; + for (char c : m_message) + { + if (c == '\n' || x > tft.width() - rightMargin - 10) + { + x = leftMargin + 5; + y += tft.fontHeight(4); + } + + if (c != '\n') + { + const auto addedWidth = tft.drawChar(tft.decodeUTF8(c), x, y, 4); + x += addedWidth; + } + + if (y >= tft.height() - bottomMargin) + break; + } + + tft.setTextColor(TFT_BLACK, color565(170, 170, 170)); + + if constexpr (false) + { + tft.drawSunkenRect(leftMargin + 15, bottom - 40, + (width - 15 - 10 - 15) / 2, + 30, + color565(240, 240, 240), + color565(100, 100, 100), + color565(170, 170, 170)); + + tft.drawString("Retry", leftMargin + 18, bottom - 37); + } + + tft.drawSunkenRect(leftMargin + 15 + ((width - 15 - 30 - 15) / 2) + 15, bottom - 40, + (width - 15 - 10 - 15) / 2, + 30, + color565(240, 240, 240), + color565(100, 100, 100), + color565(170, 170, 170)); + + tft.drawString("Ok", leftMargin + 18 + ((width - 15 - 30 - 15) / 2) + 15 + 1, bottom - 37); +} + +} // namespace espgui diff --git a/src/messagepopupdisplay.h b/src/messagepopupdisplay.h new file mode 100644 index 0000000..0ab46ba --- /dev/null +++ b/src/messagepopupdisplay.h @@ -0,0 +1,23 @@ +#pragma once + +// local includes +#include "popupdisplay.h" + +namespace espgui { + +class MessagePopupDisplay : public PopupDisplay +{ + using Base = PopupDisplay; + +public: + MessagePopupDisplay(std::string &&message, std::unique_ptr &&lastDisplay); + + void buttonPressed(Button button) override; + + void initOverlay() override; + +private: + std::string m_message; +}; + +} // namespace espgui diff --git a/src/popupdisplay.cpp b/src/popupdisplay.cpp index 5fc783b..cb1cd71 100644 --- a/src/popupdisplay.cpp +++ b/src/popupdisplay.cpp @@ -3,12 +3,11 @@ // 3rdparty lib includes #include #include -#include namespace espgui { -PopupDisplay::PopupDisplay(std::string &&message, std::unique_ptr &&lastDisplay) : - m_message{std::move(message)}, m_lastDisplay{std::move(lastDisplay)} +PopupDisplay::PopupDisplay(std::unique_ptr &&lastDisplay) : + m_lastDisplay{std::move(lastDisplay)} { } @@ -21,86 +20,6 @@ void PopupDisplay::initScreen() initOverlay(); } -void PopupDisplay::buttonPressed(espgui::Button button) -{ - //Base::buttonPressed(button); - - switch (button) - { - using espgui::Button; - case Button::Left: - case Button::Right: - closeOverlay(); - break; - default:; - } -} - -void PopupDisplay::initOverlay() -{ - const auto leftMargin = 20; - const auto rightMargin = leftMargin; - const auto topMargin = tft.height() > 300 ? 50 : 20; - const auto bottomMargin = topMargin; - - const auto width = espgui::tft.width() - leftMargin - rightMargin; - const auto height = espgui::tft.height() - topMargin - bottomMargin; - const auto right = espgui::tft.width() - rightMargin; - const auto bottom = espgui::tft.height() - bottomMargin; - - CPP_UNUSED(right) - - espgui::tft.drawSunkenRect(leftMargin, topMargin, width, height, - color565(240, 240, 240), - color565(100, 100, 100), - color565(30, 30, 30)); - - espgui::tft.setTextColor(TFT_WHITE, color565(30, 30, 30)); - - int x = leftMargin + 5; - int y = topMargin + 5; - for (char c : m_message) - { - if (c == '\n' || x > espgui::tft.width() - rightMargin - 10) - { - x = leftMargin + 5; - y += espgui::tft.fontHeight(4); - } - - if (c != '\n') - { - const auto addedWidth = espgui::tft.drawChar(espgui::tft.decodeUTF8(c), x, y, 4); - x += addedWidth; - } - - if (y >= espgui::tft.height() - bottomMargin) - break; - } - - espgui::tft.setTextColor(TFT_BLACK, color565(170, 170, 170)); - - if constexpr (false) - { - espgui::tft.drawSunkenRect(leftMargin + 15, bottom - 40, - (width - 15 - 10 - 15) / 2, - 30, - color565(240, 240, 240), - color565(100, 100, 100), - color565(170, 170, 170)); - - espgui::tft.drawString("Retry", leftMargin + 18, bottom - 37); - } - - espgui::tft.drawSunkenRect(leftMargin + 15 + ((width - 15 - 30 - 15) / 2) + 15, bottom - 40, - (width - 15 - 10 - 15) / 2, - 30, - color565(240, 240, 240), - color565(100, 100, 100), - color565(170, 170, 170)); - - espgui::tft.drawString("Ok", leftMargin + 18 + ((width - 15 - 30 - 15) / 2) + 15 + 1, bottom - 37); -} - void PopupDisplay::closeOverlay() { auto guard = std::move(espgui::currentDisplay); diff --git a/src/popupdisplay.h b/src/popupdisplay.h index 055cc0d..173ab29 100644 --- a/src/popupdisplay.h +++ b/src/popupdisplay.h @@ -13,21 +13,14 @@ class PopupDisplay : public Display using Base = Display; public: - PopupDisplay(std::string &&message, std::unique_ptr &&lastDisplay); + PopupDisplay(std::unique_ptr &&lastDisplay); - //void start() override; void initScreen() override; - //void update() override; - //void redraw() override; - //void stop() override; - void buttonPressed(espgui::Button button) override; - - virtual void initOverlay(); - virtual void closeOverlay(); + virtual void initOverlay() = 0; + void closeOverlay(); private: - std::string m_message; std::unique_ptr m_lastDisplay; };