Line data Source code
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" 27 : #include "scriptinterface/Interface.h" 28 : #include "scriptinterface/StructuredClone.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 : 39 : class CCanvas2D; 40 : class CGUI; 41 : class CParamNode; 42 : namespace Script { class Context; } 43 : namespace JS { class HandleValueArray; } 44 : namespace JS { class Value; } 45 : namespace 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 : */ 56 : class CGUIManager 57 : { 58 : NONCOPYABLE(CGUIManager); 59 : public: 60 : CGUIManager(Script::Context& scriptContext, Script::Interface& scriptInterface); 61 : ~CGUIManager(); 62 : 63 9 : Script::Interface& GetScriptInterface() 64 : { 65 9 : return m_ScriptInterface; 66 : } 67 : Script::Context& GetContext() { return m_ScriptContext; } 68 7 : 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 : */ 96 : Status ReloadAllPages(); 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 : 141 : private: 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 32 : struct Close 163 : { 164 : Script::StructuredClone arg; 165 : bool rejected; 166 : }; 167 : 168 1 : struct OpenRequest 169 : { 170 : std::wstring path; 171 : Script::StructuredClone arg; 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 : 209 : Script::Context& m_ScriptContext; 210 : Script::Interface& m_ScriptInterface; 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 10 : class PageStackType : public std::deque<SGUIPage> 219 : { 220 : public: 221 10 : ~PageStackType() 222 : { 223 20 : clear(); 224 10 : } 225 : 226 10 : void clear() 227 : { 228 16 : while (!std::deque<SGUIPage>::empty()) 229 6 : std::deque<SGUIPage>::pop_back(); 230 : } 231 : }; 232 : PageStackType m_PageStack; 233 : 234 : /** 235 : * Returns an immutable copy so iterators aren't invalidated by handlers. 236 : */ 237 : PS::StaticVector<SGUIPage, 16> GetCopyOfFrozenStack() const; 238 : 239 : CTemplateLoader m_TemplateLoader; 240 : 241 : struct InputHandler 242 : { 243 : CGUIManager& gui; 244 : Input::Reaction operator()(const SDL_Event& ev); 245 : }; 246 : Input::Handler<InputHandler> m_InputHandler; 247 : }; 248 : 249 : extern CGUIManager* g_GUI; 250 : 251 : #endif // INCLUDED_GUIMANAGER