TinyMaterialDesign
Loading...
Searching...
No Matches
SettingRows.h
Go to the documentation of this file.
1#pragma once
2#include <math.h>
3#include <stdio.h>
4
5#include <functional>
6#include <string>
7
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Widgets/RadioButton.h"
10#include "TinyMaterialDesign/Widgets/Slider.h"
11#include "TinyMaterialDesign/Widgets/Switch.h"
12
13namespace tinymd {
14
15/**
16 * @file SettingRows.h
17 * @brief Rows for settings and detail pages: a caption combined with a
18 * control or a value. Each row is one widget, so a page only places rows
19 * from top to bottom. By default they are drawn on the page background; call
20 * setOnSurface(true) for rows inside a Card.
21 *
22 * - SliderRow: caption and formatted value, a Slider below (kHeight 40)
23 * - SwitchRow: caption, Switch at the right; tapping the row toggles it
24 * - RadioRow: RadioButton and caption; tapping the row selects it
25 * - KeyValueRow: key at the left, value at the right
26 */
27
28/// Common part of the rows: caption and background.
29template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
30class SettingRow : public Widget<RGB_T> {
31 public:
32 void setCaption(const char* caption) { caption_ = caption != nullptr ? caption : ""; }
33 const std::string& caption() const { return caption_; }
34 /// Draw on a surface (e.g. inside a Card) instead of the page background.
35 void setOnSurface(bool onSurface) { onSurface_ = onSurface; }
36
37 protected:
38 std::string caption_;
39 bool onSurface_ = false;
40
41 RGB_T background() const { return onSurface_ ? this->theme().colors.surface : this->theme().colors.background; }
42
43 void text(tinygpu::ISurface<RGB_T>& target, tinygpu::IFont<RGB_T>& font, int32_t x, int32_t y, const char* s,
44 RGB_T color) {
45 font.drawText(target, static_cast<int16_t>(x), static_cast<int16_t>(y), s, color, background(), false);
46 }
47
48 RGB_T dimmed(RGB_T color) const { return this->enabled ? color : blend(color, background(), 0.5f); }
49};
50
51/**
52 * @brief Caption and formatted value on one line, a Slider below. The value
53 * is quantized to `step`:
54 *
55 * SliderRow<RGB565> gain("Gain", 0, 40, 1, [](float v, char* b, size_t n) {
56 * snprintf(b, n, "%+.0f dB", v);
57 * });
58 * gain.onChange = [](float v) { ... };
59 */
60template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
61class SliderRow : public SettingRow<RGB_T> {
62 public:
63 using Formatter = std::function<void(float value, char* buf, size_t len)>;
64 static constexpr int32_t kHeight = 40;
65
66 SliderRow() = default;
67 SliderRow(const char* caption, float minValue, float maxValue, float step, Formatter format = nullptr)
68 : step_(step), format_(std::move(format)), slider_(Bounds(), minValue, maxValue) {
69 this->setCaption(caption);
70 slider_.onChange = [this](float v) {
71 const float q = quantize(v);
72 if (q == value_) return;
73 value_ = q;
74 if (onChange) onChange(value_);
75 };
76 }
77
78 std::function<void(float)> onChange;
79
80 float value() const { return value_; }
81 void setValue(float value) {
82 value_ = quantize(value);
83 slider_.setValue(value_);
84 }
85
86 void setTheme(const MaterialTheme<RGB_T>& theme) override {
87 Widget<RGB_T>::setTheme(theme);
88 slider_.setTheme(theme);
89 }
90
91 void draw(tinygpu::ISurface<RGB_T>& target) override {
92 const MaterialTheme<RGB_T>& t = this->theme();
93 const Bounds& b = this->bounds;
94 tinygpu::IFont<RGB_T>& small = *t.typography.label;
95 tinygpu::IFont<RGB_T>& body = *t.typography.body;
96 this->text(target, small, b.x, b.y + 3, this->caption_.c_str(), this->dimmed(t.colors.onSurfaceVariant));
97 char buf[24] = "";
98 if (format_) {
99 format_(value_, buf, sizeof(buf));
100 } else {
101 snprintf(buf, sizeof(buf), "%g", value_);
102 }
103 this->text(target, body, b.right() - static_cast<int32_t>(body.measureTextWidth(buf)), b.y + 2, buf,
104 this->dimmed(t.colors.onSurface));
105 syncSlider();
106 slider_.enabled = this->enabled;
107 slider_.draw(target);
108 }
109
110 bool onGesture(const tinygpu::GestureEvent& event) override {
111 syncSlider();
112 return slider_.onGesture(event);
113 }
114
115 bool isDraggableAt(int32_t x, int32_t y) const override {
116 (void)x;
117 return y >= this->bounds.y + kTextHeight;
118 }
119
120 private:
121 static constexpr int32_t kTextHeight = 14;
122 float step_ = 1.0f;
123 float value_ = 0.0f;
124 Formatter format_;
125 Slider<RGB_T> slider_;
126
127 float quantize(float v) const { return step_ > 0 ? roundf(v / step_) * step_ : v; }
128
129 /// The slider band below the text (the bounds move while a Container scrolls).
130 void syncSlider() {
131 const Bounds& b = this->bounds;
132 slider_.bounds = Bounds(b.x + 9, b.y + kTextHeight + 2, b.w - 18, 18);
133 }
134};
135
136/// Caption on the left, Switch on the right; tapping the row toggles it.
137template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
138class SwitchRow : public SettingRow<RGB_T> {
139 public:
140 static constexpr int32_t kHeight = 30;
141
142 SwitchRow() = default;
143 explicit SwitchRow(const char* caption) { this->setCaption(caption); }
144
145 std::function<void(bool)> onChange;
146
147 bool value() const { return switch_.value(); }
148 void setValue(bool value) { switch_.setValue(value); }
149
150 void setTheme(const MaterialTheme<RGB_T>& theme) override {
151 Widget<RGB_T>::setTheme(theme);
152 switch_.setTheme(theme);
153 }
154
155 void draw(tinygpu::ISurface<RGB_T>& target) override {
156 const MaterialTheme<RGB_T>& t = this->theme();
157 const Bounds& b = this->bounds;
158 tinygpu::IFont<RGB_T>& body = *t.typography.body;
159 this->text(target, body, b.x, b.centerY() - static_cast<int32_t>(body.getHeight(1)) / 2, this->caption_.c_str(),
160 this->dimmed(t.colors.onSurface));
161 switch_.bounds = Bounds(b.right() - 44, b.centerY() - 12, 44, 24);
162 switch_.enabled = this->enabled;
163 switch_.draw(target);
164 }
165
166 bool onGesture(const tinygpu::GestureEvent& event) override {
167 if (!isTapGesture(event.type) || !this->enabled) return false;
168 switch_.setValue(!switch_.value());
169 if (onChange) onChange(switch_.value());
170 return true;
171 }
172
173 private:
174 Switch<RGB_T> switch_;
175};
176
177/// RadioButton and caption; tapping the row selects it.
178template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
179class RadioRow : public SettingRow<RGB_T> {
180 public:
181 static constexpr int32_t kHeight = 26;
182
183 RadioRow() = default;
184 explicit RadioRow(const char* caption) { this->setCaption(caption); }
185
186 /// Called when the row becomes selected by a tap.
187 std::function<void()> onSelected;
188
189 bool selected() const { return radio_.selected(); }
190 void setSelected(bool selected) { radio_.setSelected(selected); }
191
192 void setTheme(const MaterialTheme<RGB_T>& theme) override {
193 Widget<RGB_T>::setTheme(theme);
194 radio_.setTheme(theme);
195 }
196
197 void draw(tinygpu::ISurface<RGB_T>& target) override {
198 const MaterialTheme<RGB_T>& t = this->theme();
199 const Bounds& b = this->bounds;
200 radio_.bounds = Bounds(b.x, b.centerY() - 10, 20, 20);
201 radio_.enabled = this->enabled;
202 radio_.draw(target);
203 tinygpu::IFont<RGB_T>& body = *t.typography.body;
204 this->text(target, body, b.x + 26, b.centerY() - static_cast<int32_t>(body.getHeight(1)) / 2,
205 this->caption_.c_str(), this->dimmed(t.colors.onSurface));
206 }
207
208 bool onGesture(const tinygpu::GestureEvent& event) override {
209 if (!isTapGesture(event.type) || !this->enabled) return false;
210 if (!radio_.selected()) {
211 radio_.setSelected(true);
212 if (onSelected) onSelected();
213 }
214 return true;
215 }
216
217 private:
218 RadioButton<RGB_T> radio_{Bounds()};
219};
220
221/// Key on the left, value on the right (label font).
222template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
223class KeyValueRow : public SettingRow<RGB_T> {
224 public:
225 static constexpr int32_t kHeight = 14;
226
227 KeyValueRow() = default;
228 explicit KeyValueRow(const char* key, const char* value = "") : value_(value) { this->setCaption(key); }
229
230 void setValue(const char* value) { value_ = value != nullptr ? value : ""; }
231 const std::string& value() const { return value_; }
232 void setValueColor(RGB_T color) {
233 valueColor_ = color;
234 hasValueColor_ = true;
235 }
236 void clearValueColor() { hasValueColor_ = false; }
237
238 void draw(tinygpu::ISurface<RGB_T>& target) override {
239 const MaterialTheme<RGB_T>& t = this->theme();
240 const Bounds& b = this->bounds;
241 tinygpu::IFont<RGB_T>& small = *t.typography.label;
242 const int32_t y = b.centerY() - static_cast<int32_t>(small.getHeight(1)) / 2;
243 this->text(target, small, b.x, y, this->caption_.c_str(), this->dimmed(t.colors.onSurfaceVariant));
244 this->text(target, small, b.right() - static_cast<int32_t>(small.measureTextWidth(value_.c_str())), y,
245 value_.c_str(), this->dimmed(hasValueColor_ ? valueColor_ : t.colors.onSurface));
246 }
247
248 private:
249 std::string value_;
250 RGB_T valueColor_{};
251 bool hasValueColor_ = false;
252};
253
254} // namespace tinymd
Key on the left, value on the right (label font).
Definition: SettingRows.h:223
KeyValueRow(const char *key, const char *value="")
Definition: SettingRows.h:228
void setValueColor(RGB_T color)
Definition: SettingRows.h:232
void setValue(const char *value)
Definition: SettingRows.h:230
const std::string & value() const
Definition: SettingRows.h:231
static constexpr int32_t kHeight
Definition: SettingRows.h:225
void clearValueColor()
Definition: SettingRows.h:236
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SettingRows.h:238
One radio button. Tapping it selects it (radio semantics: a tap never deselects); mutual exclusion ac...
Definition: RadioButton.h:16
RadioButton and caption; tapping the row selects it.
Definition: SettingRows.h:179
RadioRow()=default
RadioRow(const char *caption)
Definition: SettingRows.h:184
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: SettingRows.h:208
static constexpr int32_t kHeight
Definition: SettingRows.h:181
std::function< void()> onSelected
Called when the row becomes selected by a tap.
Definition: SettingRows.h:187
bool selected() const
Definition: SettingRows.h:189
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SettingRows.h:197
void setSelected(bool selected)
Definition: SettingRows.h:190
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: SettingRows.h:192
Common part of the rows: caption and background.
Definition: SettingRows.h:30
bool onSurface_
Definition: SettingRows.h:39
const std::string & caption() const
Definition: SettingRows.h:33
RGB_T dimmed(RGB_T color) const
Definition: SettingRows.h:48
void setCaption(const char *caption)
Definition: SettingRows.h:32
RGB_T background() const
Definition: SettingRows.h:41
void setOnSurface(bool onSurface)
Draw on a surface (e.g. inside a Card) instead of the page background.
Definition: SettingRows.h:35
std::string caption_
Definition: SettingRows.h:38
void text(tinygpu::ISurface< RGB_T > &target, tinygpu::IFont< RGB_T > &font, int32_t x, int32_t y, const char *s, RGB_T color)
Definition: SettingRows.h:43
Caption and formatted value on one line, a Slider below. The value is quantized to step:
Definition: SettingRows.h:61
SliderRow()=default
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: SettingRows.h:115
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: SettingRows.h:110
void setValue(float value)
Definition: SettingRows.h:81
static constexpr int32_t kHeight
Definition: SettingRows.h:64
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SettingRows.h:91
float value() const
Definition: SettingRows.h:80
std::function< void(float)> onChange
Definition: SettingRows.h:78
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: SettingRows.h:86
SliderRow(const char *caption, float minValue, float maxValue, float step, Formatter format=nullptr)
Definition: SettingRows.h:67
Draggable track-and-thumb slider over [minValue, maxValue].
Definition: Slider.h:23
Caption on the left, Switch on the right; tapping the row toggles it.
Definition: SettingRows.h:138
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: SettingRows.h:166
std::function< void(bool)> onChange
Definition: SettingRows.h:145
bool value() const
Definition: SettingRows.h:147
static constexpr int32_t kHeight
Definition: SettingRows.h:140
SwitchRow()=default
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SettingRows.h:155
SwitchRow(const char *caption)
Definition: SettingRows.h:143
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: SettingRows.h:150
void setValue(bool value)
Definition: SettingRows.h:148
Track-and-thumb toggle switch. Toggled by tap (drag-to-slide is a possible follow-up,...
Definition: Switch.h:14
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t w
Definition: Bounds.h:24
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerY() const
Definition: Bounds.h:33
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
Bounds()=default
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50