guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
value.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/components/value.hpp"
24
25#include <algorithm>
26#include <cmath>
27
29{
30 float Value::getMin(void) const
31 {
32 return _min;
33 }
34
35 Value &Value::setMin(float min)
36 {
37 if (_min == min) {
38 return *this;
39 }
40 _min = min;
41 setHasChanged(true);
42 setValue(_value);
43 return *this;
44 }
45
46 float Value::getMax(void) const
47 {
48 return _max;
49 }
50
51 Value &Value::setMax(float max)
52 {
53 if (_max == max) {
54 return *this;
55 }
56 _max = max;
57 setHasChanged(true);
58 setValue(_value);
59 return *this;
60 }
61
62 float Value::getValue(void) const
63 {
64 return _value;
65 }
66
67 Value &Value::setValue(float value)
68 {
69 const float sanitized = sanitize(value);
70 if (sanitized == _value) {
71 return *this;
72 }
73 _value = sanitized;
74 setHasChanged(true);
75 if (_onChanged) {
76 _onChanged(_value);
77 }
78 return *this;
79 }
80
81 float Value::getStep(void) const
82 {
83 return _step;
84 }
85
86 Value &Value::setStep(float step)
87 {
88 if (_step == step) {
89 return *this;
90 }
91 _step = step;
92 setHasChanged(true);
93 return *this;
94 }
95
96 float Value::getNormalizedValue(void) const
97 {
98 const float range = _max - _min;
99 if (range <= 0.0f) {
100 return 0.0f;
101 }
102 return (_value - _min) / range;
103 }
104
106 {
107 const float clamped = std::clamp(normalized, 0.0f, 1.0f);
108 return setValue(_min + clamped * (_max - _min));
109 }
110
112 {
113 _onChanged = handler;
114 return *this;
115 }
116
118 {
119 return _onChanged;
120 }
121
122 float Value::sanitize(float value) const
123 {
124 float result = std::clamp(value, _min, _max);
125 if (_step > 0.0f) {
126 const float steps = std::round((result - _min) / _step);
127 result = std::clamp(_min + steps * _step, _min, _max);
128 }
129 return result;
130 }
131} // namespace guillaume::components
float getStep(void) const
Get the snapping step.
Definition value.cpp:81
float getNormalizedValue(void) const
Get where the value sits in its range.
Definition value.cpp:96
std::function< void(float)> Handler
Callback receiving the new value, already clamped and snapped.
Definition value.hpp:59
Value & setMin(float min)
Set the lower bound of the range.
Definition value.cpp:35
Value(void)=default
Create a value of 0 in the range 0 to 1, without snapping.
Handler getOnChangedHandler(void) const
Get the callback run when the value changes.
Definition value.cpp:117
Value & setOnChangedHandler(const Handler &handler)
Set the callback run when the value changes.
Definition value.cpp:111
float getValue(void) const
Get the current value.
Definition value.cpp:62
float getMin(void) const
Get the lower bound of the range.
Definition value.cpp:30
Value & setNormalizedValue(float normalized)
Set the value from its position in the range.
Definition value.cpp:105
float getMax(void) const
Get the upper bound of the range.
Definition value.cpp:46
Value & setValue(float value)
Set the current value.
Definition value.cpp:67
Value & setMax(float max)
Set the upper bound of the range.
Definition value.cpp:51
Value & setStep(float step)
Set the snapping step.
Definition value.cpp:86
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
Definition component.cpp:32
State. Every class here is plain data attached to an entity.