23#include "guillaume/components/range.hpp"
101 _onChanged = handler;
110 float Range::sanitize(
float value)
const
112 float result = std::clamp(value, _min, _max);
114 const float steps = std::round((result - _min) / _step);
115 result = std::clamp(_min + steps * _step, _min, _max);
120 void Range::apply(
float low,
float high)
122 float newLow = sanitize(low);
123 float newHigh = sanitize(high);
125 if (newLow > newHigh) {
126 std::swap(newLow, newHigh);
129 if (newLow == _low && newHigh == _high) {
138 _onChanged(_low, _high);
Handler getOnChangedHandler(void) const
Get the callback run when the interval changes.
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.
float getLow(void) const
Get the low end of the interval.
std::function< void(float, float)> Handler
Callback receiving the new low and high ends, in that order.
float getStep(void) const
Get the snapping step.
Range & setOnChangedHandler(const Handler &handler)
Set the callback run when the interval changes.
float getMin(void) const
Get the lower bound of the range.
Range & setMin(float min)
Set the lower bound of the range.
float getMax(void) const
Get the upper bound of the range.
Range & setLow(float low)
Set the low end of the interval.
Range & setMax(float max)
Set the upper bound of the range.
float getHigh(void) const
Get the high end of the interval.
Range & setHigh(float high)
Set the high end of the interval.
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
State. Every class here is plain data attached to an entity.