1
1
forked from 0ad/0ad
0ad/source/gui/CGUI.h

529 lines
13 KiB
C
Raw Normal View History

/*
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
//--------------------------------------------------------
// Includes / Compiler directives
//--------------------------------------------------------
#include "GUI.h"
2003-12-01 08:06:55 +01:00
#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
//--------------------------------------------------------
2003-12-01 08:06:55 +01:00
/**
* @author Gustav Larsson
*
* Contains a list of values for new defaults to objects.
*/
struct SGUIStyle
{
// A list of defualts for
std::map<CStr, CStr> m_SettingsDefaults;
};
/**
* @author Gustav Larsson
*
* The main object that includes the whole GUI. Is singleton
* and accessed by g_GUI.
2003-11-24 03:18:41 +01:00
*
* No interfacial functions throws.
*/
class CGUI : public Singleton<CGUI>
{
2003-11-24 03:18:41 +01:00
friend class IGUIObject;
friend class IGUIScrollBarOwner;
friend class CInternalCGUIAccessorBase;
private:
// Private typedefs
2003-11-24 03:18:41 +01:00
typedef IGUIObject *(*ConstructObjectFunction)();
public:
CGUI();
~CGUI();
// TODO Gee: (MEGA) Extremely temporary.
2003-11-25 03:47:12 +01:00
std::string TEMPmessage;
/**
* Initializes the GUI, needs to be called before the GUI is used
*/
void Initialize();
/**
* @deprecated Will be removed
*/
void Process();
/**
* Displays the whole GUI
*/
void Draw();
2003-12-01 08:06:55 +01:00
/**
* Draw GUI Sprite, cooperates with CRenderer.
*
* @param SpriteName By name! The GUI will fetch the real object itself.
* @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 CStr& SpriteName, const float &Z,
2004-05-29 06:06:50 +02:00
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(const SGUIText &Text, const CColor &DefaultColor,
const CPos &pos, const float &z);
2003-12-01 08:06:55 +01:00
/**
* 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);
2003-11-05 00:41:42 +01:00
/**
* Load a GUI XML file into the GUI.
*
2003-12-01 08:06:55 +01:00
* <b>VERY IMPORTANT!</b> All \<styles\>-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; }
2003-11-24 18:13:37 +01:00
/**
* Update Resolution, should be called every time the resolution
* of the opengl screen has been changed, this is becuase it needs
* to re-cache all its actual sizes
*
* Needs no input since screen resolution is global.
*
* @see IGUIObject#UpdateCachedSize()
*/
void UpdateResolution();
2004-05-29 06:06:50 +02:00
/**
* 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 Color Default color
* @param Font Default font, notice both Default color and defult 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.
*/
SGUIText GenerateText(const CGUIString &Text, /*const CColor &Color, */
const CStr& Font, const int &Width, const int &BufferZone);
2004-05-29 06:06:50 +02:00
/**
* Returns the JSObject* associated with the GUI
*
* @return A JSobject* (as a void* to avoid #including all of JS)
*/
void* GetScriptObject() { return m_ScriptObject; }
private:
/**
* Updates the object pointers, needs to be called each
* time an object has been added or removed.
2003-11-22 16:07:22 +01:00
*
* This function is atomic, meaning if it throws anything, it will
* have seen it through that nothing was ultimately changed.
*
2003-11-24 03:18:41 +01:00
* @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? Becasue it enables the GUI to
* be much more encapsulated and safe.
2003-11-22 16:07:22 +01:00
*
2003-11-24 03:18:41 +01:00
* @throws Rethrows PS_RESULT from IGUIObject::SetGUI() and
* IGUIObject::AddChild().
*/
2003-11-24 03:18:41 +01:00
void AddObject(IGUIObject* pObject);
/**
2003-11-22 16:07:22 +01:00
* Report XML Reading Error, should be called from within the
* Xerces_* functions.
*
* @param str Error message
*/
void ReportParseError(const CStr& 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
2003-11-24 03:18:41 +01:00
* @return Newly constructed IGUIObject (but constructed as a subclass)
*/
IGUIObject *ConstructObject(const CStr& str);
//--------------------------------------------------------
/** @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
*/
2003-11-22 16:07:22 +01:00
//--------------------------------------------------------
2003-11-22 16:07:22 +01:00
/**
Xeromyces_* functions tree
2003-11-22 16:07:22 +01:00
<code>
\<objects\> (ReadRootObjects)
|
+-\<script\> (ReadScript)
|
2003-11-22 16:07:22 +01:00
+-\<object\> (ReadObject)
|
2003-11-22 16:07:22 +01:00
+-\<action\>
|
2003-11-24 03:18:41 +01:00
+-Optional Type Extensions (IGUIObject::ReadExtendedElement) TODO
|
+-�object� *recursive*
2003-11-22 16:07:22 +01:00
\<styles\> (ReadRootStyles)
|
2003-11-22 16:07:22 +01:00
+-\<style\> (ReadStyle)
2003-11-22 16:07:22 +01:00
\<sprites\> (ReadRootSprites)
|
2003-11-22 16:07:22 +01:00
+-\<sprite\> (ReadSprite)
|
2003-11-22 16:07:22 +01:00
+-\<image\> (ReadImage)
2003-11-22 16:07:22 +01:00
\<setup>\ (ReadRootSetup)
|
2003-11-22 16:07:22 +01:00
+-\<tooltip>\ (ReadToolTip)
|
2003-11-22 16:07:22 +01:00
+-\<scrollbar>\ (ReadScrollBar)
|
2003-11-22 16:07:22 +01:00
+-\<icon>\ (ReadIcon)
2003-11-22 16:07:22 +01:00
</code>
*/
2003-11-22 16:07:22 +01:00
//@{
// Read Roots
2003-11-22 16:07:22 +01:00
/**
2003-11-24 03:18:41 +01:00
* Reads in the root element \<objects\> (the DOMElement).
2003-11-22 16:07:22 +01:00
*
* @param Element The Xeromyces object that represents
2003-11-22 16:07:22 +01:00
* the objects-tag.
* @param pFile The Xeromyces object for the file being read
2003-11-24 03:18:41 +01:00
*
* @see LoadXMLFile()
2003-11-22 16:07:22 +01:00
*/
void Xeromyces_ReadRootObjects(XMBElement Element, CXeromyces* pFile);
2003-11-22 16:07:22 +01:00
/**
2003-11-24 03:18:41 +01:00
* Reads in the root element \<sprites\> (the DOMElement).
2003-11-22 16:07:22 +01:00
*
* @param Element The Xeromyces object that represents
2003-11-22 16:07:22 +01:00
* the sprites-tag.
* @param pFile The Xeromyces object for the file being read
2003-11-24 03:18:41 +01:00
*
* @see LoadXMLFile()
2003-11-22 16:07:22 +01:00
*/
void Xeromyces_ReadRootSprites(XMBElement Element, CXeromyces* pFile);
2003-12-01 08:06:55 +01:00
/**
* Reads in the root element \<styles\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the styles-tag.
* @param pFile The Xeromyces object for the file being read
2003-12-01 08:06:55 +01:00
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadRootStyles(XMBElement Element, CXeromyces* pFile);
2003-12-01 08:06:55 +01:00
/**
* Reads in the root element \<setup\> (the DOMElement).
*
* @param Element The Xeromyces object that represents
* the setup-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadRootSetup(XMBElement Element, CXeromyces* pFile);
2003-11-22 16:07:22 +01:00
// Read Subs
/**
* Notice! Recursive function!
*
2003-11-24 03:18:41 +01:00
* Read in an \<object\> (the DOMElement) and stores it
2003-11-22 16:07:22 +01:00
* as a child in the pParent.
*
* It will also check the object's children and call this function
* on them too. Also it will call all other functions that reads
2003-11-24 03:18:41 +01:00
* in other stuff that can be found within an object. Such as a
* \<action\> will call Xerces_ReadAction (TODO, real funcion?).
2003-11-22 16:07:22 +01:00
*
2003-11-24 03:18:41 +01:00
* Reads in the root element \<sprites\> (the DOMElement).
2003-11-22 16:07:22 +01:00
*
* @param Element The Xeromyces object that represents
2003-11-22 16:07:22 +01:00
* the object-tag.
* @param pFile The Xeromyces object for the file being read
2003-11-22 16:07:22 +01:00
* @param pParent Parent to add this object as child in.
2003-11-24 03:18:41 +01:00
*
* @see LoadXMLFile()
2003-11-22 16:07:22 +01:00
*/
void Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent);
2003-11-22 16:07:22 +01:00
/**
* Reads in the element \<script\> (the DOMElement) and executes
* the script's code.
*
* @param Element The Xeromyces object that represents
* the sprite-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadScript(XMBElement Element, CXeromyces* pFile);
2003-11-22 16:07:22 +01:00
/**
2003-11-24 03:18:41 +01:00
* Reads in the element \<sprite\> (the DOMElement) and stores the
2003-11-22 16:07:22 +01:00
* result in a new CGUISprite.
*
* @param Element The Xeromyces object that represents
2003-11-22 16:07:22 +01:00
* the sprite-tag.
* @param pFile The Xeromyces object for the file being read
2003-11-24 03:18:41 +01:00
*
* @see LoadXMLFile()
2003-11-22 16:07:22 +01:00
*/
void Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile);
2003-11-22 16:07:22 +01:00
/**
2003-11-24 03:18:41 +01:00
* Reads in the element \<image\> (the DOMElement) and stores the
2003-11-22 16:07:22 +01:00
* result within the CGUISprite.
*
* @param Element The Xeromyces object that represents
2003-11-22 16:07:22 +01:00
* the image-tag.
* @param pFile The Xeromyces object for the file being read
2003-11-24 03:18:41 +01:00
* @param parent Parent sprite.
*
* @see LoadXMLFile()
2003-11-22 16:07:22 +01:00
*/
void Xeromyces_ReadImage(XMBElement Element, CXeromyces* pFile, CGUISprite &parent);
2003-12-01 08:06:55 +01:00
/**
* Reads in the element \<style\> (the DOMElement) and stores the
* result in m_Styles.
*
* @param Element The Xeromyces object that represents
* the style-tag.
* @param pFile The Xeromyces object for the file being read
2003-12-01 08:06:55 +01:00
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadStyle(XMBElement Element, CXeromyces* pFile);
2003-12-01 08:06:55 +01:00
/**
* Reads in the element \<scrollbar\> (the DOMElement) and stores the
* result in m_ScrollBarStyles.
*
* @param Element The Xeromyces object that represents
* the scrollbar-tag.
* @param pFile The Xeromyces object for the file being read
*
* @see LoadXMLFile()
*/
void Xeromyces_ReadScrollBarStyle(XMBElement Element, CXeromyces* pFile);
//@}
private:
// Variables
//--------------------------------------------------------
/** @name Miscellaneous */
2003-11-22 16:07:22 +01:00
//--------------------------------------------------------
//@{
/**
* An JSObject* under which all GUI JavaScript things will
* be created, so that they can be garbage-collected
* when the GUI shuts down. (Stored as void* to avoid
* to avoid pulling in all the JS headers)
*/
void* m_ScriptObject;
/**
* don't want to pass this around with the
* ChooseMouseOverAndClosest broadcast -
* we'd need to pack this and pNearest in a struct
*/
2004-05-29 06:06:50 +02:00
CPos m_MousePos;
/**
* Indicates which buttons are pressed (bit 0 = LMB,
* bit 1 = RMB, bit 2 = MMB)
*/
unsigned int m_MouseButtons;
/// Used when reading in XML files
2004-05-29 06:06:50 +02:00
// TODO Gee: Used?
int16_t m_Errors;
//@}
//--------------------------------------------------------
/** @name Objects */
2003-11-22 16:07:22 +01:00
//--------------------------------------------------------
//@{
/**
* Base Object, all its children are considered parentless
* because this is not a real object per se.
*/
2003-11-24 03:18:41 +01:00
IGUIObject* m_BaseObject;
/**
* Just pointers for fast name access, each object
* is really constructed within its parent for easy
* recursive management.
* Notice m_BaseObject won't belong here since it's
* not considered a real object.
*/
2003-11-24 03:18:41 +01:00
map_pObjects m_pAllObjects;
2004-05-29 06:06:50 +02:00
/**
* Number of object that has been given name automatically.
* the name given will be '__internal(#)', the number (#)
* being this variable. When an object's name has been set
* as followed, the value will increment.
*/
int m_InternalNameNumber;
/**
* Function pointers to functions that constructs
2003-11-24 03:18:41 +01:00
* IGUIObjects by name... For instance m_ObjectTypes["button"]
* is filled with a function that will "return new CButton();"
*/
std::map<CStr, ConstructObjectFunction> m_ObjectTypes;
//@}
//--------------------------------------------------------
2003-12-01 08:06:55 +01:00
/** @name Databases */
2003-11-22 16:07:22 +01:00
//--------------------------------------------------------
//@{
2003-12-01 08:06:55 +01:00
/// Sprites
2003-11-24 03:18:41 +01:00
std::map<CStr, CGUISprite> m_Sprites;
2003-12-01 08:06:55 +01:00
/// Styles
std::map<CStr, SGUIStyle> m_Styles;
/// Scroll-bar styles
std::map<CStr, SGUIScrollBarStyle> m_ScrollBarStyles;
//@}
};
2003-11-24 18:13:37 +01:00
#endif