FastLED 3.9.15
Loading...
Searching...
No Matches
ui.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <stdint.h>
5
6#include "fl/audio.h"
7#include "fl/engine_events.h"
8#include "fl/function_list.h"
9#include "fl/math_macros.h"
10#include "fl/namespace.h"
11#include "fl/template_magic.h"
12#include "fl/ui_impl.h"
13#include "fl/unused.h"
14#include "platforms/ui_defs.h"
15#include "sensors/button.h"
16
17#define FL_NO_COPY(CLASS) \
18 CLASS(const CLASS &) = delete; \
19 CLASS &operator=(const CLASS &) = delete;
20
21namespace fl {
22
23// If the platform is missing ui components, provide stubs.
24
25class UISlider : public UISliderImpl {
26 public:
28 using Super = UISliderImpl;
29 // If step is -1, it will be calculated as (max - min) / 100
30 UISlider(const char *name, float value = 128.0f, float min = 1,
31 float max = 255, float step = -1.f)
32 : UISliderImpl(name, value, min, max, step), mListener(this) {}
33 float value() const { return Super::value(); }
34 float value_normalized() const {
35 float min = Super::getMin();
36 float max = Super::getMax();
37 if (ALMOST_EQUAL(max, min, 0.0001f)) {
38 return 0;
39 }
40 return (value() - min) / (max - min);
41 }
42 float getMax() const { return Super::getMax(); }
43 void setValue(float value);
44 operator float() const { return Super::value(); }
45 operator uint8_t() const { return static_cast<uint8_t>(Super::value()); }
46 operator uint16_t() const { return static_cast<uint16_t>(Super::value()); }
47 operator int() const { return static_cast<int>(Super::value()); }
48 template <typename T> T as() const {
49 return static_cast<T>(Super::value());
50 }
51
52 int as_int() const { return static_cast<int>(Super::value()); }
53
55 Super::setValue(value);
56 return *this;
57 }
59 Super::setValue(static_cast<float>(value));
60 return *this;
61 }
62
63
64
65 int onChanged(function<void(UISlider &)> callback) {
66 int out = mCallbacks.add(callback);
67 mListener.addToEngineEventsOnce();
68 return out;
69 }
70 void clearCallbacks() { mCallbacks.clear(); }
71
72 protected:
74 Listener(UISlider *owner) : mOwner(owner) {
76 }
78 if (added) {
80 }
81 }
83 if (added) {
84 return;
85 }
87 added = true;
88 }
89 void onBeginFrame() override;
90
91 private:
93 bool added = false;
94 };
95
96 private:
98 float mLastFrameValue = 0;
101};
102
103// template operator for >= against a jsSliderImpl
104
105class UIButton : public UIButtonImpl {
106 public:
108 using Super = UIButtonImpl;
109 UIButton(const char *name) : UIButtonImpl(name), mListener(this) {}
111 bool isPressed() const {
112 if (Super::isPressed()) {
113 return true;
114 }
115 // If we have a real button, check if it's pressed
116 if (mRealButton) {
117 return mRealButton->isPressed();
118 }
119 // Otherwise, return the default state
120 return false;
121 }
122 bool clicked() const {
123 if (Super::clicked()) {
124 return true;
125 }
126 if (mRealButton) {
127 // If we have a real button, check if it was clicked
128 return mRealButton->isPressed();
129 }
130 return false;
131 }
132 int clickedCount() const { return Super::clickedCount(); }
133 operator bool() const { return clicked(); }
134
135 void addRealButton(const Button& pin) {
136 mRealButton.reset(new Button(pin));
137 }
138
139 void click() { Super::click(); }
140 int onChanged(function<void(UIButton &)> callback) {
141 int id = mCallbacks.add(callback);
142 mListener.addToEngineEventsOnce();
143 return id;
144 }
145
146 int onClicked(function<void()> callback) {
147 int id = mCallbacks.add([callback](UIButton &btn) {
148 if (btn.clicked()) {
149 callback();
150 }
151 });
152 mListener.addToEngineEventsOnce();
153 return id;
154 }
155
156 void removeCallback(int id) { mCallbacks.remove(id); }
157 void clearCallbacks() { mCallbacks.clear(); }
158
159 protected:
161 Listener(UIButton *owner) : mOwner(owner) {
163 }
165 if (added) {
167 }
168 }
170 if (added) {
171 return;
172 }
174 added = true;
175 }
176 void onBeginFrame() override;
177
178 private:
180 bool added = false;
181 bool mClickedLastFrame = false;
182 };
183
184 private:
188};
189
191 public:
194 UICheckbox(const char *name, bool value = false)
195 : UICheckboxImpl(name, value), mListener(this) {}
197
198 operator bool() const { return value(); }
199 explicit operator int() const { return static_cast<int>(value()); }
201 impl() = value;
202 return *this;
203 }
204
205
206 void onChanged(function<void(UICheckbox &)> callback) {
207 mCallbacks.add(callback);
208 mListener.addToEngineEventsOnce();
209 }
210 void clearCallbacks() { mCallbacks.clear(); }
211
212 protected:
214 Listener(UICheckbox *owner) : mOwner(owner) {
216 }
218 if (added) {
220 }
221 }
223 if (added) {
224 return;
225 }
227 added = true;
228 }
229 void onBeginFrame() override;
230
231 private:
233 bool added = false;
234 };
235
236 private:
237 Super &impl() { return *this; }
239 bool mLastFrameValue = false;
242};
243
245 public:
248 UINumberField(const char *name, double value, double min = 0,
249 double max = 100)
250 : UINumberFieldImpl(name, value, min, max), mListener(this) {}
252 double value() const { return Super::value(); }
254 operator double() const { return Super::value(); }
255 operator int() const { return static_cast<int>(Super::value()); }
258 return *this;
259 }
261 setValue(static_cast<double>(value));
262 return *this;
263 }
264
265
266
267 void onChanged(function<void(UINumberField &)> callback) {
268 mCallbacks.add(callback);
269 mListener.addToEngineEventsOnce();
270 }
271 void clearCallbacks() { mCallbacks.clear(); }
272
273 private:
275 Listener(UINumberField *owner) : mOwner(owner) {
277 }
279 if (added) {
281 }
282 }
284 if (added) {
285 return;
286 }
288 added = true;
289 }
290 void onBeginFrame() override;
291
292 private:
294 bool added = false;
295 };
296
298 double mLastFrameValue = 0;
301
302 Super &impl() { return *this; }
303};
304
305class UITitle : public UITitleImpl {
306 public:
308 UITitle(const char *name) : UITitleImpl(name) {}
310};
311
313 public:
315 UIDescription(const char *name) : UIDescriptionImpl(name) {}
317};
318
319class UIAudio : public UIAudioImpl {
320 public:
322 using Super = UIAudioImpl;
323 UIAudio(const char *name) : UIAudioImpl(name) {}
325 AudioSample next() { return Super::next(); }
326 bool hasNext() { return Super::hasNext(); }
327};
328
329#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS) \
330 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >=) \
331 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <=) \
332 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >) \
333 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <) \
334 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, ==) \
335 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, !=)
336
341
342} // end namespace fl
static void addListener(Listener *listener, int priority=0)
static void removeListener(Listener *listener)
~UIAudio()
Definition ui.h:324
AudioSample next()
Definition ui.h:325
UIAudio(const char *name)
Definition ui.h:323
FL_NO_COPY(UIAudio) using Super
bool hasNext()
Definition ui.h:326
UIAudioImpl(const char *name)
Definition ui_impl.h:195
FunctionList< UIButton & > mCallbacks
Definition ui.h:185
void removeCallback(int id)
Definition ui.h:156
void addRealButton(const Button &pin)
Definition ui.h:135
bool clicked() const
Definition ui.h:122
fl::scoped_ptr< Button > mRealButton
Definition ui.h:187
bool isPressed() const
Definition ui.h:111
int onChanged(function< void(UIButton &)> callback)
Definition ui.h:140
void clearCallbacks()
Definition ui.h:157
FL_NO_COPY(UIButton) using Super
int onClicked(function< void()> callback)
Definition ui.h:146
Listener mListener
Definition ui.h:186
UIButton(const char *name)
Definition ui.h:109
int clickedCount() const
Definition ui.h:132
~UIButton()
Definition ui.h:110
void click()
Definition ui.h:139
fl::Str name() const
Definition ui_impl.h:106
UIButtonImpl(const char *name)
Definition ui_impl.h:99
UICheckbox & operator=(bool value)
Definition ui.h:200
Listener mListener
Definition ui.h:241
Super & impl()
Definition ui.h:237
void clearCallbacks()
Definition ui.h:210
FL_NO_COPY(UICheckbox)
UICheckboxImpl Super
Definition ui.h:193
bool mLastFrameValue
Definition ui.h:239
FunctionList< UICheckbox & > mCallbacks
Definition ui.h:238
void onChanged(function< void(UICheckbox &)> callback)
Definition ui.h:206
~UICheckbox()
Definition ui.h:196
bool mLastFrameValueValid
Definition ui.h:240
UICheckbox(const char *name, bool value=false)
Definition ui.h:194
bool value() const
Definition ui_impl.h:132
UICheckboxImpl(const char *name, bool value=false)
Definition ui_impl.h:118
FL_NO_COPY(UIDescription)
UIDescription(const char *name)
Definition ui.h:315
UIDescriptionImpl(const char *name)
Definition ui_impl.h:186
double value() const
Definition ui.h:252
UINumberField & operator=(double value)
Definition ui.h:256
UINumberFieldImpl Super
Definition ui.h:247
FunctionList< UINumberField & > mCallbacks
Definition ui.h:300
Listener mListener
Definition ui.h:297
UINumberField & operator=(int value)
Definition ui.h:260
FL_NO_COPY(UINumberField)
UINumberField(const char *name, double value, double min=0, double max=100)
Definition ui.h:248
bool mLastFrameValueValid
Definition ui.h:299
double mLastFrameValue
Definition ui.h:298
Super & impl()
Definition ui.h:302
void setValue(double value)
Definition ui.h:253
void onChanged(function< void(UINumberField &)> callback)
Definition ui.h:267
void clearCallbacks()
Definition ui.h:271
UINumberFieldImpl(const char *name, double value, double min=0, double max=100)
Definition ui_impl.h:145
double value() const
Definition ui_impl.h:151
void setValue(double value)
Definition ui_impl.h:152
Listener mListener
Definition ui.h:100
bool mLastFramevalueValid
Definition ui.h:99
UISlider & operator=(int value)
Definition ui.h:58
UISlider(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition ui.h:30
T as() const
Definition ui.h:48
void clearCallbacks()
Definition ui.h:70
float getMax() const
Definition ui.h:42
FL_NO_COPY(UISlider) using Super
float mLastFrameValue
Definition ui.h:98
void setValue(float value)
Definition ui.cpp:11
FunctionList< UISlider & > mCallbacks
Definition ui.h:97
int as_int() const
Definition ui.h:52
UISlider & operator=(float value)
Definition ui.h:54
float value_normalized() const
Definition ui.h:34
int onChanged(function< void(UISlider &)> callback)
Definition ui.h:65
float value() const
Definition ui.h:33
UISliderImpl(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition ui_impl.h:51
~UITitle()
Definition ui.h:309
UITitle(const char *name)
Definition ui.h:308
FL_NO_COPY(UITitle)
UITitleImpl(const char *name)
Definition ui_impl.h:176
#define ALMOST_EQUAL(a, b, small)
Definition math_macros.h:33
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Listener(UIButton *owner)
Definition ui.h:161
bool mClickedLastFrame
Definition ui.h:181
void addToEngineEventsOnce()
Definition ui.h:169
UIButton * mOwner
Definition ui.h:179
void onBeginFrame() override
Definition ui.cpp:32
UICheckbox * mOwner
Definition ui.h:232
void onBeginFrame() override
Definition ui.cpp:53
Listener(UICheckbox *owner)
Definition ui.h:214
void addToEngineEventsOnce()
Definition ui.h:222
Listener(UINumberField *owner)
Definition ui.h:275
void addToEngineEventsOnce()
Definition ui.h:283
UINumberField * mOwner
Definition ui.h:293
void onBeginFrame() override
Definition ui.cpp:67
void addToEngineEventsOnce()
Definition ui.h:82
void onBeginFrame() override
Definition ui.cpp:18
UISlider * mOwner
Definition ui.h:92
Listener(UISlider *owner)
Definition ui.h:74
#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS)
Definition ui.h:329