8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Widgets/RadioButton.h"
10#include "TinyMaterialDesign/Widgets/Slider.h"
11#include "TinyMaterialDesign/Widgets/Switch.h"
16
17
18
19
20
21
22
23
24
25
26
41 RGB_T
background()
const {
return onSurface_ ?
this->theme().colors.surface :
this->theme().colors.background; }
43 void text(tinygpu::ISurface<RGB_T>& target, tinygpu::IFont<RGB_T>& font, int32_t x, int32_t y,
const char* s,
45 font.drawText(target,
static_cast<int16_t>(x),
static_cast<int16_t>(y), s, color, background(),
false);
48 RGB_T
dimmed(RGB_T color)
const {
return this->enabled ? color : blend(color,
background(), 0.5f); }
52
53
54
55
56
57
58
59
63 using Formatter = std::function<
void(
float value,
char* buf, size_t len)>;
67 SliderRow(
const char* caption,
float minValue,
float maxValue,
float step, Formatter format =
nullptr)
69 this->setCaption(caption);
70 slider_.onChange = [
this](
float v) {
71 const float q = quantize(v);
72 if (q == value_)
return;
74 if (onChange) onChange(value_);
80 float value()
const {
return value_; }
82 value_ = quantize(value);
83 slider_.setValue(value_);
87 Widget<RGB_T>::setTheme(theme);
88 slider_.setTheme(theme);
91 void draw(tinygpu::ISurface<RGB_T>& target)
override {
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));
99 format_(value_, buf,
sizeof(buf));
101 snprintf(buf,
sizeof(buf),
"%g", value_);
103 this->text(target, body, b.right() -
static_cast<int32_t>(body.measureTextWidth(buf)), b.y + 2, buf,
104 this->dimmed(t.colors.onSurface));
106 slider_.enabled =
this->enabled;
107 slider_.draw(target);
110 bool onGesture(
const tinygpu::GestureEvent& event)
override {
112 return slider_.onGesture(event);
117 return y >=
this->bounds.y + kTextHeight;
121 static constexpr int32_t kTextHeight = 14;
127 float quantize(
float v)
const {
return step_ > 0 ? roundf(v / step_) * step_ : v; }
131 const Bounds& b =
this->bounds;
143 explicit SwitchRow(
const char* caption) {
this->setCaption(caption); }
147 bool value()
const {
return switch_.value(); }
148 void setValue(
bool value) { switch_.setValue(value); }
151 Widget<RGB_T>::setTheme(theme);
152 switch_.setTheme(theme);
155 void draw(tinygpu::ISurface<RGB_T>& target)
override {
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));
162 switch_.enabled =
this->enabled;
163 switch_.draw(target);
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());
184 explicit RadioRow(
const char* caption) {
this->setCaption(caption); }
189 bool selected()
const {
return radio_.selected(); }
193 Widget<RGB_T>::setTheme(theme);
194 radio_.setTheme(theme);
197 void draw(tinygpu::ISurface<RGB_T>& target)
override {
199 const Bounds& b =
this->bounds;
201 radio_.enabled =
this->enabled;
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));
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();
230 void setValue(
const char* value) { value_ = value !=
nullptr ? value :
""; }
231 const std::string&
value()
const {
return value_; }
234 hasValueColor_ =
true;
238 void draw(tinygpu::ISurface<RGB_T>& target)
override {
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));
251 bool hasValueColor_ =
false;
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
RadioButton and caption; tapping the row selects it.
Definition: SettingRows.h:179
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
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
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
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
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50