TinyMaterialDesign
Loading...
Searching...
No Matches
Switch.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3
4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyMaterialDesign/Core/Widget.h"
8
9namespace tinymd {
10
11/// @brief Track-and-thumb toggle switch. Toggled by tap (drag-to-slide is a
12/// possible follow-up, not implemented here).
13template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
14class Switch : public Widget<RGB_T> {
15 public:
16 Switch() = default;
17 explicit Switch(Bounds bounds, bool value = false) : value_(value) { this->bounds = bounds; }
18
19 std::function<void(bool)> onChange;
20
21 bool value() const { return value_; }
22 void setValue(bool value) { value_ = value; }
23
24 void draw(tinygpu::ISurface<RGB_T>& target) override {
25 RGB_T trackColor = value_ ? this->theme().colors.primary : this->theme().colors.surfaceVariant;
26 RGB_T trackBorder = this->theme().colors.outline;
27 RGB_T thumbColor = value_ ? this->theme().colors.onPrimary : this->theme().colors.outline;
28 if (!this->enabled) {
29 trackColor = blend(trackColor, this->theme().colors.surface, 0.5f);
30 trackBorder = blend(trackBorder, this->theme().colors.surface, 0.5f);
31 thumbColor = blend(thumbColor, this->theme().colors.surface, 0.4f);
32 }
33
34 const size_t trackRadius = toPx(this->bounds.h / 2);
35 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
36 toPx(this->bounds.w), toPx(this->bounds.h), trackRadius,
37 trackColor);
38 if (!value_) {
39 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
40 toPx(this->bounds.w), toPx(this->bounds.h), trackRadius,
41 trackBorder);
42 }
43
44 const int32_t thumbRadius = this->bounds.h / 2 - 3;
45 const int32_t thumbCx =
46 value_ ? this->bounds.right() - this->bounds.h / 2 : this->bounds.x + this->bounds.h / 2;
47 const int32_t thumbCy = this->bounds.centerY();
48 target.fillCircle(toPx(thumbCx), toPx(thumbCy), toPx(thumbRadius), thumbColor);
49 }
50
51 bool onGesture(const tinygpu::GestureEvent& event) override {
52 if (!isTapGesture(event.type)) return false;
53 value_ = !value_;
54 if (onChange) onChange(value_);
55 return true;
56 }
57
58 private:
59 bool value_ = false;
60};
61
65
66} // namespace tinymd
Track-and-thumb toggle switch. Toggled by tap (drag-to-slide is a possible follow-up,...
Definition: Switch.h:14
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Switch.h:51
Switch(Bounds bounds, bool value=false)
Definition: Switch.h:17
std::function< void(bool)> onChange
Definition: Switch.h:19
bool value() const
Definition: Switch.h:21
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Switch.h:24
Switch()=default
void setValue(bool value)
Definition: Switch.h:22
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