Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CGUI.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/*
19 * This is the top class of the whole GUI, all objects
20 * and settings are stored within this class.
21 */
22
23#ifndef INCLUDED_CGUI
24#define INCLUDED_CGUI
25
26#include "gui/GUITooltip.h"
27#include "gui/IGUIScrollBar.h"
28#include "gui/SGUIIcon.h"
29#include "gui/SGUIMessage.h"
30#include "gui/SGUIStyle.h"
32#include "lib/code_annotation.h"
34#include "maths/Rect.h"
35#include "maths/Vector2D.h"
36#include "ps/CStr.h"
39#include "ps/Input.h"
40
41#include <js/RootingAPI.h>
42#include <js/TypeDecls.h>
43#include <js/ValueArray.h>
44#include <map>
45#include <memory>
46#include <optional>
47#include <string>
48#include <string_view>
49#include <unordered_map>
50#include <unordered_set>
51#include <utility>
52#include <vector>
53
54class CCanvas2D;
55class CGUISprite;
57class CSize2D;
58class GUIProxyProps;
59class IGUIObject;
60class JSObject;
61class XMBData;
62class XMBElement;
63namespace JS { class HandleValueArray; }
64namespace JS { class Value; }
65namespace js { class BaseProxyHandler; }
66namespace Script { class Context; }
67namespace Script { class Interface; }
68namespace Script { class Request; }
69struct SGUIImageEffects;
70union SDL_Event;
71
72extern const double SELECT_DBLCLICK_RATE;
73
74using map_pObjects = std::map<CStr, std::unique_ptr<IGUIObject>>;
75
76/**
77 * The main object that represents a whole GUI page.
78 */
79class CGUI
80{
82
83private:
84 // Private typedefs
85 using ConstructObjectFunction = std::unique_ptr<IGUIObject> (*)(CGUI&);
86
87public:
88 CGUI(Script::Context& context);
89 ~CGUI();
90
91 /**
92 * Informs the GUI page which GUI object types may be constructed from XML.
93 */
94 void AddObjectTypes();
95
96 JS::Value GetHotloadData(const Script::Request& rq);
97
98 JSObject* CallPageInit(const Script::Request& rq, Script::StructuredClone initDataVal,
99 JS::HandleValue hotloadDataVal, const std::string_view scriptName);
100
101 /**
102 * Performs processing that should happen every frame
103 * (including sending the "Tick" event to scripts)
104 */
105 JSObject* TickObjects(const Script::Request& rq, Script::StructuredClone initData,
106 const std::string_view scriptName);
107
108 /**
109 * Sends a specified script event to every object
110 *
111 * @param eventName String representation of event name
112 * @param paramData JS::HandleValueArray storing the arguments passed to the event handler.
113 */
114 void SendEventToAll(const CStr& eventName,
115 const JS::HandleValueArray paramData = JS::HandleValueArray::empty());
116
117 /**
118 * Displays the whole GUI
119 */
120 void Draw(CCanvas2D& canvas);
121
122 /**
123 * Draw GUI Sprite
124 *
125 * @param Sprite Object referring to the sprite (which also caches
126 * calculations for faster rendering)
127 * @param Canvas Canvas to draw on
128 * @param Rect Position and Size
129 * @param Clipping The sprite shouldn't be drawn outside this rectangle
130 */
131 void DrawSprite(const CGUISpriteInstance& Sprite, CCanvas2D& canvas, const CRect& Rect, const CRect& Clipping = CRect());
132
133 /**
134 * The replacement of Process(), handles an SDL_Event
135 *
136 * @param ev SDL Event, like mouse/keyboard input
137 */
138 Input::Reaction HandleEvent(const SDL_Event& ev);
139
140 /**
141 * Load a GUI XML file into the GUI.
142 *
143 * <b>VERY IMPORTANT!</b> All <styles>-files must be read before
144 * everything else!
145 *
146 * @param Filename Name of file
147 * @param Paths Set of paths; all XML and JS files loaded will be added to this
148 */
149 void LoadXmlFile(const VfsPath& Filename, std::unordered_set<VfsPath>& Paths);
150
151 /**
152 * Called after all XML files linked in the page file were loaded.
153 */
154 void LoadedXmlFiles();
155
156 /**
157 * Allows the JS side to modify the hotkey setting assigned to a GUI object.
158 */
159 void SetObjectHotkey(IGUIObject& object, const CStr& hotkeyTag);
160 void UnsetObjectHotkey(IGUIObject& object, const CStr& hotkeyTag);
161
162 /**
163 * Allows the JS side to modify the style setting assigned to a GUI object.
164 */
165 void SetObjectStyle(IGUIObject& object, const CStr& styleName);
166 void UnsetObjectStyle(IGUIObject& object);
167
168 /**
169 * Allows the JS side to add or remove global hotkeys.
170 */
171 void SetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName, JS::HandleValue function);
172 void UnsetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName);
173
174 /**
175 * Return the object which is an ancestor of every other GUI object.
176 */
178
179 /**
180 * Checks if object exists and return true or false accordingly
181 *
182 * @param Name String name of object
183 * @return true if object exists
184 */
185 bool ObjectExists(const CStr& Name) const;
186
187 /**
188 * Returns the GUI object with the desired name, or nullptr
189 * if no match is found.
190 *
191 * @param Name String name of object
192 * @return Matching object, or nullptr
193 */
194 IGUIObject* TryFindObjectByName(const CStr& Name) const;
195
196 /**
197 * Returns the GUI object with the desired name, or nullptr
198 * and logs an error if no match is found.
199 * .
200 *
201 * @param Name String name of object
202 * @return Matching object, or nullptr
203 */
204 IGUIObject* FindObjectByName(const CStr& Name) const;
205
206 /**
207 * Returns the GUI object under the mouse, or nullptr if none.
208 */
210
211 /**
212 * Returns the current screen coordinates of the cursor.
213 */
214 const CVector2D& GetMousePos() const { return m_MousePos; }
215
216 /**
217 * Returns the currently pressed mouse buttons.
218 */
219 const unsigned int& GetMouseButtons() { return m_MouseButtons; }
220
221 const SGUIScrollBarStyle* GetScrollBarStyle(const CStr& style) const;
222
223 /**
224 * Returns the current GUI window size.
225 */
226 CSize2D GetWindowSize() const;
227
228 /**
229 * The GUI needs to have all object types inputted and
230 * their constructors. Also it needs to associate a type
231 * by a string name of the type.
232 *
233 * To add a type:
234 * @code
235 * AddObjectType("button", &CButton::ConstructObject);
236 * @endcode
237 *
238 * @param str Reference name of object type
239 * @param pFunc Pointer of function ConstuctObject() in the object
240 *
241 * @see CGUI#ConstructObject()
242 */
243 void AddObjectType(const CStr& str, ConstructObjectFunction pFunc) { m_ObjectTypes[str] = pFunc; }
244
245 /**
246 * Update Resolution, should be called every time the resolution
247 * of the OpenGL screen has been changed, this is because it needs
248 * to re-cache all its actual sizes
249 *
250 * Needs no input since screen resolution is global.
251 *
252 * @see IGUIObject#HandleSizeChanged()
253 */
254 void UpdateResolution();
255
256 /**
257 * Check if an icon exists
258 */
259 bool HasIcon(const CStr& name) const { return (m_Icons.find(name) != m_Icons.end()); }
260
261 /**
262 * Get Icon (a const reference, can never be changed)
263 */
264 const SGUIIcon& GetIcon(const CStr& name) const { return m_Icons.at(name); }
265
266 /**
267 * Log a warning when text markup refers to an icon that doesn't exist.
268 * Mods may reference icons dynamically (e.g. an icon name
269 * built from a civ or resource identifier) that can be temporarily
270 * missing while dependent mods are updated together; each distinct
271 * missing icon name is only logged once per GUI page to avoid log spam.
272 */
273 void ReportMissingIcon(const CStr& name) const;
274
275 /**
276 * Check if a style exists
277 */
278 bool HasStyle(const CStr& name) const { return (m_Styles.find(name) != m_Styles.end()); }
279
280 /**
281 * Get Style if it exists, otherwise throws an exception.
282 */
283 const SGUIStyle& GetStyle(const CStr& name) const { return m_Styles.at(name); }
284
285 /**
286 * Check if a predefined color of that name exists.
287 */
288 bool HasPreDefinedColor(const CStr& name) const { return (m_PreDefinedColors.find(name) != m_PreDefinedColors.end()); }
289
290 /**
291 * Resolve the predefined color if it exists, otherwise throws an exception.
292 */
293 const CGUIColor& GetPreDefinedColor(const CStr& name) const { return m_PreDefinedColors.at(name); }
294
295 GUIProxyProps* GetProxyData(const js::BaseProxyHandler* ptr) { return m_ProxyData.at(ptr).get(); }
296
297 std::shared_ptr<Script::Interface> GetScriptInterface() { return m_ScriptInterface; };
298
299private:
300 /**
301 * The CGUI takes ownership of the child object and links the parent with the child.
302 * Returns false on failure to take over ownership of the child object.
303 */
304 void AddObject(IGUIObject& parent, std::unique_ptr<IGUIObject> child);
305
306 /**
307 * You input the name of the object type, and let's
308 * say you input "button", then it will construct a
309 * CGUIObjet* as a CButton.
310 *
311 * @param str Name of object type
312 * @return Newly constructed IGUIObject (but constructed as a subclass)
313 */
314 std::unique_ptr<IGUIObject> ConstructObject(const CStr& str);
315
316public:
317 /**
318 * Get Focused Object.
319 */
321
322 /**
323 * Change focus to new object.
324 * Will send LOST_FOCUS/GOT_FOCUS messages as appropriate.
325 * pObject can be nullptr to remove all focus.
326 */
327 void SetFocusedObject(IGUIObject* pObject);
328
329 /**
330 * Alert the focussed object of this GUIPage that the focus of the page has changed.
331 */
333
334 /**
335 * Reads a string value and modifies the given value of type T if successful.
336 * Does not change the value upon conversion failure.
337 *
338 * @param pGUI The GUI page which may contain data relevant to the parsing
339 * (for example predefined colors).
340 * @param Value The value in string form, like "0 0 100% 100%"
341 * @param tOutput Parsed value of type T
342 * @return True at success.
343 */
344 template <typename T>
345 static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput);
346
347private:
348
349 //--------------------------------------------------------
350 /** @name XML Reading Xeromyces specific subroutines
351 *
352 * These does not throw!
353 * Because when reading in XML files, it won't be fatal
354 * if an error occurs, perhaps one particular object
355 * fails, but it'll still continue reading in the next.
356 * All Error are reported with ReportParseError
357 */
358 //--------------------------------------------------------
359
360 /*
361 Xeromyces_* functions tree
362 <objects> (ReadRootObjects)
363 |
364 +-<script> (ReadScript)
365 |
366 +-<object> (ReadObject)
367 |
368 +-<action>
369 |
370 +-Optional Type Extensions (IGUIObject::ReadExtendedElement) TODO
371 |
372 +-<<object>> *recursive*
373
374
375 <styles> (ReadRootStyles)
376 |
377 +-<style> (ReadStyle)
378
379
380 <sprites> (ReadRootSprites)
381 |
382 +-<sprite> (ReadSprite)
383 |
384 +-<image> (ReadImage)
385
386
387 <setup> (ReadRootSetup)
388 |
389 +-<tooltip> (ReadToolTip)
390 |
391 +-<scrollbar> (ReadScrollBar)
392 |
393 +-<icon> (ReadIcon)
394 |
395 +-<color> (ReadColor)
396 */
397 //@{
398
399 // Read Roots
400
401 /**
402 * Reads in the root element <objects> (the DOMElement).
403 *
404 * @param Element The Xeromyces object that represents
405 * the objects-tag.
406 * @param pFile The Xeromyces object for the file being read
407 * @param Paths Collects the set of all XML/JS files that are loaded
408 *
409 * @see LoadXmlFile()
410 */
411 void Xeromyces_ReadRootObjects(const XMBData& xmb, XMBElement element,
412 std::unordered_set<VfsPath>& Paths);
413
414 /**
415 * Reads in the root element <sprites> (the DOMElement).
416 *
417 * @param Element The Xeromyces object that represents
418 * the sprites-tag.
419 * @param pFile The Xeromyces object for the file being read
420 *
421 * @see LoadXmlFile()
422 */
423 void Xeromyces_ReadRootSprites(const XMBData& xmb, XMBElement element);
424
425 /**
426 * Reads in the root element <styles> (the DOMElement).
427 *
428 * @param Element The Xeromyces object that represents
429 * the styles-tag.
430 * @param pFile The Xeromyces object for the file being read
431 *
432 * @see LoadXmlFile()
433 */
434 void Xeromyces_ReadRootStyles(const XMBData& xmb, XMBElement element);
435
436 /**
437 * Reads in the root element <setup> (the DOMElement).
438 *
439 * @param Element The Xeromyces object that represents
440 * the setup-tag.
441 * @param pFile The Xeromyces object for the file being read
442 *
443 * @see LoadXmlFile()
444 */
445 void Xeromyces_ReadRootSetup(const XMBData& xmb, XMBElement element);
446
447 // Read Subs
448
449 /**
450 * Notice! Recursive function!
451 *
452 * Read in an <object> (the XMBElement) and stores it
453 * as a child in the pParent.
454 *
455 * It will also check the object's children and call this function
456 * on them too. Also it will call all other functions that reads
457 * in other stuff that can be found within an object. Check the
458 * tree in the beginning of this class' Xeromyces_* section.
459 *
460 * @param Element The Xeromyces object that represents
461 * the object-tag.
462 * @param pFile The Xeromyces object for the file being read
463 * @param pParent Parent to add this object as child in.
464 * @param NameSubst A set of substitution strings that will be
465 * applied to all object names within this object.
466 * @param Paths Output set of file paths that this GUI object
467 * relies on.
468 *
469 * @see LoadXmlFile()
470 */
471 void Xeromyces_ReadObject(const XMBData& xmb, XMBElement element, IGUIObject& parent,
472 std::vector<std::pair<CStr, CStr> >& NameSubst, std::unordered_set<VfsPath>& Paths,
473 std::uint32_t nesting_depth);
474
475 /**
476 * Reads in the element <repeat>, which repeats its child <object>s
477 * 'count' times, replacing the string "[n]" (or the value of the attribute
478 * 'var' enclosed in square brackets) in its descendants' names with "[0]",
479 * "[1]", etc.
480 */
481 void Xeromyces_ReadRepeat(const XMBData& xmb, XMBElement element, IGUIObject& parent,
482 std::vector<std::pair<CStr, CStr> >& NameSubst, std::unordered_set<VfsPath>& Paths,
483 std::uint32_t nesting_depth);
484
485 /**
486 * Reads in the element <script> (the XMBElement) and executes
487 * the script's code.
488 *
489 * @param Element The Xeromyces object that represents
490 * the script-tag.
491 * @param pFile The Xeromyces object for the file being read
492 * @param Paths Output set of file paths that this script is loaded from.
493 *
494 * @see LoadXmlFile()
495 */
496 void Xeromyces_ReadScript(const XMBData& xmb, XMBElement element, std::unordered_set<VfsPath>& Paths);
497
498 /**
499 * Reads in the element <sprite> (the XMBElement) and stores the
500 * result in a new CGUISprite.
501 *
502 * @param Element The Xeromyces object that represents
503 * the sprite-tag.
504 * @param pFile The Xeromyces object for the file being read
505 *
506 * @see LoadXmlFile()
507 */
508 void Xeromyces_ReadSprite(const XMBData& xmb, XMBElement element);
509
510 /**
511 * Reads in the element <image> (the XMBElement) and stores the
512 * result within the CGUISprite.
513 *
514 * @param Element The Xeromyces object that represents
515 * the image-tag.
516 * @param pFile The Xeromyces object for the file being read
517 * @param parent Parent sprite.
518 *
519 * @see LoadXmlFile()
520 */
521 void Xeromyces_ReadImage(const XMBData& xmb, XMBElement element, CGUISprite& parent);
522
523 /**
524 * Reads in the element <effect> (the XMBElement) and stores the
525 * result within the SGUIImageEffects.
526 *
527 * @param Element The Xeromyces object that represents
528 * the image-tag.
529 * @param pFile The Xeromyces object for the file being read
530 * @param effects Effects object to add this effect to.
531 *
532 * @see LoadXmlFile()
533 */
534 void Xeromyces_ReadEffects(const XMBData& xmb, XMBElement element, SGUIImageEffects& effects);
535
536 /**
537 * Reads in the element <style> (the XMBElement) and stores the
538 * result in m_Styles.
539 *
540 * @param Element The Xeromyces object that represents
541 * the style-tag.
542 * @param pFile The Xeromyces object for the file being read
543 *
544 * @see LoadXmlFile()
545 */
546 void Xeromyces_ReadStyle(const XMBData& xmb, XMBElement element);
547
548 /**
549 * Reads in the element <scrollbar> (the XMBElement) and stores the
550 * result in m_ScrollBarStyles.
551 *
552 * @param Element The Xeromyces object that represents
553 * the scrollbar-tag.
554 * @param pFile The Xeromyces object for the file being read
555 *
556 * @see LoadXmlFile()
557 */
558 void Xeromyces_ReadScrollBarStyle(const XMBData& xmb, XMBElement element);
559
560 /**
561 * Reads in the element <icon> (the XMBElement) and stores the
562 * result in m_Icons.
563 *
564 * @param Element The Xeromyces object that represents
565 * the scrollbar-tag.
566 * @param pFile The Xeromyces object for the file being read
567 *
568 * @see LoadXmlFile()
569 */
570 void Xeromyces_ReadIcon(const XMBData& xmb, XMBElement element);
571
572 /**
573 * Reads in the element <tooltip> (the XMBElement) and stores the
574 * result as an object with the name __tooltip_#.
575 *
576 * @param Element The Xeromyces object that represents
577 * the scrollbar-tag.
578 * @param pFile The Xeromyces object for the file being read
579 *
580 * @see LoadXmlFile()
581 */
582 void Xeromyces_ReadTooltip(const XMBData& xmb, XMBElement element);
583
584 /**
585 * Reads in the element <color> (the XMBElement) and stores the
586 * result in m_PreDefinedColors
587 *
588 * @param Element The Xeromyces object that represents
589 * the scrollbar-tag.
590 * @param pFile The Xeromyces object for the file being read
591 *
592 * @see LoadXmlFile()
593 */
594 void Xeromyces_ReadColor(const XMBData& xmb, XMBElement element);
595
596 //@}
597
598private:
599
600 // Variables
601
602 //--------------------------------------------------------
603 /** @name Miscellaneous */
604 //--------------------------------------------------------
605 //@{
606
607 std::shared_ptr<Script::Interface> m_ScriptInterface;
608
609 /**
610 * don't want to pass this around with the
611 * ChooseMouseOverAndClosest broadcast -
612 * we'd need to pack this and pNearest in a struct
613 */
615
616 /**
617 * Indicates which buttons are pressed (bit 0 = LMB,
618 * bit 1 = RMB, bit 2 = MMB)
619 */
620 unsigned int m_MouseButtons;
621
622 // Tooltip
624
625 //@}
626 //--------------------------------------------------------
627 /** @name Objects */
628 //--------------------------------------------------------
629 //@{
630
631 /**
632 * Base Object, all its children are considered parentless
633 * because this is not a real object per se.
634 */
635 std::unique_ptr<IGUIObject> m_BaseObject;
636
637 /**
638 * Focused object!
639 * Say an input box that is selected. That one is focused.
640 * There can only be one focused object.
641 */
643
644 /**
645 * Just pointers for fast name access, each object
646 * is really constructed within its parent for easy
647 * recursive management.
648 * Notice m_BaseObject won't belong here since it's
649 * not considered a real object.
650 */
652
653 /**
654 * Number of object that has been given name automatically.
655 * the name given will be '__internal(#)', the number (#)
656 * being this variable. When an object's name has been set
657 * as followed, the value will increment.
658 */
660
661 /**
662 * Function pointers to functions that constructs
663 * IGUIObjects by name... For instance m_ObjectTypes["button"]
664 * is filled with a function that will "return new CButton();"
665 */
666 std::map<CStr, ConstructObjectFunction> m_ObjectTypes;
667
668 /**
669 * This is intended to store the JSFunction when accessing certain properties.
670 * The problem is that these functions are per-scriptInterface, and proxy handlers aren't.
671 * So we know what we want to store, but we don't really have anywhere to store it.
672 * It would be simpler to recreate the functions on every JS call, but that is slower
673 * (this may or may not matter now and in the future).
674 * It's not a great solution, but I can't find a better one at the moment.
675 * An alternative would be to store these on the proxy's prototype,
676 * but that embarks a lot of un-necessary code.
677 */
678 std::unordered_map<const js::BaseProxyHandler*, std::unique_ptr<GUIProxyProps>> m_ProxyData;
679
680 /**
681 * Map from hotkey names to objects that listen to the hotkey.
682 * (This is an optimisation to avoid recursing over the whole GUI
683 * tree every time a hotkey is pressed).
684 */
685 std::map<CStr, std::vector<IGUIObject*> > m_HotkeyObjects;
686
687 /**
688 * Map from hotkey names to maps of eventNames to functions that are triggered
689 * when the hotkey goes through the event. Contrary to object hotkeys, this
690 * allows for only one global function per hotkey name per event type.
691 */
692 std::map<CStr, std::map<CStr, JS::PersistentRootedValue>> m_GlobalHotkeys;
693
694 /**
695 * XML and JS can subscribe handlers to events identified by these names.
696 * Store in static const members to avoid string copies, gain compile errors when misspelling and
697 * to allow reuse in other classes.
698 */
699 static const CStr EventNameLoad;
700 static const CStr EventNameTick;
701 static const CStr EventNamePress;
702 static const CStr EventNameKeyDown;
703 static const CStr EventNameRelease;
704 static const CStr EventNameMouseRightPress;
705 static const CStr EventNameMouseLeftPress;
706 static const CStr EventNameMouseWheelDown;
707 static const CStr EventNameMouseWheelUp;
708 static const CStr EventNameMouseWheelLeft;
709 static const CStr EventNameMouseWheelRight;
711 static const CStr EventNameMouseLeftRelease;
713 static const CStr EventNameMouseRightRelease;
714
715 //--------------------------------------------------------
716 // Databases
717 // These are loaded from XML files and marked as noncopyable and const to
718 // rule out unintentional modification and copy, especially during Draw calls.
719 //--------------------------------------------------------
720
721 // Colors
722 std::map<CStr, const CGUIColor> m_PreDefinedColors;
723
724 // Sprites
725 std::map<CStr, std::unique_ptr<const CGUISprite>> m_Sprites;
726
727 // Styles
728 std::map<CStr, const SGUIStyle> m_Styles;
729
730 // Scroll-bar styles
731 std::map<CStr, const SGUIScrollBarStyle> m_ScrollBarStyles;
732
733 // Icons
734 std::map<CStr, const SGUIIcon> m_Icons;
735
736 // Icons that have already been reported as missing, to avoid repeating
737 // the same warning every time referencing text is regenerated.
738 mutable std::unordered_set<CStr> m_MissingIconsReported;
739
740public:
742 {
743 ModuleArtifact(const Script::Request& rq, VfsPath filename);
744
747 JS::PersistentRootedObject moduleNamespace;
748 };
749 std::optional<ModuleArtifact> m_LoadModuleResult;
750
751 /**
752 * Map from event names to object which listen to a given event.
753 */
754 std::unordered_map<CStr, std::vector<IGUIObject*>> m_EventObjects;
755};
756
757#endif // INCLUDED_CGUI
std::map< CStr, std::unique_ptr< IGUIObject > > map_pObjects
Definition: CGUI.h:74
const double SELECT_DBLCLICK_RATE
Definition: CGUI.cpp:73
EGUIMessageType
Message types.
Definition: SGUIMessage.h:29
Definition: Canvas2D.h:36
Definition: CGUISprite.h:137
The GUI sprite, is actually several real sprites (images) like a collage.
Definition: CGUISprite.h:116
The main object that represents a whole GUI page.
Definition: CGUI.h:80
void ReportMissingIcon(const CStr &name) const
Log a warning when text markup refers to an icon that doesn't exist.
Definition: CGUI.cpp:1379
std::unordered_map< const js::BaseProxyHandler *, std::unique_ptr< GUIProxyProps > > m_ProxyData
This is intended to store the JSFunction when accessing certain properties.
Definition: CGUI.h:678
bool ObjectExists(const CStr &Name) const
Checks if object exists and return true or false accordingly.
Definition: CGUI.cpp:432
const SGUIStyle & GetStyle(const CStr &name) const
Get Style if it exists, otherwise throws an exception.
Definition: CGUI.h:283
static const CStr EventNamePress
Definition: CGUI.h:701
std::map< CStr, std::map< CStr, JS::PersistentRootedValue > > m_GlobalHotkeys
Map from hotkey names to maps of eventNames to functions that are triggered when the hotkey goes thro...
Definition: CGUI.h:692
const unsigned int & GetMouseButtons()
Returns the currently pressed mouse buttons.
Definition: CGUI.h:219
void AddObjectType(const CStr &str, ConstructObjectFunction pFunc)
The GUI needs to have all object types inputted and their constructors.
Definition: CGUI.h:243
std::map< CStr, const SGUIIcon > m_Icons
Definition: CGUI.h:734
Input::Reaction HandleEvent(const SDL_Event &ev)
The replacement of Process(), handles an SDL_Event.
Definition: CGUI.cpp:114
std::map< CStr, ConstructObjectFunction > m_ObjectTypes
Function pointers to functions that constructs IGUIObjects by name... For instance m_ObjectTypes["but...
Definition: CGUI.h:666
void Xeromyces_ReadEffects(const XMBData &xmb, XMBElement element, SGUIImageEffects &effects)
Reads in the element <effect> (the XMBElement) and stores the result within the SGUIImageEffects.
Definition: CGUI.cpp:1218
std::map< CStr, std::vector< IGUIObject * > > m_HotkeyObjects
Map from hotkey names to objects that listen to the hotkey.
Definition: CGUI.h:685
CVector2D m_MousePos
don't want to pass this around with the ChooseMouseOverAndClosest broadcast - we'd need to pack this ...
Definition: CGUI.h:614
std::shared_ptr< Script::Interface > m_ScriptInterface
Definition: CGUI.h:607
const SGUIScrollBarStyle * GetScrollBarStyle(const CStr &style) const
Definition: CGUI.cpp:568
JSObject * TickObjects(const Script::Request &rq, Script::StructuredClone initData, const std::string_view scriptName)
Performs processing that should happen every frame (including sending the "Tick" event to scripts)
Definition: CGUI.cpp:339
bool HasIcon(const CStr &name) const
Check if an icon exists.
Definition: CGUI.h:259
GUITooltip m_Tooltip
Definition: CGUI.h:623
static const CStr EventNameMouseWheelRight
Definition: CGUI.h:709
static const CStr EventNameRelease
Definition: CGUI.h:703
void Xeromyces_ReadStyle(const XMBData &xmb, XMBElement element)
Reads in the element <style> (the XMBElement) and stores the result in m_Styles.
Definition: CGUI.cpp:1235
void Xeromyces_ReadImage(const XMBData &xmb, XMBElement element, CGUISprite &parent)
Reads in the element <image> (the XMBElement) and stores the result within the CGUISprite.
Definition: CGUI.cpp:1115
std::map< CStr, const CGUIColor > m_PreDefinedColors
Definition: CGUI.h:722
std::unique_ptr< IGUIObject >(*)(CGUI &) ConstructObjectFunction
Definition: CGUI.h:85
CSize2D GetWindowSize() const
Returns the current GUI window size.
Definition: CGUI.cpp:467
void SendEventToAll(const CStr &eventName, const JS::HandleValueArray paramData=JS::HandleValueArray::empty())
Sends a specified script event to every object.
Definition: CGUI.cpp:364
void Xeromyces_ReadTooltip(const XMBData &xmb, XMBElement element)
Reads in the element <tooltip> (the XMBElement) and stores the result as an object with the name __to...
Definition: CGUI.cpp:1388
IGUIObject * TryFindObjectByName(const CStr &Name) const
Returns the GUI object with the desired name, or nullptr if no match is found.
Definition: CGUI.cpp:437
CGUI(Script::Context &context)
Definition: CGUI.cpp:92
bool HasPreDefinedColor(const CStr &name) const
Check if a predefined color of that name exists.
Definition: CGUI.h:288
std::unique_ptr< IGUIObject > m_BaseObject
Base Object, all its children are considered parentless because this is not a real object per se.
Definition: CGUI.h:635
void Xeromyces_ReadScript(const XMBData &xmb, XMBElement element, std::unordered_set< VfsPath > &Paths)
Reads in the element <script> (the XMBElement) and executes the script's code.
Definition: CGUI.cpp:1016
std::unordered_set< CStr > m_MissingIconsReported
Definition: CGUI.h:738
IGUIObject * GetBaseObject()
Return the object which is an ancestor of every other GUI object.
Definition: CGUI.cpp:427
void UnsetGlobalHotkey(const CStr &hotkeyTag, const CStr &eventName)
Definition: CGUI.cpp:556
static const CStr EventNameMouseRightRelease
Definition: CGUI.h:713
map_pObjects m_pAllObjects
Just pointers for fast name access, each object is really constructed within its parent for easy recu...
Definition: CGUI.h:651
void SetObjectHotkey(IGUIObject &object, const CStr &hotkeyTag)
Allows the JS side to modify the hotkey setting assigned to a GUI object.
Definition: CGUI.cpp:513
void SendFocusMessage(EGUIMessageType msg)
Alert the focussed object of this GUIPage that the focus of the page has changed.
Definition: CGUI.cpp:472
static const CStr EventNameTick
Definition: CGUI.h:700
void Xeromyces_ReadObject(const XMBData &xmb, XMBElement element, IGUIObject &parent, std::vector< std::pair< CStr, CStr > > &NameSubst, std::unordered_set< VfsPath > &Paths, std::uint32_t nesting_depth)
Notice! Recursive function!
Definition: CGUI.cpp:667
void Xeromyces_ReadRootObjects(const XMBData &xmb, XMBElement element, std::unordered_set< VfsPath > &Paths)
Reads in the root element <objects> (the DOMElement).
Definition: CGUI.cpp:618
static const CStr EventNameLoad
XML and JS can subscribe handlers to events identified by these names.
Definition: CGUI.h:699
const CGUIColor & GetPreDefinedColor(const CStr &name) const
Resolve the predefined color if it exists, otherwise throws an exception.
Definition: CGUI.h:293
static const CStr EventNameKeyDown
Definition: CGUI.h:702
static const CStr EventNameMouseWheelDown
Definition: CGUI.h:706
void Xeromyces_ReadRootSprites(const XMBData &xmb, XMBElement element)
Reads in the root element <sprites> (the DOMElement).
Definition: CGUI.cpp:637
void SetFocusedObject(IGUIObject *pObject)
Change focus to new object.
Definition: CGUI.cpp:481
static const CStr EventNameMouseWheelLeft
Definition: CGUI.h:708
static const CStr EventNameMouseLeftPress
Definition: CGUI.h:705
static const CStr EventNameMouseLeftDoubleClick
Definition: CGUI.h:710
void Xeromyces_ReadColor(const XMBData &xmb, XMBElement element)
Reads in the element <color> (the XMBElement) and stores the result in m_PreDefinedColors.
Definition: CGUI.cpp:1406
JS::Value GetHotloadData(const Script::Request &rq)
Definition: CGUI.cpp:301
JSObject * CallPageInit(const Script::Request &rq, Script::StructuredClone initDataVal, JS::HandleValue hotloadDataVal, const std::string_view scriptName)
Definition: CGUI.cpp:310
void LoadXmlFile(const VfsPath &Filename, std::unordered_set< VfsPath > &Paths)
Load a GUI XML file into the GUI.
Definition: CGUI.cpp:580
void UpdateResolution()
Update Resolution, should be called every time the resolution of the OpenGL screen has been changed,...
Definition: CGUI.cpp:394
static const CStr EventNameMouseRightDoubleClick
Definition: CGUI.h:712
void UnsetObjectHotkey(IGUIObject &object, const CStr &hotkeyTag)
Definition: CGUI.cpp:519
NONCOPYABLE(CGUI)
IGUIObject * m_FocusedObject
Focused object! Say an input box that is selected.
Definition: CGUI.h:642
void DrawSprite(const CGUISpriteInstance &Sprite, CCanvas2D &canvas, const CRect &Rect, const CRect &Clipping=CRect())
Draw GUI Sprite.
Definition: CGUI.cpp:381
void Xeromyces_ReadRepeat(const XMBData &xmb, XMBElement element, IGUIObject &parent, std::vector< std::pair< CStr, CStr > > &NameSubst, std::unordered_set< VfsPath > &Paths, std::uint32_t nesting_depth)
Reads in the element <repeat>, which repeats its child <object>s 'count' times, replacing the string ...
Definition: CGUI.cpp:986
unsigned int m_MouseButtons
Indicates which buttons are pressed (bit 0 = LMB, bit 1 = RMB, bit 2 = MMB)
Definition: CGUI.h:620
std::unique_ptr< IGUIObject > ConstructObject(const CStr &str)
You input the name of the object type, and let's say you input "button", then it will construct a CGU...
Definition: CGUI.cpp:399
static bool ParseString(const CGUI *pGUI, const CStrW &Value, T &tOutput)
Reads a string value and modifies the given value of type T if successful.
const CVector2D & GetMousePos() const
Returns the current screen coordinates of the cursor.
Definition: CGUI.h:214
void Draw(CCanvas2D &canvas)
Displays the whole GUI.
Definition: CGUI.cpp:376
static const CStr EventNameMouseRightPress
Definition: CGUI.h:704
void Xeromyces_ReadScrollBarStyle(const XMBData &xmb, XMBElement element)
Reads in the element <scrollbar> (the XMBElement) and stores the result in m_ScrollBarStyles.
Definition: CGUI.cpp:1255
void AddObjectTypes()
Informs the GUI page which GUI object types may be constructed from XML.
Definition: GUIObjectTypes.cpp:47
IGUIObject * FindObjectByName(const CStr &Name) const
Returns the GUI object with the desired name, or nullptr and logs an error if no match is found.
Definition: CGUI.cpp:447
static const CStr EventNameMouseWheelUp
Definition: CGUI.h:707
void LoadedXmlFiles()
Called after all XML files linked in the page file were loaded.
Definition: CGUI.cpp:604
GUIProxyProps * GetProxyData(const js::BaseProxyHandler *ptr)
Definition: CGUI.h:295
void Xeromyces_ReadIcon(const XMBData &xmb, XMBElement element)
Reads in the element <icon> (the XMBElement) and stores the result in m_Icons.
Definition: CGUI.cpp:1346
std::optional< ModuleArtifact > m_LoadModuleResult
Definition: CGUI.h:749
std::map< CStr, std::unique_ptr< const CGUISprite > > m_Sprites
Definition: CGUI.h:725
void Xeromyces_ReadRootSetup(const XMBData &xmb, XMBElement element)
Reads in the root element <setup> (the DOMElement).
Definition: CGUI.cpp:649
static const CStr EventNameMouseLeftRelease
Definition: CGUI.h:711
std::shared_ptr< Script::Interface > GetScriptInterface()
Definition: CGUI.h:297
void SetGlobalHotkey(const CStr &hotkeyTag, const CStr &eventName, JS::HandleValue function)
Allows the JS side to add or remove global hotkeys.
Definition: CGUI.cpp:535
void AddObject(IGUIObject &parent, std::unique_ptr< IGUIObject > child)
The CGUI takes ownership of the child object and links the parent with the child.
Definition: CGUI.cpp:409
void Xeromyces_ReadRootStyles(const XMBData &xmb, XMBElement element)
Reads in the root element <styles> (the DOMElement).
Definition: CGUI.cpp:643
IGUIObject * FindObjectUnderMouse()
Returns the GUI object under the mouse, or nullptr if none.
Definition: CGUI.cpp:460
bool HasStyle(const CStr &name) const
Check if a style exists.
Definition: CGUI.h:278
~CGUI()
Definition: CGUI.cpp:108
IGUIObject * GetFocusedObject()
Get Focused Object.
Definition: CGUI.h:320
std::unordered_map< CStr, std::vector< IGUIObject * > > m_EventObjects
Map from event names to object which listen to a given event.
Definition: CGUI.h:754
void UnsetObjectStyle(IGUIObject &object)
Definition: CGUI.cpp:508
std::map< CStr, const SGUIStyle > m_Styles
Definition: CGUI.h:728
void SetObjectStyle(IGUIObject &object, const CStr &styleName)
Allows the JS side to modify the style setting assigned to a GUI object.
Definition: CGUI.cpp:501
void Xeromyces_ReadSprite(const XMBData &xmb, XMBElement element)
Reads in the element <sprite> (the XMBElement) and stores the result in a new CGUISprite.
Definition: CGUI.cpp:1067
std::map< CStr, const SGUIScrollBarStyle > m_ScrollBarStyles
Definition: CGUI.h:731
const SGUIIcon & GetIcon(const CStr &name) const
Get Icon (a const reference, can never be changed)
Definition: CGUI.h:264
int m_InternalNameNumber
Number of object that has been given name automatically.
Definition: CGUI.h:659
Rectangle class used for screen rectangles.
Definition: Rect.h:31
Definition: Size2D.h:25
Definition: Vector2D.h:32
Proxies need to store some data whose lifetime is tied to an interface.
Definition: JSInterface_GUIProxy.h:96
Definition: GUITooltip.h:29
GUI object such as a button or an input-box.
Definition: IGUIObject.h:63
Definition: path.h:84
Wrapper class for OS paths used by the game.
Definition: Paths.h:29
Definition: Context.h:51
Definition: ModuleLoader.h:185
Definition: ModuleLoader.h:162
iterator begin() noexcept
Definition: ModuleLoader.cpp:471
Definition: Request.h:51
Definition: XMBData.h:95
Definition: XMBData.h:135
Reaction
Definition: Input.h:39
Definition: CGUI.h:63
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
Definition: CGUI.h:65
#define T(string_literal)
Definition: secure_crt.cpp:81
Same as the CColor class, but this one can also parse colors predefined in the GUI page (such as "yel...
Definition: CGUIColor.h:31
Definition: CGUI.h:742
Script::ModuleLoader::Result result
Definition: CGUI.h:745
JS::PersistentRootedObject moduleNamespace
Definition: CGUI.h:747
Script::ModuleLoader::Result::iterator iterator
Definition: CGUI.h:746
ModuleArtifact(const Script::Request &rq, VfsPath filename)
Definition: CGUI.cpp:1429
Icon, you create them in the XML file with root element <setup>.
Definition: SGUIIcon.h:30
Definition: CGUISprite.h:43
The GUI Scroll-bar style.
Definition: IGUIScrollBar.h:49
Contains a list of values for new defaults to objects.
Definition: SGUIStyle.h:30