HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
checkbox_widget.hpp
1// Copyright Take Vos 2020.
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
5#pragma once
6
7#include "abstract_toggle_button_widget.hpp"
8#include "../stencils/label_stencil.hpp"
9#include "../GUI/draw_context.hpp"
10#include "../text/font_book.hpp"
11#include "../observable.hpp"
12#include "../label.hpp"
13#include <memory>
14#include <string>
15#include <array>
16#include <optional>
17#include <future>
18
19namespace tt {
20
25template<typename T>
27public:
29 using value_type = typename super::value_type;
30
31 observable<label> true_label;
32 observable<label> false_label;
33 observable<label> other_label;
34
35 template<typename Value = observable<value_type>>
39 value_type true_value,
40 value_type false_value,
41 Value &&value = {}) noexcept :
43 window,
44 parent,
45 std::move(true_value),
46 std::move(false_value),
47 std::forward<Value>(value))
48 {
49 }
50
51 void init() noexcept override
52 {
53 _true_label_callback = true_label.subscribe([this](auto...) {
54 this->_request_reconstrain = true;
55 });
56 _false_label_callback = false_label.subscribe([this](auto...) {
57 this->_request_reconstrain = true;
58 });
59 _other_label_callback = other_label.subscribe([this](auto...) {
60 this->_request_reconstrain = true;
61 });
62 }
63
64 [[nodiscard]] bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
65 {
66 tt_axiom(gui_system_mutex.recurse_lock_count());
67
68 if (super::update_constraints(display_time_point, need_reconstrain)) {
69 _true_label_stencil = stencil::make_unique(alignment::top_left, *true_label, theme::global->labelStyle);
70 _false_label_stencil = stencil::make_unique(alignment::top_left, *false_label, theme::global->labelStyle);
71 _other_label_stencil = stencil::make_unique(alignment::top_left, *other_label, theme::global->labelStyle);
72
73 ttlet minimum_height = std::max(
74 {_true_label_stencil->preferred_extent().height(),
75 _false_label_stencil->preferred_extent().height(),
76 _other_label_stencil->preferred_extent().height(),
77 theme::global->smallSize});
78
79 ttlet minimum_width_of_labels = std::max(
80 {_true_label_stencil->preferred_extent().width(),
81 _false_label_stencil->preferred_extent().width(),
82 _other_label_stencil->preferred_extent().width()});
83 ttlet minimum_width = theme::global->smallSize + theme::global->margin + minimum_width_of_labels;
84
85 this->_preferred_size = interval_extent2::make_minimum(minimum_width, minimum_height);
86
87 return true;
88 } else {
89 return false;
90 }
91 }
92
93 [[nodiscard]] void update_layout(hires_utc_clock::time_point displayTimePoint, bool need_layout) noexcept override
94 {
95 tt_axiom(gui_system_mutex.recurse_lock_count());
96
97 need_layout |= std::exchange(this->_request_relayout, false);
98 if (need_layout) {
99 _checkbox_rectangle = aarectangle{0.0f, std::round(this->base_line() - theme::global->smallSize * 0.5f), theme::global->smallSize, theme::global->smallSize};
100
101 ttlet label_x = _checkbox_rectangle.right() + theme::global->margin;
102 _label_rectangle = aarectangle{label_x, 0.0f, this->rectangle().width() - label_x, this->rectangle().height()};
103 _true_label_stencil->set_layout_parameters(_label_rectangle, this->base_line());
104 _false_label_stencil->set_layout_parameters(_label_rectangle, this->base_line());
105 _other_label_stencil->set_layout_parameters(_label_rectangle, this->base_line());
106
107 _check_glyph = to_font_glyph_ids(elusive_icon::Ok);
108 ttlet check_glyph_bb = pipeline_SDF::device_shared::getBoundingBox(_check_glyph);
109 _check_glyph_rectangle =
110 align(_checkbox_rectangle, scale(check_glyph_bb, theme::global->small_icon_size), alignment::middle_center);
111
112 _minus_glyph = to_font_glyph_ids(elusive_icon::Minus);
113 ttlet minus_glyph_bb = pipeline_SDF::device_shared::getBoundingBox(_minus_glyph);
114 _minus_glyph_rectangle =
115 align(_checkbox_rectangle, scale(minus_glyph_bb, theme::global->small_icon_size), alignment::middle_center);
116 }
117
118 super::update_layout(displayTimePoint, need_layout);
119 }
120
121 void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
122 {
123 tt_axiom(gui_system_mutex.recurse_lock_count());
124
125 if (overlaps(context, this->_clipping_rectangle)) {
126 draw_check_box(context);
127 draw_check_mark(context);
128 draw_label(context);
129 }
130
131 super::draw(std::move(context), display_time_point);
132 }
133
134private:
135 typename decltype(true_label)::callback_ptr_type _true_label_callback;
136 typename decltype(false_label)::callback_ptr_type _false_label_callback;
137 typename decltype(other_label)::callback_ptr_type _other_label_callback;
138
139 std::unique_ptr<label_stencil> _true_label_stencil;
140 std::unique_ptr<label_stencil> _false_label_stencil;
141 std::unique_ptr<label_stencil> _other_label_stencil;
142
143 font_glyph_ids _check_glyph;
144 aarectangle _check_glyph_rectangle;
145
146 font_glyph_ids _minus_glyph;
147 aarectangle _minus_glyph_rectangle;
148
149 aarectangle _checkbox_rectangle;
150
151 aarectangle _label_rectangle;
152
153 void draw_check_box(draw_context const &context) noexcept
154 {
155 tt_axiom(gui_system_mutex.recurse_lock_count());
156
157 context.draw_box_with_border_inside(_checkbox_rectangle, this->background_color(), this->focus_color());
158 }
159
160 void draw_check_mark(draw_context context) noexcept
161 {
162 tt_axiom(gui_system_mutex.recurse_lock_count());
163
164 // Checkmark or tristate.
165 if (this->value == this->true_value) {
166 context.draw_glyph(_check_glyph, translate_z(0.1f) * _check_glyph_rectangle, this->accent_color());
167 } else if (this->value == this->false_value) {
168 ;
169 } else {
170 context.draw_glyph(_minus_glyph, translate_z(0.1f) * _minus_glyph_rectangle, this->accent_color());
171 }
172 }
173
174 void draw_label(draw_context context) noexcept
175 {
176 tt_axiom(gui_system_mutex.recurse_lock_count());
177
178 ttlet &labelCell = this->value == this->true_value ?
179 _true_label_stencil :
180 this->value == this->false_value ? _false_label_stencil : _other_label_stencil;
181
182 labelCell->draw(context, this->label_color());
183 }
184};
185
186} // namespace tt
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
Definition gui_window.hpp:37
static aarectangle getBoundingBox(font_glyph_ids const &glyphs) noexcept
Get the bounding box, including draw border of a glyph.
Definition observable.hpp:20
Definition font_glyph_ids.hpp:78
int recurse_lock_count() const noexcept
This function should be used in tt_axiom() to check if the lock is held by current thread.
Definition unfair_recursive_mutex.hpp:60
An abstract toggle button widget.
Definition abstract_toggle_button_widget.hpp:15
A checkbox widget.
Definition checkbox_widget.hpp:26
void update_layout(hires_utc_clock::time_point displayTimePoint, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition checkbox_widget.hpp:93
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition checkbox_widget.hpp:121
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition checkbox_widget.hpp:64
void init() noexcept override
Should be called right after allocating and constructing a widget.
Definition checkbox_widget.hpp:51
virtual void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
aarectangle rectangle() const noexcept
Get the rectangle in local coordinates.
Definition widget.hpp:342
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:101
virtual float base_line() const noexcept
Return the base-line where the text should be located.
Definition widget.hpp:351
virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept
Draw the widget.
Definition widget.hpp:462
virtual bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
abstract_container_widget const & parent() const noexcept
Get a reference to the parent.
T max(T... args)
T move(T... args)
T round(T... args)