/* CGUI by Gustav Larsson gee@pyro.nu --Overview-- This is the top class of the whole GUI, all objects and settings are stored within this class. --More info-- Check GUI.h */ #ifndef CGUI_H #define CGUI_H ERROR_GROUP(GUI); ERROR_TYPE(GUI, JSOpenFailed); //-------------------------------------------------------- // Includes / Compiler directives //-------------------------------------------------------- #include "GUI.h" #include "Singleton.h" #include "input.h" // JW: grr, classes suck in this case :P #include "Xeromyces.h" extern int gui_handler(const SDL_Event* ev); //-------------------------------------------------------- // Macros //-------------------------------------------------------- //-------------------------------------------------------- // Types //-------------------------------------------------------- //-------------------------------------------------------- // Error declarations //-------------------------------------------------------- //-------------------------------------------------------- // Declarations //-------------------------------------------------------- /** * @author Gustav Larsson * * Contains a list of values for new defaults to objects. */ struct SGUIStyle { // A list of defualts for std::map m_SettingsDefaults; }; struct JSObject; // The GUI stores a JSObject*, so needs to know that JSObject exists /** * @author Gustav Larsson * * The main object that includes the whole GUI. Is singleton * and accessed by g_GUI. * * No interfacial functions throws. */ class CGUI : public Singleton { friend class IGUIObject; friend class IGUIScrollBarOwner; friend class CInternalCGUIAccessorBase; private: // Private typedefs typedef IGUIObject *(*ConstructObjectFunction)(); public: CGUI(); ~CGUI(); /** * Initializes the GUI, needs to be called before the GUI is used */ void Initialize(); /** * @deprecated Will be removed */ void Process(); /** * Sends a "Tick" event to every object */ void TickObjects(); /** * Displays the whole GUI */ void Draw(); /** * Draw GUI Sprite * * @param Sprite Object refering to the sprite (which also caches * calculations for faster rendering) * @param IconID Number of the icon to use. (Ignored if this sprite doesn't * have any images with "icon-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(CGUISpriteInstance& Sprite, int IconID, const float &Z, const CRect &Rect, const CRect &Clipping=CRect()); /** * Draw a SGUIText object * * @param Text Text object. * @param DefaultColor Color used if no tag applied. * @param pos position * @param z z value. */ void DrawText(SGUIText &Text, const CColor &DefaultColor, const CPos &pos, const float &z); /** * Clean up, call this to clean up all memory allocated * within the GUI. */ void Destroy(); /** * The replacement of Process(), handles an SDL_Event * * @param ev SDL Event, like mouse/keyboard input */ int 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 */ void LoadXMLFile(const std::string &Filename); /** * 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 NULL * if no match is found, * * @param Name String name of object * @return Matching object, or NULL */ IGUIObject* FindObjectByName(const CStr& Name) 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(); /** * Generate a SGUIText object from the inputted string. * The function will break down the string and its * tags to calculate exactly which rendering queries * will be sent to the Renderer. * * Done through the CGUI since it can communicate with * * @param Text Text to generate SGUIText object from * @param Font Default font, notice both Default color and default font * can be changed by tags. * @param Width Width, 0 if no word-wrapping. * @param BufferZone space between text and edge, and space between text and images. * * pObject is *only* if error parsing fails, and we need to be able to output * which object the error occured in to aid the user. The parameter is completely * optional. */ SGUIText GenerateText(const CGUIString &Text, const CStr& Font, const float &Width, const float &BufferZone, const IGUIObject *pObject=NULL); /** * Returns the JSObject* associated with the GUI * * @return The relevant JS object */ JSObject* GetScriptObject() { return m_ScriptObject; } /** * Check if an icon exists */ bool IconExists(const CStr &str) const { return (m_Icons.count(str) != 0); } /** * Get Icon (a copy, can never be changed) */ SGUIIcon GetIcon(const CStr &str) const { return m_Icons.find(str)->second; } private: /** * Updates the object pointers, needs to be called each * time an object has been added or removed. * * This function is atomic, meaning if it throws anything, it will * have seen it through that nothing was ultimately changed. * * @throws PS_RESULT that is thrown from IGUIObject::AddToPointersMap(). */ void UpdateObjects(); /** * Adds an object to the GUI's object database * Private, since you can only add objects through * XML files. Why? Because it enables the GUI to * be much more encapsulated and safe. * * @throws Rethrows PS_RESULT from IGUIObject::SetGUI() and * IGUIObject::AddChild(). */ void AddObject(IGUIObject* pObject); /** * Report XML Reading Error, should be called from within the * Xerces_* functions. * * @param str Error message */ void ReportParseError(const char *str, ...); /** * 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); /** * Get Focused Object. */ IGUIObject *GetFocusedObject() { return m_FocusedObject; } //-------------------------------------------------------- /** @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) | +-