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 "widget_mode.hpp"
12#include "../layout/module.hpp"
13#include "../geometry/module.hpp"
14#include "../observer/module.hpp"
15#include "../time/module.hpp"
16#include "../settings/module.hpp"
17#include "../numeric/module.hpp"
18#include "../GUI/module.hpp"
19#include "../generator.hpp"
20#include <memory>
21#include <vector>
22#include <string>
23#include <ranges>
24
25namespace hi { inline namespace v1 {
26
36class widget : public widget_intf {
37public:
41 observer<widget_mode> mode = widget_mode::enabled;
42
45 observer<bool> hover = false;
46
49 observer<bool> focus = false;
50
66
75
78 observer<extent2> minimum = extent2{};
79
82 observer<extent2> maximum = extent2::large();
83
86 explicit widget(widget *parent) noexcept;
87
88 virtual ~widget();
89 widget(const widget&) = delete;
90 widget& operator=(const widget&) = delete;
91 widget(widget&&) = delete;
92 widget& operator=(widget&&) = delete;
93
95 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
96 {
97 co_return;
98 }
99
107 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
108 {
109 return {};
110 }
111
118 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point2 position) const noexcept
119 {
120 return hitbox_test(_layout.from_parent * position);
121 }
122
130 [[nodiscard]] virtual hitbox hitbox_test_from_parent(point2 position, hitbox sibling_hitbox) const noexcept
131 {
132 return std::max(sibling_hitbox, hitbox_test(_layout.from_parent * position));
133 }
134
138 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
139 {
140 hi_axiom(loop::main().on_thread());
141 return false;
142 }
143
144 [[nodiscard]] box_constraints update_constraints() noexcept override
145 {
146 _layout = {};
147 return {*minimum, *minimum, *maximum};
148 }
149
150 void set_layout(widget_layout const& context) noexcept override
151 {
152 _layout = context;
153 }
154
155 widget_layout const& layout() const noexcept override
156 {
157 return _layout;
158 }
159
160 void draw(draw_context const& context) noexcept override {}
161
164 bool process_event(gui_event const& event) const noexcept override
165 {
166 if (parent != nullptr) {
167 return parent->process_event(event);
168 } else {
169 return true;
170 }
171 }
172
179
184 bool handle_event(gui_event const& event) noexcept override;
185
187 gui_event const& event,
188 std::vector<widget_id> const& reject_list = std::vector<widget_id>{}) noexcept override;
189
190 [[nodiscard]] virtual widget_id find_next_widget(
191 widget_id current_keyboard_widget,
192 keyboard_focus_group group,
193 keyboard_focus_direction direction) const noexcept override;
194
195 [[nodiscard]] widget_id find_first_widget(keyboard_focus_group group) const noexcept override;
196
197 [[nodiscard]] widget_id find_last_widget(keyboard_focus_group group) const noexcept override;
198
201 [[nodiscard]] bool is_first(keyboard_focus_group group) const noexcept;
202
205 [[nodiscard]] bool is_last(keyboard_focus_group group) const noexcept;
206
208 void scroll_to_show(hi::aarectangle rectangle) noexcept override;
209
210 void set_window(gui_window *window) noexcept override
211 {
212 if (parent) {
213 return parent->set_window(window);
214 } else {
215 return;
216 }
217 }
218
219 [[nodiscard]] gui_window *window() const noexcept override
220 {
221 if (parent) {
222 return parent->window();
223 } else {
224 return nullptr;
225 }
226 }
227
228 [[nodiscard]] hi::theme const &theme() const noexcept
229 {
230 hilet w = window();
232 return w->theme;
233 }
234
235 [[nodiscard]] gfx_surface const *surface() const noexcept
236 {
237 if (auto w = window()) {
238 return w->surface.get();
239 } else {
240 return nullptr;
241 }
242 }
243
244 [[nodiscard]] virtual color background_color() const noexcept;
245
246 [[nodiscard]] virtual color foreground_color() const noexcept;
247
248 [[nodiscard]] virtual color focus_color() const noexcept;
249
250 [[nodiscard]] virtual color accent_color() const noexcept;
251
252 [[nodiscard]] virtual color label_color() const noexcept;
253
254protected:
255 widget_layout _layout;
256
257 decltype(mode)::callback_token _mode_cbt;
258
268 [[nodiscard]] aarectangle make_overlay_rectangle(aarectangle requested_rectangle) const noexcept;
269};
270
271}} // namespace hi::v1
Defines widget_mode.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ window_redraw
Request that part of the window gets redrawn on the next frame.
@ enabled
The widget is fully enabled.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:26
A high-level geometric extent.
Definition extent2.hpp:26
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:209
A user interface event.
Definition gui_event.hpp:74
Definition widget_intf.hpp:16
virtual gui_window * window() const noexcept=0
Get the window that the widget is owned by.
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:195
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:27
virtual bool process_event(gui_event const &event) const noexcept=0
Send a event to the window.
virtual generator< widget_intf & > children(bool include_invisible) noexcept=0
Get a list of child widgets.
virtual void set_window(gui_window *window) noexcept=0
Set the window for this tree of widgets.
The layout of a widget.
Definition widget_layout.hpp:35
constexpr aarectangle clipping_rectangle_on_window() const noexcept
Get the clipping rectangle in window coordinate system.
Definition widget_layout.hpp:165
translate2 from_parent
This matrix transforms parent widget's coordinates to local coordinates.
Definition widget_layout.hpp:54
2D constraints.
Definition box_constraints.hpp:22
An interactive graphical object as part of the user-interface.
Definition widget.hpp:36
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:155
bool is_first(keyboard_focus_group group) const noexcept
Is this widget the first widget in the parent container.
int logical_layer
The logical layer of the widget.
Definition widget.hpp:74
bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
Check if the widget will accept keyboard focus.
Definition widget.hpp:138
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:65
observer< extent2 > minimum
The minimum size this widget is allowed to be.
Definition widget.hpp:78
observer< bool > hover
Mouse cursor is hovering over the widget.
Definition widget.hpp:45
virtual hitbox hitbox_test_from_parent(point2 position, hitbox sibling_hitbox) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:130
bool handle_event_recursive(gui_event const &event, std::vector< widget_id > const &reject_list=std::vector< widget_id >{}) noexcept override
Handle command recursive.
gui_window * window() const noexcept override
Get the window that the widget is owned by.
Definition widget.hpp:219
virtual widget_id find_next_widget(widget_id current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept override
Find the next widget that handles keyboard focus.
void scroll_to_show(hi::aarectangle rectangle) noexcept override
Scroll to show the given rectangle on the window.
void set_layout(widget_layout const &context) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:150
widget(widget *parent) noexcept
generator< widget_intf & > children(bool include_invisible) noexcept override
Get a list of child widgets.
Definition widget.hpp:95
void draw(draw_context const &context) noexcept override
Draw the widget.
Definition widget.hpp:160
void request_redraw() const noexcept override
Request the widget to be redrawn on the next frame.
Definition widget.hpp:175
virtual hitbox hitbox_test_from_parent(point2 position) const noexcept
Call hitbox_test from a parent widget.
Definition widget.hpp:118
hitbox hitbox_test(point2 position) const noexcept override
Find the widget that is under the mouse cursor.
Definition widget.hpp:107
box_constraints update_constraints() noexcept override
Update the constraints of the widget.
Definition widget.hpp:144
void set_window(gui_window *window) noexcept override
Set the window for this tree of widgets.
Definition widget.hpp:210
bool is_last(keyboard_focus_group group) const noexcept
Is this widget the last widget in the parent container.
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:164
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:41
observer< bool > focus
The widget has keyboard focus.
Definition widget.hpp:49
bool handle_event(gui_event const &event) noexcept override
Handle command.
observer< extent2 > maximum
The maximum size this widget is allowed to be.
Definition widget.hpp:82
T max(T... args)