FastLED 3.9.15
Loading...
Searching...
No Matches
ui_impl.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stdint.h"
4
5#include "fl/audio.h"
6#include "fl/math_macros.h"
7#include "fl/namespace.h"
8#include "fl/str.h"
9#include "fl/type_traits.h"
10#include "fl/unused.h"
11#include "fl/vector.h"
12#include "fl/warn.h"
13#include "platforms/ui_defs.h"
14
15#ifndef FASTLED_HAS_UI_SLIDER
16#define FASTLED_HAS_UI_SLIDER 0
17#endif
18
19#ifndef FASTLED_HAS_UI_BUTTON
20#define FASTLED_HAS_UI_BUTTON 0
21#endif
22
23#ifndef FASTLED_HAS_UI_CHECKBOX
24#define FASTLED_HAS_UI_CHECKBOX 0
25#endif
26
27#ifndef FASTLED_HAS_UI_NUMBER_FIELD
28#define FASTLED_HAS_UI_NUMBER_FIELD 0
29#endif
30
31#ifndef FASTLED_HAS_UI_TITLE
32#define FASTLED_HAS_UI_TITLE 0
33#endif
34
35#ifndef FASTLED_HAS_UI_DESCRIPTION
36#define FASTLED_HAS_UI_DESCRIPTION 0
37#endif
38
39#ifndef FASTLED_HAS_UI_AUDIO
40#define FASTLED_HAS_UI_AUDIO 0
41#endif
42
43#ifndef FASTLED_HAS_UI_DROPDOWN
44#define FASTLED_HAS_UI_DROPDOWN 0
45#endif
46
47namespace fl {
48
49// If the platform is missing ui components, provide stubs.
50
51#if !FASTLED_HAS_UI_SLIDER
52
54 public:
55 // If step is -1, it will be calculated as (max - min) / 100
56 UISliderImpl(const char *name, float value = 128.0f, float min = 1,
57 float max = 255, float step = -1.f)
58 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
59 FASTLED_UNUSED(name);
60 FASTLED_UNUSED(step);
61 if (value < min) {
62 mValue = min;
63 }
64 if (value > max) {
65 mValue = max;
66 }
67 }
69 float value() const { return mValue; }
70 float getMax() const { return mMax; }
71 float getMin() const { return mMin; }
72 void setValue(float value) { mValue = MAX(mMin, MIN(mMax, value)); }
73 operator float() const { return mValue; }
74 operator u8() const { return static_cast<u8>(mValue); }
75 operator u16() const { return static_cast<u16>(mValue); }
76 operator int() const { return static_cast<int>(mValue); }
77 template <typename T> T as() const { return static_cast<T>(mValue); }
78
79 int as_int() const { return static_cast<int>(mValue); }
80
81 // Stub method for group setting (does nothing on non-WASM platforms)
82 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
83
86 return *this;
87 }
89 setValue(static_cast<float>(value));
90 return *this;
91 }
92
93 private:
94 float mValue;
95 float mMin;
96 float mMax;
97};
98
99// template operator for >= against a jsSliderImpl
100
101#endif
102
103#if !FASTLED_HAS_UI_BUTTON
104
106 public:
109 bool isPressed() const { return false; }
110 bool clicked() const { return false; }
111 int clickedCount() const { return 0; }
112 operator bool() const { return false; }
113 void click() {}
114 fl::string name() const { return mName; }
115
116 // Stub method for group setting (does nothing on non-WASM platforms)
117 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
118
119 private:
121};
122
123#endif
124
125#if !FASTLED_HAS_UI_CHECKBOX
126
128 public:
129 UICheckboxImpl(const char *name, bool value = false) : mValue(value) {
130 FASTLED_UNUSED(name);
131 }
133 operator bool() const { return mValue; }
134 explicit operator int() const { return mValue ? 1 : 0; }
137 return *this;
138 }
140 setValue(value != 0);
141 return *this;
142 }
143 bool value() const { return mValue; }
144
145 // Stub method for group setting (does nothing on non-WASM platforms)
146 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
147
148 private:
149 void setValue(bool value) { mValue = value; }
150 bool mValue;
151};
152
153#endif
154
155#if !FASTLED_HAS_UI_NUMBER_FIELD
156
158 public:
159 UINumberFieldImpl(const char *name, double value, double min = 0,
160 double max = 100)
161 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
162 FASTLED_UNUSED(name);
163 }
165 double value() const { return mValue; }
166 void setValue(double value) { mValue = MAX(mMin, MIN(mMax, value)); }
167 operator double() const { return mValue; }
168 operator int() const { return static_cast<int>(mValue); }
171 return *this;
172 }
174 setValue(static_cast<double>(value));
175 return *this;
176 }
177
178 // Stub method for group setting (does nothing on non-WASM platforms)
179 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
180
181 private:
182 double mValue;
183 double mMin;
184 double mMax;
185};
186
187#endif
188
189#if !FASTLED_HAS_UI_TITLE
190
192 public:
193 UITitleImpl(const char *name) { FASTLED_UNUSED(name); }
195
196 // Stub method for group setting (does nothing on non-WASM platforms)
197 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
198};
199
200#endif
201
202#if !FASTLED_HAS_UI_DESCRIPTION
203
205 public:
206 UIDescriptionImpl(const char *name) { FASTLED_UNUSED(name); }
208
209 // Stub method for group setting (does nothing on non-WASM platforms)
210 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
211};
212
213#endif
214
215#if !FASTLED_HAS_UI_HELP
217 public:
220
221 // Stub method for group setting (does nothing on non-WASM platforms)
222 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
223
224 // Stub method for accessing markdown content
225 const fl::string& markdownContent() const { return mContent; }
226
227 private:
229};
230
231#endif
232
233#if !FASTLED_HAS_UI_AUDIO
235 public:
236 UIAudioImpl(const char *name) { FASTLED_UNUSED(name); }
238
240 FASTLED_WARN("Audio sample not implemented");
241 return AudioSample();
242 }
243
244 bool hasNext() {
245 FASTLED_WARN("Audio sample not implemented");
246 return false;
247 }
248
249 // Stub method for group setting (does nothing on non-WASM platforms)
250 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
251};
252#endif
253
254#if !FASTLED_HAS_UI_DROPDOWN
256 public:
257 // Constructor with array of options (size determined automatically)
258 template<fl::size N>
259 UIDropdownImpl(const fl::string &name, const fl::string (&options)[N])
260 : UIDropdownImpl(name, options, N) {}
261
262 // Constructor with fl::vector of options
264 : mSelectedIndex(0) {
265 FASTLED_UNUSED(name);
266 for (fl::size i = 0; i < options.size(); ++i) {
267 mOptions.push_back(options[i]);
268 }
269 if (mOptions.empty()) {
270 mOptions.push_back(fl::string("No options"));
271 }
272 }
273
274 template<typename Iterator>
275 UIDropdownImpl(const fl::string &name, Iterator begin, Iterator end)
276 : mSelectedIndex(0) {
277 FASTLED_UNUSED(name);
278 for (Iterator it = begin; it != end; ++it) {
279 mOptions.push_back(*it);
280 }
281 if (mOptions.empty()) {
282 mOptions.push_back(fl::string("No options"));
283 }
284 }
285
286 // Constructor with fl::span<fl::string>
288 : mSelectedIndex(0) {
289 FASTLED_UNUSED(name);
290 for (fl::size i = 0; i < options.size(); ++i) {
291 mOptions.push_back(options[i]);
292 }
293 if (mOptions.empty()) {
294 mOptions.push_back(fl::string("No options"));
295 }
296 }
297
298 // Constructor with initializer_list (only available if C++11 support exists)
299 UIDropdownImpl(const fl::string &name, fl::initializer_list<fl::string> options)
300 : mSelectedIndex(0) {
301 FASTLED_UNUSED(name);
302 for (const auto& option : options) {
303 mOptions.push_back(option);
304 }
305 if (mOptions.empty()) {
306 mOptions.push_back(fl::string("No options"));
307 }
308 }
309
311
312 fl::string value() const {
313 if (mSelectedIndex < mOptions.size()) {
314 return mOptions[mSelectedIndex];
315 }
316 return fl::string("Invalid");
317 }
318
319 int value_int() const { return static_cast<int>(mSelectedIndex); }
320
321 void setSelectedIndex(int index) {
322 if (index >= 0 && index < static_cast<int>(mOptions.size())) {
323 mSelectedIndex = static_cast<fl::size>(index);
324 }
325 }
326
327 fl::size getOptionCount() const { return mOptions.size(); }
328 fl::string getOption(fl::size index) const {
329 if (index < mOptions.size()) {
330 return mOptions[index];
331 }
332 return fl::string("Invalid");
333 }
334
335 // Stub method for group setting (does nothing on non-WASM platforms)
336 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
337
338 private:
339 // Private constructor with array of options and count (used by template constructor)
340 UIDropdownImpl(const fl::string &name, const fl::string* options, fl::size count)
341 : mSelectedIndex(0) {
342 FASTLED_UNUSED(name);
343 for (fl::size i = 0; i < count; ++i) {
344 mOptions.push_back(options[i]);
345 }
346 if (mOptions.empty()) {
347 mOptions.push_back(fl::string("No options"));
348 }
349 }
350
353};
354#endif
355
356#ifndef FASTLED_HAS_UI_GROUP
357#define FASTLED_HAS_UI_GROUP 0
358#endif
359
360#if !FASTLED_HAS_UI_GROUP
361
363 public:
364 UIGroupImpl(const char *name) : mGroupName(name) {
366 }
368 fl::string name() const { return mGroupName; }
369
370 private:
372};
373
374#endif
375
376} // end namespace fl
fl::size size() const
Definition vector.h:545
fl::size size() const
Definition slice.h:142
void setGroup(const fl::string &groupName)
Definition ui_impl.h:250
bool hasNext()
Definition ui_impl.h:244
AudioSample next()
Definition ui_impl.h:239
UIAudioImpl(const char *name)
Definition ui_impl.h:236
fl::string name() const
Definition ui_impl.h:114
bool isPressed() const
Definition ui_impl.h:109
bool clicked() const
Definition ui_impl.h:110
void setGroup(const fl::string &groupName)
Definition ui_impl.h:117
UIButtonImpl(const char *name)
Definition ui_impl.h:107
int clickedCount() const
Definition ui_impl.h:111
fl::string mName
Definition ui_impl.h:120
void setValue(bool value)
Definition ui_impl.h:149
UICheckboxImpl & operator=(int value)
Definition ui_impl.h:139
void setGroup(const fl::string &groupName)
Definition ui_impl.h:146
bool value() const
Definition ui_impl.h:143
UICheckboxImpl(const char *name, bool value=false)
Definition ui_impl.h:129
UICheckboxImpl & operator=(bool value)
Definition ui_impl.h:135
UIDescriptionImpl(const char *name)
Definition ui_impl.h:206
void setGroup(const fl::string &groupName)
Definition ui_impl.h:210
UIDropdownImpl(const fl::string &name, const fl::string *options, fl::size count)
Definition ui_impl.h:340
UIDropdownImpl(const fl::string &name, fl::initializer_list< fl::string > options)
Definition ui_impl.h:299
void setSelectedIndex(int index)
Definition ui_impl.h:321
UIDropdownImpl(const fl::string &name, const fl::vector< fl::string > &options)
Definition ui_impl.h:263
int value_int() const
Definition ui_impl.h:319
UIDropdownImpl(const fl::string &name, fl::span< fl::string > options)
Definition ui_impl.h:287
fl::size mSelectedIndex
Definition ui_impl.h:352
void setGroup(const fl::string &groupName)
Definition ui_impl.h:336
fl::size getOptionCount() const
Definition ui_impl.h:327
UIDropdownImpl(const fl::string &name, Iterator begin, Iterator end)
Definition ui_impl.h:275
fl::string value() const
Definition ui_impl.h:312
fl::vector< fl::string > mOptions
Definition ui_impl.h:351
UIDropdownImpl(const fl::string &name, const fl::string(&options)[N])
Definition ui_impl.h:259
fl::string getOption(fl::size index) const
Definition ui_impl.h:328
UIGroupImpl(const char *name)
Definition ui_impl.h:364
fl::string name() const
Definition ui_impl.h:368
fl::string mGroupName
Definition ui_impl.h:371
void setGroup(const fl::string &groupName)
Definition ui_impl.h:222
const fl::string & markdownContent() const
Definition ui_impl.h:225
UIHelpImpl(const char *markdownContent)
Definition ui_impl.h:218
fl::string mContent
Definition ui_impl.h:228
UINumberFieldImpl & operator=(int value)
Definition ui_impl.h:173
UINumberFieldImpl & operator=(double value)
Definition ui_impl.h:169
void setGroup(const fl::string &groupName)
Definition ui_impl.h:179
UINumberFieldImpl(const char *name, double value, double min=0, double max=100)
Definition ui_impl.h:159
double value() const
Definition ui_impl.h:165
void setValue(double value)
Definition ui_impl.h:166
float getMax() const
Definition ui_impl.h:70
UISliderImpl & operator=(int value)
Definition ui_impl.h:88
float value() const
Definition ui_impl.h:69
int as_int() const
Definition ui_impl.h:79
void setValue(float value)
Definition ui_impl.h:72
float getMin() const
Definition ui_impl.h:71
T as() const
Definition ui_impl.h:77
UISliderImpl & operator=(float value)
Definition ui_impl.h:84
UISliderImpl(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition ui_impl.h:56
void setGroup(const fl::string &groupName)
Definition ui_impl.h:82
UITitleImpl(const char *name)
Definition ui_impl.h:193
void setGroup(const fl::string &groupName)
Definition ui_impl.h:197
#define MIN(a, b)
Definition math_macros.h:41
#define MAX(a, b)
Definition math_macros.h:37
Implements the FastLED namespace macros.
unsigned char u8
Definition int.h:17
Slice< T > span
Definition span.h:8
constexpr T * begin(T(&array)[N]) noexcept
Definition range_access.h:9
constexpr T * end(T(&array)[N]) noexcept
HeapVector< T, Allocator > vector
Definition vector.h:1214
IMPORTANT!
Definition crgb.h:20
#define FASTLED_UNUSED(x)
Definition unused.h:4
#define FASTLED_WARN
Definition warn.h:7