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>
struct SliderOption {
Ref<T> value;
std::function<void(T)> callback = nullptr;
ConstRef<T> min = T(0);
ConstRef<T> max = T(100);
ConstRef<T> increment = (max() - min()) / 20;

View File

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