Merge pull request #14 from CommanderRedYT/better-ui

Color/Font utilities and new TextWithValueHelper
This commit is contained in:
2022-06-09 16:12:15 +02:00
committed by GitHub
3 changed files with 57 additions and 6 deletions

View File

@ -38,6 +38,7 @@ set(headers
src/menuitem.h
src/messagepopupdisplay.h
src/popupdisplay.h
src/richtexthelper.h
src/richtextrenderer.h
src/screenmanager.h
src/scrollinterface.h

20
src/richtexthelper.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
namespace espgui {
namespace colors {
constexpr char RED[] = "&1"; // #ff0000 (TFT_RED)
constexpr char GREEN[] = "&2"; // #00ff00 (TFT_GREEN)
constexpr char BLUE[] = "&3"; // #0000ff (TFT_BLUE)
constexpr char YELLOW[] = "&4"; // #ffff00 (TFT_YELLOW)
constexpr char BLACK[] = "&5"; // #000000 (TFT_BLACK)
constexpr char WHITE[] = "&6"; // #ffffff (TFT_WHITE)
constexpr char GREY[] = "&7"; // #888888 (TFT_GREY)
constexpr char RESET[] = "&0"; // reset to previous color set by tft.setTextColor()
} // namespace colors
namespace fonts {
constexpr char SMALL[] = "&s"; // tft.setTextFont(2)
constexpr char MEDIUM[] = "&m"; // tft.setTextFont(4)
constexpr char RESTORE[] = "&f"; // restore original font
} // namespace fonts
} // namespace espgui

View File

@ -4,11 +4,10 @@
#include <fmt/core.h>
#include <strutils.h>
#include <espstrutils.h>
#include <espwifiutils.h>
#include <espchrono.h>
// local includes
#include "textinterface.h"
#include "richtexthelper.h"
#include "richtextrenderer.h"
namespace espgui {
@ -22,8 +21,6 @@ struct TextWithValueHelper : public Taccessor, public virtual TextInterface
{
using cpputils::toString;
using espcpputils::toString;
using wifi_stack::toString;
using espchrono::toString;
return fmt::format("{} {}", Tprefix, richTextEscape(toString(Taccessor::getValue())));
}
@ -38,8 +35,6 @@ struct ChangeableTextWithValueHelper : public Taccessor, public virtual TextInte
{
using cpputils::toString;
using espcpputils::toString;
using wifi_stack::toString;
using espchrono::toString;
return fmt::format("{} {}", m_prefix, richTextEscape(toString(Taccessor::getValue())));
}
@ -52,4 +47,39 @@ private:
std::string m_prefix;
};
template<const char *Tprefix, typename Taccessor, const char *Tguilib_color = espgui::colors::GREY>
struct TextWithHighlightedValueHelper : public Taccessor, public virtual TextInterface
{
using Taccessor::Taccessor;
std::string text() const override
{
using cpputils::toString;
using espcpputils::toString;
return fmt::format("{} {}{}", Tprefix, Tguilib_color, richTextEscape(toString(Taccessor::getValue())));
}
};
template<typename Taccessor, const char *Tguilib_color = espgui::colors::GREY>
struct ChangeableTextWithHighlightedValueHelper : public Taccessor, public virtual TextInterface
{
using Taccessor::Taccessor;
std::string text() const override
{
using cpputils::toString;
using espcpputils::toString;
return fmt::format("{} {}{}", m_prefix, Tguilib_color, richTextEscape(toString(Taccessor::getValue())));
}
const std::string &prefix() const { return m_prefix; }
void setPrefix(std::string_view prefix) { m_prefix = std::string{prefix}; }
void setPrefix(std::string &&prefix) { m_prefix = std::move(prefix); }
private:
std::string m_prefix;
};
} // namespace espgui