HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget.hpp
1// Copyright Take Vos 2019-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 "../GUI/theme.hpp"
8#include "../GFX/draw_context.hpp"
9#include "../GUI/hitbox.hpp"
10#include "../GUI/keyboard_focus_direction.hpp"
11#include "../GUI/keyboard_focus_group.hpp"
12#include "../geometry/extent.hpp"
13#include "../geometry/axis_aligned_rectangle.hpp"
14#include "../geometry/transform.hpp"
15#include "../observable.hpp"
16#include "../command.hpp"
17#include "../chrono.hpp"
18#include <memory>
19#include <vector>
20#include <string>
21
22namespace tt {
23class gui_window;
24struct mouse_event;
25struct keyboard_event;
26class font_book;
27
37class widget {
38public:
42
46 widget *const parent;
47
51
56
61
75
91
100
104
105 virtual ~widget();
106 widget(const widget &) = delete;
107 widget &operator=(const widget &) = delete;
108 widget(widget &&) = delete;
109 widget &operator=(widget &&) = delete;
110
113 virtual void init() noexcept;
114
117 virtual void deinit() noexcept;
118
119 [[nodiscard]] bool is_gui_thread() const noexcept;
120
125 tt::theme const &theme() const noexcept;
126
131 tt::font_book &font_book() const noexcept;
132
140 [[nodiscard]] virtual float margin() const noexcept;
141
147 [[nodiscard]] extent2 minimum_size() const noexcept;
148
157 [[nodiscard]] extent2 preferred_size() const noexcept;
158
165 [[nodiscard]] extent2 maximum_size() const noexcept;
166
178 geo::transformer auto const &local_to_parent,
179 extent2 size,
180 aarectangle const &clipping_rectangle) noexcept;
181
182 void set_layout_parameters_from_parent(
183 aarectangle child_rectangle,
184 aarectangle parent_clipping_rectangle,
185 float draw_layer_delta) noexcept;
186
187 void set_layout_parameters_from_parent(aarectangle child_rectangle) noexcept;
188
189 [[nodiscard]] matrix3 parent_to_local() const noexcept;
190
191 [[nodiscard]] matrix3 local_to_parent() const noexcept;
192
193 [[nodiscard]] matrix3 window_to_local() const noexcept;
194
195 [[nodiscard]] matrix3 local_to_window() const noexcept;
196
197 [[nodiscard]] extent2 size() const noexcept;
198
199 [[nodiscard]] float width() const noexcept;
200
201 [[nodiscard]] float height() const noexcept;
202
207 [[nodiscard]] aarectangle rectangle() const noexcept;
208
212 [[nodiscard]] virtual float base_line() const noexcept;
213
214 [[nodiscard]] aarectangle clipping_rectangle() const noexcept;
215
223 [[nodiscard]] virtual hitbox hitbox_test(point2 position) const noexcept;
224
229 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
230 {
231 tt_axiom(is_gui_thread());
232 return false;
233 }
234
254 [[nodiscard]] virtual bool constrain(utc_nanoseconds display_time_point, bool need_reconstrain) noexcept;
255
273 [[nodiscard]] virtual void layout(utc_nanoseconds display_time_point, bool need_layout) noexcept;
274
275 virtual [[nodiscard]] color background_color() const noexcept;
276
277 virtual [[nodiscard]] color foreground_color() const noexcept;
278
279 virtual [[nodiscard]] color focus_color() const noexcept;
280
281 virtual [[nodiscard]] color accent_color() const noexcept;
282
283 virtual [[nodiscard]] color label_color() const noexcept;
284
300 virtual void draw(draw_context context, utc_nanoseconds display_time_point) noexcept;
301
302 virtual void request_redraw() const noexcept;
303
308 [[nodiscard]] virtual bool handle_event(command command) noexcept;
309
310 [[nodiscard]] virtual bool handle_event(std::vector<command> const &commands) noexcept;
311
319 [[nodiscard]] virtual bool handle_command_recursive(command command, std::vector<widget const *> const &reject_list) noexcept;
320
335 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept;
336
343 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept;
344
357 [[nodiscard]] virtual widget const *find_next_widget(
358 widget const *current_keyboard_widget,
359 keyboard_focus_group group,
360 keyboard_focus_direction direction) const noexcept;
361
362 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
363
364 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
365
368 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
369
372 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
373
378 virtual void scroll_to_show(tt::rectangle rectangle) noexcept;
379
383 [[nodiscard]] std::vector<widget const *> parent_chain() const noexcept;
384
387 void clear() noexcept;
388
392 widget &add_widget(std::unique_ptr<widget> widget) noexcept;
393
394protected:
397 std::vector<std::unique_ptr<widget>> _children;
398
401 bool _hover = false;
402
405 bool _focus = false;
406
409 matrix3 _window_to_local;
410
413 matrix3 _local_to_window;
414
417 matrix3 _parent_to_local;
418
421 matrix3 _local_to_parent;
422
425 extent2 _size;
426
429 aarectangle _clipping_rectangle;
430
436 aarectangle _visible_rectangle;
437
440 std::atomic<bool> _request_constrain = true;
441
444 std::atomic<bool> _request_layout = true;
445
446 extent2 _minimum_size;
447 extent2 _preferred_size;
448 extent2 _maximum_size;
449
450 std::shared_ptr<std::function<void()>> _redraw_callback;
451 std::shared_ptr<std::function<void()>> _relayout_callback;
452 std::shared_ptr<std::function<void()>> _reconstrain_callback;
453
456 template<typename T, typename... Args>
457 T &make_widget(Args &&...args)
458 {
459 tt_axiom(is_gui_thread());
460 auto tmp = std::make_unique<T>(window, this, std::forward<Args>(args)...);
461 tmp->init();
462 return static_cast<T &>(add_widget(std::move(tmp)));
463 }
464
474 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
475};
476
477} // namespace tt
STL namespace.
This is a RGBA floating point color.
Definition color.hpp:36
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Class which represents an rectangle.
Definition rectangle.hpp:16
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:28
Definition gui_window.hpp:39
Definition hitbox.hpp:14
Definition keyboard_event.hpp:40
Definition mouse_event.hpp:15
Definition theme.hpp:22
A value which can be observed for modifications.
Definition observable.hpp:464
font_book keeps track of multiple fonts.
Definition font_book.hpp:30
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:46
virtual bool constrain(utc_nanoseconds display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
virtual void init() noexcept
Should be called right after allocating and constructing a widget.
std::string id
A name of widget, should be unique between siblings.
Definition widget.hpp:50
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:90
observable< bool > visible
The widget is visible.
Definition widget.hpp:60
observable< bool > enabled
The widget is enabled.
Definition widget.hpp:55
extent2 maximum_size() const noexcept
Maximum size.
virtual bool handle_event(command command) noexcept
Handle command.
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:41
virtual float base_line() const noexcept
Return the base-line where the text should be located.
virtual void layout(utc_nanoseconds display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
void clear() noexcept
Remove and deallocate all child widgets.
extent2 minimum_size() const noexcept
Minimum size.
extent2 preferred_size() const noexcept
Preferred size.
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
virtual widget const * find_next_widget(widget const *current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept
Find the next widget that handles keyboard focus.
int logical_layer
The logical layer of the widget.
Definition widget.hpp:99
virtual hitbox hitbox_test(point2 position) const noexcept
Find the widget that is under the mouse cursor.
widget & add_widget(std::unique_ptr< widget > widget) noexcept
Add a widget directly to this widget.
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
Check if the widget will accept keyboard focus.
Definition widget.hpp:229
widget(gui_window &window, widget *parent) noexcept
virtual float margin() const noexcept
Get the margin around the Widget.
virtual void scroll_to_show(tt::rectangle rectangle) noexcept
Scroll to show the given rectangle on the window.
virtual bool handle_command_recursive(command command, std::vector< widget const * > const &reject_list) noexcept
Handle command recursive.
float draw_layer
The draw layer of the widget.
Definition widget.hpp:74
virtual void deinit() noexcept
Should be called right after allocating and constructing a widget.
std::vector< widget const * > parent_chain() const noexcept
Get a list of parents of a given widget.
void set_layout_parameters(geo::transformer auto const &local_to_parent, extent2 size, aarectangle const &clipping_rectangle) noexcept
Set the location and size of the widget inside the window.
virtual void draw(draw_context context, utc_nanoseconds display_time_point) noexcept
Draw the widget.
T move(T... args)