TinyMaterialDesign
Loading...
Searching...
No Matches
StatTile.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
4#include "TinyMaterialDesign/Core/Widget.h"
5#include "TinyMaterialDesign/Widgets/Label.h"
6
7namespace tinymd {
8
9/**
10 * @brief A small tile with one value: a caption at the top, the value in an
11 * accent color and an optional unit / sub text - for dashboards and KPIs.
12 *
13 * StatTile<RGB565> rate(Bounds(8, 40, 72, 46), "RATE", "s/day");
14 * rate.setValue("+4.2");
15 * rate.setAccent(colorFromHex<RGB565>(0x10B981));
16 *
17 * The value is drawn in the title font and falls back to the body and label
18 * fonts if it does not fit. Outlined (default, on the page) or tonal (filled
19 * with surfaceVariant, e.g. inside a Card).
20 */
21template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
22class StatTile : public Widget<RGB_T> {
23 public:
24 StatTile() = default;
25 explicit StatTile(const char* caption, const char* sub = "") : caption_(caption), sub_(sub) {}
26 StatTile(Bounds bounds, const char* caption, const char* sub = "") : caption_(caption), sub_(sub) {
27 this->bounds = bounds;
28 }
29
30 void setCaption(const char* caption) { caption_ = caption; }
31 void setValue(const char* value) { value_ = value; }
32 const std::string& value() const { return value_; }
33 void setSub(const char* sub) { sub_ = sub; }
34 void setAccent(RGB_T color) {
35 accent_ = color;
36 hasAccent_ = true;
37 }
38 /// Filled with surfaceVariant instead of outlined.
39 bool tonal = false;
40 /// Font of the value (default: title, which falls back to smaller fonts).
42
43 void draw(tinygpu::ISurface<RGB_T>& target) override {
44 const MaterialTheme<RGB_T>& t = this->theme();
45 const Bounds& b = this->bounds;
46 const RGB_T bg = tonal ? t.colors.surfaceVariant : t.colors.surface;
47 const size_t radius = toPx(t.shape.medium);
48 target.fillRoundRect(toPx(b.x), toPx(b.y), toPx(b.w), toPx(b.h), radius, bg);
49 if (!tonal) target.drawRoundRect(toPx(b.x), toPx(b.y), toPx(b.w), toPx(b.h), radius, t.colors.outline);
50
51 tinygpu::IFont<RGB_T>& small = *t.typography.label;
52 const int32_t x = b.x + 5;
53 small.drawText(target, static_cast<int16_t>(x), static_cast<int16_t>(b.y + 4), caption_.c_str(),
54 t.colors.onSurfaceVariant, bg, false);
55
56 // the largest font that fits
57 tinygpu::IFont<RGB_T>* fonts[3] = {fontForTypographyRole(valueRole, t), t.typography.body, t.typography.label};
58 tinygpu::IFont<RGB_T>* font = fonts[2];
59 for (tinygpu::IFont<RGB_T>* f : fonts) {
60 if (static_cast<int32_t>(f->measureTextWidth(value_.c_str())) <= b.w - 8) {
61 font = f;
62 break;
63 }
64 }
65 const int32_t smallH = static_cast<int32_t>(small.getHeight(1));
66 const int32_t valueY = sub_.empty() ? b.bottom() - 5 - static_cast<int32_t>(font->getHeight(1))
67 : b.y + 4 + smallH + 3;
68 font->drawText(target, static_cast<int16_t>(x), static_cast<int16_t>(valueY), value_.c_str(),
69 hasAccent_ ? accent_ : t.colors.primary, bg, false);
70 if (!sub_.empty()) {
71 small.drawText(target, static_cast<int16_t>(x), static_cast<int16_t>(b.bottom() - 4 - smallH), sub_.c_str(),
72 t.colors.onSurfaceVariant, bg, false);
73 }
74 }
75
76 private:
77 std::string caption_;
78 std::string value_ = "--";
79 std::string sub_;
80 RGB_T accent_{};
81 bool hasAccent_ = false;
82};
83
84} // namespace tinymd
A small tile with one value: a caption at the top, the value in an accent color and an optional unit ...
Definition: StatTile.h:22
StatTile(Bounds bounds, const char *caption, const char *sub="")
Definition: StatTile.h:26
bool tonal
Filled with surfaceVariant instead of outlined.
Definition: StatTile.h:39
void setValue(const char *value)
Definition: StatTile.h:31
const std::string & value() const
Definition: StatTile.h:32
void setSub(const char *sub)
Definition: StatTile.h:33
StatTile()=default
void setCaption(const char *caption)
Definition: StatTile.h:30
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: StatTile.h:43
TypographyRole valueRole
Font of the value (default: title, which falls back to smaller fonts).
Definition: StatTile.h:41
void setAccent(RGB_T color)
Definition: StatTile.h:34
StatTile(const char *caption, const char *sub="")
Definition: StatTile.h:25
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
TypographyRole
Definition: Label.h:11
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t x
Definition: Bounds.h:22
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50