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/mouse_event.hpp"
8//#include "../GUI/keyboard_event.hpp"
9//#include "../GUI/theme.hpp"
10#include "../GFX/draw_context.hpp"
11#include "../GUI/hitbox.hpp"
12#include "../GUI/keyboard_focus_direction.hpp"
13#include "../GUI/keyboard_focus_group.hpp"
14//#include "../alignment.hpp"
15#include "../geometry/extent.hpp"
16#include "../geometry/axis_aligned_rectangle.hpp"
17#include "../geometry/transform.hpp"
18#include "../hires_utc_clock.hpp"
19#include "../observable.hpp"
20#include "../command.hpp"
21#include <memory>
22#include <vector>
23#include <string>
24
25namespace tt {
26class gui_window;
27struct mouse_event;
28struct keyboard_event;
29
39class widget {
40public:
44
48 widget *const parent;
49
53
58
63
77
93
102
106
107 virtual ~widget();
108 widget(const widget &) = delete;
109 widget &operator=(const widget &) = delete;
110 widget(widget &&) = delete;
111 widget &operator=(widget &&) = delete;
112
115 virtual void init() noexcept;
116
119 virtual void deinit() noexcept;
120
128 [[nodiscard]] virtual float margin() const noexcept;
129
135 [[nodiscard]] extent2 minimum_size() const noexcept;
136
145 [[nodiscard]] extent2 preferred_size() const noexcept;
146
153 [[nodiscard]] extent2 maximum_size() const noexcept;
154
166 geo::transformer auto const &local_to_parent,
167 extent2 size,
168 aarectangle const &clipping_rectangle) noexcept;
169
170 void set_layout_parameters_from_parent(
171 aarectangle child_rectangle,
172 aarectangle parent_clipping_rectangle,
173 float draw_layer_delta) noexcept;
174
175 void set_layout_parameters_from_parent(aarectangle child_rectangle) noexcept;
176
177 [[nodiscard]] matrix3 parent_to_local() const noexcept;
178
179 [[nodiscard]] matrix3 local_to_parent() const noexcept;
180
181 [[nodiscard]] matrix3 window_to_local() const noexcept;
182
183 [[nodiscard]] matrix3 local_to_window() const noexcept;
184
185 [[nodiscard]] extent2 size() const noexcept;
186
187 [[nodiscard]] float width() const noexcept;
188
189 [[nodiscard]] float height() const noexcept;
190
195 [[nodiscard]] aarectangle rectangle() const noexcept;
196
200 [[nodiscard]] virtual float base_line() const noexcept;
201
202 [[nodiscard]] aarectangle clipping_rectangle() const noexcept;
203
211 [[nodiscard]] virtual hitbox hitbox_test(point2 position) const noexcept;
212
217 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
218 {
219 tt_axiom(is_gui_thread());
220 return false;
221 }
222
242 [[nodiscard]] virtual bool constrain(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept;
243
261 [[nodiscard]] virtual void layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept;
262
263 virtual [[nodiscard]] color background_color() const noexcept;
264
265 virtual [[nodiscard]] color foreground_color() const noexcept;
266
267 virtual [[nodiscard]] color focus_color() const noexcept;
268
269 virtual [[nodiscard]] color accent_color() const noexcept;
270
271 virtual [[nodiscard]] color label_color() const noexcept;
272
288 virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept;
289
290 virtual void request_redraw() const noexcept;
291
296 [[nodiscard]] virtual bool handle_event(command command) noexcept;
297
298 [[nodiscard]] virtual bool handle_event(std::vector<command> const &commands) noexcept;
299
307 [[nodiscard]] virtual bool
308 handle_command_recursive(command command, std::vector<widget const *> const &reject_list) noexcept;
309
324 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept;
325
332 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept;
333
346 [[nodiscard]] virtual widget const *find_next_widget(
347 widget const *current_keyboard_widget,
348 keyboard_focus_group group,
349 keyboard_focus_direction direction) const noexcept;
350
351 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
352
353 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
354
357 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
358
361 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
362
367 virtual void scroll_to_show(tt::rectangle rectangle) noexcept;
368
372 [[nodiscard]] std::vector<widget const *>parent_chain() const noexcept;
373
376 void clear() noexcept;
377
381 widget &add_widget(std::unique_ptr<widget> widget) noexcept;
382
383protected:
386 std::vector<std::unique_ptr<widget>> _children;
387
390 bool _hover = false;
391
394 bool _focus = false;
395
398 matrix3 _window_to_local;
399
402 matrix3 _local_to_window;
403
406 matrix3 _parent_to_local;
407
410 matrix3 _local_to_parent;
411
414 extent2 _size;
415
418 aarectangle _clipping_rectangle;
419
425 aarectangle _visible_rectangle;
426
429 std::atomic<bool> _request_constrain = true;
430
433 std::atomic<bool> _request_layout = true;
434
435 extent2 _minimum_size;
436 extent2 _preferred_size;
437 extent2 _maximum_size;
438
439 std::shared_ptr<std::function<void()>> _redraw_callback;
440 std::shared_ptr<std::function<void()>> _relayout_callback;
441 std::shared_ptr<std::function<void()>> _reconstrain_callback;
442
445 template<typename T, typename... Args>
446 T &make_widget(Args &&...args)
447 {
448 tt_axiom(is_gui_thread());
449 auto tmp = std::make_unique<T>(window, this, std::forward<Args>(args)...);
450 tmp->init();
451 return static_cast<T &>(add_widget(std::move(tmp)));
452 }
453
463 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
464};
465
466} // 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:29
Definition gui_window.hpp:36
Definition hitbox.hpp:14
Definition keyboard_event.hpp:40
Definition mouse_event.hpp:15
Timestamp.
Definition hires_utc_clock.hpp:19
An observable value.
Definition observable.hpp:280
An interactive graphical object as part of the user-interface.
Definition widget.hpp:39
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:48
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:52
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:92
observable< bool > visible
The widget is visible.
Definition widget.hpp:62
observable< bool > enabled
The widget is enabled.
Definition widget.hpp:57
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:43
virtual float base_line() const noexcept
Return the base-line where the text should be located.
virtual void draw(draw_context context, hires_utc_clock::time_point display_time_point) noexcept
Draw the widget.
void clear() noexcept
Remove and deallocate all child widgets.
virtual bool constrain(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept
Update the constraints of the widget.
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:101
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:217
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:76
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 layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept
Update the internal layout of the widget.
T move(T... args)