HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
checkbox_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 "../log.hpp"
13
14namespace hi { inline namespace v1 {
15
42template<fixed_string Name = "">
43class checkbox_widget final : public abstract_button_widget<Name / "checkbox"> {
44public:
45 using super = abstract_button_widget<Name / "checkbox">;
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 different_from<std::shared_ptr<delegate_type>> auto&& value,
81 button_widget_attribute auto&&...attributes) noexcept
82 requires requires { make_default_toggle_button_delegate(hi_forward(value)); }
84 {
85 }
86
98 template<
99 different_from<std::shared_ptr<delegate_type>> Value,
100 forward_of<observer<observer_decay_t<Value>>> OnValue,
101 button_widget_attribute... Attributes>
102 checkbox_widget(widget *parent, Value&& value, OnValue&& on_value, Attributes&&...attributes) noexcept
103 requires requires { make_default_toggle_button_delegate(hi_forward(value), hi_forward(on_value)); }
104 :
106 parent,
108 hi_forward(attributes)...)
109 {
110 }
111
124 template<
125 different_from<std::shared_ptr<delegate_type>> Value,
126 forward_of<observer<observer_decay_t<Value>>> OnValue,
127 forward_of<observer<observer_decay_t<Value>>> OffValue,
128 button_widget_attribute... Attributes>
130 widget *parent,
131 Value&& value,
132 OnValue&& on_value,
133 OffValue&& off_value,
134 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 _button_size = theme<prefix>.size(this);
150 hilet extra_size = extent2i{theme<prefix>.horizontal_spacing(this) + _button_size.width(), 0};
151
152 auto constraints = max(_label_constraints + extra_size, _button_size);
153 constraints.margins = theme<prefix>.margin(this);
154 constraints.alignment = *this->alignment;
155 return constraints;
156 }
157
158 void set_layout(widget_layout const& context) noexcept override
159 {
160 if (compare_store(this->layout, context)) {
161 auto alignment_ = os_settings::left_to_right() ? *this->alignment : mirror(*this->alignment);
162
163 if (alignment_ == horizontal_alignment::left or alignment_ == horizontal_alignment::right) {
164 _button_rectangle = align(context.rectangle(), _button_size, alignment_);
165 } else {
167 }
168
169 hilet inner_margin = theme<prefix>.horizontal_spacing(this);
170 hilet baseline_offset = theme<prefix>.cap_height(this);
171 hilet icon_size = theme<prefix / "icon">.size(this);
172
173 hilet label_width = context.width() - (_button_rectangle.width() + inner_margin);
174 if (alignment_ == horizontal_alignment::left) {
175 hilet label_left = _button_rectangle.right() + inner_margin;
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, baseline_offset);
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, baseline_offset);
184 } else {
186 }
187
188 _check_glyph = find_glyph(elusive_icon::Ok);
189 hilet check_glyph_bb = narrow_cast<aarectanglei>(_check_glyph.get_bounding_rectangle() * icon_size);
190 _check_glyph_rectangle = align(_button_rectangle, check_glyph_bb, alignment::middle_center());
191
192 _minus_glyph = find_glyph(elusive_icon::Minus);
193 hilet minus_glyph_bb = narrow_cast<aarectanglei>(_minus_glyph.get_bounding_rectangle() * icon_size);
194 _minus_glyph_rectangle = align(_button_rectangle, minus_glyph_bb, alignment::middle_center());
195 }
196 super::set_layout(context);
197 }
198
199 void draw(widget_draw_context const& context) noexcept override
200 {
201 if (*this->mode > widget_mode::invisible and overlaps(context, this->layout)) {
202 draw_check_box(context);
203 draw_check_mark(context);
204 this->draw_button(context);
205 }
206 }
208private:
209 box_constraints _label_constraints;
210
211 extent2i _button_size;
212 aarectanglei _button_rectangle;
213 font_book::font_glyph_type _check_glyph;
214 aarectanglei _check_glyph_rectangle;
215 font_book::font_glyph_type _minus_glyph;
216 aarectanglei _minus_glyph_rectangle;
217
218 void draw_check_box(widget_draw_context const& context) noexcept
219 {
220 context.draw_box(
221 this->layout,
222 _button_rectangle,
223 theme<prefix>.background_color(this),
224 theme<prefix>.border_color(this),
225 theme<prefix>.border_width(this),
227 }
228
229 void draw_check_mark(widget_draw_context const& context) noexcept
230 {
231 if (this->state == widget_state::on) {
232 // Checkmark
233 context.draw_glyph(
234 this->layout,
235 translate_z(0.1f) * narrow_cast<aarectangle>(_check_glyph_rectangle),
236 *_check_glyph.font,
237 _check_glyph.glyph,
238 theme<prefix>.fill_color(this));
239
240 } else if (this->state == widget_state::off) {
241 ;
242
243 } else {
244 // Tri-state
245 context.draw_glyph(
246 this->layout,
247 translate_z(0.1f) * narrow_cast<aarectangle>(_minus_glyph_rectangle),
248 *_minus_glyph.font,
249 _minus_glyph.glyph,
250 theme<prefix>.fill_color(this));
251 }
252 }
253};
254
255}} // 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 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_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
auto find_glyph(font const &font, grapheme grapheme) noexcept
Find a glyph using the given code-point.
Definition font_book.hpp:234
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.
@ on
The widget is in the on-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
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 checkbox_widget.hpp:43
checkbox_widget(widget *parent, std::shared_ptr< delegate_type > delegate, button_widget_attribute auto &&...attributes) noexcept
Construct a checkbox widget.
Definition checkbox_widget.hpp:58
checkbox_widget(widget *parent, Value &&value, OnValue &&on_value, Attributes &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:102
checkbox_widget(widget *parent, Value &&value, OnValue &&on_value, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:129
checkbox_widget(widget *parent, different_from< std::shared_ptr< delegate_type > > auto &&value, button_widget_attribute auto &&...attributes) noexcept
Construct a checkbox widget with a default button delegate.
Definition checkbox_widget.hpp:78
Definition abstract_button_widget.hpp:26
T align(T... args)
T move(T... args)