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
33
FASTLED_NAMESPACE_BEGIN
34
35
36
// If the platform is missing ui components, provide stubs.
37
38
#if !FASTLED_HAS_UI_SLIDER
39
40
class
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
70
class
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
83
class
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
100
class
NumberField
{
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
122
class
Title
{
123
public
:
124
Title
(
const
char
*name) {}
125
~Title
() {}
126
};
127
128
#endif
129
130
#if !FASTLED_HAS_UI_DESCRIPTION
131
132
class
Description
{
133
public
:
134
Description
(
const
char
*name) {}
135
~Description
() {}
136
};
137
138
#endif
139
140
#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS) \
141
template <typename T> bool operator>= (T v, const UI_CLASS& ui) { return ui >= v; } \
142
template <typename T> bool operator<= (T v, const UI_CLASS& ui) { return ui <= v; } \
143
template <typename T> bool operator> (T v, const UI_CLASS& ui) { return ui > v; } \
144
template <typename T> bool operator< (T v, const UI_CLASS& ui) { return ui < v; } \
145
template <typename T> bool operator== (T v, const UI_CLASS& ui) { return ui == v; } \
146
template <typename T> bool operator!= (T v, const UI_CLASS& ui) { return ui != v; } \
147
template <typename T> bool operator>=(const UI_CLASS& ui, T v) { return ui >= v; } \
148
template <typename T> bool operator<=(const UI_CLASS& ui, T v) { return ui <= v; } \
149
template <typename T> bool operator>(const UI_CLASS& ui, T v) { return ui > v; } \
150
template <typename T> bool operator<(const UI_CLASS& ui, T v) { return ui < v; } \
151
template <typename T> bool operator==(const UI_CLASS& ui, T v) { return ui == v; } \
152
template <typename T> bool operator!=(const UI_CLASS& ui, T v) { return ui != v; }
153
154
FASTLED_UI_DEFINE_OPERATORS(
Slider
);
155
FASTLED_UI_DEFINE_OPERATORS(
NumberField
);
156
FASTLED_UI_DEFINE_OPERATORS(
Checkbox
);
157
FASTLED_UI_DEFINE_OPERATORS(
Button
);
158
159
160
FASTLED_NAMESPACE_END
Button
Definition
ui.h:70
Checkbox
Definition
ui.h:83
Description
Definition
ui.h:132
NumberField
Definition
ui.h:100
Slider
Definition
ui.h:40
Title
Definition
ui.h:122
src
ui.h
Generated on Thu Nov 14 2024 00:00:34 for FastLED by
1.11.0