From 26c0be33b1500f5658374ce77f54792e46491f72 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Tue, 10 Jan 2023 17:41:03 +0100 Subject: [PATCH] Add TitleInterface --- CMakeLists.txt | 1 + src/display.h | 16 +++++++++ src/displaywithtitle.cpp | 2 +- src/displaywithtitle.h | 8 ++--- src/titleinterface.h | 75 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 src/titleinterface.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9971d28..02276c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ set(headers src/tftcolors.h src/tftespiimpl.h src/tftinterface.h + src/titleinterface.h src/visibleinterface.h src/widgets/graph.h src/widgets/label.h diff --git a/src/display.h b/src/display.h index 403e2b7..f1a7b94 100644 --- a/src/display.h +++ b/src/display.h @@ -11,6 +11,7 @@ namespace espgui { class TftInterface; class TextInterface; +class TitleInterface; class MenuDisplay; class ChangeValueDisplayInterface; template class ChangeValueDisplay; @@ -60,6 +61,18 @@ public: } }; +template +class makeComponentArgs12 : public T1, public T2, public T3, public T4... +{ +public: + template + makeComponentArgs12(Targ1&& arg1, Targ2&& arg2, Targ3&& arg3) : + T2{std::forward(arg1)}, + T3{std::forward(arg2), std::forward(arg3)} + { + } +}; + class Display : public virtual ButtonsInterface { public: @@ -83,6 +96,9 @@ public: virtual TextInterface *asTextInterface() { return nullptr; } virtual const TextInterface *asTextInterface() const { return nullptr; } + virtual TitleInterface *asTitleInterface() { return nullptr; } + virtual const TitleInterface *asTitleInterface() const { return nullptr; } + virtual MenuDisplay *asMenuDisplay() { return nullptr; } virtual const MenuDisplay *asMenuDisplay() const { return nullptr; } diff --git a/src/displaywithtitle.cpp b/src/displaywithtitle.cpp index 5b23a49..3179891 100644 --- a/src/displaywithtitle.cpp +++ b/src/displaywithtitle.cpp @@ -18,7 +18,7 @@ void DisplayWithTitle::redraw(TftInterface &tft) { Base::redraw(tft); - m_titleLabel.redraw(tft, text(), TFT_YELLOW, TFT_BLACK, 4); + m_titleLabel.redraw(tft, title(), TFT_YELLOW, TFT_BLACK, 4); } } // namespace espgui diff --git a/src/displaywithtitle.h b/src/displaywithtitle.h index 3ea4a49..ebc4feb 100644 --- a/src/displaywithtitle.h +++ b/src/displaywithtitle.h @@ -2,20 +2,20 @@ // local includes #include "display.h" -#include "textinterface.h" +#include "titleinterface.h" #include "widgets/label.h" namespace espgui { class DisplayWithTitle : public Display, - public virtual TextInterface + public virtual TitleInterface { using Base = Display; public: - TextInterface *asTextInterface() override { return this; } - const TextInterface *asTextInterface() const override { return this; } + TitleInterface *asTitleInterface() override { return this; } + const TitleInterface *asTitleInterface() const override { return this; } void initScreen(TftInterface &tft) override; void redraw(TftInterface &tft) override; diff --git a/src/titleinterface.h b/src/titleinterface.h new file mode 100644 index 0000000..bac07fc --- /dev/null +++ b/src/titleinterface.h @@ -0,0 +1,75 @@ +#pragma once + +// system includes +#include +#include + +namespace espgui { + +class TitleInterface { +public: + virtual std::string title() const = 0; +}; + +class EmptyTitle : public virtual TitleInterface +{ +public: + std::string title() const override { return {}; } +}; + +template +class StaticTitle : public virtual TitleInterface +{ +public: + static constexpr const char *STATIC_TITLE = Ttitle; + + std::string title() const override { return Ttitle; } +}; + +class ChangeableTitle : public virtual TitleInterface +{ +public: + ChangeableTitle() = default; + ChangeableTitle(std::string &&title) : m_title{std::move(title)} {} + ChangeableTitle(std::string_view title) : m_title{title} {} + + std::string title() const override { return m_title; } + void setTitle(std::string &&title) { m_title = std::move(title); } + void setTitle(const std::string &title) { m_title = title; } + +private: + std::string m_title; +}; + +template +class CachedTitle : public virtual T +{ +public: + std::string title() const override + { + if (!m_loaded) + { + m_title = T::title(); + m_loaded = true; + } + + return m_title; + } + +private: + mutable bool m_loaded{}; + mutable std::string m_title; +}; + +template +class StaticallyCachedTitle : public virtual T +{ +public: + std::string title() const override + { + static const auto title = T::title(); + return title; + } +}; + +} // namespace espgui