Index: ps/trunk/source/gui/CGUI.h =================================================================== --- ps/trunk/source/gui/CGUI.h (revision 23506) +++ ps/trunk/source/gui/CGUI.h (revision 23507) @@ -1,657 +1,657 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2020 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ /* * This is the top class of the whole GUI, all objects * and settings are stored within this class. */ #ifndef INCLUDED_CGUI #define INCLUDED_CGUI #include "gui/GUITooltip.h" #include "gui/ObjectTypes/CGUIDummyObject.h" #include "gui/SettingTypes/CGUIColor.h" #include "gui/SGUIIcon.h" #include "gui/SGUIStyle.h" #include "lib/input.h" #include "ps/Shapes.h" #include "ps/XML/Xeromyces.h" #include "scriptinterface/ScriptInterface.h" #include #include #include extern const double SELECT_DBLCLICK_RATE; class CGUISpriteInstance; class CGUISprite; class IGUIObject; struct SGUIImageEffects; struct SGUIScrollBarStyle; /** * The main object that represents a whole GUI page. */ class CGUI { NONCOPYABLE(CGUI); private: // Private typedefs using ConstructObjectFunction = IGUIObject* (*)(CGUI&); public: CGUI(const shared_ptr& runtime); ~CGUI(); /** * Informs the GUI page which GUI object types may be constructed from XML. */ void AddObjectTypes(); /** * Performs processing that should happen every frame * (including sending the "Tick" event to scripts) */ void TickObjects(); /** * Sends a specified script event to every object * * @param eventName String representation of event name */ void SendEventToAll(const CStr& eventName); /** * Sends a specified script event to every object * * @param eventName String representation of event name * @param paramData JS::HandleValueArray storing the arguments passed to the event handler. */ void SendEventToAll(const CStr& eventName, const JS::HandleValueArray& paramData); /** * Displays the whole GUI */ void Draw(); /** * Draw GUI Sprite * * @param Sprite Object referring to the sprite (which also caches * calculations for faster rendering) * @param CellID Number of the icon cell to use. (Ignored if this sprite doesn't * have any images with "cell-size") * @param Z Drawing order, depth value * @param Rect Position and Size * @param Clipping The sprite shouldn't be drawn outside this rectangle */ void DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float& Z, const CRect& Rect, const CRect& Clipping = CRect()); /** * The replacement of Process(), handles an SDL_Event_ * * @param ev SDL Event, like mouse/keyboard input */ InReaction HandleEvent(const SDL_Event_* ev); /** * Load a GUI XML file into the GUI. * * VERY IMPORTANT! All \-files must be read before * everything else! * * @param Filename Name of file * @param Paths Set of paths; all XML and JS files loaded will be added to this */ void LoadXmlFile(const VfsPath& Filename, std::unordered_set& Paths); /** * Called after all XML files linked in the page file were loaded. */ void LoadedXmlFiles(); /** * Allows the JS side to modify the hotkey setting assigned to a GUI object. */ void SetObjectHotkey(IGUIObject* pObject, const CStr& hotkeyTag); void UnsetObjectHotkey(IGUIObject* pObject, const CStr& hotkeyTag); /** * Allows the JS side to add or remove global hotkeys. */ void SetGlobalHotkey(const CStr& hotkeyTag, JS::HandleValue function); void UnsetGlobalHotkey(const CStr& hotkeyTag); /** * Return the object which is an ancestor of every other GUI object. */ CGUIDummyObject& GetBaseObject() { return m_BaseObject; }; /** * Checks if object exists and return true or false accordingly * * @param Name String name of object * @return true if object exists */ bool ObjectExists(const CStr& Name) const; /** * Returns the GUI object with the desired name, or nullptr * if no match is found, * * @param Name String name of object * @return Matching object, or nullptr */ IGUIObject* FindObjectByName(const CStr& Name) const; /** * Returns the GUI object under the mouse, or nullptr if none. */ IGUIObject* FindObjectUnderMouse(); /** * Returns the current screen coordinates of the cursor. */ const CPos& GetMousePos() const { return m_MousePos; }; /** * Returns the currently pressed mouse buttons. */ const unsigned int& GetMouseButtons() { return m_MouseButtons; }; const SGUIScrollBarStyle* GetScrollBarStyle(const CStr& style) const; /** * The GUI needs to have all object types inputted and * their constructors. Also it needs to associate a type * by a string name of the type. * * To add a type: * @code * AddObjectType("button", &CButton::ConstructObject); * @endcode * * @param str Reference name of object type * @param pFunc Pointer of function ConstuctObject() in the object * * @see CGUI#ConstructObject() */ void AddObjectType(const CStr& str, ConstructObjectFunction pFunc) { m_ObjectTypes[str] = pFunc; } /** * Update Resolution, should be called every time the resolution * of the OpenGL screen has been changed, this is because it needs * to re-cache all its actual sizes * * Needs no input since screen resolution is global. * * @see IGUIObject#UpdateCachedSize() */ void UpdateResolution(); /** * Check if an icon exists */ bool HasIcon(const CStr& name) const { return (m_Icons.find(name) != m_Icons.end()); } /** * Get Icon (a const reference, can never be changed) */ const SGUIIcon& GetIcon(const CStr& name) const { return m_Icons.at(name); } /** * Check if a style exists */ bool HasStyle(const CStr& name) const { return (m_Styles.find(name) != m_Styles.end()); } /** * Get Style if it exists, otherwise throws an exception. */ const SGUIStyle& GetStyle(const CStr& name) const { return m_Styles.at(name); } /** * Check if a predefined color of that name exists. */ bool HasPreDefinedColor(const CStr& name) const { return (m_PreDefinedColors.find(name) != m_PreDefinedColors.end()); } /** * Resolve the predefined color if it exists, otherwise throws an exception. */ const CGUIColor& GetPreDefinedColor(const CStr& name) const { return m_PreDefinedColors.at(name); } shared_ptr GetScriptInterface() { return m_ScriptInterface; }; JS::Value GetGlobalObject() { return m_ScriptInterface->GetGlobalObject(); }; private: /** * The CGUI takes ownership of the child object and links the parent with the child. * Returns false on failure to take over ownership of the child object. */ bool AddObject(IGUIObject& parent, IGUIObject& child); /** * You input the name of the object type, and let's * say you input "button", then it will construct a * CGUIObjet* as a CButton. * * @param str Name of object type * @return Newly constructed IGUIObject (but constructed as a subclass) */ IGUIObject* ConstructObject(const CStr& str); public: /** * Get Focused Object. */ IGUIObject* GetFocusedObject() { return m_FocusedObject; } /** * Change focus to new object. * Will send LOST_FOCUS/GOT_FOCUS messages as appropriate. * pObject can be nullptr to remove all focus. */ void SetFocusedObject(IGUIObject* pObject); /** * Reads a string value and modifies the given value of type T if successful. * Does not change the value upon conversion failure. * * @param pGUI The GUI page which may contain data relevant to the parsing * (for example predefined colors). * @param Value The value in string form, like "0 0 100% 100%" * @param tOutput Parsed value of type T * @return True at success. */ template static bool ParseString(const CGUI* pGUI, const CStrW& Value, T& tOutput); private: //-------------------------------------------------------- /** @name XML Reading Xeromyces specific subroutines * * These does not throw! * Because when reading in XML files, it won't be fatal * if an error occurs, perhaps one particular object * fails, but it'll still continue reading in the next. * All Error are reported with ReportParseError */ //-------------------------------------------------------- /* Xeromyces_* functions tree (ReadRootObjects) | +-