TinyMaterialDesign
Loading...
Searching...
No Matches
RadioButton.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <vector>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Draw/Icons.h"
10
11namespace tinymd {
12
13/// @brief One radio button. Tapping it selects it (radio semantics: a tap never
14/// deselects); mutual exclusion across a group is RadioGroup's job.
15template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
16class RadioButton : public Widget<RGB_T> {
17 public:
18 RadioButton() = default;
19 explicit RadioButton(Bounds bounds, bool selected = false) : selected_(selected) {
20 this->bounds = bounds;
21 }
22
23 /// Wired by RadioGroup::addButton(); not meant to be set directly.
24 std::function<void()> onSelected;
25
26 bool selected() const { return selected_; }
27 void setSelected(bool selected) { selected_ = selected; }
28
29 void draw(tinygpu::ISurface<RGB_T>& target) override {
30 RGB_T ring = selected_ ? this->theme().colors.primary : this->theme().colors.outline;
31 RGB_T dot = this->theme().colors.primary;
32 if (!this->enabled) {
33 ring = blend(ring, this->theme().colors.surface, 0.5f);
34 dot = blend(dot, this->theme().colors.surface, 0.4f);
35 }
36
37 const int32_t diameter = this->bounds.w < this->bounds.h ? this->bounds.w : this->bounds.h;
38 target.drawCircle(toPx(this->bounds.centerX()), toPx(this->bounds.centerY()),
39 toPx(diameter / 2), ring);
40 if (selected_) {
41 drawDot(target, this->bounds, dot);
42 }
43 }
44
45 bool onGesture(const tinygpu::GestureEvent& event) override {
46 if (!isTapGesture(event.type)) return false;
47 if (!selected_) {
48 selected_ = true;
49 if (onSelected) onSelected();
50 }
51 return true;
52 }
53
54 private:
55 bool selected_ = false;
56};
57
58/// @brief Coordinates a set of RadioButtons so selecting one deselects the rest.
59/// Not itself a Widget - each RadioButton must still be added to the
60/// Screen individually, e.g.:
61///
62/// RadioGroup<RGB565> group;
63/// group.onChange = [](int index) { ... };
64/// for (auto& button : buttons) { screen.addWidget(button); group.addButton(button); }
65template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
67 public:
68 std::function<void(int)> onChange;
69
70 void addButton(RadioButton<RGB_T>& button) {
71 const int index = static_cast<int>(buttons_.size());
72 buttons_.push_back(&button);
73 button.onSelected = [this, index]() { select(index); };
74 }
75
76 int selectedIndex() const {
77 for (size_t i = 0; i < buttons_.size(); ++i) {
78 if (buttons_[i]->selected()) return static_cast<int>(i);
79 }
80 return -1;
81 }
82
83 private:
84 std::vector<RadioButton<RGB_T>*> buttons_;
85
86 void select(int index) {
87 for (size_t i = 0; i < buttons_.size(); ++i) {
88 buttons_[i]->setSelected(static_cast<int>(i) == index);
89 }
90 if (onChange) onChange(index);
91 }
92};
93
100
101} // namespace tinymd
One radio button. Tapping it selects it (radio semantics: a tap never deselects); mutual exclusion ac...
Definition: RadioButton.h:16
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: RadioButton.h:45
RadioButton(Bounds bounds, bool selected=false)
Definition: RadioButton.h:19
std::function< void()> onSelected
Wired by RadioGroup::addButton(); not meant to be set directly.
Definition: RadioButton.h:24
bool selected() const
Definition: RadioButton.h:26
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: RadioButton.h:29
void setSelected(bool selected)
Definition: RadioButton.h:27
Coordinates a set of RadioButtons so selecting one deselects the rest. Not itself a Widget - each Rad...
Definition: RadioButton.h:66
void addButton(RadioButton< RGB_T > &button)
Definition: RadioButton.h:70
int selectedIndex() const
Definition: RadioButton.h:76
std::function< void(int)> onChange
Definition: RadioButton.h:68
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