TinyMaterialDesign
Loading...
Searching...
No Matches
Label.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyMaterialDesign/Core/Widget.h"
8
9namespace tinymd {
10
12enum class TextAlign { kStart, kCenter };
13
14/// Maps a TypographyRole to the theme's corresponding font - shared by
15/// every widget that lets a caller pick a text size (Label, ListItem, ...)
16/// rather than each duplicating this switch.
17template <typename RGB_T>
19 switch (role) {
21 return theme.typography.headline;
23 return theme.typography.title;
25 return theme.typography.label;
27 default:
28 return theme.typography.body;
29 }
30}
31
32/// Single line of themed, non-interactive text.
33template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
34class Label : public Widget<RGB_T> {
35 public:
36 Label() = default;
37 Label(Bounds bounds, const char* text, TypographyRole role = TypographyRole::kBody,
39 : text_(text), role_(role), align_(align) {
40 this->bounds = bounds;
41 }
42
43 void setText(const char* text) { text_ = text; }
44 const std::string& text() const { return text_; }
45 void setRole(TypographyRole role) { role_ = role; }
46 void setAlign(TextAlign align) { align_ = align; }
47 void setColor(RGB_T color) {
48 colorOverride_ = color;
49 hasColorOverride_ = true;
50 }
51 /// Color behind the text (default: the surface color of the theme). Use
52 /// the background color for a label directly on a page.
53 void setBackgroundColor(RGB_T color) {
54 background_ = color;
55 hasBackground_ = true;
56 }
57 /// Draw the text on the theme's page background instead of the surface.
58 void setOnBackground(bool onBackground) { onBackground_ = onBackground; }
59 /// Secondary text: the theme's onSurfaceVariant color instead of onSurface
60 /// (ignored if a color was set with setColor()).
61 void setVariantColor(bool variant) { variant_ = variant; }
62
63 /// Explicit font, taking priority over the role-derived one in draw()
64 /// below - nullptr (the default) means "use the theme's font for role()".
65 void setFont(tinygpu::IFont<RGB_T>* font) { font_ = font; }
66 tinygpu::IFont<RGB_T>* font() const { return font_; }
67
68 void draw(tinygpu::ISurface<RGB_T>& target) override {
69 tinygpu::IFont<RGB_T>& font = font_ != nullptr ? *font_ : *fontForTypographyRole(role_, this->theme());
70 RGB_T color = hasColorOverride_ ? colorOverride_
71 : variant_ ? this->theme().colors.onSurfaceVariant
72 : this->theme().colors.onSurface;
73 if (!this->enabled) color = blend(color, this->theme().colors.surface, 0.5f);
74
75 const size_t textWidth = font.measureTextWidth(text_.c_str());
76 const size_t textHeight = font.getHeight(1);
77 const int32_t textX = align_ == TextAlign::kCenter
78 ? this->bounds.centerX() - static_cast<int32_t>(textWidth) / 2
79 : this->bounds.x;
80 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(textHeight) / 2;
81
82 const RGB_T background = hasBackground_ ? background_
83 : onBackground_ ? this->theme().colors.background
84 : this->theme().colors.surface;
85 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
86 text_.c_str(), color, background, false);
87 }
88
89 private:
90 std::string text_;
93 RGB_T colorOverride_{};
94 bool hasColorOverride_ = false;
95 RGB_T background_{};
96 bool hasBackground_ = false;
97 bool onBackground_ = false;
98 bool variant_ = false;
99 tinygpu::IFont<RGB_T>* font_ = nullptr;
100};
101
105
106} // namespace tinymd
Single line of themed, non-interactive text.
Definition: Label.h:34
void setBackgroundColor(RGB_T color)
Definition: Label.h:53
void setRole(TypographyRole role)
Definition: Label.h:45
tinygpu::IFont< RGB_T > * font() const
Definition: Label.h:66
void setText(const char *text)
Definition: Label.h:43
Label()=default
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Label.h:68
void setVariantColor(bool variant)
Definition: Label.h:61
void setColor(RGB_T color)
Definition: Label.h:47
const std::string & text() const
Definition: Label.h:44
void setFont(tinygpu::IFont< RGB_T > *font)
Definition: Label.h:65
void setOnBackground(bool onBackground)
Draw the text on the theme's page background instead of the surface.
Definition: Label.h:58
Label(Bounds bounds, const char *text, TypographyRole role=TypographyRole::kBody, TextAlign align=TextAlign::kStart)
Definition: Label.h:37
void setAlign(TextAlign align)
Definition: Label.h:46
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
TypographyRole
Definition: Label.h:11
tinygpu::IFont< RGB_T > * fontForTypographyRole(TypographyRole role, const MaterialTheme< RGB_T > &theme)
Definition: Label.h:18
TextAlign
Definition: Label.h:12
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21