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
13namespace hi { inline namespace v1 {
14
52template<fixed_string Name = "">
53class toggle_widget final : public abstract_button_widget<Name / "toggle"> {
54public:
55 using super = abstract_button_widget<Name / "toggle">;
56 using delegate_type = typename super::delegate_type;
57 constexpr static auto prefix = super::prefix;
58
70 {
71 this->alignment = alignment::top_left();
72 this->set_attributes<0>(hi_forward(attributes)...);
73 }
74
85 template<different_from<std::shared_ptr<delegate_type>> Value, button_widget_attribute... Attributes>
86 toggle_widget(widget *parent, Value&& value, Attributes&&...attributes) noexcept
87 requires requires { make_default_toggle_button_delegate(hi_forward(value)); }
89 {
90 }
91
103 template<
104 different_from<std::shared_ptr<delegate_type>> Value,
105 forward_of<observer<observer_decay_t<Value>>> OnValue,
106 button_widget_attribute... Attributes>
107 toggle_widget(widget *parent, Value&& value, OnValue&& on_value, Attributes&&...attributes) noexcept
108 requires requires { make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value)); }
109 :
111 parent,
113 hi_forward(attributes)...)
114 {
115 }
116
129 template<
130 different_from<std::shared_ptr<delegate_type>> Value,
131 forward_of<observer<observer_decay_t<Value>>> OnValue,
132 forward_of<observer<observer_decay_t<Value>>> OffValue,
133 button_widget_attribute... Attributes>
134 toggle_widget(widget *parent, Value&& value, OnValue&& on_value, OffValue&& off_value, Attributes&&...attributes) noexcept
135 requires requires { make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value), hi_forward(off_value)); }
136 :
138 parent,
140 hi_forward(attributes)...)
141 {
142 }
143
145 [[nodiscard]] box_constraints update_constraints() noexcept override
146 {
147 _label_constraints = super::update_constraints();
148
149 // Make room for button and margin.
150 _button_size = theme<prefix>.size(this);
151 hilet extra_size = extent2i{theme<prefix>.spacing_horizontal(this) + _button_size.width(), 0};
152
153 auto r = max(_label_constraints + extra_size, _button_size);
154 r.margins = theme<prefix>.margin(this);
155 r.alignment = *this->alignment;
156 return r;
157 }
158
159 void set_layout(widget_layout const& context) noexcept override
160 {
161 if (compare_store(this->layout, context)) {
162 auto alignment_ = os_settings::left_to_right() ? *this->alignment : mirror(*this->alignment);
163
164 if (alignment_ == horizontal_alignment::left or alignment_ == horizontal_alignment::right) {
165 _button_rectangle = align(context.rectangle(), _button_size, alignment_);
166 } else {
168 }
169
170 hilet spacing = theme<prefix>.spacing_horizontal(this);
171 hilet cap_height = theme<prefix>.cap_height(this);
172
173 hilet label_width = context.width() - (_button_rectangle.width() + spacing);
174 if (alignment_ == horizontal_alignment::left) {
175 hilet label_left = _button_rectangle.right() + spacing;
176 hilet label_rectangle = aarectanglei{label_left, 0, label_width, context.height()};
177 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
178 box_shape{_label_constraints, label_rectangle, cap_height};
179
180 } else if (alignment_ == horizontal_alignment::right) {
181 hilet label_rectangle = aarectanglei{0, 0, label_width, context.height()};
182 this->_on_label_shape = this->_off_label_shape = this->_other_label_shape =
183 box_shape{_label_constraints, label_rectangle, cap_height};
184
185 } else {
187 }
188
189 hilet button_square = aarectangle{
190 narrow_cast<point2>(get<0>(_button_rectangle)),
191 extent2{narrow_cast<float>(_button_rectangle.height()), narrow_cast<float>(_button_rectangle.height())}};
192
193 hilet pip_size = theme<prefix / "pip">.size(this);
194 hi_axiom(pip_size.width() == pip_size.height());
195
196 _pip_circle = align(button_square, circle{pip_size.width() / 2}, alignment::middle_center());
197
198 hilet pip_to_button_margin_x2 = _button_rectangle.height() - narrow_cast<int>(_pip_circle.diameter());
199 _pip_move_range = _button_rectangle.width() - narrow_cast<int>(_pip_circle.diameter()) - pip_to_button_margin_x2;
200 }
201 super::set_layout(context);
202 }
203
204 void draw(widget_draw_context const& context) noexcept override
205 {
206 if (*this->mode > widget_mode::invisible and overlaps(context, this->layout)) {
207 draw_toggle_button(context);
208 draw_toggle_pip(context);
209 this->draw_button(context);
210 }
211 }
213private:
214 static constexpr std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
215
216 box_constraints _label_constraints;
217
218 extent2i _button_size;
219 aarectanglei _button_rectangle;
220 animator<float> _animated_value = _animation_duration;
221 circle _pip_circle;
222 int _pip_move_range;
223
224 void draw_toggle_button(widget_draw_context const& context) noexcept
225 {
226 context.draw_box(
227 this->layout,
228 _button_rectangle,
229 theme<prefix>.background_color(this),
230 theme<prefix>.border_color(this),
231 theme<prefix>.border_width(this),
233 corner_radii{_button_rectangle.height() * 0.5f});
234 }
235
236 void draw_toggle_pip(widget_draw_context const& context) noexcept
237 {
238 _animated_value.update(this->state != widget_state::off ? 1.0f : 0.0f, context.display_time_point);
239 if (_animated_value.is_animating()) {
240 this->request_redraw();
241 }
242
243 hilet positioned_pip_circle = translate3{_pip_move_range * _animated_value.current_value(), 0.0f, 0.1f} * _pip_circle;
244
245 context.draw_circle(this->layout, positioned_pip_circle * 1.02f, theme<prefix>.fill_color(this));
246 }
247};
248
249}} // 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
geo::extent< float, 2 > extent2
A 2D extent.
Definition extent.hpp:502
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:223
@ 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 GUI widget that permits the user to make a binary choice.
Definition toggle_widget.hpp:53
toggle_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a toggle widget.
Definition toggle_widget.hpp:68
toggle_widget(widget *parent, Value &&value, Attributes &&...attributes) noexcept
Construct a toggle widget with a default button delegate.
Definition toggle_widget.hpp:86
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:107
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:134
Definition abstract_button_widget.hpp:26
T align(T... args)
T move(T... args)