diff --git a/headers/enduro2d/high/components/slider.hpp b/headers/enduro2d/high/components/slider.hpp index a49ce175..fe1ffc94 100644 --- a/headers/enduro2d/high/components/slider.hpp +++ b/headers/enduro2d/high/components/slider.hpp @@ -29,6 +29,15 @@ namespace e2d slider& value(f32 value) noexcept; [[nodiscard]] f32 value() const noexcept; + slider& raw_value(f32 value) noexcept; + [[nodiscard]] f32 raw_value() const noexcept; + + slider& normalized_value(f32 value) noexcept; + [[nodiscard]] f32 normalized_value() const noexcept; + + slider& normalized_raw_value(f32 value) noexcept; + [[nodiscard]] f32 normalized_raw_value() const noexcept; + slider& whole_numbers(bool value) noexcept; [[nodiscard]] bool whole_numbers() const noexcept; @@ -98,23 +107,57 @@ namespace e2d } inline slider& slider::value(f32 value) noexcept { - value_ = math::clamp( - whole_numbers_ ? math::trunc(value) : value, - min_value_, - max_value_); + value_ = math::clamp(value, min_value_, max_value_); return *this; } inline f32 slider::value() const noexcept { + return whole_numbers_ ? math::round(value_) : value_; + } + + inline slider& slider::raw_value(f32 value) noexcept { + value_ = math::clamp(value, min_value_, max_value_); + return *this; + } + + inline f32 slider::raw_value() const noexcept { return value_; } + inline slider& slider::normalized_value(f32 value) noexcept { + value_ = math::lerp( + min_value_, + max_value_, + math::saturate(value)); + return *this; + } + + inline f32 slider::normalized_value() const noexcept { + return math::approximately(min_value_, max_value_) + ? 0.f + : math::inverse_lerp(min_value_, max_value_, value()); + } + + inline slider& slider::normalized_raw_value(f32 value) noexcept { + value_ = math::lerp( + min_value_, + max_value_, + math::saturate(value)); + return *this; + } + + inline f32 slider::normalized_raw_value() const noexcept { + return math::approximately(min_value_, max_value_) + ? 0.f + : math::inverse_lerp(min_value_, max_value_, raw_value()); + } + inline slider& slider::whole_numbers(bool value) noexcept { whole_numbers_ = value; if ( whole_numbers_ ) { min_value_ = math::floor(min_value_); max_value_ = math::ceil(max_value_); - value_ = math::clamp(math::trunc(value_), min_value_, max_value_); + value_ = math::clamp(value_, min_value_, max_value_); } return *this; } diff --git a/samples/bin/library/scripts/emmy/components/slider.lua b/samples/bin/library/scripts/emmy/components/slider.lua index 58421fb1..3d8e474a 100644 --- a/samples/bin/library/scripts/emmy/components/slider.lua +++ b/samples/bin/library/scripts/emmy/components/slider.lua @@ -15,6 +15,15 @@ local slider = { ---@type number value = 0.0, + ---@type number + raw_value = 0.0, + + ---@type number + normalized_value = 0.0, + + ---@type number + normalized_raw_value = 0.0, + ---@type boolean whole_numbers = false, @@ -39,4 +48,4 @@ function slider.enable(self) end function slider.disable(self) end ---@type slider -_G.slider = _G.slider or slider \ No newline at end of file +_G.slider = _G.slider or slider diff --git a/sources/enduro2d/high/bindings/high_binds/components/slider_binds.cpp b/sources/enduro2d/high/bindings/high_binds/components/slider_binds.cpp index a5e98157..77c84ac7 100644 --- a/sources/enduro2d/high/bindings/high_binds/components/slider_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/components/slider_binds.cpp @@ -74,6 +74,30 @@ namespace e2d::bindings::high c->value(v); }), + "raw_value", sol::property( + [](const gcomponent& c) -> f32 { + return c->raw_value(); + }, + [](gcomponent& c, f32 v){ + c->raw_value(v); + }), + + "normalized_value", sol::property( + [](const gcomponent& c) -> f32 { + return c->normalized_value(); + }, + [](gcomponent& c, f32 v){ + c->normalized_value(v); + }), + + "normalized_raw_value", sol::property( + [](const gcomponent& c) -> f32 { + return c->normalized_raw_value(); + }, + [](gcomponent& c, f32 v){ + c->normalized_raw_value(v); + }), + "whole_numbers", sol::property( [](const gcomponent& c) -> bool { return c->whole_numbers();