TinyMaterialDesign
Loading...
Searching...
No Matches
Slider.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
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
10namespace tinymd {
11
12/**
13 * @brief Draggable track-and-thumb slider over [minValue, maxValue].
14 *
15 * Reports isDraggable() == true so Screen::isDraggableAt() (wired to
16 * GestureDetector::isDraggable) classifies a press-and-move starting on the
17 * slider as GestureType::kDrag rather than kPan/kScroll - and Screen latches
18 * the drag to this widget for its whole lifetime, so the thumb keeps
19 * tracking the finger even past the slider's own bounds. Tapping anywhere
20 * on the track also jumps the thumb there.
21 */
22template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
23class Slider : public Widget<RGB_T> {
24 public:
25 Slider() = default;
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_);
30 }
31
32 std::function<void(float)> onChange;
33
34 float value() const { return value_; }
35 void setValue(float value) { value_ = clampValue(value); }
36
37 bool isDraggable() const override { return true; }
38
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;
43 if (!this->enabled) {
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);
47 }
48
49 const int32_t trackHeight = 4;
50 const int32_t trackTop = this->bounds.centerY() - trackHeight / 2;
51 const size_t trackRadius = toPx(trackHeight / 2);
52
53 target.fillRoundRect(toPx(this->bounds.x), toPx(trackTop), toPx(this->bounds.w),
54 toPx(trackHeight), trackRadius, trackColor);
55
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);
61 }
62
63 const int32_t thumbRadius = this->bounds.h / 2;
64 target.fillCircle(toPx(thumbX), toPx(this->bounds.centerY()), toPx(thumbRadius), thumbColor);
65 }
66
67 bool onGesture(const tinygpu::GestureEvent& event) override {
68 if (!isTapGesture(event.type) && event.type != tinygpu::GestureType::kDrag) {
69 return false;
70 }
71 const float previous = value_;
72 setValueFromX(event.point.x);
73 if (onChange && value_ != previous) onChange(value_);
74 return true;
75 }
76
77 private:
78 float minValue_ = 0.0f;
79 float maxValue_ = 1.0f;
80 float value_ = 0.0f;
81
82 float clampValue(float value) const {
83 return std::min(maxValue_, std::max(minValue_, value));
84 }
85
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));
89 }
90
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)
94 : 0.0f;
95 t = std::min(1.0f, std::max(0.0f, t));
96 value_ = clampValue(minValue_ + t * (maxValue_ - minValue_));
97 }
98};
99
103
104} // namespace tinymd
Draggable track-and-thumb slider over [minValue, maxValue].
Definition: Slider.h:23
Slider()=default
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
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