Debian Codegraph @

include/igui.h

raw
1 #pragma once
2
3 #include <vector>
4 #include <string>
5 #include <memory>
6 #include <map>
7
8 #include <sigc++/signal.h>
9 #include <sigc++/connection.h>
10 #include "imodule.h"
11 #include "math/Vector4.h"
12 #include "string/convert.h"
13 #include "ishaders.h"
14
15 namespace gui
16 {
17
18 class IGui;
19
20 class IRenderableText
21 {
22 public:
23 virtual ~IRenderableText() {}
24
25 // Submits the openGL calls
26 virtual void render() = 0;
27
28 // Re-construct this structure, called when the text in the owning windowDef has been changed
29 virtual void recompile() = 0;
30 };
31
32 // An expression as encountered in the GUI code
33 // Evaluates to an instance of a certain type (bool, string, etc.)
34 template<typename ValueType>
35 class IGuiExpression
36 {
37 public:
38 // Shared ptr typedef
39 typedef std::shared_ptr<IGuiExpression<ValueType>> Ptr;
40
41 virtual ~IGuiExpression() {}
42
43 // Evaluate this expression to retrieve the result
44 virtual ValueType evaluate() = 0;
45
46 // Value changed signal
47 virtual sigc::signal<void>& signal_valueChanged() = 0;
48 };
49
50 // An expression representing a constant value
51 template<typename ValueType>
52 class ConstantExpression :
53 public IGuiExpression<ValueType>
54 {
55 private:
56 ValueType _value;
57
58 sigc::signal<void> _sigValueChanged;
59
60 public:
61 ConstantExpression(const ValueType& value) :
62 _value(value)
63 {}
64
65 virtual ValueType evaluate() override
66 {
67 return _value;
68 }
69
70 // Provide a value changed signal, though it's never invoked
71 sigc::signal<void>& signal_valueChanged() override
72 {
73 return _sigValueChanged;
74 }
75
76 template<typename OtherType>
77 static std::shared_ptr<ConstantExpression<ValueType>> Create(const OtherType& value)
78 {
79 return std::make_shared<ConstantExpression<ValueType>>(value);
80 }
81 };
82
83 // Represents a variable or property of a GuiWindowDef
84 // e.g. "text", "notime", "forecolor" or a user-defined variable
85 class IWindowVariable
86 {
87 private:
88 sigc::signal<void> _changedSignal;
89
90 public:
91 virtual ~IWindowVariable() {}
92
93 // value-changed signal, to get notified when this value changes
94 sigc::signal<void>& signal_variableChanged()
95 {
96 return _changedSignal;
97 }
98
99 // To be implemented by subclasses
100 virtual void setValueFromString(const std::string& stringVal) = 0;
101 };
102
103 // Represents a GUI property carrying a certain type
104 // e.g. "text" (std::string), "rect" (Vector4), "textscale" (float), ...
105 template<typename ValueType>
106 class WindowVariable :
107 public IWindowVariable
108 {
109 protected:
110 // Types used by this window variable
111 typedef IGuiExpression<ValueType> ExpressionType;
112 typedef std::shared_ptr<ExpressionType> ExpressionTypePtr;
113
114 // The expression which can be evaluated
115 ExpressionTypePtr _expression;
116
117 sigc::connection _exprChangedSignal;
118
119 public:
120 typedef std::shared_ptr<WindowVariable<ValueType>> Ptr; // smart ptr typedef
121
122 operator ValueType() const
123 {
124 return getValue();
125 }
126
127 virtual ValueType getValue() const
128 {
129 return _expression ? _expression->evaluate() : ValueType();
130 }
131
132 // Assigns a new Expression to this variable. The expression needs
133 // to match the ValueType of this variable
134 virtual void setValue(const ExpressionTypePtr& newExpr)
135 {
136 if (_expression == newExpr) return;
137
138 // Disconnect from any previously subscribed signals
139 _exprChangedSignal.disconnect();
140
141 _expression = newExpr;
142
143 signal_variableChanged().emit();
144
145 // Subscribe to this new expression's changed signal
146 if (_expression)
147 {
148 _expression->signal_valueChanged().connect([this]()
149 {
150 signal_variableChanged().emit();
151 });
152 }
153 }
154
155 // Assigns a constant value to this variable
156 virtual void setValue(const ValueType& constantValue)
157 {
158 // Disconnect from any previously subscribed signals
159 _exprChangedSignal.disconnect();
160
161 _expression = ConstantExpression<ValueType>::Create(constantValue);
162
163 signal_variableChanged().emit();
164
165 // Since this is a constant expression, we don't need to subscribe to any signal
166 }
167
168 // Implement the required string->ValueType conversion by means of string::convert<>
169 virtual void setValueFromString(const std::string& stringVal) override
170 {
171 ValueType converted = string::convert<ValueType>(stringVal);
172 setValue(converted);
173 }
174 };
175
176 class IGuiWindowDef;
177 typedef std::shared_ptr<IGuiWindowDef> IGuiWindowDefPtr;
178
179 class IGuiWindowDef
180 {
181 public:
182 // Public properties
183
184 // The name of this windowDef
185 std::string name;
186
187 // Window size (x,y,width,height)
188 WindowVariable<Vector4> rect;
189
190 // Visible or hidden
191 WindowVariable<bool> visible;
192
193 // The body text of this window
194 WindowVariable<std::string> text;
195
196 // Whether this gui is full screen (use on desktop window)
197 WindowVariable<bool> menugui;
198
199 WindowVariable<Vector4> forecolor;
200 WindowVariable<Vector4> hovercolor;
201 WindowVariable<Vector4> backcolor;
202 WindowVariable<Vector4> bordercolor;
203 WindowVariable<float> bordersize;
204 WindowVariable<Vector4> matcolor;
205
206 WindowVariable<float> rotate;
207
208 // background shader name
209 WindowVariable<std::string> background;
210
211 // background shader (NULL until realised)
212 MaterialPtr backgroundShader;
213
214 // The name of the font
215 WindowVariable<std::string> font;
216
217 // The scale for rendering the font
218 WindowVariable<float> textscale;
219
220 // The text alignment (left, right, center)
221 WindowVariable<int> textalign;
222
223 // Text offsets
224 WindowVariable<float> textalignx;
225 WindowVariable<float> textaligny;
226
227 // Force a specific aspect ratio
228 WindowVariable<float> forceaspectwidth;
229 WindowVariable<float> forceaspectheight;
230
231 // No mouse events for this window
232 WindowVariable<bool> noevents;
233
234 // Whether this window forces text to wrap at their borders
235 WindowVariable<bool> noclip;
236
237 // Whether time is running for this windowDef
238 WindowVariable<bool> notime;
239
240 // Don't display the cursor
241 WindowVariable<bool> nocursor;
242
243 // Don't wrap words at rectangle borders
244 WindowVariable<bool> nowrap;
245
246 // The window time (0..infinity)
247 std::size_t time;
248
249 // Each window can define its own set of named variables,
250 // which can be of type float or Vector4
251 typedef std::shared_ptr<IWindowVariable> IWindowVariablePtr;
252 typedef std::map<std::string, IWindowVariablePtr> NamedVariables;
253 NamedVariables variables;
254
255 // All child windowDefs of this window
256 typedef std::vector<IGuiWindowDefPtr> ChildWindows;
257 ChildWindows children;
258
259 public:
260 virtual ~IGuiWindowDef() {}
261
262 // Returns the owning GUI
263 virtual IGui& getGui() const = 0;
264
265 virtual void addWindow(const IGuiWindowDefPtr& window) = 0;
266
267 // Recursively looks for a named child windowDef
268 // Returns NULL if not found
269 virtual IGuiWindowDefPtr findWindowDef(const std::string& name) = 0;
270
271 // Get the renderable text object containing the OpenGLRenderables
272 virtual IRenderableText& getRenderableText() = 0;
273
274 /**
275 * greebo: This is some sort of "think" method, giving this windowDef
276 * a chance to handle timed events.
277 *
278 * @updateChildren: recursively updates child windowDef if true
279 */
280 virtual void update(const std::size_t timeStep, bool updateChildren = true) = 0;
281
282 // Initialises the time of this windowDef and all children
283 virtual void initTime(const std::size_t time, bool updateChildren = true) = 0;
284
285 // Prepares renderable objects, to be called by the parent Gui only
286 virtual void pepareRendering(bool prepareChildren = true) = 0;
287
288 // Returns a reference to the named variable, or throws a std::invalid_argument exception
289 virtual IWindowVariable& findVariableByName(const std::string& name) = 0;
290 };
291
292 /**
293 * greebo: This class represents a single D3 GUI. It holds all
294 * the windowDefs and the source code behind.
295 */
296 class IGui
297 {
298 public:
299 virtual ~IGui() {}
300
301 virtual const IGuiWindowDefPtr& getDesktop() const = 0;
302 virtual void setDesktop(const IGuiWindowDefPtr& newDesktop) = 0;
303
304 // Sets the given state variable (gui::<key> = <value>)
305 virtual void setStateString(const std::string& key, const std::string& value) = 0;
306
307 // Retrieve a changed signal for the given key, which is invoked
308 // whenever setStateString() is called
309 virtual sigc::signal<void>& getChangedSignalForState(const std::string& key) = 0;
310
311 // Returns the state string "gui::<key>" or an empty string if non-existent
312 virtual std::string getStateString(const std::string& key) = 0;
313
314 // Sets up the time of the entire GUI (all windowDefs)
315 virtual void initTime(const std::size_t time) = 0;
316
317 // "Think" routine, advances all active windowDefs (where notime == false)
318 virtual void update(const std::size_t timestep) = 0;
319
320 // Returns a reference to the named windowDef, returns NULL if not found
321 virtual IGuiWindowDefPtr findWindowDef(const std::string& name) = 0;
322
323 // Called by the GuiRenderer to re-compile text VBOs, etc.
324 virtual void pepareRendering() = 0;
325 };
326 typedef std::shared_ptr<IGui> IGuiPtr;
327
328 enum GuiType
329 {
330 NOT_LOADED_YET, // no attempt to load the GUI has been made
331 UNDETERMINED, // not checked yet for type
332 ONE_SIDED_READABLE, // 1-sided
333 TWO_SIDED_READABLE, // 2-sided
334 NO_READABLE, // not a readable
335 IMPORT_FAILURE, // failed to load
336 FILE_NOT_FOUND, // file doesn't exist
337 };
338
339 /**
340 * greebo: Interface managing the idTech4 GUI files,
341 * keeping track of all the loaded GUIs,
342 * parsing the .gui files on demand.
343 */
344 class IGuiManager :
345 public RegisterableModule
346 {
347 public:
348 typedef std::vector<std::string> StringList;
349
350 // A visitor class used to traverse all known GUIs by path
351 class Visitor
352 {
353 public:
354 virtual ~Visitor() {}
355
356 virtual void visit(const std::string& guiPath, const GuiType& guiType) = 0;
357 };
358
359 public:
360 virtual ~IGuiManager() {}
361
362 // Gets a GUI from the given VFS path, parsing it on demand
363 // Returns NULL if the GUI couldn't be found or loaded.
364 virtual IGuiPtr getGui(const std::string& guiPath) = 0;
365
366 // Returns the number of known GUIs (or GUI paths)
367 virtual std::size_t getNumGuis() = 0;
368
369 // Traverse all known GUIs using the given Visitor
370 virtual void foreachGui(Visitor& visitor) = 0;
371
372 // Returns the GUI appearance type for the given GUI path
373 virtual GuiType getGuiType(const std::string& guiPath) = 0;
374
375 // Reload the gui
376 virtual void reloadGui(const std::string& guiPath) = 0;
377
378 // Returns the _errorList for use in a GUI.
379 virtual const StringList& getErrorList() = 0;
380
381 // Clears out the GUIs and reloads them
382 virtual void reloadGuis() = 0;
383 };
384
385 }
386
387 const char* const MODULE_GUIMANAGER("GuiManager");
388
389 // Application-wide Accessor to the global GUI manager
390 inline gui::IGuiManager& GlobalGuiManager()
391 {
392 static module::InstanceReference<gui::IGuiManager> _reference(MODULE_GUIMANAGER);
393 return _reference;
394 }