HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget_intf.hpp
1
2
3#pragma once
4
5#include "hitbox.hpp"
6#include "widget_layout.hpp"
7#include "widget_id.hpp"
8#include "keyboard_focus_group.hpp"
9#include "../layout/module.hpp"
10#include "../GFX/module.hpp"
11#include "../telemetry/module.hpp"
12
13namespace hi { inline namespace v1 {
14class gui_window;
15
17public:
22 widget_id id = {};
23
27 widget_intf *parent = nullptr;
28
29 virtual ~widget_intf() = default;
30
31 widget_intf(widget_intf *parent) noexcept : id(narrow_cast<uint32_t>(++global_counter<"widget::id">)), parent(parent) {}
32
38 virtual void set_window(gui_window *window) noexcept = 0;
39
45 [[nodiscard]] virtual gui_window *window() const noexcept = 0;
46
49 [[nodiscard]] virtual generator<widget_intf&> children(bool include_invisible) noexcept = 0;
50
53 [[nodiscard]] virtual generator<widget_intf const&> children(bool include_invisible) const noexcept final
54 {
55 for (auto& child : const_cast<widget_intf *>(this)->children(include_invisible)) {
56 co_yield child;
57 }
58 }
59
71 virtual [[nodiscard]] box_constraints update_constraints() noexcept = 0;
72
84 virtual void set_layout(widget_layout const& context) noexcept = 0;
85
88 virtual widget_layout const& layout() const noexcept = 0;
89
104 virtual void draw(draw_context const& context) noexcept = 0;
105
113 [[nodiscard]] virtual hitbox hitbox_test(point2 position) const noexcept = 0;
114
118 [[nodiscard]] virtual bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept = 0;
119
122 virtual void request_redraw() const noexcept = 0;
123
126 virtual bool process_event(gui_event const& event) const noexcept = 0;
127
132 virtual bool handle_event(gui_event const& event) noexcept = 0;
133
142 gui_event const& event,
143 std::vector<widget_id> const& reject_list = std::vector<widget_id>{}) noexcept = 0;
144
159 [[nodiscard]] virtual widget_id find_next_widget(
160 widget_id current_keyboard_widget,
161 keyboard_focus_group group,
162 keyboard_focus_direction direction) const noexcept = 0;
163
164 [[nodiscard]] virtual widget_id find_first_widget(keyboard_focus_group group) const noexcept = 0;
165
166 [[nodiscard]] virtual widget_id find_last_widget(keyboard_focus_group group) const noexcept = 0;
167
171 [[nodiscard]] std::vector<widget_id> parent_chain() const noexcept
172 {
174
175 if (auto w = this) {
176 chain.push_back(w->id);
177 while (to_bool(w = w->parent)) {
178 chain.push_back(w->id);
179 }
180 }
181
182 return chain;
183 }
184
191 virtual void scroll_to_show(hi::aarectangle rectangle) noexcept = 0;
192
195 void scroll_to_show() noexcept
196 {
198 }
199};
200
201inline widget_intf *get_if(widget_intf *start, widget_id id, bool include_invisible) noexcept
202{
203 hi_assert_not_null(start);
204
205 if (start->id == id) {
206 return start;
207 }
208 for (auto& child : start->children(include_invisible)) {
209 if (hilet r = get_if(&child, id, include_invisible); r != nullptr) {
210 return r;
211 }
212 }
213 return nullptr;
214}
215
216inline widget_intf& get(widget_intf& start, widget_id id, bool include_invisible)
217{
218 if (auto r = get_if(std::addressof(start), id, include_invisible); r != nullptr) {
219 return *r;
220 }
221 throw not_found_error("get widget by id");
222}
223
224}} // namespace hi::v1
#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
@ rectangle
The gui_event has rectangle data.
STL namespace.
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 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.
std::vector< widget_id > parent_chain() const noexcept
Get a list of parents of a given widget.
Definition widget_intf.hpp:171
virtual bool handle_event_recursive(gui_event const &event, std::vector< widget_id > const &reject_list=std::vector< widget_id >{}) noexcept=0
Handle command recursive.
widget_id id
The numeric identifier of a widget.
Definition widget_intf.hpp:22
virtual void request_redraw() const noexcept=0
Request the widget to be redrawn on the next frame.
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 bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept=0
Check if the widget will accept keyboard focus.
virtual box_constraints update_constraints() noexcept=0
Update the constraints of the widget.
virtual widget_id find_next_widget(widget_id current_keyboard_widget, keyboard_focus_group group, keyboard_focus_direction direction) const noexcept=0
Find the next widget that handles keyboard focus.
virtual void set_layout(widget_layout const &context) noexcept=0
Update the internal layout of the widget.
virtual void scroll_to_show(hi::aarectangle rectangle) noexcept=0
Scroll to show the given rectangle on the window.
virtual bool handle_event(gui_event const &event) noexcept=0
Handle command.
virtual hitbox hitbox_test(point2 position) const noexcept=0
Find the widget that is under the mouse cursor.
virtual widget_layout const & layout() const noexcept=0
Get the current layout for this widget.
virtual generator< widget_intf & > children(bool include_invisible) noexcept=0
Get a list of child widgets.
virtual void draw(draw_context const &context) noexcept=0
Draw the widget.
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
2D constraints.
Definition box_constraints.hpp:22
T addressof(T... args)
T push_back(T... args)