guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
range.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/range.hpp"
24
25#include <algorithm>
26#include <cmath>
27
29{
30 float Range::getMin(void) const
31 {
32 return _min;
33 }
34
35 Range &Range::setMin(float min)
36 {
37 if (_min == min) {
38 return *this;
39 }
40 _min = min;
41 setHasChanged(true);
42 apply(_low, _high);
43 return *this;
44 }
45
46 float Range::getMax(void) const
47 {
48 return _max;
49 }
50
51 Range &Range::setMax(float max)
52 {
53 if (_max == max) {
54 return *this;
55 }
56 _max = max;
57 setHasChanged(true);
58 apply(_low, _high);
59 return *this;
60 }
61
62 float Range::getLow(void) const
63 {
64 return _low;
65 }
66
67 Range &Range::setLow(float low)
68 {
69 apply(low, _high);
70 return *this;
71 }
72
73 float Range::getHigh(void) const
74 {
75 return _high;
76 }
77
78 Range &Range::setHigh(float high)
79 {
80 apply(_low, high);
81 return *this;
82 }
83
84 float Range::getStep(void) const
85 {
86 return _step;
87 }
88
89 Range &Range::setStep(float step)
90 {
91 if (_step == step) {
92 return *this;
93 }
94 _step = step;
95 setHasChanged(true);
96 return *this;
97 }
98
100 {
101 _onChanged = handler;
102 return *this;
103 }
104
106 {
107 return _onChanged;
108 }
109
110 float Range::sanitize(float value) const
111 {
112 float result = std::clamp(value, _min, _max);
113 if (_step > 0.0f) {
114 const float steps = std::round((result - _min) / _step);
115 result = std::clamp(_min + steps * _step, _min, _max);
116 }
117 return result;
118 }
119
120 void Range::apply(float low, float high)
121 {
122 float newLow = sanitize(low);
123 float newHigh = sanitize(high);
124
125 if (newLow > newHigh) {
126 std::swap(newLow, newHigh);
127 }
128
129 if (newLow == _low && newHigh == _high) {
130 return;
131 }
132
133 _low = newLow;
134 _high = newHigh;
135 setHasChanged(true);
136
137 if (_onChanged) {
138 _onChanged(_low, _high);
139 }
140 }
141} // namespace guillaume::components
Handler getOnChangedHandler(void) const
Get the callback run when the interval changes.
Definition range.cpp:105
Range(void)=default
Create the interval 0 to 1 in the range 0 to 1, without snapping.
Range & setStep(float step)
Set the snapping step.
Definition range.cpp:89
float getLow(void) const
Get the low end of the interval.
Definition range.cpp:62
std::function< void(float, float)> Handler
Callback receiving the new low and high ends, in that order.
Definition range.hpp:62
float getStep(void) const
Get the snapping step.
Definition range.cpp:84
Range & setOnChangedHandler(const Handler &handler)
Set the callback run when the interval changes.
Definition range.cpp:99
float getMin(void) const
Get the lower bound of the range.
Definition range.cpp:30
Range & setMin(float min)
Set the lower bound of the range.
Definition range.cpp:35
float getMax(void) const
Get the upper bound of the range.
Definition range.cpp:46
Range & setLow(float low)
Set the low end of the interval.
Definition range.cpp:67
Range & setMax(float max)
Set the upper bound of the range.
Definition range.cpp:51
float getHigh(void) const
Get the high end of the interval.
Definition range.cpp:73
Range & setHigh(float high)
Set the high end of the interval.
Definition range.cpp:78
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.