ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
root.h
Go to the documentation of this file.
1
8
9#ifndef ASW_UI_ROOT_H
10#define ASW_UI_ROOT_H
11
12#include <chrono>
13#include <cstddef>
14#include <deque>
15#include <functional>
16#include <optional>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "context.h"
22#include "modal.h"
23#include "panel.h"
24
25namespace asw::ui {
26
29struct Toast {
31 std::string text;
32
35
37 float seconds = 0.0F;
38
40 std::optional<asw::Color> color { };
41};
42
49class Root {
50public:
53 Root();
54
57
61
64 std::function<void()> on_back;
65
68 bool auto_size = true;
69
75 void set_size(float w, float h);
76
83 bool update();
84
87 void draw();
88
94 void focus(Widget& w, bool show = false);
95
98 void clear_focus();
99
105 void toast(const std::string& text);
106
111 void toast(Toast message);
112
115 void clear_toasts();
116
121 std::size_t toast_count() const
122 {
123 return _toasts.size();
124 }
125
134 template <class T = Modal, class... Args> T& open_modal(Args&&... args)
135 {
136 auto& modal = _modals.add_child<T>(std::forward<Args>(args)...);
137 opened_modal();
138 return modal;
139 }
140
145 bool has_modal() const;
146
149 void close_modals();
150
151private:
152 void fit_to_screen();
153 bool attached(const Widget* w) const;
154
155 // True if w is in the tree under root, not in a modal or removed
156 bool in_root(const Widget* w) const;
157 void validate();
158 void free_removed(Widget& w);
159 Widget* hit_test(Widget& w, const asw::Vec2<float>& pointer_pos);
160 bool bubble(Widget* target, const UIEvent& e);
161 bool dispatch_pointer(const UIEvent& e);
162 bool dispatch_to_focused(const UIEvent& e);
163 void update_pointer();
164 void update_keys();
165 void activate(Widget& w);
166
167 // The top open modal, or nullptr
168 Modal* top_modal() const;
169
170 // Where the pointer and focus go: the top modal, or the root panel
172
173 void opened_modal();
174 void layout_modals();
175 void update_modal_focus();
176 void update_toasts();
177 void draw_toasts();
178
179 bool _used = false;
180
181 // Open modals, drawn over root. See through and screen sized.
183
184 // Focus to give back once the modals close, and the modal whose first
185 // widget was focused
188
189 struct ShownToast {
191 float age = 0.0F;
192 };
193 std::deque<ShownToast> _toasts;
194 std::optional<std::chrono::steady_clock::time_point> _last_update;
195};
196
197} // namespace asw::ui
198
199#endif // ASW_UI_ROOT_H
A 2D vector in space.
Definition geometry.h:27
Shared state for the UI system.
Definition context.h:97
A box over the rest of the UI that has to be dealt with first, e.g. a question or a login code.
Definition modal.h:31
A container widget with a background color or image.
Definition panel.h:20
Root container that manages the UI tree, input, and rendering.
Definition root.h:49
T & open_modal(Args &&... args)
Open a modal over the rest of the UI. Its first focusable widget takes focus, and focus goes back whe...
Definition root.h:134
void focus(Widget &w, bool show=false)
Focus a widget.
Definition root.cpp:658
bool in_root(const Widget *w) const
Definition root.cpp:87
Widget * _focused_modal
Definition root.h:187
void fit_to_screen()
Definition root.cpp:56
void draw_toasts()
Definition root.cpp:591
void update_keys()
Definition root.cpp:405
Panel root
The root panel widget. See through by default, set bg for a background.
Definition root.h:60
bool attached(const Widget *w) const
Definition root.cpp:78
Root()
Default constructor.
Definition root.cpp:48
bool dispatch_pointer(const UIEvent &e)
Definition root.cpp:268
void clear_toasts()
Remove every message, shown or waiting.
Definition root.cpp:562
void close_modals()
Close every open modal, top first.
Definition root.cpp:129
Widget * hit_test(Widget &w, const asw::Vec2< float > &pointer_pos)
Definition root.cpp:212
bool update()
Process input for this frame.
Definition root.cpp:521
void activate(Widget &w)
Definition root.cpp:290
bool dispatch_to_focused(const UIEvent &e)
Definition root.cpp:281
void clear_focus()
Remove focus from every widget.
Definition root.cpp:664
void update_modal_focus()
Definition root.cpp:151
void layout_modals()
Definition root.cpp:137
void opened_modal()
Definition root.cpp:118
void toast(const std::string &text)
Show a message at the top of the screen for a few seconds, e.g. "Achievement unlocked"....
Definition root.cpp:552
std::optional< std::chrono::steady_clock::time_point > _last_update
Definition root.h:194
std::function< void()> on_back
Called when back is pressed and the focused widget does not handle it, e.g. to leave a menu.
Definition root.h:64
bool _used
Definition root.h:179
bool auto_size
Keep the root panel the size of the logical screen. Turned off by set_size.
Definition root.h:68
void free_removed(Widget &w)
Definition root.cpp:204
void validate()
Definition root.cpp:177
bool has_modal() const
True while a modal is open.
Definition root.cpp:113
void update_toasts()
Definition root.cpp:567
void update_pointer()
Definition root.cpp:302
Context ctx
The UI context.
Definition root.h:56
std::size_t toast_count() const
Messages shown or waiting.
Definition root.h:121
Widget & active_root()
Definition root.cpp:105
Widget * _focus_before_modal
Definition root.h:186
void draw()
Draw the UI tree, then the focus ring.
Definition root.cpp:670
void set_size(float w, float h)
Set a fixed size for the root panel.
Definition root.cpp:68
Panel _modals
Definition root.h:182
Modal * top_modal() const
Definition root.cpp:95
bool bubble(Widget *target, const UIEvent &e)
Definition root.cpp:235
std::deque< ShownToast > _toasts
Definition root.h:193
Base class for all UI widgets.
Definition widget.h:43
T & add_child(Args &&... args)
Add a child widget.
Definition widget.h:168
UI context and focus management for the ASW UI module.
Modal widget for the ASW UI module.
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:76
Panel widget for the ASW UI module.
A short message shown at the top of the screen, see Root::toast.
Definition root.h:29
std::string text
The message.
Definition root.h:31
float seconds
How long it stays, the theme toast seconds when 0.
Definition root.h:37
std::optional< asw::Color > color
Text colour, the theme toast text colour when empty.
Definition root.h:40
asw::Texture icon
Picture before the text, e.g. an achievement icon. Optional.
Definition root.h:34
Event structure for UI interactions.
Definition event.h:21