HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
radio_button_widget.hpp
1// Copyright Take Vos 2020-2021.
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_radio_button_widget.hpp"
8#include "../GUI/draw_context.hpp"
9#include "../observable.hpp"
10#include "../label.hpp"
11#include <memory>
12#include <string>
13#include <array>
14#include <optional>
15#include <future>
16
17namespace tt {
18
19template<typename T>
21public:
23 using value_type = typename super::value_type;
24
26
27 template<typename Value, typename Label>
31 value_type true_value,
32 Value &&value,
33 Label &&label) noexcept :
34 abstract_radio_button_widget<T>(window, parent, std::move(true_value), std::forward<Value>(value)),
35 label(std::forward<Label>(label))
36 {
37 }
38
39 template<typename Value>
43 value_type true_value,
44 Value &&value) noexcept :
45 radio_button_widget(window, parent, std::move(true_value), std::forward<Value>(value), observable<tt::label>{})
46 {
47 }
48
51 {
52 }
53
55
56 void init() noexcept override
57 {
58 label_callback = label.subscribe([this](auto...) {
59 this->_request_reconstrain = true;
60 });
61 }
62
63 [[nodiscard]] bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
64 {
65 tt_axiom(gui_system_mutex.recurse_lock_count());
66
67 if (super::update_constraints(display_time_point, need_reconstrain)) {
68 _label_stencil = stencil::make_unique(alignment::top_left, *label, theme::global->labelStyle);
69
70 ttlet minimum_height = std::max(_label_stencil->preferred_extent().height(), theme::global->smallSize);
71 ttlet minimum_width = theme::global->smallSize + theme::global->margin + _label_stencil->preferred_extent().width();
72
73 super::_preferred_size = interval_extent2::make_minimum(minimum_width, minimum_height);
74 return true;
75 } else {
76 return false;
77 }
78 }
79
80 [[nodiscard]] void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
81 {
82 tt_axiom(gui_system_mutex.recurse_lock_count());
83
84 need_layout |= std::exchange(this->_request_relayout, false);
85 if (need_layout) {
86 _outline_rectangle = aarectangle{
87 0.0f, std::round(this->base_line() - theme::global->smallSize * 0.5f), theme::global->smallSize, theme::global->smallSize};
88
89 ttlet labelX = _outline_rectangle.right() + theme::global->margin;
90 _label_rectangle = aarectangle{labelX, 0.0f, this->rectangle().width() - labelX, this->rectangle().height()};
91 _label_stencil->set_layout_parameters(_label_rectangle, this->base_line());
92
93 _pip_rectangle = shrink(_outline_rectangle, 2.5f);
94 }
95 super::update_layout(display_time_point, need_layout);
96 }
97
98 void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
99 {
100 tt_axiom(gui_system_mutex.recurse_lock_count());
101
102 if (overlaps(context, this->_clipping_rectangle)) {
103 draw_outline(context);
104 draw_pip(context);
105 draw_label(context);
106 }
107 super::draw(std::move(context), display_time_point);
108 }
109
110private:
111 aarectangle _outline_rectangle;
112 aarectangle _pip_rectangle;
113 aarectangle _label_rectangle;
114 std::unique_ptr<stencil> _label_stencil;
115
116 typename decltype(label)::callback_ptr_type label_callback;
117
118 void draw_outline(draw_context context) noexcept
119 {
120 tt_axiom(gui_system_mutex.recurse_lock_count());
121
122 context.draw_box_with_border_inside(
123 _outline_rectangle, this->background_color(), this->focus_color(), corner_shapes{_outline_rectangle.height() * 0.5f});
124 }
125
126 void draw_pip(draw_context context) noexcept
127 {
128 tt_axiom(gui_system_mutex.recurse_lock_count());
129
130 // draw pip
131 if (this->value == this->true_value) {
132 context.draw_box(
133 _pip_rectangle, this->accent_color(), corner_shapes{_pip_rectangle.height() * 0.5f});
134 }
135 }
136
137 void draw_label(draw_context context) noexcept
138 {
139 tt_axiom(gui_system_mutex.recurse_lock_count());
140 _label_stencil->draw(context, this->label_color());
141 }
142};
143
144} // namespace tt
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Definition corner_shapes.hpp:9
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
Definition gui_window.hpp:37
A localized text + icon label.
Definition label.hpp:76
Definition observable.hpp:20
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 radio button widget.
Definition abstract_radio_button_widget.hpp:15
Definition radio_button_widget.hpp:20
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
Definition radio_button_widget.hpp:63
void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept override
Draw the widget.
Definition radio_button_widget.hpp:98
void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition radio_button_widget.hpp:80
void init() noexcept override
Should be called right after allocating and constructing a widget.
Definition radio_button_widget.hpp:56
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)