mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-24 03:30:49 +08:00
Merge 27388d456f
into 1d40687a40
This commit is contained in:
commit
e4bbf883d7
@ -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;
|
||||||
|
@ -36,13 +36,12 @@ Decorator flexDirection(Direction direction) {
|
|||||||
|
|
||||||
template <class T>
|
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)
|
||||||
@ -55,57 +54,57 @@ 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:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
@ -144,32 +143,34 @@ 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: {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +197,15 @@ class SliderBase : public ComponentBase {
|
|||||||
|
|
||||||
bool Focusable() const final { return true; }
|
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_;
|
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>);
|
||||||
|
Loading…
Reference in New Issue
Block a user