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 "../GFX/draw_context.hpp"
8#include "../GUI/theme.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 "../generator.hpp"
19#include "widget_constraints.hpp"
20#include "widget_layout.hpp"
21#include <memory>
22#include <vector>
23#include <string>
24#include <ranges>
25
26namespace hi::inline v1{
27 class gui_window;
28struct mouse_event;
29struct keyboard_event;
30class font_book;
31
40class widget {
41public:
45
49 widget *const parent;
50
54
58 observable<bool> enabled = true;
59
63 observable<bool> visible = true;
64
67 observable<bool> hover = false;
68
71 observable<bool> focus = false;
72
88
97
100 widget(gui_window &window, widget *parent) noexcept;
101
102 virtual ~widget();
103 widget(const widget &) = delete;
104 widget &operator=(const widget &) = delete;
105 widget(widget &&) = delete;
106 widget &operator=(widget &&) = delete;
107
108 [[nodiscard]] bool is_gui_thread() const noexcept;
109
114 hi::theme const &theme() const noexcept;
115
120 hi::font_book &font_book() const noexcept;
121
129 [[nodiscard]] virtual hitbox hitbox_test(point3 position) const noexcept
130 {
131 return {};
132 }
133
140 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
141 {
142 return hitbox_test(_layout.from_parent * position);
143 }
144
152 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
153 {
154 return std::max(sibling_hitbox, hitbox_test(_layout.from_parent * position));
155 }
156
160 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
161 {
162 hi_axiom(is_gui_thread());
163 return false;
164 }
165
177 virtual widget_constraints const &set_constraints() noexcept = 0;
178
179 widget_constraints const &constraints() const noexcept
180 {
181 return _constraints;
182 }
183
196 virtual void set_layout(widget_layout const &layout) noexcept = 0;
197
200 widget_layout const &layout() const noexcept
201 {
202 return _layout;
203 }
204
219 virtual void draw(draw_context const &context) noexcept = 0;
220
223 virtual void request_redraw() const noexcept;
224
227 void request_relayout() const noexcept;
228
231 void request_reconstrain() const noexcept;
232
235 void request_resize() const noexcept;
236
241 [[nodiscard]] virtual bool handle_event(command command) noexcept;
242
243 [[nodiscard]] virtual bool handle_event(std::vector<command> const &commands) noexcept;
244
252 virtual bool handle_command_recursive(command command, std::vector<widget const *> const &reject_list = std::vector<widget const *>{}) noexcept;
253
268 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept;
269
276 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept;
277
290 [[nodiscard]] virtual widget const *find_next_widget(
291 widget const *current_keyboard_widget,
292 keyboard_focus_group group,
293 keyboard_focus_direction direction) const noexcept;
294
295 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
296
297 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
298
301 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
302
305 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
306
313 virtual void scroll_to_show(hi::aarectangle rectangle) noexcept;
314
317 void scroll_to_show() noexcept
318 {
319 scroll_to_show(layout().rectangle());
320 }
321
325 [[nodiscard]] std::vector<widget const *> parent_chain() const noexcept;
326
327 virtual [[nodiscard]] color background_color() const noexcept;
328
329 virtual [[nodiscard]] color foreground_color() const noexcept;
330
331 virtual [[nodiscard]] color focus_color() const noexcept;
332
333 virtual [[nodiscard]] color accent_color() const noexcept;
334
335 virtual [[nodiscard]] color label_color() const noexcept;
336
337protected:
338 widget_constraints _constraints;
339 widget_layout _layout;
340
341 decltype(enabled)::token_type _enabled_cbt;
342 decltype(visible)::token_type _visible_cbt;
343
344 [[nodiscard]] virtual generator<widget *> children() const noexcept
345 {
346 co_return;
347 }
348
358 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
359};
360
361} // namespace hi::inline v1
STL namespace.
This is a RGBA floating point color.
Definition color.hpp:37
A return value for a generator-function.
Definition generator.hpp:27
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:51
Definition gui_window.hpp:40
Definition hitbox.hpp:16
Definition keyboard_event.hpp:47
Definition mouse_event.hpp:15
Definition theme.hpp:22
An observable value.
Definition observable.hpp:356
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:40
virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:140
std::vector< widget const * > parent_chain() const noexcept
Get a list of parents of a given widget.
virtual void scroll_to_show(hi::aarectangle rectangle) noexcept
Scroll to show the given rectangle on the window.
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
Check if the widget will accept keyboard focus.
Definition widget.hpp:160
virtual void set_layout(widget_layout const &layout) noexcept=0
Update the internal layout of the widget.
int logical_layer
The logical layer of the widget.
Definition widget.hpp:96
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:49
virtual widget_constraints const & set_constraints() noexcept=0
Update the constraints of the widget.
virtual void draw(draw_context const &context) noexcept=0
Draw the widget.
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget.hpp:317
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:44
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
widget(gui_window &window, widget *parent) noexcept
virtual void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:87
virtual bool handle_event(mouse_event const &event) noexcept
virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:152
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:200
virtual bool handle_event(keyboard_event const &event) noexcept
Handle keyboard event.
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.
std::string id
A name of widget, should be unique between siblings.
Definition widget.hpp:53
Definition widget_constraints.hpp:12
Definition widget_layout.hpp:17
T max(T... args)