FastLED 3.9.15
Loading...
Searching...
No Matches
slider.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/int.h" // IWYU pragma: keep
4#include "fl/stl/function.h"
5#include "fl/math/math.h"
6#include "fl/stl/compiler_control.h" // IWYU pragma: keep
7#include "fl/stl/noexcept.h"
9#include "fl/ui/element.h"
10#include "platforms/ui_defs.h"
11
12#ifndef FASTLED_HAS_UI_SLIDER
13#define FASTLED_HAS_UI_SLIDER 0
14#endif
15
16namespace fl {
17
18#if !FASTLED_HAS_UI_SLIDER
19
21 public:
22 // If step is -1, it will be calculated as (max - min) / 100
23 UISliderImpl(const char *name, float value = 128.0f, float min = 1,
24 float max = 255, float step = -1.f)
25 : mMin(fl::min(min, max)),
26 mMax(fl::max(min, max)),
27 mValue(fl::max(mMin, fl::min(mMax, value))) {
28 FASTLED_UNUSED(name);
30 }
32 float value() const { return mValue; }
33 float getMax() const { return mMax; }
34 float getMin() const { return mMin; }
36 operator float() const { return mValue; }
37 operator u8() const { return static_cast<u8>(mValue); }
38 operator u16() const { return static_cast<u16>(mValue); }
39 operator int() const { return static_cast<int>(mValue); }
40 template <typename T> T as() const { return static_cast<T>(mValue); }
41
42 int as_int() const { return static_cast<int>(mValue); }
43
44 // Stub method for group setting (does nothing on non-WASM platforms)
45 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
46
49 return *this;
50 }
52 setValue(static_cast<float>(value));
53 return *this;
54 }
55
56 private:
57 float mMin;
58 float mMax;
59 float mValue;
60};
61
62#endif
63
64class UISlider : public UIElement {
65 public:
67 // If step is -1, it will be calculated as (max - min) / 100
68 UISlider(const char *name, float value = 128.0f, float min = 1,
69 float max = 255, float step = -1.f) FL_NOEXCEPT;
70 float value() const FL_NOEXCEPT { return mImpl.value(); }
72 float min = mImpl.getMin();
73 float max = mImpl.getMax();
74 if (fl::almost_equal(max, min, 0.0001f)) {
75 return 0;
76 }
77 return (value() - min) / (max - min);
78 }
79 float getMax() const FL_NOEXCEPT { return mImpl.getMax(); }
80 float getMin() const FL_NOEXCEPT { return mImpl.getMin(); }
82 operator float() const FL_NOEXCEPT { return mImpl.value(); }
83 operator u8() const FL_NOEXCEPT { return static_cast<u8>(mImpl.value()); }
84 operator fl::u16() const FL_NOEXCEPT { return static_cast<fl::u16>(mImpl.value()); }
85 operator int() const FL_NOEXCEPT { return static_cast<int>(mImpl.value()); }
86 template <typename T> T as() const FL_NOEXCEPT {
87 return static_cast<T>(mImpl.value());
88 }
89
90 int as_int() const FL_NOEXCEPT { return static_cast<int>(mImpl.value()); }
91
93 mImpl.setValue(value);
94 return *this;
95 }
97 mImpl.setValue(static_cast<float>(value));
98 return *this;
99 }
100
101 // Override setGroup to also update the implementation
102 void setGroup(const fl::string& groupName) FL_NOEXCEPT override {
103 UIElement::setGroup(groupName);
104 // Update the implementation's group if it has the method (WASM platforms)
105 mImpl.setGroup(groupName);
106 }
107
108
109 int onChanged(function<void(UISlider &)> callback) FL_NOEXCEPT {
110 int out = mCallbacks.add(callback);
111 mListener.addToEngineEventsOnce();
112 return out;
113 }
115
116 protected:
118
121
122 }
124 if (added) {
126 }
127 }
129 if (added) {
130 return;
131 }
133 added = true;
134 }
135 void onBeginFrame() FL_NOEXCEPT override;
136
137 private:
139 bool added = false;
140 };
141
142 private:
143 function_list<void(UISlider &)> mCallbacks;
147};
148
150
151} // 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
UISlider & operator=(float value) FL_NOEXCEPT
Definition slider.h:92
Listener mListener
Definition slider.h:146
float float float float step
Definition slider.h:69
float value
Definition slider.h:68
bool mLastFrameValueValid
Definition slider.h:145
FL_NO_COPY(UISlider) UISlider(const char *name
function_list< void(UISlider &)> mCallbacks
Definition slider.h:143
float value_normalized() const FL_NOEXCEPT
Definition slider.h:71
UISliderImpl mImpl
Definition slider.h:117
void clearCallbacks() FL_NOEXCEPT
Definition slider.h:114
float float float max
Definition slider.h:69
float getMin() const FL_NOEXCEPT
Definition slider.h:80
float float min
Definition slider.h:68
UISlider & operator=(int value) FL_NOEXCEPT
Definition slider.h:96
void setGroup(const fl::string &groupName) FL_NOEXCEPT override
Definition slider.h:102
float mLastFrameValue
Definition slider.h:144
void setValue(float value) FL_NOEXCEPT
int as_int() const FL_NOEXCEPT
Definition slider.h:90
int onChanged(function< void(UISlider &)> callback) FL_NOEXCEPT
Definition slider.h:109
float getMax() const FL_NOEXCEPT
Definition slider.h:79
T as() const FL_NOEXCEPT
Definition slider.h:86
float getMax() const
Definition slider.h:33
float value() const
Definition slider.h:32
int as_int() const
Definition slider.h:42
void setValue(float value)
Definition slider.h:35
UISliderImpl & operator=(float value) FL_NOEXCEPT
Definition slider.h:47
UISliderImpl & operator=(int value) FL_NOEXCEPT
Definition slider.h:51
float getMin() const
Definition slider.h:34
T as() const
Definition slider.h:40
~UISliderImpl() FL_NOEXCEPT
Definition slider.h:31
UISliderImpl(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition slider.h:23
void setGroup(const fl::string &groupName)
Definition slider.h:45
#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
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
constexpr bool almost_equal(T a, T b, U tolerance) FL_NOEXCEPT
Definition math.h:90
constexpr enable_if< is_fixed_point< T >::value, T >::type step(T edge, T x) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT
~Listener() FL_NOEXCEPT
Definition slider.h:123
Listener(UISlider *owner) FL_NOEXCEPT
Definition slider.h:120
UISlider * mOwner
Definition slider.h:138
void addToEngineEventsOnce() FL_NOEXCEPT
Definition slider.h:128
void onBeginFrame() FL_NOEXCEPT override