Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
GUIManager.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_GUIMANAGER
19#define INCLUDED_GUIMANAGER
20
21#include "lib/code_annotation.h"
22#include "lib/path.h"
23#include "lib/status.h"
24#include "ps/CStr.h"
25#include "ps/Input.h"
26#include "ps/TemplateLoader.h"
29
30#include <cstddef>
31#include <deque>
32#include <js/TypeDecls.h>
33#include <memory>
34#include <optional>
35#include <string>
36#include <unordered_set>
37#include <variant>
38
39class CCanvas2D;
40class CGUI;
41class CParamNode;
42namespace Script { class Context; }
43namespace JS { class HandleValueArray; }
44namespace JS { class Value; }
45namespace PS { template <typename T, size_t N> class StaticVector; }
46
47/**
48 * External interface to the GUI system.
49 *
50 * The GUI consists of a set of pages. Each page is constructed from a
51 * series of XML files, and is independent from any other page.
52 * Only one page is active at a time. All events and render requests etc
53 * will go to the active page. This lets the GUI switch between pre-game menu
54 * and in-game UI.
55 */
57{
59public:
60 CGUIManager(Script::Context& scriptContext, Script::Interface& scriptInterface);
62
64 {
65 return m_ScriptInterface;
66 }
68 std::shared_ptr<CGUI> GetActiveGUI() { return top(); }
69
70 /**
71 * Returns the number of currently open GUI pages.
72 */
73 size_t GetPageCount() const;
74
75 /**
76 * Load a new GUI page and make it active. All current pages will be destroyed.
77 */
78 void SwitchPage(const CStrW& name, const Script::Interface* srcScriptInterface, JS::HandleValue initData);
79
80 /**
81 * Load a new GUI page and make it active. All current pages will be retained,
82 * and will still be drawn and receive tick events, but will not receive
83 * user inputs.
84 * The returned promise will be fulfilled once the pushed page is closed.
85 */
86 JS::Value OpenChildPage(const CStrW& pageName, Script::StructuredClone initData);
87
88 /**
89 * Called when a file has been modified, to hotload changes.
90 */
91 Status ReloadChangedFile(const VfsPath& path);
92
93 /**
94 * Called when we should reload all pages (e.g. translation hotloading update).
95 */
97
98 /**
99 * Pass input events to the currently active GUI page.
100 */
101 Input::Reaction HandleEvent(const SDL_Event& ev);
102
103 /**
104 * See CGUI::SendEventToAll; applies to the currently active page.
105 */
106 void SendEventToAll(const CStr& eventName,
107 std::optional<JS::HandleValueArray> paramData = std::nullopt) const;
108
109 /**
110 * See CGUI::TickObjects; applies to @em all loaded pages.
111 * When the root page is closed it's returned wheter Atlas should be
112 * started.
113 */
114 std::optional<bool> TickObjects();
115
116 /**
117 * See CGUI::Draw; applies to @em all loaded pages.
118 */
119 void Draw(CCanvas2D& canvas) const;
120
121 /**
122 * See CGUI::UpdateResolution; applies to @em all loaded pages.
123 */
124 void UpdateResolution();
125
126 /**
127 * Check if a template with this name exists
128 */
129 bool TemplateExists(const std::string& templateName) const;
130
131 /**
132 * Retrieve the requested template, used for displaying faction specificities.
133 */
134 const CParamNode& GetTemplate(const std::string& templateName);
135
136 /**
137 * Display progress / description in loading screen.
138 */
139 void DisplayLoadProgress(int percent, const wchar_t* pending_task);
140
141private:
142 struct SGUIPage
143 {
144 // COPYABLE, because event handlers may invalidate page stack iterators by open or close pages,
145 // and event handlers need to be called for the entire stack.
146
147 /**
148 * Initializes the data that will be used to create the CGUI page one or multiple times (hotloading).
149 */
150 SGUIPage(const CStrW& pageName, const Script::StructuredClone initData);
151
152 /**
153 * Create the CGUI with it's own Script::Interface. Deletes the previous CGUI if it existed.
154 */
155 void LoadPage(Script::Context& context);
156
157 /**
158 * A reference to the promise is returned. The promise will settle when the page is closed.
159 */
160 JS::Value GetPromise();
161
162 struct Close
163 {
166 };
167
169 {
170 std::wstring path;
172 };
173
174 using CloseResult = std::variant<Close, OpenRequest>;
175 /**
176 * If the page should be closed this function closes the page and
177 * returns the result of the @c init function.
178 * If this page wasn't closed an empty optional is returned.
179 */
180 std::optional<CloseResult> MaybeClose(const bool isRootPage);
181
182 /**
183 * This function should be called when a child page got closed. The
184 * result of the closed page should be the argument of this
185 * function. This function resolves the @c receivingPromise.
186 */
187 void Refocus(const Close& result);
188
189 std::wstring m_Name;
190 std::unordered_set<VfsPath> inputs; // for hotloading
191 Script::StructuredClone initData; // data to be passed to the init() function
192 std::shared_ptr<CGUI> gui; // the actual GUI page
193
194 /**
195 * When this promise is settled this page wants to be closed. It
196 * settles with the page completion value.
197 */
198 std::shared_ptr<JS::PersistentRootedObject> sendingPromise;
199
200 /**
201 * The parent page waits on this promise. It also gets the
202 * completion value through this promise.
203 */
204 std::shared_ptr<JS::PersistentRootedObject> receivingPromise;
205 };
206
207 std::shared_ptr<CGUI> top() const;
208
211
212 /**
213 * The page stack must not move pointers on push/pop, or pushing a page in a page's init method
214 * may crash (as the pusher page will suddenly have moved, and the stack will be confused).
215 * Therefore use std::deque over std::vector.
216 * Also the elements have to be destructed back to front.
217 */
218 class PageStackType : public std::deque<SGUIPage>
219 {
220 public:
222 {
223 clear();
224 }
225
226 void clear()
227 {
228 while (!std::deque<SGUIPage>::empty())
229 std::deque<SGUIPage>::pop_back();
230 }
231 };
233
234 /**
235 * Returns an immutable copy so iterators aren't invalidated by handlers.
236 */
238
240
242 {
244 Input::Reaction operator()(const SDL_Event& ev);
245 };
247};
248
249extern CGUIManager* g_GUI;
250
251#endif // INCLUDED_GUIMANAGER
CGUIManager * g_GUI
Definition: GUIManager.cpp:73
Definition: Canvas2D.h:36
The page stack must not move pointers on push/pop, or pushing a page in a page's init method may cras...
Definition: GUIManager.h:219
void clear()
Definition: GUIManager.h:226
~PageStackType()
Definition: GUIManager.h:221
External interface to the GUI system.
Definition: GUIManager.h:57
Status ReloadAllPages()
Called when we should reload all pages (e.g.
Definition: GUIManager.cpp:362
PageStackType m_PageStack
Definition: GUIManager.h:232
CGUIManager(Script::Context &scriptContext, Script::Interface &scriptInterface)
Definition: GUIManager.cpp:93
Script::Interface & m_ScriptInterface
Definition: GUIManager.h:210
void DisplayLoadProgress(int percent, const wchar_t *pending_task)
Display progress / description in loading screen.
Definition: GUIManager.cpp:506
NONCOPYABLE(CGUIManager)
std::optional< bool > TickObjects()
See CGUI::TickObjects; applies to all loaded pages.
Definition: GUIManager.cpp:428
std::shared_ptr< CGUI > top() const
Definition: GUIManager.cpp:525
Script::Interface & GetScriptInterface()
Definition: GUIManager.h:63
bool TemplateExists(const std::string &templateName) const
Check if a template with this name exists.
Definition: GUIManager.cpp:492
Script::Context & GetContext()
Definition: GUIManager.h:67
void SwitchPage(const CStrW &name, const Script::Interface *srcScriptInterface, JS::HandleValue initData)
Load a new GUI page and make it active.
Definition: GUIManager.cpp:119
Script::Context & m_ScriptContext
Definition: GUIManager.h:209
void Draw(CCanvas2D &canvas) const
See CGUI::Draw; applies to all loaded pages.
Definition: GUIManager.cpp:473
std::shared_ptr< CGUI > GetActiveGUI()
Definition: GUIManager.h:68
void SendEventToAll(const CStr &eventName, std::optional< JS::HandleValueArray > paramData=std::nullopt) const
See CGUI::SendEventToAll; applies to the currently active page.
Definition: GUIManager.cpp:414
Status ReloadChangedFile(const VfsPath &path)
Called when a file has been modified, to hotload changes.
Definition: GUIManager.cpp:348
Input::Reaction HandleEvent(const SDL_Event &ev)
Pass input events to the currently active GUI page.
Definition: GUIManager.cpp:371
void UpdateResolution()
See CGUI::UpdateResolution; applies to all loaded pages.
Definition: GUIManager.cpp:481
PS::StaticVector< SGUIPage, 16 > GetCopyOfFrozenStack() const
Returns an immutable copy so iterators aren't invalidated by handlers.
Definition: GUIManager.cpp:531
JS::Value OpenChildPage(const CStrW &pageName, Script::StructuredClone initData)
Load a new GUI page and make it active.
Definition: GUIManager.cpp:141
const CParamNode & GetTemplate(const std::string &templateName)
Retrieve the requested template, used for displaying faction specificities.
Definition: GUIManager.cpp:497
size_t GetPageCount() const
Returns the number of currently open GUI pages.
Definition: GUIManager.cpp:114
Input::Handler< InputHandler > m_InputHandler
Definition: GUIManager.h:246
~CGUIManager()
Definition: GUIManager.cpp:109
CTemplateLoader m_TemplateLoader
Definition: GUIManager.h:239
The main object that represents a whole GUI page.
Definition: CGUI.h:80
An entity initialisation parameter node.
Definition: ParamNode.h:153
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:50
Usable type to register a handler to the associated Manager.
Definition: Input.h:180
A conntainer close to std::vector but the elements are stored in place: There is a fixed capacity and...
Definition: StaticVector.h:89
Definition: path.h:84
Definition: Context.h:51
Definition: Interface.h:93
Reaction
Definition: Input.h:39
Definition: CGUI.h:63
Definition: GUIManager.h:45
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
std::shared_ptr< JSStructuredCloneData > StructuredClone
Structured clones are a way to serialize 'simple' JS::Values into a buffer that can safely be passed ...
Definition: StructuredClone.h:39
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: GUIManager.h:242
CGUIManager & gui
Definition: GUIManager.h:243
Input::Reaction operator()(const SDL_Event &ev)
Definition: GUIManager.cpp:82
Definition: GUIManager.h:163
bool rejected
Definition: GUIManager.h:165
Script::StructuredClone arg
Definition: GUIManager.h:164
Definition: GUIManager.h:169
Script::StructuredClone arg
Definition: GUIManager.h:171
std::wstring path
Definition: GUIManager.h:170
Definition: GUIManager.h:143
std::shared_ptr< JS::PersistentRootedObject > sendingPromise
When this promise is settled this page wants to be closed.
Definition: GUIManager.h:198
std::unordered_set< VfsPath > inputs
Definition: GUIManager.h:190
SGUIPage(const CStrW &pageName, const Script::StructuredClone initData)
Initializes the data that will be used to create the CGUI page one or multiple times (hotloading).
Definition: GUIManager.cpp:163
void LoadPage(Script::Context &context)
Create the CGUI with it's own Script::Interface.
Definition: GUIManager.cpp:168
std::shared_ptr< CGUI > gui
Definition: GUIManager.h:192
std::wstring m_Name
Definition: GUIManager.h:189
std::optional< CloseResult > MaybeClose(const bool isRootPage)
If the page should be closed this function closes the page and returns the result of the init functio...
Definition: GUIManager.cpp:281
std::variant< Close, OpenRequest > CloseResult
Definition: GUIManager.h:174
void Refocus(const Close &result)
This function should be called when a child page got closed.
Definition: GUIManager.cpp:327
std::shared_ptr< JS::PersistentRootedObject > receivingPromise
The parent page waits on this promise.
Definition: GUIManager.h:204
JS::Value GetPromise()
A reference to the promise is returned.
Definition: GUIManager.cpp:269
Script::StructuredClone initData
Definition: GUIManager.h:191