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 "../GUI/gui_event.hpp"
13#include "../geometry/extent.hpp"
14#include "../geometry/axis_aligned_rectangle.hpp"
15#include "../geometry/transform.hpp"
16#include "../observable.hpp"
17#include "../chrono.hpp"
18#include "../generator.hpp"
19#include "widget_constraints.hpp"
20#include "widget_layout.hpp"
21#include "widget_mode.hpp"
22#include <memory>
23#include <vector>
24#include <string>
25#include <ranges>
26
27namespace hi::inline v1 {
28class gui_window;
29class font_book;
30
39class widget {
40public:
44
48 widget *const parent;
49
53
57 observable<widget_mode> mode = widget_mode::enabled;
58
61 observable<bool> hover = false;
62
65 observable<bool> focus = false;
66
82
91
94 widget(gui_window& window, widget *parent) noexcept;
95
96 virtual ~widget();
97 widget(const widget&) = delete;
98 widget& operator=(const widget&) = delete;
99 widget(widget&&) = delete;
100 widget& operator=(widget&&) = delete;
101
102 [[nodiscard]] bool is_gui_thread() const noexcept;
103
108 hi::theme const& theme() const noexcept;
109
114 hi::font_book& font_book() const noexcept;
115
123 [[nodiscard]] virtual hitbox hitbox_test(point3 position) const noexcept
124 {
125 return {};
126 }
127
134 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
135 {
136 return hitbox_test(_layout.from_parent * position);
137 }
138
146 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
147 {
148 return std::max(sibling_hitbox, hitbox_test(_layout.from_parent * position));
149 }
150
154 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
155 {
156 hi_axiom(is_gui_thread());
157 return false;
158 }
159
171 virtual widget_constraints const& set_constraints() noexcept = 0;
172
173 widget_constraints const& constraints() const noexcept
174 {
175 return _constraints;
176 }
177
190 virtual void set_layout(widget_layout const& layout) noexcept = 0;
191
194 widget_layout const& layout() const noexcept
195 {
196 return _layout;
197 }
198
213 virtual void draw(draw_context const& context) noexcept = 0;
214
217 virtual void request_redraw() const noexcept;
218
221 void request_relayout() const noexcept;
222
225 void request_reconstrain() const noexcept;
226
229 void request_resize() const noexcept;
230
235 virtual bool handle_event(gui_event const& event) noexcept;
236
244 virtual bool handle_event_recursive(
245 gui_event const& event,
246 std::vector<widget const *> const& reject_list = std::vector<widget const *>{}) noexcept;
247
260 [[nodiscard]] virtual widget const *find_next_widget(
261 widget const *current_keyboard_widget,
262 keyboard_focus_group group,
263 keyboard_focus_direction direction) const noexcept;
264
265 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
266
267 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
268
271 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
272
275 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
276
283 virtual void scroll_to_show(hi::aarectangle rectangle) noexcept;
284
287 void scroll_to_show() noexcept
288 {
289 scroll_to_show(layout().rectangle());
290 }
291
295 [[nodiscard]] std::vector<widget const *> parent_chain() const noexcept;
296
297 virtual [[nodiscard]] color background_color() const noexcept;
298
299 virtual [[nodiscard]] color foreground_color() const noexcept;
300
301 virtual [[nodiscard]] color focus_color() const noexcept;
302
303 virtual [[nodiscard]] color accent_color() const noexcept;
304
305 virtual [[nodiscard]] color label_color() const noexcept;
306
307protected:
308 widget_constraints _constraints;
309 widget_layout _layout;
310
311 decltype(mode)::token_type _mode_cbt;
312
313 [[nodiscard]] virtual generator<widget *> children() const noexcept
314 {
315 co_return;
316 }
317
327 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
328};
329
330} // namespace hi::inline v1
STL namespace.
This is a RGBA floating point color.
Definition color.hpp:39
A return value for a generator-function.
Definition generator.hpp:28
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:52
A user interface event.
Definition gui_event.hpp:58
Definition gui_window.hpp:39
Definition hitbox.hpp:16
Definition theme.hpp:21
An observable value.
Definition observable.hpp:359
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:39
virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:134
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:154
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:90
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 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:287
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:43
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:81
virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:146
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:194
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:52
Definition widget_constraints.hpp:13
Definition widget_layout.hpp:18
T max(T... args)