FastLED 3.9.3
Loading...
Searching...
No Matches
ui.h
1#pragma once
2
3#include <stdint.h>
4
5#include "platforms/ui_defs.h"
6#include "namespace.h"
7#include "math_macros.h"
8
9#ifndef FASTLED_HAS_UI_SLIDER
10#define FASTLED_HAS_UI_SLIDER 0
11#endif
12
13#ifndef FASTLED_HAS_UI_BUTTON
14#define FASTLED_HAS_UI_BUTTON 0
15#endif
16
17#ifndef FASTLED_HAS_UI_CHECKBOX
18#define FASTLED_HAS_UI_CHECKBOX 0
19#endif
20
21#ifndef FASTLED_HAS_UI_NUMBER_FIELD
22#define FASTLED_HAS_UI_NUMBER_FIELD 0
23#endif
24
25#ifndef FASTLED_HAS_UI_TITLE
26#define FASTLED_HAS_UI_TITLE 0
27#endif
28
29#ifndef FASTLED_HAS_UI_DESCRIPTION
30#define FASTLED_HAS_UI_DESCRIPTION 0
31#endif
32
33FASTLED_NAMESPACE_BEGIN
34
35
36// If the platform is missing ui components, provide stubs.
37
38#if !FASTLED_HAS_UI_SLIDER
39
40class Slider {
41 public:
42 Slider(const char *name, float value = 128.0f, float min = 1, float max = 255, float step = 1)
43 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {}
44 ~Slider() {}
45 float value() const { return mValue; }
46 void setValue(float value) { mValue = MAX(mMin, MIN(mMax, value)); }
47 operator float() const { return mValue; }
48 operator uint8_t() const { return static_cast<uint8_t>(mValue); }
49 operator uint16_t() const { return static_cast<uint16_t>(mValue); }
50 operator int() const { return static_cast<int>(mValue); }
51 template <typename T> T as() const { return static_cast<T>(mValue); }
52
53 Slider& operator=(float value) { setValue(value); return *this; }
54 Slider& operator=(int value) { setValue(static_cast<float>(value)); return *this; }
55 private:
56 float mValue;
57 float mMin;
58 float mMax;
59};
60
61
62// template operator for >= against a jsSlider
63
64
65
66#endif
67
68#if !FASTLED_HAS_UI_BUTTON
69
70class Button {
71 public:
72 Button(const char *name) {}
73 ~Button() {}
74 bool isPressed() const { return false; }
75 bool clicked() const { return false; }
76 operator bool() const { return false; }
77};
78
79#endif
80
81#if !FASTLED_HAS_UI_CHECKBOX
82
83class Checkbox {
84 public:
85 Checkbox(const char *name, bool value = false) : mValue(value) {}
86 ~Checkbox() {}
87 operator bool() const { return mValue; }
88 operator int() const { return mValue ? 1 : 0; }
89 Checkbox& operator=(bool value) { setValue(value); return *this; }
90 Checkbox& operator=(int value) { setValue(value != 0); return *this; }
91 private:
92 void setValue(bool value) { mValue = value; }
93 bool mValue;
94};
95
96#endif
97
98#if !FASTLED_HAS_UI_NUMBER_FIELD
99
101 public:
102 NumberField(const char *name, double value, double min = 0, double max = 100)
103 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {}
104 ~NumberField() {}
105 double value() const { return mValue; }
106 void setValue(double value) { mValue = MAX(mMin, MIN(mMax, value)); }
107 operator double() const { return mValue; }
108 operator int() const { return static_cast<int>(mValue); }
109 NumberField& operator=(double value) { setValue(value); return *this; }
110 NumberField& operator=(int value) { setValue(static_cast<double>(value)); return *this; }
111 private:
112 double mValue;
113 double mMin;
114 double mMax;
115};
116
117#endif
118
119#if !FASTLED_HAS_UI_TITLE
120
121
122class Title {
123 public:
124 Title(const char *name) {}
125 ~Title() {}
126};
127
128#endif
129
130#if !FASTLED_HAS_UI_DESCRIPTION
131
133 public:
134 Description(const char *name) {}
135 ~Description() {}
136};
137
138#endif
139
140#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS) \
141template <typename T> bool operator>= (T v, const UI_CLASS& ui) { return ui >= v; } \
142template <typename T> bool operator<= (T v, const UI_CLASS& ui) { return ui <= v; } \
143template <typename T> bool operator> (T v, const UI_CLASS& ui) { return ui > v; } \
144template <typename T> bool operator< (T v, const UI_CLASS& ui) { return ui < v; } \
145template <typename T> bool operator== (T v, const UI_CLASS& ui) { return ui == v; } \
146template <typename T> bool operator!= (T v, const UI_CLASS& ui) { return ui != v; } \
147template <typename T> bool operator>=(const UI_CLASS& ui, T v) { return ui >= v; } \
148template <typename T> bool operator<=(const UI_CLASS& ui, T v) { return ui <= v; } \
149template <typename T> bool operator>(const UI_CLASS& ui, T v) { return ui > v; } \
150template <typename T> bool operator<(const UI_CLASS& ui, T v) { return ui < v; } \
151template <typename T> bool operator==(const UI_CLASS& ui, T v) { return ui == v; } \
152template <typename T> bool operator!=(const UI_CLASS& ui, T v) { return ui != v; }
153
154FASTLED_UI_DEFINE_OPERATORS(Slider);
155FASTLED_UI_DEFINE_OPERATORS(NumberField);
156FASTLED_UI_DEFINE_OPERATORS(Checkbox);
157FASTLED_UI_DEFINE_OPERATORS(Button);
158
159
160FASTLED_NAMESPACE_END
Definition ui.h:70
Definition ui.h:83
Definition ui.h:40
Definition ui.h:122