Refactored Slider to use an internal range of 0-1 and map it to a set range for the end-user.

This commit is contained in:
2025-06-23 21:05:40 -05:00
parent 0e62fcd5f6
commit 8af6030d1a
4 changed files with 52 additions and 11 deletions

View File

@@ -64,12 +64,13 @@ namespace JUI {
hue_slider = new JUI::Slider(row_layout);
hue_slider->Size({100_percent, 28_px});
hue_slider->Minimum(0.f); hue_slider->Maximum(1.f);
hue_slider->Minimum(0.f); hue_slider->Maximum(360);
hue_slider->Interval(1e-3f);
hue_slider->ValueChanged += [this] (float val) mutable {
hue_label->Content(std::format("Hue: {}", Math::FloorInt(val*360)));
//val = val*360;
hue_label->Content(std::format("Hue: {}", Math::FloorInt(val)));
hue = val * 360;
hue = val;
hue_slider->ScrubberColor(Color4::FromHSV(hue, 1.f, 1.f));

View File

@@ -25,6 +25,8 @@ namespace JUI
return static_cast<T>(std::round(static_cast<double>(value)/static_cast<double>(multiple))*static_cast<double>(multiple));
}
/// A slider is a widget with a handle which can be pulled back and forth to change the value.
class Slider : public Rect, public Clickable, public Hoverable
{
@@ -47,12 +49,15 @@ namespace JUI
void Maximum(float max);
void Interval(float inter);
void CurrentValue(float value);
float Percentage() const;
void Percentage(float value);
void ScrubberColor(const Color4& color);
void ScrubberWidth(float width);
void SetDragging(bool value);
void OnClick(const J3ML::LinearAlgebra::Vector2 &MousePos, const JUI::MouseButton &MouseButton) override;
void OnRelease(const J3ML::LinearAlgebra::Vector2 &MousePos, const JUI::MouseButton &MouseButton, bool MouseStillOver) override;
void OnHover(const J3ML::LinearAlgebra::Vector2 &MousePos) override;
@@ -65,6 +70,7 @@ namespace JUI
float maximum = 1;
float interval = 0.1;
float current;
float percentage = 0.f;
bool dragging = false;
float scrubber_width = 20;
Color4 scrubber_color = Colors::White;

View File

@@ -36,6 +36,7 @@
#include "JUI/Widgets/ColorPicker.hpp"
#include "JUI/Widgets/FpsGraph.hpp"
#include "JUI/Widgets/LabeledSlider.hpp"
#include "JUI/Widgets/Link.hpp"
using namespace JUI;
@@ -343,6 +344,18 @@ JUI::Rect* CreateWidgetList(JUI::Widget* root) {
};
}
auto* slider = new LabeledSlider(collapsible->ContentBox());
slider->Size({100_percent, 30_px});
slider->Position({0_px, 30_px});
slider->Content("Value: 0");
slider->Minimum(-1.f);
slider->Maximum(1.f);
slider->CurrentValue(0.f);
slider->Interval(1/5.f);
slider->ValueChanged += [slider](float value) mutable {
slider->Content(std::format("Value: {}", value));
};

View File

@@ -4,6 +4,22 @@
namespace JUI
{
template <typename T>
struct Range {
T min;
T max;
};
template <typename T>
T map(T value, T in_min, T in_max, T out_min, T out_max) {
return (value - in_min) / (in_max - in_min) * (out_max - out_min) + out_min;
}
template <typename T>
T map(T value, const Range<T>& in, const Range<T>& out) {
return map(value, in.min, in.max, out.min, out.max);
}
Slider::Slider(JUI::Widget *parent) : Slider()
{
this->Parent(parent);
@@ -47,7 +63,6 @@ namespace JUI
if (dragging)
{
float range = maximum - minimum;
Vector2 mouse = last_known_mouse_pos;
float slider_abs_left = this->GetAbsolutePosition().x;
@@ -58,7 +73,7 @@ namespace JUI
float offset = scrubberP - slider_abs_left;
float percentage = offset / slider_total_width;
percentage = offset / slider_total_width;
float translated = Math::Clamp(roundMultiple(percentage * Range(), interval), 0.f, Range());
@@ -68,7 +83,6 @@ namespace JUI
ValueChanged(value);
current = value;
//std::cout << "slider value: " << value << std::endl;
}
// TODO there is probably a more elegant fix for this.
@@ -83,7 +97,7 @@ namespace JUI
//scrubber->Draw();
Rect::Draw();
Vector2 pos = {
current * (GetAbsoluteSize().x - scrubber_width) + GetAbsolutePosition().x
percentage * (GetAbsoluteSize().x - scrubber_width) + GetAbsolutePosition().x
,GetAbsolutePosition().y
};
/// TODO: Implement internal padding on scrubber element?
@@ -98,7 +112,9 @@ namespace JUI
float Slider::Interval() const { return interval; }
float Slider::CurrentValue() const { return current; }
float Slider::CurrentValue() const {
return map(percentage, {0, 1}, {minimum, maximum});
}
Color4 Slider::ScrubberColor() const { return scrubber_color; }
@@ -112,7 +128,12 @@ namespace JUI
void Slider::Interval(float inter) { interval = inter; }
void Slider::CurrentValue(float value) { current = value; }
void Slider::CurrentValue(float value) {
percentage = map(value, {minimum, maximum}, {0.f, 1.f});
}
float Slider::Percentage() const { return percentage;}
void Slider::Percentage(float value) { percentage = value; }
void Slider::ScrubberColor(const Color4 &color) { scrubber_color = color;}