HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
toggle_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#include "../macros.hpp"
13
14namespace hi { inline namespace v1 {
15
54public:
56 using delegate_type = typename super::delegate_type;
57
76
87 template<different_from<std::shared_ptr<delegate_type>> Value, button_widget_attribute... Attributes>
88 toggle_widget(widget *parent, Value&& value, Attributes&&...attributes) noexcept requires requires
89 {
90 make_default_toggle_button_delegate(hi_forward(value));
91 } : toggle_widget(parent, make_default_toggle_button_delegate(hi_forward(value)), hi_forward(attributes)...) {}
92
104 template<
107 button_widget_attribute... Attributes>
108 toggle_widget(widget *parent, Value&& value, OnValue&& on_value, Attributes&&...attributes) noexcept
109 requires requires
110 {
111 make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value));
112 } :
114 parent,
115 make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value)),
116 hi_forward(attributes)...)
117 {
118 }
119
132 template<
136 button_widget_attribute... Attributes>
138 widget *parent,
139 Value&& value,
140 OnValue&& on_value,
141 OffValue&& off_value,
142 Attributes&&...attributes) noexcept requires requires
143 {
144 make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value), hi_forward(off_value));
145 } :
147 parent,
148 make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value), hi_forward(off_value)),
149 hi_forward(attributes)...)
150 {
151 }
152
154 [[nodiscard]] box_constraints update_constraints() noexcept override
155 {
156 _label_constraints = super::update_constraints();
157
158 // Make room for button and margin.
159 _button_size = {theme().size() * 2.0f, theme().size()};
160 hilet extra_size = extent2{theme().margin<float>() + _button_size.width(), 0.0f};
161
162 auto r = max(_label_constraints + extra_size, _button_size);
163 r.margins = theme().margin();
164 r.alignment = *alignment;
165 return r;
166 }
167
168 void set_layout(widget_layout const& context) noexcept override
169 {
170 if (compare_store(_layout, context)) {
171 auto alignment_ = os_settings::left_to_right() ? *alignment : mirror(*alignment);
172
174 _button_rectangle = align(context.rectangle(), _button_size, alignment_);
175 } else {
176 hi_not_implemented();
177 }
178
179 hilet label_width = context.width() - (_button_rectangle.width() + theme().margin<float>());
181 hilet label_left = _button_rectangle.right() + theme().margin<float>();
182 hilet label_rectangle = aarectangle{label_left, 0.0f, label_width, context.height()};
183 _on_label_shape = _off_label_shape = _other_label_shape =
184 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
185
187 hilet label_rectangle = aarectangle{0, 0, label_width, context.height()};
188 _on_label_shape = _off_label_shape = _other_label_shape =
189 box_shape{_label_constraints, label_rectangle, theme().baseline_adjustment()};
190
191 } else {
192 hi_not_implemented();
193 }
194
195 hilet button_square =
196 aarectangle{get<0>(_button_rectangle), extent2{_button_rectangle.height(), _button_rectangle.height()}};
197
198 _pip_circle = align(button_square, circle{theme().size() * 0.5f - 3.0f}, alignment::middle_center());
199
200 hilet pip_to_button_margin_x2 = _button_rectangle.height() - _pip_circle.diameter();
201 _pip_move_range = _button_rectangle.width() - _pip_circle.diameter() - pip_to_button_margin_x2;
202 }
203 super::set_layout(context);
204 }
205
206 void draw(draw_context const& context) noexcept override
207 {
208 if (*mode > widget_mode::invisible and overlaps(context, layout())) {
209 draw_toggle_button(context);
210 draw_toggle_pip(context);
211 draw_button(context);
212 }
213 }
214
216private:
217 constexpr static std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
218
219 box_constraints _label_constraints;
220
221 extent2 _button_size;
222 aarectangle _button_rectangle;
223 animator<float> _animated_value = _animation_duration;
224 circle _pip_circle;
225 float _pip_move_range;
226
227 void draw_toggle_button(draw_context const& context) noexcept
228 {
229 context.draw_box(
230 layout(),
231 _button_rectangle,
232 background_color(),
233 focus_color(),
234 theme().border_width(),
236 corner_radii{_button_rectangle.height() * 0.5f});
237 }
238
239 void draw_toggle_pip(draw_context const& context) noexcept
240 {
241 _animated_value.update(state() == button_state::on ? 1.0f : 0.0f, context.display_time_point);
242 if (_animated_value.is_animating()) {
244 }
245
246 hilet positioned_pip_circle = translate3{_pip_move_range * _animated_value.current_value(), 0.0f, 0.1f} * _pip_circle;
247
248 hilet forground_color_ = state() == button_state::on ? accent_color() : foreground_color();
249 context.draw_circle(layout(), positioned_pip_circle * 1.02f, forground_color_);
250 }
251};
252
253}} // namespace hi::v1
Defines abstract_button_widget.
@ right
Align the text to the right side.
@ left
Align the text to the left side.
std::shared_ptr< button_delegate > make_default_toggle_button_delegate(auto &&value, auto &&...args) noexcept
Make a shared pointer to a toggle-button delegate.
Definition button_delegate.hpp:245
@ on
The 'on' state of a button.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
@ inside
The border is drawn inside the edge of a quad.
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
constexpr horizontal_alignment mirror(horizontal_alignment const &rhs) noexcept
Mirror the horizontal alignment.
Definition alignment.hpp:203
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Horizontal/Vertical alignment combination.
Definition alignment.hpp:242
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:104
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
2D constraints.
Definition box_constraints.hpp:25
Base class for implementing button widgets.
Definition abstract_button_widget.hpp:33
std::shared_ptr< delegate_type > delegate
The delegate that controls the button widget.
Definition abstract_button_widget.hpp:40
button_state state() const noexcept
Get the current state of the button.
Definition abstract_button_widget.hpp:88
observer< alignment > alignment
The alignment of the button and on/off/other label.
Definition abstract_button_widget.hpp:56
A button delegate controls the state of a button widget.
Definition button_delegate.hpp:45
A GUI widget that permits the user to make a binary choice.
Definition toggle_widget.hpp:53
toggle_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:108
toggle_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a toggle widget.
Definition toggle_widget.hpp:67
toggle_widget(widget *parent, Value &&value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:88
toggle_widget(widget *parent, Value &&value, OnValue &&on_value, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:137
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:189
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
Definition abstract_button_widget.hpp:27
T align(T... args)
T max(T... args)
T move(T... args)