13#include "../GUI/module.hpp"
14#include "../utility/module.hpp"
15#include "../animator.hpp"
16#include "../notifier.hpp"
23namespace hi {
inline namespace v1 {
25template<
typename Context>
32template<fixed_
string Prefix>
38 constexpr static auto prefix = Prefix;
75 _delegate_cbt = this->delegate->subscribe([&] {
76 ++global_counter<
"abstract_button_widget:delegate:redraw">;
78 state = this->delegate->state(
this);
81 this->delegate->init(*
this);
85 [[nodiscard]] box_constraints update_constraints() noexcept
override
87 _on_label_constraints = _on_label_widget->update_constraints();
88 _off_label_constraints = _off_label_widget->update_constraints();
89 _other_label_constraints = _other_label_widget->update_constraints();
90 return max(_on_label_constraints, _off_label_constraints, _other_label_constraints);
93 void set_layout(widget_layout
const& context)
noexcept override
99 _on_label_widget->set_layout(context.transform(_on_label_shape));
100 _off_label_widget->set_layout(context.transform(_off_label_shape));
101 _other_label_widget->set_layout(context.transform(_other_label_shape));
104 [[nodiscard]] generator<widget const&> children(
bool include_invisible)
const noexcept override
106 co_yield *_on_label_widget;
107 co_yield *_off_label_widget;
108 co_yield *_other_label_widget;
111 [[nodiscard]] hitbox hitbox_test(point2i position)
const noexcept final
116 return {
id, layout.
elevation, hitbox_type::button};
122 [[nodiscard]]
bool accepts_keyboard_focus(keyboard_focus_group group)
const noexcept override
128 void activate() noexcept
132 this->_state_changed();
135 bool handle_event(gui_event
const& event)
noexcept override
139 switch (event.type()) {
140 case gui_event_type::gui_activate:
147 case gui_event_type::mouse_down:
155 case gui_event_type::mouse_up:
159 if (layout.rectangle().
contains(event.mouse().position)) {
160 handle_event(gui_event_type::gui_activate);
175 box_constraints _on_label_constraints;
176 box_shape _on_label_shape;
179 box_constraints _off_label_constraints;
180 box_shape _off_label_shape;
183 box_constraints _other_label_constraints;
184 box_shape _other_label_shape;
186 bool _pressed =
false;
187 notifier<>::callback_token _delegate_cbt;
190 void set_attributes() noexcept
195 void set_attributes(button_widget_attribute
auto&& first, button_widget_attribute
auto&&...rest)
noexcept
197 if constexpr (forward_of<
decltype(first), observer<hi::label>>) {
198 if constexpr (I == 0) {
202 }
else if constexpr (I == 1) {
206 }
else if constexpr (I == 2) {
213 }
else if constexpr (forward_of<
decltype(first), observer<hi::alignment>>) {
222 void draw_button(widget_draw_context& context)
noexcept
224 _on_label_widget->draw(context);
225 _off_label_widget->draw(context);
226 _other_label_widget->draw(context);
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:323
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
@ window_redraw
Request that part of the window gets redrawn on the next frame.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
@ display
The widget is in display-only mode.
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr auto join_path(fixed_string< L > const &lhs, fixed_string< R > const &rhs) noexcept
lhs / rhs
Definition fixed_string.hpp:273
geometry/margins.hpp
Definition cache.hpp:11
@ off
The widget in the off-state.
@ other
The widget is in the other-state.
@ on
The widget is in the on-state.
constexpr bool contains(point< value_type, 2 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:265
widget_id id
The numeric identifier of a widget.
Definition widget.hpp:35
virtual void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:265
observer< bool > clicked
The widget is being clicked by the mouse.
Definition widget.hpp:57
observer< widget_state > state
The state of the widget.
Definition widget.hpp:65
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
Definition widget.hpp:274
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
constexpr bool contains(point3i mouse_position) const noexcept
Check if the mouse position is inside the widget.
Definition widget_layout.hpp:126
float elevation
The elevation of the widget above the window.
Definition widget_layout.hpp:72
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:33
observer< hi::alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:58
observer< label > on_label
The label to show when the button is in the 'on' state.
Definition abstract_button_widget.hpp:46
observer< label > other_label
The label to show when the button is in the 'other' state.
Definition abstract_button_widget.hpp:54
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:42
observer< label > off_label
The label to show when the button is in the 'off' state.
Definition abstract_button_widget.hpp:50
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:23
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:42
Definition abstract_button_widget.hpp:26
Definition label_widget.hpp:26