TinyMaterialDesign
Loading...
Searching...
No Matches
SegmentedButton.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/LinearLayout.h"
9#include "TinyMaterialDesign/Core/Widget.h"
10
11namespace tinymd {
12
13/// @brief Connected row of segments sharing one pill-shaped outline (M3 "segmented
14/// button"). Single-select (radio-like, the default) or multi-select
15/// (checkbox-like, toggling independently) - see setMultiSelect().
16template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
17class SegmentedButton : public Widget<RGB_T> {
18 public:
19 SegmentedButton() = default;
20 explicit SegmentedButton(Bounds bounds) { this->bounds = bounds; }
21
22 /// Fired whenever selection changes, with a bitmask of selected segments
23 /// (bit i set == segment i selected) - a single-select bar always has
24 /// exactly one bit set. Limits a bar to at most 32 segments (the bitmask's
25 /// width), well beyond what's usable as an actual row of segments.
26 std::function<void(uint32_t)> onChange;
27
28 void addSegment(const char* label) { segments_.push_back(label); }
29 void setMultiSelect(bool multiSelect) { multiSelect_ = multiSelect; }
30
31 bool isSelected(int index) const { return (selectedMask_ & (1u << index)) != 0; }
32 void setSelected(int index, bool selected) {
33 if (index < 0 || index >= static_cast<int>(segments_.size())) return;
34 if (!multiSelect_) {
35 selectedMask_ = selected ? (1u << index) : 0u;
36 return;
37 }
38 if (selected) {
39 selectedMask_ |= (1u << index);
40 } else {
41 selectedMask_ &= ~(1u << index);
42 }
43 }
44
45 void draw(tinygpu::ISurface<RGB_T>& target) override {
46 const int count = static_cast<int>(segments_.size());
47 if (count == 0) return;
48 const LinearLayout slots(this->bounds, LayoutAxis::Horizontal, /*spacing=*/0);
49 const size_t radius = toPx(this->theme().shape.full);
50 RGB_T outline = this->theme().colors.outline;
51 if (!this->enabled) outline = blend(outline, this->theme().colors.surface, 0.5f);
52
53 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
54 toPx(this->bounds.h), radius, outline);
55
56 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
57 for (int i = 0; i < count; ++i) {
58 const Bounds slot = slots.itemRect(i, count);
59 const int32_t x = slot.x;
60 const int32_t segmentWidth = slot.w;
61 const bool selected = isSelected(i);
62 RGB_T background = selected ? this->theme().colors.secondaryContainer : this->theme().colors.surface;
63 RGB_T foreground =
64 selected ? this->theme().colors.onSecondaryContainer : this->theme().colors.onSurfaceVariant;
65 if (!this->enabled) {
66 background = blend(background, this->theme().colors.surface, 0.5f);
67 foreground = blend(foreground, this->theme().colors.surface, 0.5f);
68 }
69
70 if (selected) {
71 // Inset by 1px so the selected fill sits inside the shared outline
72 // rather than painting over it.
73 target.fillRect(toPx(x + 1), toPx(this->bounds.y + 1), toPx(segmentWidth - 1),
74 toPx(this->bounds.h - 2), background);
75 }
76 if (i > 0) {
77 target.drawLine(toPx(x), toPx(this->bounds.y), toPx(x), toPx(this->bounds.bottom()), outline);
78 }
79
80 const size_t textWidth = font.measureTextWidth(segments_[i]);
81 const int32_t textX = x + segmentWidth / 2 - static_cast<int32_t>(textWidth) / 2;
82 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
83 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY), segments_[i],
84 foreground, background, false);
85 }
86 }
87
88 bool onGesture(const tinygpu::GestureEvent& event) override {
89 const int count = static_cast<int>(segments_.size());
90 if (!isTapGesture(event.type) || count == 0) return false;
91 const int32_t segmentWidth = this->bounds.w / count;
92 int index = (event.point.x - this->bounds.x) / segmentWidth;
93 if (index < 0 || index >= count) return true;
94
95 if (multiSelect_) {
97 } else {
98 selectedMask_ = 1u << index;
99 }
100 if (onChange) onChange(selectedMask_);
101 return true;
102 }
103
104 private:
105 std::vector<const char*> segments_;
106 uint32_t selectedMask_ = 1u; // segment 0 selected by default
107 bool multiSelect_ = false;
108};
109
113
114} // namespace tinymd
Computes equal- or weighted-split rects along one axis of a container.
Definition: LinearLayout.h:24
Bounds itemRect(int index, int count) const
Bounds for the index-th of count equally-sized items (0-based).
Definition: LinearLayout.h:30
Connected row of segments sharing one pill-shaped outline (M3 "segmented button")....
Definition: SegmentedButton.h:17
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: SegmentedButton.h:88
SegmentedButton(Bounds bounds)
Definition: SegmentedButton.h:20
bool isSelected(int index) const
Definition: SegmentedButton.h:31
void setMultiSelect(bool multiSelect)
Definition: SegmentedButton.h:29
void setSelected(int index, bool selected)
Definition: SegmentedButton.h:32
void addSegment(const char *label)
Definition: SegmentedButton.h:28
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SegmentedButton.h:45
std::function< void(uint32_t)> onChange
Definition: SegmentedButton.h:26
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
LayoutAxis
Definition: LinearLayout.h:6
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t w
Definition: Bounds.h:24
int32_t x
Definition: Bounds.h:22