1.10
valuewidget.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_VALUEWIDGET_H
7#define EGT_VALUEWIDGET_H
8
14#include <egt/signal.h>
15#include <egt/widget.h>
16
17namespace egt
18{
19inline namespace v1
20{
21
28template<class T>
29class ValueWidget : public Widget
30{
31public:
32
43 ValueWidget() = delete;
44
49 explicit ValueWidget(const Rect& rect, T value = T()) noexcept
50 : Widget(rect),
52 {}
53
61 T value(T v)
62 {
63 T orig = m_value;
64 if (detail::change_if_diff<T>(m_value, v))
65 {
66 damage();
68 }
69 return orig;
70 }
71
75 T value() const { return m_value; }
76
77protected:
78
83};
84
91template<class T>
93{
94public:
95
107
114 ValueRangeWidget(const Rect& rect, T start, T end,
115 T value = T(), T step = T()) noexcept
116 : Widget(rect),
117 m_start(start),
118 m_end(end),
119 m_value(value),
120 m_step(step)
121 {
123 }
124
129 : ValueRangeWidget(props, false)
130 {
131 }
132
133protected:
134
135 explicit ValueRangeWidget(Serializer::Properties& props, bool is_derived)
136 : Widget(props, true)
137 {
138 deserialize(props);
139
140 if (!is_derived)
141 deserialize_leaf(props);
142 }
143
144public:
145
157 virtual T value(T value)
158 {
159 T orig = m_value;
160
161 if (set_value(value))
162 {
163 damage();
165 }
166
167 return orig;
168 }
169
173 EGT_NODISCARD T starting() const { return m_start; }
174
178 EGT_NODISCARD T ending() const { return m_end; }
179
183 EGT_NODISCARD T stepping() const { return m_step; }
184
190 void starting(T v)
191 {
192 if (detail::change_if_diff<>(m_start, v))
193 {
194 damage();
195 if (set_value(m_value))
197 }
198 }
199
205 void ending(T v)
206 {
207 if (detail::change_if_diff<>(m_end, v))
208 {
209 damage();
210 if (set_value(m_value))
212 }
213 }
214
220 void stepping(T v)
221 {
222 if (detail::change_if_diff<>(m_step, v))
223 {
224 damage();
225 if (set_value(m_value))
227 }
228 }
229
233 EGT_NODISCARD T value() const
234 {
235 return m_value;
236 }
237
238 void serialize(Serializer& serializer) const override;
239
240protected:
241
242 bool set_value(T v)
243 {
244 T value = v;
245
246 if (!detail::float_equal(static_cast<float>(m_step), 0.f))
247 value = std::round(static_cast<float>(value) / m_step) * m_step;
248
249 if (m_start < m_end)
250 value = detail::clamp<T>(value, m_start, m_end);
251 else
252 value = detail::clamp<T>(value, m_end, m_start);
253
254 return detail::change_if_diff<>(m_value, value);
255 }
256
259
262
265
268
269private:
270
271 void deserialize(Serializer::Properties& props);
272};
273
274template <class T>
276{
277 Widget::serialize(serializer);
278
280 {
281 {"starting", detail::to_string(starting())},
282 {"ending", detail::to_string(ending())},
283 };
284
285 if (!detail::float_equal(static_cast<float>(m_step), 0.f))
286 attrs.emplace_back("stepping", detail::to_string(stepping()));
287
288 serializer.add_property("value", this->value(), attrs);
289}
290
291template <class T>
293{
294 props.erase(std::remove_if(props.begin(), props.end(), [&](auto & p)
295 {
296 if (std::get<0>(p) == "value")
297 {
298 float starting_value = 0, ending_value = 100, stepping_value = 0;
299 auto attrs = std::get<2>(p);
300 for (auto& a : attrs)
301 {
302 switch (detail::hash(a.first))
303 {
304 case detail::hash("starting"):
305 starting_value = std::stof(a.second);
306 break;
307 case detail::hash("ending"):
308 ending_value = std::stof(a.second);
309 break;
310 case detail::hash("stepping"):
311 stepping_value = std::stof(a.second);
312 break;
313 }
314 }
315
316 this->stepping(stepping_value);
317 this->ending(ending_value);
318 this->starting(starting_value);
319 this->value(std::stof(std::get<1>(p)));
320
321 return true;
322 }
323 return false;
324 }), props.end());
325}
326
327}
328}
329
330#endif
Abstract base serializer class.
Definition serialize.h:34
std::list< std::pair< std::string, std::string > > Attributes
Attributes array type.
Definition serialize.h:45
virtual void add_property(const std::string &name, const std::string &value, const Attributes &attrs={})=0
Add a property.
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
Signal class used for defining a signal and dispatching events.
Definition signal.h:30
void invoke(Args... args)
Invoke all handlers with the specified args.
Definition signal.h:85
Manages a value in a range.
Definition valuewidget.h:93
void stepping(T v)
Set the step value.
Definition valuewidget.h:220
virtual T value(T value)
Set value.
Definition valuewidget.h:157
T m_start
The start value.
Definition valuewidget.h:258
EGT_NODISCARD T stepping() const
Get the step value.
Definition valuewidget.h:183
EGT_NODISCARD T ending() const
Get the end value.
Definition valuewidget.h:178
ValueRangeWidget(Serializer::Properties &props)
Definition valuewidget.h:128
bool set_value(T v)
Definition valuewidget.h:242
void starting(T v)
Set the start value.
Definition valuewidget.h:190
ValueRangeWidget(const Rect &rect, T start, T end, T value=T(), T step=T()) noexcept
Definition valuewidget.h:114
EGT_NODISCARD T starting() const
Get the start value.
Definition valuewidget.h:173
void ending(T v)
Set the end value.
Definition valuewidget.h:205
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
Definition valuewidget.h:275
Signal on_value_changed
Event signal.
Definition valuewidget.h:103
ValueRangeWidget(Serializer::Properties &props, bool is_derived)
Definition valuewidget.h:135
T m_step
The rounding step, if any.
Definition valuewidget.h:267
EGT_NODISCARD T value() const
Get the current value.
Definition valuewidget.h:233
T m_end
The end value.
Definition valuewidget.h:261
T m_value
The current value.
Definition valuewidget.h:264
Manages an unbounded value.
Definition valuewidget.h:30
T value(T v)
Set the value.
Definition valuewidget.h:61
Signal on_value_changed
Event signal.
Definition valuewidget.h:40
T value() const
Get the value.
Definition valuewidget.h:75
ValueWidget(const Rect &rect, T value=T()) noexcept
Definition valuewidget.h:49
T m_value
The current value.
Definition valuewidget.h:82
Base Widget class.
Definition widget.h:53
void deserialize_leaf(Serializer::Properties &props)
Deserialize widget properties that require to call overridden methods.
virtual void damage()
Damage the box() of the widget and cause a redraw.
virtual void serialize(Serializer &serializer) const
Serialize the widget to the specified serializer.
constexpr bool float_equal(const float f1, const float f2)
Safe equal comparison of float values.
Definition math.h:107
std::string to_string(const T &x)
Convert a type to a std::string using std::ostringstream.
Definition detail/string.h:110
EGT framework namespace.
Definition animation.h:24