HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
radio_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
8
9#pragma once
10
11#include "widget.hpp"
12#include "with_label_widget.hpp"
14#include "radio_delegate.hpp"
15#include "../telemetry/telemetry.hpp"
16#include "../macros.hpp"
17
18hi_export_module(hikogui.widgets.radio_widget);
19
20hi_export namespace hi {
21inline namespace v1 {
22
23template<typename Context>
25
45class radio_widget : public widget {
46public:
47 using super = widget;
48 using delegate_type = radio_delegate;
49
50 struct attributes_type {
51 observer<alignment> alignment = alignment::top_left();
52 keyboard_focus_group focus_group = keyboard_focus_group::normal;
53
54 attributes_type(attributes_type const&) noexcept = default;
55 attributes_type(attributes_type&&) noexcept = default;
56 attributes_type& operator=(attributes_type const&) noexcept = default;
57 attributes_type& operator=(attributes_type&&) noexcept = default;
58
59 template<radio_widget_attribute... Attributes>
60 explicit attributes_type(Attributes&&...attributes) noexcept
61 {
62 set_attributes(std::forward<Attributes>(attributes)...);
63 }
64
65 void set_attributes() noexcept {}
66
67 template<radio_widget_attribute First, radio_widget_attribute... Rest>
68 void set_attributes(First&& first, Rest&&...rest) noexcept
69 {
71 alignment = std::forward<First>(first);
72
73 } else if constexpr (forward_of<First, keyboard_focus_group>) {
74 focus_group = std::forward<First>(first);
75
76 } else {
77 hi_static_no_default();
78 }
79
80 set_attributes(std::forward<Rest>(rest)...);
81 }
82 };
83
84 attributes_type attributes;
85
89
90 hi_num_valid_arguments(consteval static, num_default_delegate_arguments, default_radio_delegate);
91 hi_call_left_arguments(static, make_default_delegate, make_shared_ctad<default_radio_delegate>);
92 hi_call_right_arguments(static, make_attributes, attributes_type);
93
95 {
96 this->delegate->deinit(*this);
97 }
98
105 attributes_type attributes,
107 super(), attributes(std::move(attributes)), delegate(std::move(delegate))
108 {
109 hi_axiom_not_null(this->delegate);
110 this->delegate->init(*this);
111 _delegate_cbt = this->delegate->subscribe([&] {
112 set_value(this->delegate->state(*this));
113 });
114 _delegate_cbt();
115 }
116
123 template<typename... Args>
124 radio_widget(Args&&...args)
125 requires(num_default_delegate_arguments<Args...>() != 0)
126 :
128 make_attributes<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...),
129 make_default_delegate<num_default_delegate_arguments<Args...>()>(std::forward<Args>(args)...))
130 {
131 }
132
134 [[nodiscard]] box_constraints update_constraints() noexcept override
135 {
136 _button_size = {theme().size(), theme().size()};
137 return box_constraints{_button_size, _button_size, _button_size, *attributes.alignment, theme().margin()};
138 }
139
140 void set_layout(widget_layout const& context) noexcept override
141 {
142 if (compare_store(_layout, context)) {
143 _button_rectangle = align(context.rectangle(), _button_size, os_settings::alignment(*attributes.alignment));
144
145 _button_circle = circle{_button_rectangle};
146
147 _pip_circle = align(_button_rectangle, circle{theme().size() * 0.5f - 3.0f}, alignment::middle_center());
148 }
149 super::set_layout(context);
150 }
151
152 void draw(draw_context const& context) noexcept override
153 {
154 if (mode() > widget_mode::invisible and overlaps(context, layout())) {
155 if (attributes.focus_group != keyboard_focus_group::menu) {
156 context.draw_circle(
157 layout(),
158 _button_circle * 1.02f,
159 background_color(),
160 focus_color(),
161 theme().border_width(),
163 }
164
165 switch (_animated_value.update(value() == widget_value::on ? 1.0f : 0.0f, context.display_time_point)) {
166 case animator_state::idle:
167 break;
168 case animator_state::running:
170 break;
171 case animator_state::end:
172 notifier();
173 break;
174 default:
175 hi_no_default();
176 }
177
178 // draw pip
179 auto float_value = _animated_value.current_value();
180 if (float_value > 0.0) {
181 context.draw_circle(layout(), _pip_circle * 1.02f * float_value, accent_color());
182 }
183 }
184 }
185
186 [[nodiscard]] color background_color() const noexcept override
187 {
188 hi_axiom(loop::main().on_thread());
189 if (phase() == widget_phase::pressed) {
190 return theme().fill_color(_layout.layer + 2);
191 } else {
192 return super::background_color();
193 }
194 }
195
196 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
197 {
198 hi_axiom(loop::main().on_thread());
199
200 if (mode() >= widget_mode::partial and layout().contains(position)) {
201 return {id, _layout.elevation, hitbox_type::button};
202 } else {
203 return {};
204 }
205 }
206
207 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
208 {
209 hi_axiom(loop::main().on_thread());
210 return mode() >= widget_mode::partial and to_bool(group & attributes.focus_group);
211 }
212
213 bool handle_event(gui_event const& event) noexcept override
214 {
215 hi_axiom(loop::main().on_thread());
216
217 switch (event.type()) {
218 case gui_event_type::gui_activate:
219 if (mode() >= widget_mode::partial) {
220 delegate->activate(*this);
222 return true;
223 }
224 break;
225
226 case gui_event_type::mouse_down:
227 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
228 set_pressed(true);
229 return true;
230 }
231 break;
232
233 case gui_event_type::mouse_up:
234 if (mode() >= widget_mode::partial and event.mouse().cause.left_button) {
235 set_pressed(false);
236
237 // with_label_widget or other widgets may have accepted the hitbox
238 // for this widget. Which means the widget_id in the mouse-event
239 // may match up with the radio.
240 if (event.mouse().hitbox.widget_id == id) {
241 // By staying we can give focus to the parent widget.
242 handle_event(gui_event_type::gui_activate_stay);
243 }
245 return true;
246 }
247 break;
248
249 default:;
250 }
251
252 return super::handle_event(event);
253 }
255
256private:
257 constexpr static std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
258
259 extent2 _button_size;
260 aarectangle _button_rectangle;
261
262 circle _button_circle;
263
264 animator<float> _animated_value = _animation_duration;
265 circle _pip_circle;
266
267 callback<void()> _delegate_cbt;
268};
269
270using radio_with_label_widget = with_label_widget<radio_widget>;
271using radio_menu_button_widget = menu_button_widget<radio_widget>;
272
273} // namespace v1
274} // namespace hi::v1
Defines widget.
Defines menu_button_widget.
Defines with_label_widget.
@ partial
A widget is partially enabled.
Definition widget_state.hpp:73
@ invisible
The widget is invisible.
Definition widget_state.hpp:41
STL namespace.
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
@ color
A color value was modified.
Definition style_modify_mask.hpp:27
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context_intf.hpp:35
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:53
notifier< void()> notifier
Notifier which is called after an action is completed by a widget.
Definition widget_intf.hpp:45
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:31
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget_intf.hpp:241
2D constraints.
Definition box_constraints.hpp:25
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
Add menu-button around a small-button.
Definition menu_button_widget.hpp:38
A radio delegate controls the state of a radio button widget.
Definition radio_delegate.hpp:16
A default radio button delegate.
Definition radio_delegate.hpp:56
A GUI widget that permits the user to make a binary choice.
Definition radio_widget.hpp:45
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition radio_widget.hpp:88
radio_widget(attributes_type attributes, std::shared_ptr< delegate_type > delegate) noexcept
Construct a radio widget.
Definition radio_widget.hpp:104
radio_widget(Args &&...args)
Construct a radio widget with a default button delegate.
Definition radio_widget.hpp:124
Definition radio_widget.hpp:50
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:116
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:136
widget() noexcept
Constructor for creating sub views.
Definition widget.hpp:50
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:110
bool handle_event(gui_event const &event) noexcept override
Handle command.
Definition widget.hpp:145
Add labels to a button.
Definition with_label_widget.hpp:41
True if T is a forwarded type of Forward.
Definition concepts.hpp:137
Definition radio_widget.hpp:24
T align(T... args)
T forward(T... args)
T move(T... args)