5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
13
14
15
16
17
18
19
20
21
26 Slider(
Bounds bounds,
float minValue = 0.0f,
float maxValue = 1.0f,
float value = 0.0f)
27 : minValue_(minValue), maxValue_(maxValue), value_(value) {
28 this->bounds = bounds;
29 value_ = clampValue(value_);
34 float value()
const {
return value_; }
35 void setValue(
float value) { value_ = clampValue(value); }
39 void draw(tinygpu::ISurface<RGB_T>& target)
override {
40 RGB_T trackColor =
this->theme().colors.surfaceVariant;
41 RGB_T activeColor =
this->theme().colors.primary;
42 RGB_T thumbColor =
this->theme().colors.primary;
44 trackColor = blend(trackColor,
this->theme().colors.surface, 0.5f);
45 activeColor = blend(activeColor,
this->theme().colors.surface, 0.5f);
46 thumbColor = blend(thumbColor,
this->theme().colors.surface, 0.4f);
49 const int32_t trackHeight = 4;
50 const int32_t trackTop =
this->bounds.centerY() - trackHeight / 2;
51 const size_t trackRadius = toPx(trackHeight / 2);
53 target.fillRoundRect(toPx(
this->bounds.x), toPx(trackTop), toPx(
this->bounds.w),
54 toPx(trackHeight), trackRadius, trackColor);
56 const int32_t thumbX = thumbCenterX();
57 const int32_t activeWidth = thumbX -
this->bounds.x;
58 if (activeWidth > 0) {
59 target.fillRoundRect(toPx(
this->bounds.x), toPx(trackTop), toPx(activeWidth),
60 toPx(trackHeight), trackRadius, activeColor);
63 const int32_t thumbRadius =
this->bounds.h / 2;
64 target.fillCircle(toPx(thumbX), toPx(
this->bounds.centerY()), toPx(thumbRadius), thumbColor);
67 bool onGesture(
const tinygpu::GestureEvent& event)
override {
68 if (!isTapGesture(event.type) && event.type != tinygpu::GestureType::kDrag) {
71 const float previous = value_;
72 setValueFromX(event.point.x);
73 if (onChange && value_ != previous) onChange(value_);
78 float minValue_ = 0.0f;
79 float maxValue_ = 1.0f;
82 float clampValue(
float value)
const {
83 return std::min(maxValue_, std::max(minValue_, value));
86 int32_t thumbCenterX()
const {
87 const float t = (maxValue_ > minValue_) ? (value_ - minValue_) / (maxValue_ - minValue_) : 0.0f;
88 return this->bounds.x +
static_cast<int32_t>(t *
static_cast<
float>(
this->bounds.w));
91 void setValueFromX(int32_t x) {
92 float t =
this->bounds.w > 0
93 ?
static_cast<
float>(x -
this->bounds.x) /
static_cast<
float>(
this->bounds.w)
95 t = std::min(1.0f, std::max(0.0f, t));
96 value_ = clampValue(minValue_ + t * (maxValue_ - minValue_));
Draggable track-and-thumb slider over [minValue, maxValue].
Definition: Slider.h:23
bool isDraggable() const override
Definition: Slider.h:37
Slider(Bounds bounds, float minValue=0.0f, float maxValue=1.0f, float value=0.0f)
Definition: Slider.h:26
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Slider.h:67
void setValue(float value)
Definition: Slider.h:35
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Slider.h:39
float value() const
Definition: Slider.h:34
std::function< void(float)> onChange
Definition: Slider.h:32
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21