This commit is contained in:
Mikołaj Lubiak 2024-10-21 21:38:19 +00:00 committed by GitHub
commit e4bbf883d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 82 additions and 69 deletions

View File

@ -222,6 +222,7 @@ struct ResizableSplitOption {
template <typename T> template <typename T>
struct SliderOption { struct SliderOption {
Ref<T> value; Ref<T> value;
std::function<void(T)> callback = nullptr;
ConstRef<T> min = T(0); ConstRef<T> min = T(0);
ConstRef<T> max = T(100); ConstRef<T> max = T(100);
ConstRef<T> increment = (max() - min()) / 20; ConstRef<T> increment = (max() - min()) / 20;

View File

@ -38,11 +38,10 @@ template <class T>
class SliderBase : public ComponentBase { class SliderBase : public ComponentBase {
public: public:
explicit SliderBase(SliderOption<T> options) explicit SliderBase(SliderOption<T> options)
: value_(options.value), : callback_(options.callback), min_(options.min), max_(options.max),
min_(options.min), increment_(options.increment), options_(options) {
max_(options.max), SetValue(options.value());
increment_(options.increment), }
options_(options) {}
Element Render() override { Element Render() override {
auto gauge_color = Focused() ? color(options_.color_active) auto gauge_color = Focused() ? color(options_.color_active)
@ -56,10 +55,10 @@ class SliderBase : public ComponentBase {
void OnLeft() { void OnLeft() {
switch (options_.direction) { switch (options_.direction) {
case Direction::Right: case Direction::Right:
value_() -= increment_(); SetValue(value_() - increment_());
break; break;
case Direction::Left: case Direction::Left:
value_() += increment_(); SetValue(value_() + increment_());
break; break;
case Direction::Up: case Direction::Up:
case Direction::Down: case Direction::Down:
@ -70,10 +69,10 @@ class SliderBase : public ComponentBase {
void OnRight() { void OnRight() {
switch (options_.direction) { switch (options_.direction) {
case Direction::Right: case Direction::Right:
value_() += increment_(); SetValue(value_() + increment_());
break; break;
case Direction::Left: case Direction::Left:
value_() -= increment_(); SetValue(value_() - increment_());
break; break;
case Direction::Up: case Direction::Up:
case Direction::Down: case Direction::Down:
@ -84,10 +83,10 @@ class SliderBase : public ComponentBase {
void OnUp() { void OnUp() {
switch (options_.direction) { switch (options_.direction) {
case Direction::Up: case Direction::Up:
value_() -= increment_(); SetValue(value_() - increment_());
break; break;
case Direction::Down: case Direction::Down:
value_() += increment_(); SetValue(value_() + increment_());
break; break;
case Direction::Left: case Direction::Left:
case Direction::Right: case Direction::Right:
@ -98,10 +97,10 @@ class SliderBase : public ComponentBase {
void OnDown() { void OnDown() {
switch (options_.direction) { switch (options_.direction) {
case Direction::Down: case Direction::Down:
value_() -= increment_(); SetValue(value_() - increment_());
break; break;
case Direction::Up: case Direction::Up:
value_() += increment_(); SetValue(value_() + increment_());
break; break;
case Direction::Left: case Direction::Left:
case Direction::Right: case Direction::Right:
@ -128,7 +127,7 @@ class SliderBase : public ComponentBase {
OnUp(); OnUp();
} }
value_() = util::clamp(value_(), min_(), max_()); SetValue(util::clamp(value_(), min_(), max_()));
if (old_value != value_()) { if (old_value != value_()) {
return true; return true;
} }
@ -145,31 +144,33 @@ class SliderBase : public ComponentBase {
switch (options_.direction) { switch (options_.direction) {
case Direction::Right: { case Direction::Right: {
value_() = min_() + (event.mouse().x - gauge_box_.x_min) * SetValue(min_() + (event.mouse().x - gauge_box_.x_min) *
(max_() - min_()) / (max_() - min_()) /
(gauge_box_.x_max - gauge_box_.x_min); (gauge_box_.x_max - gauge_box_.x_min));
break; break;
} }
case Direction::Left: { case Direction::Left: {
value_() = max_() - (event.mouse().x - gauge_box_.x_min) * SetValue(max_() - (event.mouse().x - gauge_box_.x_min) *
(max_() - min_()) / (max_() - min_()) /
(gauge_box_.x_max - gauge_box_.x_min); (gauge_box_.x_max - gauge_box_.x_min));
break; break;
} }
case Direction::Down: { case Direction::Down: {
value_() = min_() + (event.mouse().y - gauge_box_.y_min) * SetValue(min_() + (event.mouse().y - gauge_box_.y_min) *
(max_() - min_()) / (max_() - min_()) /
(gauge_box_.y_max - gauge_box_.y_min); (gauge_box_.y_max - gauge_box_.y_min));
break; break;
} }
case Direction::Up: { case Direction::Up: {
value_() = max_() - (event.mouse().y - gauge_box_.y_min) * SetValue(max_() - (event.mouse().y - gauge_box_.y_min) *
(max_() - min_()) / (max_() - min_()) /
(gauge_box_.y_max - gauge_box_.y_min); (gauge_box_.y_max - gauge_box_.y_min));
break; break;
} }
} }
value_() = std::max(min_(), std::min(max_(), value_()));
SetValue(std::max(min_(), std::min(max_(), value_())));
return true; return true;
} }
@ -196,7 +197,15 @@ class SliderBase : public ComponentBase {
bool Focusable() const final { return true; } bool Focusable() const final { return true; }
void SetValue(Ref<T> val) {
value_() = util::clamp(val(), min_(), max_());
if (callback_ != nullptr) {
callback_(value_());
}
}
private: private:
std::function<void(T)> callback_;
Ref<T> value_; Ref<T> value_;
ConstRef<T> min_; ConstRef<T> min_;
ConstRef<T> max_; ConstRef<T> max_;
@ -206,6 +215,7 @@ class SliderBase : public ComponentBase {
CapturedMouse captured_mouse_; CapturedMouse captured_mouse_;
}; };
class SliderWithLabel : public ComponentBase { class SliderWithLabel : public ComponentBase {
public: public:
SliderWithLabel(ConstStringRef label, Component inner) SliderWithLabel(ConstStringRef label, Component inner)
@ -256,6 +266,7 @@ class SliderWithLabel : public ComponentBase {
Box box_; Box box_;
bool mouse_hover_ = false; bool mouse_hover_ = false;
}; };
} // namespace } // namespace
/// @brief An horizontal slider. /// @brief An horizontal slider.
@ -340,6 +351,7 @@ template <typename T>
Component Slider(SliderOption<T> options) { Component Slider(SliderOption<T> options) {
return Make<SliderBase<T>>(options); return Make<SliderBase<T>>(options);
} }
template Component Slider(SliderOption<int8_t>); template Component Slider(SliderOption<int8_t>);
template Component Slider(SliderOption<int16_t>); template Component Slider(SliderOption<int16_t>);
template Component Slider(SliderOption<int32_t>); template Component Slider(SliderOption<int32_t>);