HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
abstract_button_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "widget.hpp"
12#include "button_delegate.hpp"
13#include "label_widget.hpp"
14#include "../animator.hpp"
15#include "../i18n/translate.hpp"
16#include "../notifier.hpp"
17#include <memory>
18#include <string>
19#include <array>
20#include <optional>
21#include <future>
22
23namespace hi { inline namespace v1 {
24
25template<typename Context>
27
33public:
34 using super = widget;
35 using delegate_type = button_delegate;
36
40
43 observer<label> on_label = tr("on");
44
47 observer<label> off_label = tr("off");
48
51 observer<label> other_label = tr("other");
52
55 observer<alignment> alignment;
56
59 observer<semantic_text_style> text_style = semantic_text_style::label;
60
61 notifier<void()> pressed;
62
64
66
70 [[nodiscard]] button_state state() const noexcept
71 {
72 hi_axiom(loop::main().on_thread());
74 return delegate->state(*this);
75 }
76
78 [[nodiscard]] box_constraints update_constraints() noexcept override;
79 void set_layout(widget_layout const &context) noexcept override;
80 [[nodiscard]] generator<widget const &> children(bool include_invisible) const noexcept override
81 {
82 co_yield *_on_label_widget;
83 co_yield *_off_label_widget;
84 co_yield *_other_label_widget;
85 }
86
87 [[nodiscard]] color background_color() const noexcept override;
88 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept final;
89 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
90 void activate() noexcept;
91 bool handle_event(gui_event const& event) noexcept override;
93protected:
94 std::unique_ptr<label_widget> _on_label_widget;
95 box_constraints _on_label_constraints;
96 box_shape _on_label_shape;
97
98 std::unique_ptr<label_widget> _off_label_widget;
99 box_constraints _off_label_constraints;
100 box_shape _off_label_shape;
101
102 std::unique_ptr<label_widget> _other_label_widget;
103 box_constraints _other_label_constraints;
104 box_shape _other_label_shape;
105
106 bool _pressed = false;
107 notifier<>::callback_token _delegate_cbt;
108
109 template<size_t I>
110 void set_attributes() noexcept
111 {
112 }
113
114 template<size_t I>
115 void set_attributes(button_widget_attribute auto&& first, button_widget_attribute auto&&...rest) noexcept
116 {
117 if constexpr (forward_of<decltype(first), observer<hi::label>>) {
118 if constexpr (I == 0) {
119 on_label = first;
120 off_label = first;
121 other_label = hi_forward(first);
122 } else if constexpr (I == 1) {
123 other_label.reset();
124 off_label.reset();
125 off_label = hi_forward(first);
126 } else if constexpr (I == 2) {
127 other_label = hi_forward(first);
128 } else {
130 }
131 set_attributes<I + 1>(hi_forward(rest)...);
132
133 } else if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
134 alignment = hi_forward(first);
135 set_attributes<I>(hi_forward(rest)...);
136
137 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
138 text_style = hi_forward(first);
139 set_attributes<I>(hi_forward(rest)...);
140
141 } else {
143 }
144 }
145
146 void draw_button(draw_context const& context) noexcept;
147};
148
149}} // namespace hi::v1
Defines widget.
Defines label_widget.
Defines button_delegate and some default button delegates.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:308
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
button_state
The state of a button.
Definition button_delegate.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
This is a RGBA floating point color.
Definition color.hpp:42
A user interface event.
Definition gui_event.hpp:74
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:32
observer< semantic_text_style > text_style
The text style to button's label.
Definition abstract_button_widget.hpp:59
observer< label > other_label
The label to show when the button is in the 'other' state.
Definition abstract_button_widget.hpp:51
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:39
button_state state() const noexcept
Get the current state of the button.
Definition abstract_button_widget.hpp:70
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition abstract_button_widget.hpp:43
observer< label > off_label
The label to show when the button is in the 'off' state.
Definition abstract_button_widget.hpp:47
observer< alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:55
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:45
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:42
An interactive graphical object as part of the user-interface.
Definition widget.hpp:46
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:51
The layout of a widget.
Definition widget_layout.hpp:38
Definition abstract_button_widget.hpp:26
Definition label_widget.hpp:26