4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyMaterialDesign/Core/Widget.h"
17 explicit Switch(
Bounds bounds,
bool value =
false) : value_(value) {
this->bounds = bounds; }
21 bool value()
const {
return value_; }
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;
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);
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,
39 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y),
40 toPx(
this->bounds.w), toPx(
this->bounds.h), trackRadius,
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);
51 bool onGesture(
const tinygpu::GestureEvent& event)
override {
52 if (!isTapGesture(event.type))
return false;
54 if (onChange) onChange(value_);
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
void setValue(bool value)
Definition: Switch.h:22
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21