HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
selection_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-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
11#include "widget.hpp"
12#include "label_widget.hpp"
13#include "overlay_widget.hpp"
14#include "scroll_widget.hpp"
15#include "row_column_widget.hpp"
18#include "../observer.hpp"
19#include <memory>
20#include <string>
21#include <array>
22#include <optional>
23#include <future>
24
25namespace hi { inline namespace v1 {
26
27template<typename Context>
29
44class selection_widget final : public widget {
45public:
46 using super = widget;
47 using delegate_type = selection_delegate;
48
50
53 observer<label> off_label;
54
73 observer<alignment> alignment = hi::alignment::top_right();
74
77 observer<semantic_text_style> text_style = semantic_text_style::label;
78
80
88
100 gui_window& window,
101 widget *parent,
103 selection_widget_attribute auto&& first_attribute,
104 selection_widget_attribute auto&&...attributes) noexcept :
106 {
107 set_attributes(hi_forward(first_attribute), hi_forward(attributes)...);
108 }
109
123 template<
124 different_from<std::shared_ptr<delegate_type>> Value,
125 forward_of<observer<std::vector<std::pair<observer_decay_t<Value>, label>>>> OptionList,
126 selection_widget_attribute... Attributes>
128 gui_window& window,
129 widget *parent,
130 Value&& value,
131 OptionList&& option_list,
132 Attributes&&...attributes) noexcept requires requires
133 {
135 } :
137 window,
138 parent,
140 hi_forward(attributes)...)
141 {
142 }
143
159 template<
160 different_from<std::shared_ptr<delegate_type>> Value,
161 forward_of<observer<std::vector<std::pair<observer_decay_t<Value>, label>>>> OptionList,
162 forward_of<observer<observer_decay_t<Value>>> OffValue,
163 selection_widget_attribute... Attributes>
165 gui_window& window,
166 widget *parent,
167 Value&& value,
168 OptionList&& option_list,
169 OffValue&& off_value,
170 Attributes&&...attributes) noexcept requires requires
171 {
172 make_default_selection_delegate(hi_forward(value), hi_forward(option_list), hi_forward(off_value));
173 } :
175 window,
176 parent,
177 make_default_selection_delegate(hi_forward(value), hi_forward(option_list), hi_forward(off_value)),
178 hi_forward(attributes)...)
179 {
180 }
181
183 [[nodiscard]] generator<widget *> children() const noexcept override;
184 widget_constraints const& set_constraints() noexcept override;
185 void set_layout(widget_layout const& layout) noexcept override;
186 void draw(draw_context const& context) noexcept override;
187 bool handle_event(gui_event const& event) noexcept override;
188 [[nodiscard]] hitbox hitbox_test(point3 position) const noexcept override;
189 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
190 [[nodiscard]] color focus_color() const noexcept override;
192private:
193 notifier<>::callback_token _delegate_cbt;
194 std::atomic<bool> _notification_from_delegate = true;
195
196 std::unique_ptr<label_widget> _current_label_widget;
197 std::unique_ptr<label_widget> _off_label_widget;
198
199 aarectangle _option_rectangle;
200 aarectangle _left_box_rectangle;
201
202 glyph_ids _chevrons_glyph;
203 aarectangle _chevrons_rectangle;
204
205 bool _selecting = false;
206 bool _has_options = false;
207
208 aarectangle _overlay_rectangle;
209 std::unique_ptr<overlay_widget> _overlay_widget;
210 vertical_scroll_widget<> *_scroll_widget = nullptr;
211 column_widget *_column_widget = nullptr;
212
213 decltype(off_label)::callback_token _off_label_cbt;
214 std::vector<menu_button_widget *> _menu_button_widgets;
215 std::vector<notifier<>::callback_token> _menu_button_tokens;
216
217 void set_attributes() noexcept {}
218 void set_attributes(label_widget_attribute auto&& first, label_widget_attribute auto&&...rest) noexcept
219 {
220 if constexpr (forward_of<decltype(first), observer<hi::label>>) {
221 off_label = hi_forward(first);
222 } else if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
223 alignment = hi_forward(first);
224 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
225 text_style = hi_forward(first);
226 } else {
227 hi_static_no_default();
228 }
229
230 set_attributes(hi_forward(rest)...);
231 }
232
233 [[nodiscard]] menu_button_widget const *get_first_menu_button() const noexcept;
234 [[nodiscard]] menu_button_widget const *get_selected_menu_button() const noexcept;
235 void start_selecting() noexcept;
236 void stop_selecting() noexcept;
237 void repopulate_options() noexcept;
238 void draw_outline(draw_context const& context) noexcept;
239 void draw_left_box(draw_context const& context) noexcept;
240 void draw_chevrons(draw_context const& context) noexcept;
241};
242
243}} // namespace hi::v1
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
Defines delegate_delegate and some default selection delegates.
Defines scroll_widget.
Defines row_column_widget.
Defines widget.
Defines label_widget.
Defines overlay_widget.
Defines menu_button_widget.
std::shared_ptr< selection_delegate > make_default_selection_delegate(auto &&value, auto &&options, auto &&...off_value) noexcept
Create a shared pointer to a default selection delegate.
Definition selection_delegate.hpp:153
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
A user interface event.
Definition gui_event.hpp:66
The GUI widget displays and lays out text together with an icon.
Definition label_widget.hpp:41
A button that is part of a menu.
Definition menu_button_widget.hpp:30
A GUI widget which may exist anywhere on a window overlaid above any other widget.
Definition overlay_widget.hpp:38
A row/column widget lays out child widgets along a row or column.
Definition row_column_widget.hpp:39
The scroll widget allows a content widget to be shown in less space than is required.
Definition scroll_widget.hpp:48
A delegate that controls the state of a selection_widget.
Definition selection_delegate.hpp:23
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:44
selection_widget(gui_window &window, widget *parent, std::shared_ptr< delegate_type > delegate, selection_widget_attribute auto &&first_attribute, selection_widget_attribute auto &&...attributes) noexcept
Construct a selection widget with a delegate.
Definition selection_widget.hpp:99
selection_widget(gui_window &window, widget *parent, Value &&value, OptionList &&option_list, Attributes &&...attributes) noexcept
Construct a selection widget which will monitor an option list and a value.
Definition selection_widget.hpp:127
selection_widget(gui_window &window, widget *parent, std::shared_ptr< delegate_type > delegate) noexcept
Construct a selection widget with a delegate.
selection_widget(gui_window &window, widget *parent, Value &&value, OptionList &&option_list, OffValue &&off_value, Attributes &&...attributes) noexcept
Construct a selection widget which will monitor an option list and a value.
Definition selection_widget.hpp:164
observer< label > off_label
The label to show when nothing is selected.
Definition selection_widget.hpp:53
observer< semantic_text_style > text_style
The text style to display the label's text in and color of the label's (non-color) icon.
Definition selection_widget.hpp:77
observer< alignment > alignment
How the label and icon are aligned.
Definition selection_widget.hpp:73
An interactive graphical object as part of the user-interface.
Definition widget.hpp:44
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:198
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:53
widget(gui_window &window, widget *parent) noexcept
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:48
The constraints of a widget.
Definition widget_constraints.hpp:26
The layout of a widget.
Definition widget_layout.hpp:37
Definition label_widget.hpp:25
Definition selection_widget.hpp:28
T move(T... args)