50class SliderBase :
public SliderOption<T>,
public ComponentBase {
52 explicit SliderBase(SliderOption<T> options)
53 : SliderOption<T>(std::move(options)) {}
57 Focused() ?
color(this->color_active) : color(this->color_inactive);
59 float(this->
value() - this->min()) / float(this->max() - this->min());
61 flexDirection(this->direction) |
reflect(gauge_box_) | gauge_color;
65 if (pressed == this->direction) {
66 this->
value() += this->increment();
70 if (pressed == Opposite(this->direction)) {
71 this->
value() -= this->increment();
76 bool OnEvent(Event event)
final {
77 if (event.is_mouse()) {
78 return OnMouseEvent(event);
81 T old_value = this->
value();
82 if (event == Event::ArrowLeft || event == Event::Character(
'h')) {
85 if (event == Event::ArrowRight || event == Event::Character(
'l')) {
88 if (event == Event::ArrowUp || event == Event::Character(
'k')) {
91 if (event == Event::ArrowDown || event == Event::Character(
'j')) {
95 this->
value() = std::max(this->min(), std::min(this->max(), this->
value()));
96 if (old_value != this->
value()) {
97 App::PostEventOrExecute(this->on_change);
101 return ComponentBase::OnEvent(event);
104 bool OnCapturedMouseEvent(Event event) {
105 if (event.mouse().motion == Mouse::Released) {
106 captured_mouse_ =
nullptr;
110 T old_value = this->
value();
111 switch (this->direction) {
113 this->
value() = this->min() + (
event.mouse().x - gauge_box_.x_min) *
114 (this->max() - this->min()) /
115 (gauge_box_.x_max - gauge_box_.x_min);
120 this->
value() = this->max() - (
event.mouse().x - gauge_box_.x_min) *
121 (this->max() - this->min()) /
122 (gauge_box_.x_max - gauge_box_.x_min);
126 this->
value() = this->min() + (
event.mouse().
y - gauge_box_.y_min) *
127 (this->max() - this->min()) /
128 (gauge_box_.y_max - gauge_box_.y_min);
132 this->
value() = this->max() - (
event.mouse().
y - gauge_box_.y_min) *
133 (this->max() - this->min()) /
134 (gauge_box_.y_max - gauge_box_.y_min);
139 this->
value() = std::max(this->min(), std::min(this->max(), this->
value()));
141 if (old_value != this->
value()) {
142 App::PostEventOrExecute(this->on_change);
147 bool OnMouseEvent(Event event) {
148 if (captured_mouse_) {
149 return OnCapturedMouseEvent(event);
152 if (event.mouse().button != Mouse::Left) {
155 if (event.mouse().motion != Mouse::Pressed) {
159 if (!gauge_box_.Contain(event.mouse().x, event.mouse().y)) {
163 captured_mouse_ = CaptureMouse(event);
165 if (captured_mouse_) {
167 return OnCapturedMouseEvent(event);
173 bool Focusable() const final {
return true; }
180class SliderWithLabel :
public ComponentBase {
182 SliderWithLabel(ConstStringRef label,
Component inner)
183 : label_(std::move(label)) {
184 Add(std::move(inner));
185 SetActiveChild(ChildAt(0));
189 bool OnEvent(Event event)
final {
190 if (ComponentBase::OnEvent(event)) {
194 if (!event.is_mouse()) {
198 mouse_hover_ = box_.Contain(event.mouse().x, event.mouse().y);
204 if (!CaptureMouse(event)) {
212 auto gauge_color = (Focused() || mouse_hover_) ?
color(Color::White)
213 :
color(Color::GrayDark);
223 gauge_color | xflex |
reflect(box_);
229 ConstStringRef label_;
231 bool mouse_hover_ =
false;
268 auto slider = Make<SliderBase<int>>(option);
282 auto slider = Make<SliderBase<float>>(option);
295 auto slider = Make<SliderBase<long>>(option);
316 return Make<SliderBase<T>>(std::move(options));
#define FTXUI_EXPORT(component)
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Element xflex(Element)
在 X 軸上盡可能擴展/在需要時最小化。
Element gaugeDirection(float progress, Direction direction)
繪製一個指定方向的高解析度進度條。
Element yflex(Element)
在 Y 軸上盡可能擴展/在需要時最小化。
Element underlined(Element child)
為給定元素加上底線。
Direction
Direction 是一個列舉,表示四個主要方向。
Element focus(Element child)
將 child 設置為其同級元素中被聚焦的元素。
Element vcenter(Element child)
垂直置中一個元素。
Element color(Color color, Element child)
設定元素的前景顏色。
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
Element hbox(Elements children)
一個逐一水平顯示元素的容器。
std::function< Element(Element)> Decorator
Component Slider(SliderOption< T > options)
任意方向的滑桿。
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component