Pyrogenesis  trunk
GUIManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 <boost/unordered_set.hpp>
22 #include <set>
23 
24 #include "lib/input.h"
25 #include "lib/file/vfs/vfs_path.h"
26 #include "ps/CLogger.h"
27 #include "ps/CStr.h"
28 #include "ps/TemplateLoader.h"
31 
32 class CGUI;
33 class JSObject;
34 class IGUIObject;
35 struct CColor;
36 struct SGUIIcon;
37 
38 /**
39  * External interface to the GUI system.
40  *
41  * The GUI consists of a set of pages. Each page is constructed from a
42  * series of XML files, and is independent from any other page.
43  * Only one page is active at a time. All events and render requests etc
44  * will go to the active page. This lets the GUI switch between pre-game menu
45  * and in-game UI.
46  */
48 {
50 public:
51  CGUIManager();
52  ~CGUIManager();
53 
54  shared_ptr<ScriptInterface> GetScriptInterface()
55  {
56  return m_ScriptInterface;
57  }
58  shared_ptr<ScriptRuntime> GetRuntime() { return m_ScriptRuntime; }
59  shared_ptr<CGUI> GetActiveGUI() { return top(); }
60 
61  /**
62  * Returns whether there are any current pages.
63  */
64  bool HasPages();
65 
66  /**
67  * Load a new GUI page and make it active. All current pages will be destroyed.
68  */
69  void SwitchPage(const CStrW& name, ScriptInterface* srcScriptInterface, JS::HandleValue initData);
70 
71  /**
72  * Load a new GUI page and make it active. All current pages will be retained,
73  * and will still be drawn and receive tick events, but will not receive
74  * user inputs.
75  */
76  void PushPage(const CStrW& pageName, shared_ptr<ScriptInterface::StructuredClone> initData);
77 
78  /**
79  * Unload the currently active GUI page, and make the previous page active.
80  * (There must be at least two pages when you call this.)
81  */
82  void PopPage();
83  void PopPageCB(shared_ptr<ScriptInterface::StructuredClone> args);
84 
85  /**
86  * Display a modal message box with an "OK" button.
87  */
88  void DisplayMessageBox(int width, int height, const CStrW& title, const CStrW& message);
89 
90  /**
91  * Called when a file has been modified, to hotload changes.
92  */
93  Status ReloadChangedFile(const VfsPath& path);
94 
95  /**
96  * Sets the default mouse pointer.
97  */
98  void ResetCursor();
99 
100  /**
101  * Called when we should reload all pages (e.g. translation hotloading update).
102  */
104 
105  /**
106  * Pass input events to the currently active GUI page.
107  */
108  InReaction HandleEvent(const SDL_Event_* ev);
109 
110  /**
111  * See CGUI::GetPreDefinedColor; applies to the currently active page.
112  */
113  bool GetPreDefinedColor(const CStr& name, CColor& output) const;
114 
115  /**
116  * See CGUI::FindObjectByName; applies to the currently active page.
117  */
118  IGUIObject* FindObjectByName(const CStr& name) const;
119 
120  /**
121  * See CGUI::SendEventToAll; applies to the currently active page.
122  */
123  void SendEventToAll(const CStr& eventName) const;
124 
125  /**
126  * See CGUI::TickObjects; applies to @em all loaded pages.
127  */
128  void TickObjects();
129 
130  /**
131  * See CGUI::Draw; applies to @em all loaded pages.
132  */
133  void Draw();
134 
135  /**
136  * See CGUI::UpdateResolution; applies to @em all loaded pages.
137  */
138  void UpdateResolution();
139 
140  /**
141  * Calls the current page's script function getSavedGameData() and returns the result.
142  */
143  std::string GetSavedGameData();
144 
145  void RestoreSavedGameData(const std::string& jsonData);
146 
147  /**
148  * Check if a template with this name exists
149  */
150  bool TemplateExists(const std::string& templateName) const;
151 
152  /**
153  * Retrieve the requested template, used for displaying faction specificities.
154  */
155  const CParamNode& GetTemplate(const std::string& templateName);
156 
157 private:
158  struct SGUIPage
159  {
160  CStrW name;
161  boost::unordered_set<VfsPath> inputs; // for hotloading
162 
163  JSContext* cx;
164  shared_ptr<ScriptInterface::StructuredClone> initData; // data to be passed to the init() function
166 
167  shared_ptr<CGUI> gui; // the actual GUI page
168  };
169 
170  void LoadPage(SGUIPage& page);
171 
172  shared_ptr<CGUI> top() const;
173 
174  shared_ptr<CGUI> m_CurrentGUI; // used to latch state during TickObjects/LoadPage (this is kind of ugly)
175  shared_ptr<ScriptRuntime> m_ScriptRuntime;
176  shared_ptr<ScriptInterface> m_ScriptInterface;
177 
178  typedef std::vector<SGUIPage> PageStackType;
179  PageStackType m_PageStack;
180 
182 };
183 
184 extern CGUIManager* g_GUI;
185 
186 extern InReaction gui_handler(const SDL_Event_* ev);
187 
188 #endif // INCLUDED_GUIMANAGER
An entity initialisation parameter node.
Definition: ParamNode.h:148
JSContext * cx
Definition: GUIManager.h:163
Template loader: Handles the loading of entity template files for:
Definition: TemplateLoader.h:45
void LoadPage(SGUIPage &page)
Definition: GUIManager.cpp:181
shared_ptr< ScriptInterface > GetScriptInterface()
Definition: GUIManager.h:54
void PopPage()
Unload the currently active GUI page, and make the previous page active.
Definition: GUIManager.cpp:105
PageStackType m_PageStack
Definition: GUIManager.h:179
Definition: Shapes.h:36
CStrW name
Definition: GUIManager.h:160
Definition: GUIManager.h:158
Status ReloadAllPages()
Called when we should reload all pages (e.g.
Definition: GUIManager.cpp:296
Base settings, all objects possess these settings in their m_BaseSettings Instructions can be found i...
Definition: IGUIObject.h:117
void RestoreSavedGameData(const std::string &jsonData)
Definition: GUIManager.cpp:322
Definition: libsdl.h:51
boost::unordered_set< VfsPath > inputs
Definition: GUIManager.h:161
void UpdateResolution()
See CGUI::UpdateResolution; applies to all loaded pages.
Definition: GUIManager.cpp:425
std::vector< SGUIPage > PageStackType
Definition: GUIManager.h:178
The main object that represents a whole GUI page.
Definition: CGUI.h:75
shared_ptr< ScriptRuntime > GetRuntime()
Definition: GUIManager.h:58
~CGUIManager()
Definition: GUIManager.cpp:72
std::string GetSavedGameData()
Calls the current page&#39;s script function getSavedGameData() and returns the result.
Definition: GUIManager.cpp:310
output
Definition: tests.py:116
bool TemplateExists(const std::string &templateName) const
Check if a template with this name exists.
Definition: GUIManager.cpp:431
Definition: path.h:77
IGUIObject * FindObjectByName(const CStr &name) const
See CGUI::FindObjectByName; applies to the currently active page.
Definition: GUIManager.cpp:383
InReaction
Definition: input.h:34
shared_ptr< ScriptInterface > m_ScriptInterface
Definition: GUIManager.h:176
void PopPageCB(shared_ptr< ScriptInterface::StructuredClone > args)
Definition: GUIManager.cpp:116
void TickObjects()
See CGUI::TickObjects; applies to all loaded pages.
Definition: GUIManager.cpp:398
i64 Status
Error handling system.
Definition: status.h:171
const CParamNode & GetTemplate(const std::string &templateName)
Retrieve the requested template, used for displaying faction specificities.
Definition: GUIManager.cpp:436
void DisplayMessageBox(int width, int height, const CStrW &title, const CStrW &message)
Display a modal message box with an "OK" button.
Definition: GUIManager.cpp:164
void ResetCursor()
Sets the default mouse pointer.
Definition: GUIManager.cpp:305
CStrW callbackPageName
Definition: GUIManager.h:165
NONCOPYABLE(CGUIManager)
CGUIManager * g_GUI
Definition: GUIManager.cpp:33
bool GetPreDefinedColor(const CStr &name, CColor &output) const
See CGUI::GetPreDefinedColor; applies to the currently active page.
Definition: GUIManager.cpp:378
void SendEventToAll(const CStr &eventName) const
See CGUI::SendEventToAll; applies to the currently active page.
Definition: GUIManager.cpp:393
Definition: GUIbase.h:144
void PushPage(const CStrW &pageName, shared_ptr< ScriptInterface::StructuredClone > initData)
Load a new GUI page and make it active.
Definition: GUIManager.cpp:95
shared_ptr< ScriptRuntime > m_ScriptRuntime
Definition: GUIManager.h:175
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptInterface.h:71
void Draw()
See CGUI::Draw; applies to all loaded pages.
Definition: GUIManager.cpp:417
shared_ptr< CGUI > gui
Definition: GUIManager.h:167
void SwitchPage(const CStrW &name, ScriptInterface *srcScriptInterface, JS::HandleValue initData)
Load a new GUI page and make it active.
Definition: GUIManager.cpp:82
shared_ptr< CGUI > top() const
Definition: GUIManager.cpp:448
External interface to the GUI system.
Definition: GUIManager.h:47
shared_ptr< CGUI > m_CurrentGUI
Definition: GUIManager.h:174
CTemplateLoader m_TemplateLoader
Definition: GUIManager.h:181
CGUIManager()
Definition: GUIManager.cpp:57
bool HasPages()
Returns whether there are any current pages.
Definition: GUIManager.cpp:77
shared_ptr< ScriptInterface::StructuredClone > initData
Definition: GUIManager.h:164
Status ReloadChangedFile(const VfsPath &path)
Called when a file has been modified, to hotload changes.
Definition: GUIManager.cpp:283
InReaction HandleEvent(const SDL_Event_ *ev)
Pass input events to the currently active GUI page.
Definition: GUIManager.cpp:334
InReaction gui_handler(const SDL_Event_ *ev)
Definition: GUIManager.cpp:46
shared_ptr< CGUI > GetActiveGUI()
Definition: GUIManager.h:59