Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
widget_tree.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include <aegis2d/ui/export.hpp>
8
9#include <cstddef>
10#include <cstdint>
11#include <optional>
12#include <span>
13#include <vector>
14
15namespace aegis2d::ui {
16
18using WidgetId = std::size_t;
19
21inline constexpr WidgetId root = 0;
22
24enum class Direction : std::uint8_t {
26 Up,
28 Down,
30 Left,
32 Right,
33};
34
37struct Navigation {
39 bool next = false;
41 bool previous = false;
43 std::optional<Direction> direction{};
45 std::optional<Vec2> pointer{};
47 bool click = false;
49 bool activate = false;
51 bool held = false;
52};
53
72class AEGIS2D_UI_EXPORT WidgetTree {
73public:
76
83 WidgetId add(render::Rect bounds, WidgetId parent = root);
84
90 WidgetId add_group(WidgetId parent = root);
91
94 [[nodiscard]] std::size_t size() const noexcept;
95
100 [[nodiscard]] WidgetId parent(WidgetId id) const;
101
106 [[nodiscard]] std::span<const WidgetId> children(WidgetId id) const;
107
112 void set_bounds(WidgetId id, render::Rect bounds);
113
118 [[nodiscard]] render::Rect bounds(WidgetId id) const;
119
126 void set_enabled(WidgetId id, bool enabled);
127
132 [[nodiscard]] bool enabled(WidgetId id) const;
133
138 void set_scale(float scale, Vec2 origin = {0.0F, 0.0F}) noexcept;
139
142 [[nodiscard]] float scale() const noexcept;
143
147 [[nodiscard]] Vec2 scaled(Vec2 point) const noexcept;
148
151 [[nodiscard]] std::optional<WidgetId> focused() const noexcept;
152
158 bool focus(WidgetId id);
159
161 void clear_focus() noexcept;
162
165 bool focus_next();
166
169 bool focus_previous();
170
178 bool move_focus(Direction direction);
179
184 [[nodiscard]] std::optional<WidgetId> at(Vec2 point) const;
185
191 bool update(const Navigation& navigation);
192
193private:
194 struct Node {
195 render::Rect bounds;
196 WidgetId parent = root;
197 std::vector<WidgetId> children;
198 bool widget = false;
199 bool enabled = true;
200 };
201
202 [[nodiscard]] const Node& node(WidgetId id) const;
203 [[nodiscard]] Node& node(WidgetId id);
204 [[nodiscard]] std::vector<WidgetId> focus_order(bool everything = false) const;
205 bool step_focus(bool forward);
206
207 std::vector<Node> nodes_;
208 std::optional<WidgetId> focused_;
209 float scale_ = 1.0F;
210 Vec2 origin_{0.0F, 0.0F};
211};
212
213} // namespace aegis2d::ui
Widgets of a menu or a form, in groups, and which one has the focus.
Definition widget_tree.hpp:72
WidgetTree()
Creates a tree with its root group only, and no focus.
std::size_t size() const noexcept
Number of widgets and groups, the root included.
WidgetId add(render::Rect bounds, WidgetId parent=root)
Adds a widget that can take the focus.
float scale() const noexcept
Factor given to set_scale().
WidgetId add_group(WidgetId parent=root)
Adds a group, which never takes the focus.
Vector and matrix types of the engine (GLM behind engine names).
Menus and forms: widgets and their focus, moved by keyboard, gamepad and mouse (client only).
Definition console.hpp:21
Direction
Direction the focus moves in, with the arrows, the D-pad or a stick.
Definition widget_tree.hpp:24
@ Down
Towards the bottom of the screen.
@ Up
Towards the top of the screen.
@ Right
Towards the right of the screen.
@ Left
Towards the left of the screen.
std::size_t WidgetId
Identifier of a widget in its WidgetTree, given by WidgetTree::add().
Definition widget_tree.hpp:18
constexpr WidgetId root
The root group of every tree, which the other widgets go under.
Definition widget_tree.hpp:21
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
Axis-aligned rectangle.
Definition renderer.hpp:20
What the player asked of the focus during a frame.
Definition widget_tree.hpp:37
bool activate
Use the focused widget (Enter, Space, South), pressed during the frame.
Definition widget_tree.hpp:49
bool previous
Focus the previous widget (Shift+Tab, left shoulder).
Definition widget_tree.hpp:41
std::optional< Vec2 > pointer
Where the mouse points, in logical pixels, when it moved or clicked.
Definition widget_tree.hpp:45
std::optional< Direction > direction
Focus the nearest widget in a direction.
Definition widget_tree.hpp:43
bool click
The main mouse button went down at the pointer.
Definition widget_tree.hpp:47
bool held
The activate key or button, or the main mouse button, is held down.
Definition widget_tree.hpp:51
bool next
Focus the next widget (Tab, right shoulder).
Definition widget_tree.hpp:39