FastLED 3.9.15
Loading...
Searching...
No Matches
number_field.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/function.h"
4#include "fl/stl/string.h"
5#include "fl/stl/compiler_control.h" // IWYU pragma: keep
6#include "fl/stl/noexcept.h"
7#include "fl/math/math.h"
9#include "fl/ui/element.h"
10#include "platforms/ui_defs.h"
11
12#ifndef FASTLED_HAS_UI_NUMBER_FIELD
13#define FASTLED_HAS_UI_NUMBER_FIELD 0
14#endif
15
16namespace fl {
17
18#if !FASTLED_HAS_UI_NUMBER_FIELD
19
21 public:
22 UINumberFieldImpl(const char *name, double value, double min = 0,
23 double max = 100)
24 : mMin(fl::min(min, max)),
25 mMax(fl::max(min, max)),
26 mValue(fl::max(mMin, fl::min(mMax, value))) {
27 FASTLED_UNUSED(name);
28 }
30 double value() const { return mValue; }
32 operator double() const { return mValue; }
33 operator int() const { return static_cast<int>(mValue); }
36 return *this;
37 }
39 setValue(static_cast<double>(value));
40 return *this;
41 }
42
43 // Stub method for group setting (does nothing on non-WASM platforms)
44 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
45
46 private:
47 double mMin;
48 double mMax;
49 double mValue;
50};
51
52#endif
53
54class UINumberField : public UIElement {
55 public:
57 UINumberField(const char *name, double value, double min = 0,
58 double max = 100) FL_NOEXCEPT;
60 double value() const FL_NOEXCEPT { return mImpl.value(); }
61 void setValue(double value) FL_NOEXCEPT { mImpl.setValue(value); }
62 operator double() const FL_NOEXCEPT { return mImpl.value(); }
63 operator int() const FL_NOEXCEPT { return static_cast<int>(mImpl.value()); }
66 return *this;
67 }
69 setValue(static_cast<double>(value));
70 return *this;
71 }
72
73 // Override setGroup to also update the implementation
74 void setGroup(const fl::string& groupName) FL_NOEXCEPT override {
75 UIElement::setGroup(groupName);
76 // Update the implementation's group if it has the method (WASM platforms)
77 mImpl.setGroup(groupName);
78 }
79
80
81 void onChanged(function<void(UINumberField &)> callback) FL_NOEXCEPT {
82 mCallbacks.add(callback);
83 mListener.addToEngineEventsOnce();
84 }
86
87 protected:
89
90 private:
93 // Don't register in constructor - prevents callbacks before owner is fully initialized
94 }
96 if (added) {
98 }
99 }
101 if (added) {
102 return;
103 }
105 added = true;
106 }
107 void onBeginFrame() FL_NOEXCEPT override;
108
109 private:
111 bool added = false;
112 };
113
115 double mLastFrameValue = 0;
117 function_list<void(UINumberField &)> mCallbacks;
118};
119
121
122} // namespace fl
static void removeListener(Listener *listener) FL_NOEXCEPT
static void addListener(Listener *listener, int priority=0) FL_NOEXCEPT
virtual void setGroup(const fl::string &groupName) FL_NOEXCEPT
Definition element.h:31
UIElement() FL_NOEXCEPT
UINumberFieldImpl mImpl
double value() const FL_NOEXCEPT
UINumberField & operator=(double value) FL_NOEXCEPT
void setValue(double value) FL_NOEXCEPT
FL_NO_COPY(UINumberField)
void onChanged(function< void(UINumberField &)> callback) FL_NOEXCEPT
function_list< void(UINumberField &)> mCallbacks
void setGroup(const fl::string &groupName) FL_NOEXCEPT override
UINumberField(const char *name, double value, double min=0, double max=100) FL_NOEXCEPT
UINumberField & operator=(int value) FL_NOEXCEPT
void clearCallbacks() FL_NOEXCEPT
~UINumberFieldImpl() FL_NOEXCEPT
UINumberFieldImpl & operator=(int value) FL_NOEXCEPT
void setGroup(const fl::string &groupName)
UINumberFieldImpl(const char *name, double value, double min=0, double max=100)
double value() const
void setValue(double value)
UINumberFieldImpl & operator=(double value) FL_NOEXCEPT
#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS)
Definition element.h:40
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT
void addToEngineEventsOnce() FL_NOEXCEPT
void onBeginFrame() FL_NOEXCEPT override
Listener(UINumberField *owner) FL_NOEXCEPT