TinyMaterialDesign
Loading...
Searching...
No Matches
Checkbox.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#include "TinyMaterialDesign/Draw/Icons.h"
9
10namespace tinymd {
11
12/// Square checkbox, toggled by tap.
13template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
14class Checkbox : public Widget<RGB_T> {
15 public:
16 Checkbox() = default;
17 explicit Checkbox(Bounds bounds, bool checked = false) : checked_(checked) {
18 this->bounds = bounds;
19 }
20
21 std::function<void(bool)> onChange;
22
23 bool checked() const { return checked_; }
24 void setChecked(bool checked) { checked_ = checked; }
25
26 void draw(tinygpu::ISurface<RGB_T>& target) override {
27 RGB_T fill = checked_ ? this->theme().colors.primary : this->theme().colors.surface;
28 RGB_T border = checked_ ? this->theme().colors.primary : this->theme().colors.outline;
29 RGB_T mark = this->theme().colors.onPrimary;
30 if (!this->enabled) {
31 fill = blend(fill, this->theme().colors.surface, 0.6f);
32 border = blend(border, this->theme().colors.surface, 0.5f);
33 mark = blend(mark, this->theme().colors.surface, 0.4f);
34 }
35
36 const size_t radius = toPx(this->theme().shape.small);
37 if (checked_) {
38 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
39 toPx(this->bounds.w), toPx(this->bounds.h), radius, fill);
40 }
41 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y),
42 toPx(this->bounds.w), toPx(this->bounds.h), radius, border);
43 if (checked_) {
44 drawCheckmark(target, this->bounds, mark);
45 }
46 }
47
48 bool onGesture(const tinygpu::GestureEvent& event) override {
49 if (!isTapGesture(event.type)) return false;
50 checked_ = !checked_;
51 if (onChange) onChange(checked_);
52 return true;
53 }
54
55 private:
56 bool checked_ = false;
57};
58
62
63} // namespace tinymd
Square checkbox, toggled by tap.
Definition: Checkbox.h:14
void setChecked(bool checked)
Definition: Checkbox.h:24
bool checked() const
Definition: Checkbox.h:23
Checkbox(Bounds bounds, bool checked=false)
Definition: Checkbox.h:17
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Checkbox.h:48
std::function< void(bool)> onChange
Definition: Checkbox.h:21
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Checkbox.h:26
Checkbox()=default
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