Compare commits

..

1 Commits

Author SHA1 Message Date
Mikołaj Lubiak
a85fe6ace1
Merge 528c508584 into 1d40687a40 2024-10-19 16:01:30 +00:00
2 changed files with 245 additions and 59 deletions

View File

@ -222,7 +222,20 @@ 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> max = T(100);
ConstRef<T> increment = (max() - min()) / 20;
Direction direction = Direction::Right;
Color color_active = Color::White;
Color color_inactive = Color::GrayDark;
};
// @brief Option for the `SliderWithCallback` component.
// @ingroup component
template <typename T>
struct SliderWithCallbackOption {
std::function<void(T)> callback;
Ref<T> value;
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,6 +38,229 @@ 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),
min_(options.min),
max_(options.max),
increment_(options.increment),
options_(options) {}
Element Render() override {
auto gauge_color = Focused() ? color(options_.color_active)
: color(options_.color_inactive);
const float percent = float(value_() - min_()) / float(max_() - min_());
return gaugeDirection(percent, options_.direction) |
flexDirection(options_.direction) | reflect(gauge_box_) |
gauge_color;
}
void OnLeft() {
switch (options_.direction) {
case Direction::Right:
value_() -= increment_();
break;
case Direction::Left:
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;
}
}
void OnUp() {
switch (options_.direction) {
case Direction::Up:
value_() -= increment_();
break;
case Direction::Down:
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;
}
}
bool OnEvent(Event event) final {
if (event.is_mouse()) {
return OnMouseEvent(event);
}
T old_value = value_();
if (event == Event::ArrowLeft || event == Event::Character('h')) {
OnLeft();
}
if (event == Event::ArrowRight || event == Event::Character('l')) {
OnRight();
}
if (event == Event::ArrowUp || event == Event::Character('k')) {
OnDown();
}
if (event == Event::ArrowDown || event == Event::Character('j')) {
OnUp();
}
value_() = util::clamp(value_(), min_(), max_());
if (old_value != value_()) {
return true;
}
return ComponentBase::OnEvent(event);
}
bool OnMouseEvent(Event event) {
if (captured_mouse_) {
if (event.mouse().motion == Mouse::Released) {
captured_mouse_ = nullptr;
return true;
}
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;
}
}
value_() = std::max(min_(), std::min(max_(), value_()));
return true;
}
if (event.mouse().button != Mouse::Left) {
return false;
}
if (event.mouse().motion != Mouse::Pressed) {
return false;
}
if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
return false;
}
captured_mouse_ = CaptureMouse(event);
if (captured_mouse_) {
TakeFocus();
return true;
}
return false;
}
bool Focusable() const final { return true; }
private:
Ref<T> value_;
ConstRef<T> min_;
ConstRef<T> max_;
ConstRef<T> increment_;
SliderOption<T> options_;
Box gauge_box_;
CapturedMouse captured_mouse_;
};
class SliderWithLabel : public ComponentBase {
public:
SliderWithLabel(ConstStringRef label, Component inner)
: label_(std::move(label)) {
Add(std::move(inner));
SetActiveChild(ChildAt(0));
}
private:
bool OnEvent(Event event) final {
if (ComponentBase::OnEvent(event)) {
return true;
}
if (!event.is_mouse()) {
return false;
}
mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
if (!mouse_hover_) {
return false;
}
if (!CaptureMouse(event)) {
return false;
}
return true;
}
Element Render() override {
auto focus_management = Focused() ? focus : Active() ? select : nothing;
auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
: color(Color::GrayDark);
return hbox({
text(label_()) | dim | vcenter,
hbox({
text("["),
ComponentBase::Render() | underlined,
text("]"),
}) | xflex,
}) |
gauge_color | xflex | reflect(box_) | focus_management;
}
ConstStringRef label_;
Box box_;
bool mouse_hover_ = false;
};
template <class T>
class SliderWithCallback : public ComponentBase {
public:
explicit SliderWithCallback(SliderWithCallbackOption<T> options)
: callback_(options.callback), min_(options.min), max_(options.max), : callback_(options.callback), min_(options.min), max_(options.max),
increment_(options.increment), options_(options) { increment_(options.increment), options_(options) {
SetValue(options.value()); SetValue(options.value());
@ -199,10 +422,8 @@ public:
void SetValue(Ref<T> val) { void SetValue(Ref<T> val) {
value_() = util::clamp(val(), min_(), max_()); value_() = util::clamp(val(), min_(), max_());
if (callback_ != nullptr) {
callback_(value_()); callback_(value_());
} }
}
private: private:
std::function<void(T)> callback_; std::function<void(T)> callback_;
@ -210,63 +431,10 @@ private:
ConstRef<T> min_; ConstRef<T> min_;
ConstRef<T> max_; ConstRef<T> max_;
ConstRef<T> increment_; ConstRef<T> increment_;
SliderOption<T> options_; SliderWithCallbackOption<T> options_;
Box gauge_box_; Box gauge_box_;
CapturedMouse captured_mouse_; CapturedMouse captured_mouse_;
}; };
class SliderWithLabel : public ComponentBase {
public:
SliderWithLabel(ConstStringRef label, Component inner)
: label_(std::move(label)) {
Add(std::move(inner));
SetActiveChild(ChildAt(0));
}
private:
bool OnEvent(Event event) final {
if (ComponentBase::OnEvent(event)) {
return true;
}
if (!event.is_mouse()) {
return false;
}
mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
if (!mouse_hover_) {
return false;
}
if (!CaptureMouse(event)) {
return false;
}
return true;
}
Element Render() override {
auto focus_management = Focused() ? focus : Active() ? select : nothing;
auto gauge_color = (Focused() || mouse_hover_) ? color(Color::White)
: color(Color::GrayDark);
return hbox({
text(label_()) | dim | vcenter,
hbox({
text("["),
ComponentBase::Render() | underlined,
text("]"),
}) | xflex,
}) |
gauge_color | xflex | reflect(box_) | focus_management;
}
ConstStringRef label_;
Box box_;
bool mouse_hover_ = false;
};
} // namespace } // namespace
/// @brief An horizontal slider. /// @brief An horizontal slider.
@ -352,6 +520,11 @@ Component Slider(SliderOption<T> options) {
return Make<SliderBase<T>>(options); return Make<SliderBase<T>>(options);
} }
template <typename T>
Component Slider(SliderWithCallbackOption<T> options) {
return Make<SliderWithCallback<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>);