HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
radio_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
12
13namespace hi { inline namespace v1 {
14
42template<fixed_string Name = "">
43class radio_button_widget final : public abstract_button_widget<Name / "radio-button"> {
44public:
45 using super = abstract_button_widget<Name / "radio-button">;
46 using delegate_type = typename super::delegate_type;
47 constexpr static auto prefix = super::prefix;
48
61 button_widget_attribute auto&&...attributes) noexcept :
63 {
64 this->alignment = alignment::top_left();
65 this->set_attributes<0>(hi_forward(attributes)...);
66 }
67
80 template<
81 different_from<std::shared_ptr<delegate_type>> Value,
82 forward_of<observer<observer_decay_t<Value>>> OnValue,
83 button_widget_attribute... Attributes>
84 radio_button_widget(widget *parent, Value&& value, OnValue&& on_value, Attributes&&...attributes) noexcept
85 requires requires { make_default_radio_button_delegate(hi_forward(value), hi_forward(on_value)); }
86 :
88 parent,
90 hi_forward(attributes)...)
91 {
92 }
93
95 [[nodiscard]] box_constraints update_constraints() noexcept override
96 {
97 _label_constraints = super::update_constraints();
98
99 // Make room for button and margin.
100 _button_size = theme<prefix>.size(this);
101 hi_axiom(button_size.width() == button_size.height());
102
103 hilet extra_size = extent2i{theme<prefix>.horizontal_spacing(this) + _button_size.width(), 0};
104
105 auto constraints = max(_label_constraints + extra_size, _button_size);
106 constraints.margins = theme<prefix>.margin(this);
107 constraints.alignment = *this->alignment;
108 return constraints;
109 }
110
111 void set_layout(widget_layout const& context) noexcept override
112 {
113 if (compare_store(this->layout, context)) {
114 auto alignment_ = os_settings::left_to_right() ? *this->alignment : mirror(*this->alignment);
115
116 if (alignment_ == horizontal_alignment::left or alignment_ == horizontal_alignment::right) {
117 _button_rectangle = align(context.rectangle(), _button_size, alignment_);
118 } else {
120 }
121
122 hilet inner_margin = theme<prefix>.horizontal_spacing(this);
123 hilet cap_height = theme<prefix>.cap_height(this);
124
125 hilet label_width = context.width() - (_button_rectangle.width() + inner_margin);
126 if (alignment_ == horizontal_alignment::left) {
127 hilet label_left = _button_rectangle.right() + inner_margin;
128 hilet label_rectangle = aarectanglei{label_left, 0, label_width, context.height()};
129 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
130 box_shape{_label_constraints, label_rectangle, cap_height};
131
132 } else if (alignment_ == horizontal_alignment::right) {
133 hilet label_rectangle = aarectanglei{0, 0, label_width, context.height()};
134 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
135 box_shape{_label_constraints, label_rectangle, cap_height};
136
137 } else {
139 }
140
141 _button_circle = circle{narrow_cast<aarectangle>(_button_rectangle)};
142
143 _pip_size = theme<prefix / "pip">.size(this);
144 hi_axiom(_pip_size.width() == _pip_size.height());
145
146 _pip_circle =
147 align(narrow_cast<aarectangle>(_button_rectangle), circle{_pip_size.width() / 2}, alignment::middle_center());
148 }
149 super::set_layout(context);
150 }
151
152 void draw(widget_draw_context const& context) noexcept override
153 {
154 if (*this->mode > widget_mode::invisible and overlaps(context, this->layout)) {
155 draw_radio_button(context);
156 draw_radio_pip(context);
157 this->draw_button(context);
158 }
159 }
161private:
162 static constexpr std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
163
164 box_constraints _label_constraints;
165
166 extent2i _button_size;
167 aarectanglei _button_rectangle;
168 circle _button_circle;
169
170 animator<float> _animated_value = _animation_duration;
171 circle _pip_circle;
172
173 void draw_radio_button(widget_draw_context const& context) noexcept
174 {
175 context.draw_circle(
176 this->layout,
177 _button_circle * 1.02f,
178 theme<prefix>.background_color(this),
179 theme<prefix>.border_color(this),
180 theme<prefix>.border_width(this),
182 }
183
184 void draw_radio_pip(widget_draw_context const& context) noexcept
185 {
186 _animated_value.update(this->state != widget_state::off ? 1.0f : 0.0f, context.display_time_point);
187 if (_animated_value.is_animating()) {
188 this->request_redraw();
189 }
190
191 // draw pip
192 auto float_value = _animated_value.current_value();
193 if (float_value > 0.0f) {
194 context.draw_circle(
195 this->layout,
196 _pip_circle * 1.02f * float_value,
197 theme<prefix / "pip">.background_color(this),
198 theme<prefix / "pip">.border_color(this),
199 theme<prefix / "pip">.border_width(this));
200 }
201 }
202};
203
204}} // namespace hi::v1
Defines abstract_button_widget.
#define hi_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:335
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
geo::extent< int, 2 > extent2i
A 2D extent.
Definition extent.hpp:512
std::shared_ptr< button_delegate > make_default_radio_button_delegate(auto &&value, auto &&on_value) noexcept
Make a shared pointer to a radio-button delegate.
Definition button_delegate.hpp:205
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
Mirror the horizontal alignment.
Definition alignment.hpp:192
geometry/margins.hpp
Definition cache.hpp:11
@ inside
The border is drawn inside the edge of a quad.
@ off
The widget in the off-state.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
auto theme
A tagged global variable to a theme model for a widget's component.
Definition theme_model.hpp:433
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:166
Definition widget.hpp:26
virtual void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:270
observer< widget_state > state
The state of the widget.
Definition widget.hpp:69
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:53
2D constraints.
Definition box_constraints.hpp:22
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
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:42
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:23
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition radio_button_widget.hpp:43
radio_button_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a radio button widget with a default button delegate.
Definition radio_button_widget.hpp:84
radio_button_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a radio button widget.
Definition radio_button_widget.hpp:58
Definition abstract_button_widget.hpp:26
T align(T... args)
T move(T... args)