2021-04-29 06:18:58 +08:00
|
|
|
#include "ftxui/component/slider.hpp"
|
2021-05-02 02:40:35 +08:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp"
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/component/mouse.hpp"
|
2021-05-02 00:13:56 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2021-05-02 02:40:35 +08:00
|
|
|
#include "ftxui/dom/elements.hpp"
|
|
|
|
#include "ftxui/screen/box.hpp"
|
|
|
|
#include "ftxui/screen/color.hpp"
|
2021-04-29 06:18:58 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
2021-05-02 00:13:56 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
template <class T>
|
2021-05-02 00:13:56 +08:00
|
|
|
class SliderImpl : public Component {
|
2021-04-29 06:18:58 +08:00
|
|
|
public:
|
2021-05-02 00:13:56 +08:00
|
|
|
SliderImpl(std::wstring label, T* value, T min, T max, T increment)
|
2021-04-29 06:18:58 +08:00
|
|
|
: label_(label),
|
|
|
|
value_(value),
|
|
|
|
min_(min),
|
|
|
|
max_(max),
|
|
|
|
increment_(increment) {}
|
|
|
|
|
|
|
|
Element Render() {
|
|
|
|
auto gauge_color =
|
|
|
|
Focused() ? color(Color::GrayLight) : color(Color::GrayDark);
|
|
|
|
float percent = float(*value_ - min_) / float(max_ - min_);
|
|
|
|
return hbox({
|
|
|
|
text(label_) | dim | vcenter,
|
|
|
|
hbox({
|
|
|
|
text(L"["),
|
|
|
|
gauge(percent) | underlined | xflex | reflect(gauge_box_),
|
|
|
|
text(L"]"),
|
|
|
|
}) | xflex,
|
|
|
|
}) |
|
|
|
|
gauge_color | xflex | reflect(box_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnEvent(Event event) final {
|
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h')) {
|
|
|
|
*value_ -= increment_;
|
|
|
|
*value_ = std::max(*value_, min_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l')) {
|
|
|
|
*value_ += increment_;
|
|
|
|
*value_ = std::min(*value_, max_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Component::OnEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnMouseEvent(Event event) {
|
2021-05-02 00:13:56 +08:00
|
|
|
if (captured_mouse_ && event.mouse().motion == Mouse::Released) {
|
|
|
|
captured_mouse_ = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box_.Contain(event.mouse().x, event.mouse().y) &&
|
2021-05-02 02:40:35 +08:00
|
|
|
CaptureMouse(event)) {
|
2021-05-02 00:13:56 +08:00
|
|
|
TakeFocus();
|
|
|
|
}
|
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
2021-05-02 00:13:56 +08:00
|
|
|
event.mouse().motion == Mouse::Pressed &&
|
|
|
|
gauge_box_.Contain(event.mouse().x, event.mouse().y) &&
|
|
|
|
!captured_mouse_) {
|
2021-05-02 02:40:35 +08:00
|
|
|
captured_mouse_ = CaptureMouse(event);
|
2021-05-02 00:13:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (captured_mouse_) {
|
2021-04-29 06:18:58 +08:00
|
|
|
*value_ = min_ + (event.mouse().x - gauge_box_.x_min) * (max_ - min_) /
|
2021-05-02 00:13:56 +08:00
|
|
|
(gauge_box_.x_max - gauge_box_.x_min);
|
|
|
|
*value_ = std::max(min_, std::min(max_, *value_));
|
|
|
|
return true;
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
2021-05-02 00:13:56 +08:00
|
|
|
return false;
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::wstring label_;
|
2021-05-02 00:13:56 +08:00
|
|
|
T* value_;
|
|
|
|
T min_;
|
|
|
|
T max_;
|
|
|
|
T increment_ = 1;
|
2021-04-29 06:18:58 +08:00
|
|
|
Box box_;
|
|
|
|
Box gauge_box_;
|
2021-05-02 00:13:56 +08:00
|
|
|
CapturedMouse captured_mouse_;
|
2021-04-29 06:18:58 +08:00
|
|
|
};
|
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
template <class T>
|
|
|
|
ComponentPtr Slider(std::wstring label, T* value, T min, T max, T increment) {
|
|
|
|
return std::make_unique<SliderImpl<T>>(std::move(label), value, min, max,
|
|
|
|
increment);
|
2021-04-29 06:18:58 +08:00
|
|
|
}
|
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
template ComponentPtr Slider(std::wstring label,
|
|
|
|
int* value,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
int increment);
|
|
|
|
|
|
|
|
template ComponentPtr Slider(std::wstring label,
|
|
|
|
float* value,
|
|
|
|
float min,
|
|
|
|
float max,
|
|
|
|
float increment);
|
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
} // namespace ftxui
|
2021-05-02 02:40:35 +08:00
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|