TinyMaterialDesign
Loading...
Searching...
No Matches
Button.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
4#include <string>
5
6#include "TinyGPU/Color/RGB565.h"
7#include "TinyGPU/Color/RGB666.h"
8#include "TinyGPU/Color/RGB888.h"
9#include "TinyMaterialDesign/Core/Bounds.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11
12namespace tinymd {
13
15
16/**
17 * @brief Material Design button: Filled / Tonal / Outlined / Text / Elevated.
18 *
19 * Tap feedback is the (optional) ripple grown from the tap point - real
20 * touch drivers only report a recognized, completed tap (GestureType::kTap),
21 * not a raw finger-down instant, so the ripple *is* the button's touch
22 * feedback rather than a separate immediate "pressed" shade.
23 */
24template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
25class Button : public Widget<RGB_T> {
26 public:
27 Button() = default;
28 Button(Bounds bounds, const char* label, ButtonVariant variant = ButtonVariant::kFilled)
29 : label_(label), variant_(variant) {
30 this->bounds = bounds;
31 }
32
33 std::function<void()> onClick;
34
35 void setLabel(const char* label) { label_ = label; }
36 const std::string& label() const { return label_; }
37 void setVariant(ButtonVariant variant) { variant_ = variant; }
38
39 void draw(tinygpu::ISurface<RGB_T>& target) override {
40 RGB_T background;
41 RGB_T foreground;
42 bool paintBackground = true;
43 bool paintOutline = false;
44
45 switch (variant_) {
47 background = this->theme().colors.primary;
48 foreground = this->theme().colors.onPrimary;
49 break;
51 background = this->theme().colors.secondaryContainer;
52 foreground = this->theme().colors.onSecondaryContainer;
53 break;
55 background = this->theme().colors.surface;
56 foreground = this->theme().colors.primary;
57 break;
59 background = this->theme().colors.surface;
60 foreground = this->theme().colors.primary;
61 paintOutline = true;
62 break;
64 default:
65 background = this->theme().colors.surface;
66 foreground = this->theme().colors.primary;
67 paintBackground = false;
68 break;
69 }
70
71 if (!this->enabled) {
72 foreground = blend(foreground, this->theme().colors.onSurface, 0.5f);
73 background = blend(background, this->theme().colors.onSurface, 0.7f);
74 }
75
76 const int32_t radius = this->theme().shape.full; // pill-shaped, standard M3
77
78 if (variant_ == ButtonVariant::kElevated && this->enabled) {
79 const RGB_T shadow = blend(this->theme().colors.surface, this->theme().colors.onBackground, 0.35f);
80 target.fillRoundRect(toPx(this->bounds.x + 1), toPx(this->bounds.y + 2),
81 toPx(this->bounds.w), toPx(this->bounds.h), toPx(radius),
82 shadow);
83 }
84
85 if (paintBackground) {
86 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
87 toPx(this->bounds.w), toPx(this->bounds.h), toPx(radius),
88 background);
89 }
90 if (paintOutline) {
91 const RGB_T outlineColor =
92 this->enabled ? this->theme().colors.outline : blend(this->theme().colors.outline, this->theme().colors.surface, 0.5f);
93 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
94 toPx(this->bounds.w), toPx(this->bounds.h), toPx(radius),
95 outlineColor);
96 }
97
98 if (this->theme().enableRipple && rippleActive_) {
99 drawRipple(target, background, foreground);
100 }
101
102 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
103 const size_t textWidth = font.measureTextWidth(label_.c_str());
104 const size_t textHeight = font.getHeight(1);
105 const int32_t textX = this->bounds.centerX() - static_cast<int32_t>(textWidth) / 2;
106 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(textHeight) / 2;
107 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
108 label_.c_str(), foreground, background, false);
109 }
110
111 bool onGesture(const tinygpu::GestureEvent& event) override {
112 if (!isTapGesture(event.type)) return false;
113 rippleOriginX_ = event.point.x;
114 rippleOriginY_ = event.point.y;
115 // lastUpdateMs_ is one frame stale (update() hasn't run yet this frame),
116 // which is imperceptible next to a ~260ms ripple.
117 rippleStartMs_ = lastUpdateMs_;
118 rippleActive_ = true;
119 if (onClick) onClick();
120 return true;
121 }
122
123 bool update(uint32_t nowMs) override {
124 const bool wasActive = rippleActive_;
125 lastUpdateMs_ = nowMs;
126 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) {
127 rippleActive_ = false;
128 }
129 // The ripple's radius/opacity are a function of elapsed time recomputed
130 // in drawRipple() every frame, so any frame where it started the call
131 // active needs a redraw - including the final frame where it just
132 // finished fading out.
133 return wasActive;
134 }
135
136 private:
137 static constexpr uint32_t kRippleDurationMs = 260;
138
139 std::string label_;
141
142 bool rippleActive_ = false;
143 uint32_t rippleStartMs_ = 0;
144 uint32_t lastUpdateMs_ = 0;
145 int32_t rippleOriginX_ = 0;
146 int32_t rippleOriginY_ = 0;
147
148 /// Fills a circle growing from the tap point and fading out, clipped to
149 /// this button's rectangular bounds (a reasonable approximation - it
150 /// doesn't follow the pill's rounded corners, which isn't visible at
151 /// typical button sizes since the ripple fades out well before reaching
152 /// them).
153 void drawRipple(tinygpu::ISurface<RGB_T>& target, RGB_T background, RGB_T foreground) {
154 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
155 const float t = std::min(1.0f, static_cast<float>(elapsed) / kRippleDurationMs);
156 const float maxRadius =
157 static_cast<float>(this->bounds.w > this->bounds.h ? this->bounds.w : this->bounds.h);
158 const float radius = t * maxRadius;
159 const RGB_T rippleColor = blend(background, foreground, 0.20f * (1.0f - t));
160
161 const int32_t left = std::max(this->bounds.x, static_cast<int32_t>(rippleOriginX_ - radius));
162 const int32_t right = std::min(this->bounds.right(), static_cast<int32_t>(rippleOriginX_ + radius) + 1);
163 const int32_t top = std::max(this->bounds.y, static_cast<int32_t>(rippleOriginY_ - radius));
164 const int32_t bottom = std::min(this->bounds.bottom(), static_cast<int32_t>(rippleOriginY_ + radius) + 1);
165 const float radiusSq = radius * radius;
166
167 for (int32_t y = top; y < bottom; ++y) {
168 for (int32_t x = left; x < right; ++x) {
169 const float dx = static_cast<float>(x - rippleOriginX_);
170 const float dy = static_cast<float>(y - rippleOriginY_);
171 if (dx * dx + dy * dy <= radiusSq) {
172 target.setPixel(toPx(x), toPx(y), rippleColor);
173 }
174 }
175 }
176 }
177};
178
182
183} // namespace tinymd
Material Design button: Filled / Tonal / Outlined / Text / Elevated.
Definition: Button.h:25
void setLabel(const char *label)
Definition: Button.h:35
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Button.h:111
const std::string & label() const
Definition: Button.h:36
void setVariant(ButtonVariant variant)
Definition: Button.h:37
Button(Bounds bounds, const char *label, ButtonVariant variant=ButtonVariant::kFilled)
Definition: Button.h:28
Button()=default
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Button.h:39
bool update(uint32_t nowMs) override
Definition: Button.h:123
std::function< void()> onClick
Definition: Button.h:33
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
ButtonVariant
Definition: Button.h:14
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21