mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-10-30 16:07:38 +08:00
Merge 27388d456f
into 1d40687a40
This commit is contained in:
commit
e4bbf883d7
@ -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;
|
||||
|
@ -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)
|
||||
@ -56,10 +55,10 @@ class SliderBase : public ComponentBase {
|
||||
void OnLeft() {
|
||||
switch (options_.direction) {
|
||||
case Direction::Right:
|
||||
value_() -= increment_();
|
||||
SetValue(value_() - increment_());
|
||||
break;
|
||||
case Direction::Left:
|
||||
value_() += increment_();
|
||||
SetValue(value_() + increment_());
|
||||
break;
|
||||
case Direction::Up:
|
||||
case Direction::Down:
|
||||
@ -70,10 +69,10 @@ class SliderBase : public ComponentBase {
|
||||
void OnRight() {
|
||||
switch (options_.direction) {
|
||||
case Direction::Right:
|
||||
value_() += increment_();
|
||||
SetValue(value_() + increment_());
|
||||
break;
|
||||
case Direction::Left:
|
||||
value_() -= increment_();
|
||||
SetValue(value_() - increment_());
|
||||
break;
|
||||
case Direction::Up:
|
||||
case Direction::Down:
|
||||
@ -84,10 +83,10 @@ class SliderBase : public ComponentBase {
|
||||
void OnUp() {
|
||||
switch (options_.direction) {
|
||||
case Direction::Up:
|
||||
value_() -= increment_();
|
||||
SetValue(value_() - increment_());
|
||||
break;
|
||||
case Direction::Down:
|
||||
value_() += increment_();
|
||||
SetValue(value_() + increment_());
|
||||
break;
|
||||
case Direction::Left:
|
||||
case Direction::Right:
|
||||
@ -98,10 +97,10 @@ class SliderBase : public ComponentBase {
|
||||
void OnDown() {
|
||||
switch (options_.direction) {
|
||||
case Direction::Down:
|
||||
value_() -= increment_();
|
||||
SetValue(value_() - increment_());
|
||||
break;
|
||||
case Direction::Up:
|
||||
value_() += increment_();
|
||||
SetValue(value_() + increment_());
|
||||
break;
|
||||
case Direction::Left:
|
||||
case Direction::Right:
|
||||
@ -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;
|
||||
}
|
||||
@ -145,31 +144,33 @@ class SliderBase : public ComponentBase {
|
||||
|
||||
switch (options_.direction) {
|
||||
case Direction::Right: {
|
||||
value_() = min_() + (event.mouse().x - gauge_box_.x_min) *
|
||||
SetValue(min_() + (event.mouse().x - gauge_box_.x_min) *
|
||||
(max_() - min_()) /
|
||||
(gauge_box_.x_max - gauge_box_.x_min);
|
||||
(gauge_box_.x_max - gauge_box_.x_min));
|
||||
|
||||
break;
|
||||
}
|
||||
case Direction::Left: {
|
||||
value_() = max_() - (event.mouse().x - gauge_box_.x_min) *
|
||||
SetValue(max_() - (event.mouse().x - gauge_box_.x_min) *
|
||||
(max_() - min_()) /
|
||||
(gauge_box_.x_max - gauge_box_.x_min);
|
||||
(gauge_box_.x_max - gauge_box_.x_min));
|
||||
break;
|
||||
}
|
||||
case Direction::Down: {
|
||||
value_() = min_() + (event.mouse().y - gauge_box_.y_min) *
|
||||
SetValue(min_() + (event.mouse().y - gauge_box_.y_min) *
|
||||
(max_() - min_()) /
|
||||
(gauge_box_.y_max - gauge_box_.y_min);
|
||||
(gauge_box_.y_max - gauge_box_.y_min));
|
||||
break;
|
||||
}
|
||||
case Direction::Up: {
|
||||
value_() = max_() - (event.mouse().y - gauge_box_.y_min) *
|
||||
SetValue(max_() - (event.mouse().y - gauge_box_.y_min) *
|
||||
(max_() - min_()) /
|
||||
(gauge_box_.y_max - gauge_box_.y_min);
|
||||
(gauge_box_.y_max - gauge_box_.y_min));
|
||||
break;
|
||||
}
|
||||
}
|
||||
value_() = std::max(min_(), std::min(max_(), value_()));
|
||||
|
||||
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>);
|
||||
|
Loading…
Reference in New Issue
Block a user