12#include "../macros.hpp"
14namespace hi {
inline namespace v1 {
55 using super = abstract_button_widget;
56 using delegate_type =
typename super::delegate_type;
74 set_attributes<0>(hi_forward(attributes)...);
87 template<different_from<std::shared_ptr<delegate_type>> Value, button_w
idget_attribute... Attributes>
105 different_from<std::shared_ptr<delegate_type>> Value,
106 forward_of<observer<observer_decay_t<Value>>> OnValue,
107 button_widget_attribute... Attributes>
116 hi_forward(attributes)...)
133 different_from<std::shared_ptr<delegate_type>> Value,
134 forward_of<observer<observer_decay_t<Value>>> OnValue,
135 forward_of<observer<observer_decay_t<Value>>> OffValue,
136 button_widget_attribute... Attributes>
141 OffValue&& off_value,
142 Attributes&&...attributes)
noexcept requires requires
149 hi_forward(attributes)...)
159 _button_size = {theme().size() * 2.0f, theme().size()};
160 hilet extra_size = extent2{theme().margin<
float>() + _button_size.
width(), 0.0f};
162 auto r =
max(_label_constraints + extra_size, _button_size);
163 r.margins = theme().margin();
168 void set_layout(widget_layout
const& context)
noexcept override
174 _button_rectangle =
align(context.rectangle(), _button_size, alignment_);
176 hi_not_implemented();
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()};
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()};
192 hi_not_implemented();
195 hilet button_square =
196 aarectangle{get<0>(_button_rectangle), extent2{_button_rectangle.height(), _button_rectangle.height()}};
198 _pip_circle =
align(button_square, circle{theme().size() * 0.5f - 3.0f}, alignment::middle_center());
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;
203 super::set_layout(context);
206 void draw(draw_context
const& context)
noexcept override
209 draw_toggle_button(context);
210 draw_toggle_pip(context);
211 draw_button(context);
217 constexpr static std::chrono::nanoseconds _animation_duration = std::chrono::milliseconds(150);
219 box_constraints _label_constraints;
221 extent2 _button_size;
222 aarectangle _button_rectangle;
223 animator<float> _animated_value = _animation_duration;
225 float _pip_move_range;
227 void draw_toggle_button(draw_context
const& context)
noexcept
234 theme().border_width(),
236 corner_radii{_button_rectangle.height() * 0.5f});
239 void draw_toggle_pip(draw_context
const& context)
noexcept
242 if (_animated_value.is_animating()) {
246 hilet positioned_pip_circle = translate3{_pip_move_range * _animated_value.current_value(), 0.0f, 0.1f} * _pip_circle;
249 context.draw_circle(
layout(), positioned_pip_circle * 1.02f, forground_color_);
Defines abstract_button_widget.
@ right
Align the text to the right side.
Definition alignment.hpp:137
@ left
Align the text to the left side.
Definition alignment.hpp:119
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.
Definition button_delegate.hpp:30
@ invisible
The widget is invisible.
Definition widget_mode.hpp:36
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context.hpp:34
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 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
virtual box_constraints update_constraints() noexcept=0
Update the constraints of the widget.
2D constraints.
Definition box_constraints.hpp:25
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 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
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:158
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
Definition abstract_button_widget.hpp:27