HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2019-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 "../GFX/draw_context.hpp"
12#include "../GUI/theme.hpp"
13#include "../GUI/hitbox.hpp"
14#include "../GUI/keyboard_focus_direction.hpp"
15#include "../GUI/keyboard_focus_group.hpp"
16#include "../GUI/gui_event.hpp"
17#include "../geometry/extent.hpp"
18#include "../geometry/axis_aligned_rectangle.hpp"
19#include "../geometry/transform.hpp"
20#include "../observer.hpp"
21#include "../chrono.hpp"
22#include "../generator.hpp"
23#include "set_constraints_context.hpp"
25#include "widget_layout.hpp"
26#include "widget_mode.hpp"
27#include <memory>
28#include <vector>
29#include <string>
30#include <ranges>
31
32namespace hi { inline namespace v1 {
33class gui_window;
34class font_book;
35
45class widget {
46public:
51
55
59 observer<widget_mode> mode = widget_mode::enabled;
60
63 observer<bool> hover = false;
64
67 observer<bool> focus = false;
68
84
93
96 widget(widget *parent) noexcept;
97
98 virtual ~widget();
99 widget(const widget&) = delete;
100 widget& operator=(const widget&) = delete;
101 widget(widget&&) = delete;
102 widget& operator=(widget&&) = delete;
103
111 [[nodiscard]] virtual hitbox hitbox_test(point3 position) const noexcept
112 {
113 return {};
114 }
115
122 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
123 {
124 return hitbox_test(_layout.from_parent * position);
125 }
126
134 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
135 {
136 return std::max(sibling_hitbox, hitbox_test(_layout.from_parent * position));
137 }
138
142 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
143 {
144 hi_axiom(loop::main().on_thread());
145 return false;
146 }
147
159 virtual widget_constraints const& set_constraints(set_constraints_context const& context) noexcept = 0;
160
161 widget_constraints const& constraints() const noexcept
162 {
163 return _constraints;
164 }
165
177 virtual void set_layout(widget_layout const& context) noexcept = 0;
178
181 widget_layout const& layout() const noexcept
182 {
183 return _layout;
184 }
185
200 virtual void draw(draw_context const& context) noexcept = 0;
201
202 virtual bool process_event(gui_event const& event) const noexcept
203 {
204 if (parent != nullptr) {
205 return parent->process_event(event);
206 } else {
207 return true;
208 }
209 }
210
213 void request_redraw() const noexcept
214 {
216 }
217
222 virtual bool handle_event(gui_event const& event) noexcept;
223
232 gui_event const& event,
233 std::vector<widget const *> const& reject_list = std::vector<widget const *>{}) noexcept;
234
247 [[nodiscard]] virtual widget const *find_next_widget(
248 widget const *current_keyboard_widget,
249 keyboard_focus_group group,
250 keyboard_focus_direction direction) const noexcept;
251
252 [[nodiscard]] widget const *find_first_widget(keyboard_focus_group group) const noexcept;
253
254 [[nodiscard]] widget const *find_last_widget(keyboard_focus_group group) const noexcept;
255
258 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
259
262 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
263
270 virtual void scroll_to_show(hi::aarectangle rectangle) noexcept;
271
274 void scroll_to_show() noexcept
275 {
277 }
278
282 [[nodiscard]] std::vector<widget const *> parent_chain() const noexcept;
283
284 virtual [[nodiscard]] color background_color() const noexcept;
285
286 virtual [[nodiscard]] color foreground_color() const noexcept;
287
288 virtual [[nodiscard]] color focus_color() const noexcept;
289
290 virtual [[nodiscard]] color accent_color() const noexcept;
291
292 virtual [[nodiscard]] color label_color() const noexcept;
293
294protected:
295 widget_constraints _constraints;
296 widget_layout _layout;
297
298 decltype(mode)::callback_token _mode_cbt;
299
300 [[nodiscard]] virtual generator<widget *> children() const noexcept
301 {
302 co_return;
303 }
304
314 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
315};
316}} // namespace hi::v1
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
Defines widget_constraints.
Defines widget_layout.
Defines widget_mode.
Definition of GUI event types.
@ window_redraw
Request that part of the window gets redrawn on the next frame.
@ rectangle
The gui_event has rectangle data.
@ enabled
The widget is fully enabled.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
A user interface event.
Definition gui_event.hpp:76
Definition set_constraints_context.hpp:15
An interactive graphical object as part of the user-interface.
Definition widget.hpp:45
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:181
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
virtual void scroll_to_show(hi::aarectangle rectangle) noexcept
Scroll to show the given rectangle on the window.
virtual hitbox hitbox_test_from_parent(point3 position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:122
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:92
std::string id
A name of widget, should be unique between siblings.
Definition widget.hpp:54
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:83
virtual widget_constraints const & set_constraints(set_constraints_context const &context) noexcept=0
Update the constraints of the widget.
observer< bool > hover
Mouse cursor is hovering over the widget.
Definition widget.hpp:63
virtual void draw(draw_context const &context) noexcept=0
Draw the widget.
virtual bool handle_event_recursive(gui_event const &event, std::vector< widget const * > const &reject_list=std::vector< widget const * >{}) noexcept
Handle command recursive.
virtual hitbox hitbox_test_from_parent(point3 position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:134
virtual hitbox hitbox_test(point3 position) const noexcept
Find the widget that is under the mouse cursor.
Definition widget.hpp:111
widget(widget *parent) noexcept
void request_redraw() const noexcept
Request the widget to be redrawn on the next frame.
Definition widget.hpp:213
std::vector< widget const * > parent_chain() const noexcept
Get a list of parents of a given widget.
virtual void set_layout(widget_layout const &context) noexcept=0
Update the internal layout of the widget.
virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept
Check if the widget will accept keyboard focus.
Definition widget.hpp:142
virtual bool handle_event(gui_event const &event) noexcept
Handle command.
widget * parent
Pointer to the parent widget.
Definition widget.hpp:50
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget.hpp:274
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:59
observer< bool > focus
The widget has keyboard focus.
Definition widget.hpp:67
The constraints of a widget.
Definition widget_constraints.hpp:26
The layout of a widget.
Definition widget_layout.hpp:40
constexpr aarectangle clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:152
matrix3 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:54
T max(T... args)