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"
28 void addSegment(
const char* label) { segments_.push_back(label); }
31 bool isSelected(
int index)
const {
return (selectedMask_ & (1u << index)) != 0; }
33 if (index < 0 || index >=
static_cast<
int>(segments_.size()))
return;
35 selectedMask_ = selected ? (1u << index) : 0u;
39 selectedMask_ |= (1u << index);
41 selectedMask_ &= ~(1u << index);
45 void draw(tinygpu::ISurface<RGB_T>& target)
override {
46 const int count =
static_cast<
int>(segments_.size());
47 if (count == 0)
return;
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);
53 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
54 toPx(
this->bounds.h), radius, outline);
56 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
57 for (
int i = 0; i < count; ++i) {
59 const int32_t x = slot
.x;
60 const int32_t segmentWidth = slot
.w;
62 RGB_T background = selected ?
this->theme().colors.secondaryContainer :
this->theme().colors.surface;
64 selected ?
this->theme().colors.onSecondaryContainer :
this->theme().colors.onSurfaceVariant;
66 background = blend(background,
this->theme().colors.surface, 0.5f);
67 foreground = blend(foreground,
this->theme().colors.surface, 0.5f);
73 target.fillRect(toPx(x + 1), toPx(
this->bounds.y + 1), toPx(segmentWidth - 1),
74 toPx(
this->bounds.h - 2), background);
77 target.drawLine(toPx(x), toPx(
this->bounds.y), toPx(x), toPx(
this->bounds.bottom()), outline);
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);
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;
98 selectedMask_ = 1u << index;
100 if (onChange) onChange(selectedMask_);
105 std::vector<
const char*> segments_;
106 uint32_t selectedMask_ = 1u;
107 bool multiSelect_ =
false;
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
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