Split PopupDisplay and MessagePopupDisplay

This commit is contained in:
2022-03-31 14:29:03 +02:00
parent 1fed239f6b
commit d0960025a2
5 changed files with 126 additions and 93 deletions

View File

@@ -24,6 +24,7 @@ set(headers
src/iconinterface.h src/iconinterface.h
src/menudisplay.h src/menudisplay.h
src/menuitem.h src/menuitem.h
src/messagepopupdisplay.h
src/popupdisplay.h src/popupdisplay.h
src/richtextrenderer.h src/richtextrenderer.h
src/screenmanager.h src/screenmanager.h
@@ -63,6 +64,7 @@ set(sources
src/displaywithtitle.cpp src/displaywithtitle.cpp
src/graphdisplay.cpp src/graphdisplay.cpp
src/menudisplay.cpp src/menudisplay.cpp
src/messagepopupdisplay.cpp
src/screenmanager.cpp src/screenmanager.cpp
src/splitgraphdisplay.cpp src/splitgraphdisplay.cpp
src/popupdisplay.cpp src/popupdisplay.cpp

View File

@@ -0,0 +1,96 @@
#include "messagepopupdisplay.h"
// 3rdparty lib includes
#include <tftinstance.h>
#include <screenmanager.h>
#include <cppmacros.h>
namespace espgui {
MessagePopupDisplay::MessagePopupDisplay(std::string &&message, std::unique_ptr<Display> &&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

23
src/messagepopupdisplay.h Normal file
View File

@@ -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<Display> &&lastDisplay);
void buttonPressed(Button button) override;
void initOverlay() override;
private:
std::string m_message;
};
} // namespace espgui

View File

@@ -3,12 +3,11 @@
// 3rdparty lib includes // 3rdparty lib includes
#include <tftinstance.h> #include <tftinstance.h>
#include <screenmanager.h> #include <screenmanager.h>
#include <cppmacros.h>
namespace espgui { namespace espgui {
PopupDisplay::PopupDisplay(std::string &&message, std::unique_ptr<Display> &&lastDisplay) : PopupDisplay::PopupDisplay(std::unique_ptr<Display> &&lastDisplay) :
m_message{std::move(message)}, m_lastDisplay{std::move(lastDisplay)} m_lastDisplay{std::move(lastDisplay)}
{ {
} }
@@ -21,86 +20,6 @@ void PopupDisplay::initScreen()
initOverlay(); 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() void PopupDisplay::closeOverlay()
{ {
auto guard = std::move(espgui::currentDisplay); auto guard = std::move(espgui::currentDisplay);

View File

@@ -13,21 +13,14 @@ class PopupDisplay : public Display
using Base = Display; using Base = Display;
public: public:
PopupDisplay(std::string &&message, std::unique_ptr<Display> &&lastDisplay); PopupDisplay(std::unique_ptr<Display> &&lastDisplay);
//void start() override;
void initScreen() override; void initScreen() override;
//void update() override;
//void redraw() override;
//void stop() override;
void buttonPressed(espgui::Button button) override; virtual void initOverlay() = 0;
void closeOverlay();
virtual void initOverlay();
virtual void closeOverlay();
private: private:
std::string m_message;
std::unique_ptr<Display> m_lastDisplay; std::unique_ptr<Display> m_lastDisplay;
}; };