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]] generator<widget *> children() const noexcept override
79 {
80 co_yield _on_label_widget.get();
81 co_yield _off_label_widget.get();
82 co_yield _other_label_widget.get();
83 }
84
85 [[nodiscard]] color background_color() const noexcept override;
86 [[nodiscard]] hitbox hitbox_test(point3 position) const noexcept final;
87 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
88 void activate() noexcept;
89 bool handle_event(gui_event const& event) noexcept override;
91protected:
92 aarectangle _label_rectangle;
93 std::unique_ptr<label_widget> _on_label_widget;
94 std::unique_ptr<label_widget> _off_label_widget;
95 std::unique_ptr<label_widget> _other_label_widget;
96
97 bool _pressed = false;
98 notifier<>::callback_token _delegate_cbt;
99
100 template<size_t I>
101 void set_attributes() noexcept
102 {
103 }
104
105 template<size_t I>
106 void set_attributes(button_widget_attribute auto&& first, button_widget_attribute auto&&...rest) noexcept
107 {
108 if constexpr (forward_of<decltype(first), observer<hi::label>>) {
109 if constexpr (I == 0) {
110 on_label = first;
111 off_label = first;
112 other_label = hi_forward(first);
113 } else if constexpr (I == 1) {
114 other_label.reset();
115 off_label.reset();
116 off_label = hi_forward(first);
117 } else if constexpr (I == 2) {
118 other_label = hi_forward(first);
119 } else {
121 }
122 set_attributes<I + 1>(hi_forward(rest)...);
123
124 } else if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
125 alignment = hi_forward(first);
126 set_attributes<I>(hi_forward(rest)...);
127
128 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
129 text_style = hi_forward(first);
130 set_attributes<I>(hi_forward(rest)...);
131
132 } else {
134 }
135 }
136
137 widget_constraints set_constraints_button(set_constraints_context const &context) const noexcept;
138 void set_layout_button(widget_layout const& context) noexcept;
139 void draw_button(draw_context const& context) noexcept;
140};
141
142}} // namespace hi::v1
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hi_assert_not_null(x)
Assert if an expression is not nullptr.
Definition assert.hpp:118
#define hi_static_no_default()
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:172
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
Defines widget.
Defines label_widget.
Defines button_delegate and some default button delegates.
button_state
The state of a button.
Definition button_delegate.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
A user interface event.
Definition gui_event.hpp:76
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:41
An interactive graphical object as part of the user-interface.
Definition widget.hpp:45
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:50
Definition abstract_button_widget.hpp:26
Definition label_widget.hpp:25