23#include "guillaume/components/value.hpp"
69 const float sanitized = sanitize(value);
70 if (sanitized == _value) {
98 const float range = _max - _min;
102 return (_value - _min) / range;
107 const float clamped = std::clamp(normalized, 0.0f, 1.0f);
108 return setValue(_min + clamped * (_max - _min));
113 _onChanged = handler;
122 float Value::sanitize(
float value)
const
124 float result = std::clamp(value, _min, _max);
126 const float steps = std::round((result - _min) / _step);
127 result = std::clamp(_min + steps * _step, _min, _max);
float getStep(void) const
Get the snapping step.
float getNormalizedValue(void) const
Get where the value sits in its range.
std::function< void(float)> Handler
Callback receiving the new value, already clamped and snapped.
Value & setMin(float min)
Set the lower bound of the range.
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.
Value & setOnChangedHandler(const Handler &handler)
Set the callback run when the value changes.
float getValue(void) const
Get the current value.
float getMin(void) const
Get the lower bound of the range.
Value & setNormalizedValue(float normalized)
Set the value from its position in the range.
float getMax(void) const
Get the upper bound of the range.
Value & setValue(float value)
Set the current value.
Value & setMax(float max)
Set the upper bound of the range.
Value & setStep(float step)
Set the snapping step.
void setHasChanged(bool hasChanged)
Raise or lower the changed flag.
State. Every class here is plain data attached to an entity.