AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzXoml.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CZ_XOML_H_)
00015 #define _CZ_XOML_H_
00016 
00017 #include "CzString.h"
00018 #include "CzUtil.h"
00019 #include "CzXml.h"
00020 #include "CzXomlActions.h"
00021 #include "CzXomlResource.h"
00022 
00023 class CzStyle;
00024 class CzXomlClassDef;
00025 class IzXomlResource;
00026 struct CzXomlVariable;
00027 
00028 /**
00029  @addtogroup XOML
00030  @{
00031  */
00032 
00033 
00034 /**
00035  @enum  eCzXomlPropertyType
00036 
00037  @brief The different types of XOML properties
00038  */
00039 enum eCzXomlPropertyType
00040 {
00041     PT_Invalid, 
00042     PT_String, 
00043     PT_Bool, 
00044     PT_Float, 
00045     PT_Int, 
00046     PT_Vec2, 
00047     PT_Vec3, 
00048     PT_Vec4, 
00049     PT_Resource, 
00050     PT_Var, 
00051 };
00052 
00053 
00054 /**
00055  @struct    CzXomlProperty
00056 
00057  @brief A basic XOML property.
00058 
00059  */
00060 
00061 struct CzXomlProperty
00062 {
00063     eCzXomlPropertyType         Type;       ///< Type of property
00064     bool                        OwnData;    /// <If true then the property owns p_data
00065     union
00066     {
00067         void*                   p_data;     ///< Value of property as string or object
00068         bool                    p_bool;     ///< Value of property as a bool
00069         float                   p_float;    ///< Value of property as a float
00070         int                     p_int;      ///< Value of property as an integer
00071         float                   p_vec[4];   ///< Value of property as a vector
00072     };
00073 
00074     CzXomlProperty() : Type(PT_Invalid), OwnData(false)     { p_data = NULL; }
00075     CzXomlProperty(const char* data)        { OwnData = false; Type = PT_String; p_data = (void*)data; }
00076     CzXomlProperty(float data)              { OwnData = false; Type = PT_Float; p_float = data; }
00077     CzXomlProperty(bool data)               { OwnData = false; Type = PT_Bool; p_bool = data; }
00078     CzXomlProperty(int data)                { OwnData = false; Type = PT_Int; p_int = data; }
00079     CzXomlProperty(const CzVec2& data)      { OwnData = false; Type = PT_Vec2; p_vec[0] = data.x; p_vec[1] = data.y; }
00080     CzXomlProperty(const CzIVec2& data)     { OwnData = false; Type = PT_Vec2; p_vec[0] = (float)data.x; p_vec[1] = (float)data.y; }
00081     CzXomlProperty(const CzVec3& data)      { OwnData = false; Type = PT_Vec3; p_vec[0] = data.x; p_vec[1] = data.y; p_vec[2] = data.z; }
00082     CzXomlProperty(const CzVec4& data)      { OwnData = false; Type = PT_Vec4; p_vec[0] = data.x; p_vec[1] = data.y; p_vec[2] = data.z; p_vec[3] = data.w; }
00083     CzXomlProperty(const CzColour& data)    { OwnData = false; Type = PT_Vec4; p_vec[0] = (float)data.r; p_vec[1] = (float)data.g; p_vec[2] = (float)data.b; p_vec[3] = (float)data.a; }
00084     CzXomlProperty(const CzRect& data)      { OwnData = false; Type = PT_Vec4; p_vec[0] = data.x; p_vec[1] = data.y; p_vec[2] = data.w; p_vec[3] = data.h; }
00085     CzXomlProperty(const CzIRect& data)     { OwnData = false; Type = PT_Vec4; p_vec[0] = (float)data.x; p_vec[1] = (float)data.y; p_vec[2] = (float)data.w; p_vec[3] = (float)data.h; }
00086     CzXomlProperty(IzXomlResource* data)    { OwnData = false; Type = PT_Resource; p_data = (void*)data; }
00087     CzXomlProperty(CzXomlVariable* data)    { OwnData = false; Type = PT_Var; p_data = (void*)data; }
00088     CzXomlProperty(void* data)              { OwnData = false; Type = PT_Resource; p_data = data; }
00089     ~CzXomlProperty()
00090     {
00091         if (OwnData && Type == PT_String)
00092             delete [] (char*)p_data;
00093     }
00094 
00095     bool        setValue(const CzString& data, bool delta);
00096     bool        setValue(const CzXomlProperty& prop, bool delta);
00097     void        setToVariable(CzXomlVariable& var);
00098     void        toString(CzString& string) const;
00099 
00100     static CzXomlProperty* createOfType(eCzXomlPropertyType type);
00101     static CzXomlProperty* createOfType(const char* type_name);
00102 
00103 };
00104 
00105 /**
00106  @struct    CzXomlNamedProperty
00107 
00108  @brief A basic XOML property that has a name.
00109 
00110  */
00111 
00112 struct CzXomlNamedProperty
00113 {
00114     CzString                Name;           ///< Name of property
00115     CzXomlProperty*         Property;       ///< The property
00116 
00117     CzXomlNamedProperty() : Property(NULL)  {  }
00118     ~CzXomlNamedProperty()  { if (Property != NULL) delete Property; }
00119 };
00120 
00121 /**
00122  @typedef   bool (*CzXomPropertySetter)(IzXomlResource* target, const CzXomlProperty& prop,
00123     bool add)
00124 
00125  @brief Defines an alias for a XOML class property setter.
00126  */
00127 
00128 typedef bool (*CzXomPropertySetter)(IzXomlResource* target, const CzXomlProperty& prop, bool add);
00129 
00130 /**
00131  @typedef   CzXomlProperty (*CzXomPropertyGetter)(IzXomlResource* target,
00132     const CzXomlProperty& prop)
00133 
00134  @brief Defines an alias for a XOML class property getter.
00135  */
00136 
00137 typedef CzXomlProperty (*CzXomPropertyGetter)(IzXomlResource* target);
00138 
00139 /**
00140  @class CzXomlClassProperty
00141 
00142  @brief Used by classes definitions to list available properties
00143 
00144  */
00145 struct CzXomlClassProperty
00146 {
00147     // Properties
00148 protected:
00149     eCzXomlPropertyType     Type;               ///< Type of property
00150     CzString                Name;               ///< Name of property (only available in debug builds)
00151     CzXomPropertySetter     Setter;             ///< Method that is used to set the property
00152     CzXomPropertyGetter     Getter;             ///< Method that is used to get the property
00153 public:
00154     eCzXomlPropertyType     getType() const                     { return Type; }
00155     void                    setType(eCzXomlPropertyType type)   { Type = type; }
00156     void                    setName(const char* name)           { Name = name; }
00157     CzString&               getName()                           { return Name; }
00158     unsigned int            getNameHash() const                 { return Name.getHash(); }
00159     CzXomPropertySetter     getSetter()                         { return Setter; }
00160     CzXomPropertyGetter     getGetter()                         { return Getter; }
00161     
00162     // Properties end
00163 public:
00164     CzXomlClassProperty(const char* name, eCzXomlPropertyType type, CzXomPropertySetter setter, CzXomPropertyGetter getter)
00165     {
00166         setType(type);
00167         setName(name);
00168         Setter = setter;
00169         Getter = getter;
00170     }
00171 };
00172 
00173 /**
00174  @class CzXomlClassDef
00175 
00176  @brief Used by classes to define accessible properties and methods.
00177 
00178  */
00179 class CzXomlClassDef
00180 {
00181 public:
00182     // Public access to class creator iteration
00183     typedef CzList<CzXomlClassProperty*>::iterator _Iterator;
00184     _Iterator               begin()     { return Properties.begin(); }
00185     _Iterator               end()       { return Properties.end(); }
00186     // Properties
00187 protected:
00188     CzList<CzXomlClassProperty*>    Properties;
00189     // Properties end
00190 public:
00191     void Release()
00192     {
00193         for (_Iterator it = begin(); it != end(); ++it)
00194             delete *it;
00195     }
00196 
00197     void                    addProperty(CzXomlClassProperty* prop);
00198     void                    removeProperty(CzXomlClassProperty* prop);
00199     CzXomlClassProperty*    findProperty(unsigned int name_hash);
00200 };
00201 
00202 /**
00203  @class IzXomlClassCreator
00204 
00205  @brief Base class that is used by classes that create an instance of a XOML resource type.
00206 
00207  */
00208 class IzXomlClassCreator
00209 {
00210     // Properties
00211 protected:
00212 #if defined(_DEBUG)
00213     CzString                ClassName;              ///< Name of class
00214 #endif
00215     unsigned int            ClassNameHash;          ///< Name of class as a string hash
00216 public:
00217     void                    setClassName(const char* name)
00218     {
00219 #if defined(_DEBUG)
00220         ClassName = name;
00221 #endif
00222         ClassNameHash = CzString::CalculateHash(name);
00223     }
00224     unsigned int            getClassNameHash() const        { return ClassNameHash; }
00225 #if defined(_DEBUG)
00226     CzString&               getClassName()                  { return ClassName; }
00227 #endif
00228     // Properties end
00229 public:
00230     virtual IzXomlResource* CreateInstance(IzXomlResource* parent) = 0;
00231 };
00232 
00233 /**
00234  @def   CZ_XOML
00235 
00236  @brief A short cut macro that calls the global XOML singleton
00237 
00238  */
00239 #define CZ_XOML CzXoml::getInstance()
00240 
00241 /**
00242  @class CzXoml
00243 
00244  @brief The main controller responsible for instantiating objects from XOML based files.
00245 
00246  CzXoml is the mother of the XOML system and is responsible for the following:
00247  - Loading and parsing XOML files
00248  - Instantiating objects from XOML
00249  - Managing XOML classes
00250  - Managing XOML actions
00251 
00252  */
00253 class CzXoml
00254 {
00255 public:
00256     CDEFINE_SINGLETON(CzXoml)
00257 
00258     // Public access to class creator iteration
00259     typedef CzList<IzXomlClassCreator*>::iterator _Iterator;
00260     _Iterator               begin() { return ClassCreators.begin(); }
00261     _Iterator               end() { return ClassCreators.end(); }
00262     typedef CzList<IzXomlAction*>::iterator _ActionsIterator;
00263     _ActionsIterator        actions_begin() { return Actions.begin(); }
00264     _ActionsIterator        actions_end() { return Actions.end(); }
00265 
00266     static const char*      DockingNames[]; // TODO: Find new place for these
00267     static const char*      AspectLockNames[];
00268     static const char*      AlphaModeNames[];
00269     static const char*      AlignHNames[];
00270     static const char*      AlignVNames[];
00271     static const char*      SelectTypeNames[];
00272     static const char*      AxisNames[];
00273     static const char*      OrientationNames[];
00274     static const char*      InputTypeHintNames[];
00275 protected:
00276     // Properties
00277 public:
00278     void            addClass(IzXomlClassCreator* creator)
00279     {
00280 #if defined(_DEBUG)
00281         if (findClass(creator->getClassNameHash()))
00282         {
00283             CzDebug::Log(CZ_DEBUG_CHANNEL_WARNING, "XOML - Class creator already exists - ", creator->getClassName().c_str());
00284             delete creator;
00285             return;
00286         }
00287 #endif  //(_DEBUG)
00288         ClassCreators.push_back(creator);
00289     }
00290     void            addAction(IzXomlAction* action)
00291     {
00292 /*#if defined(_DEBUG)
00293         if (findAction(action->getActionNameHash()))
00294         {
00295             CzDebug::Log(CZ_DEBUG_CHANNEL_WARNING, "XOML - Actions already exist - ", action->getActionName().c_str());
00296             delete action;
00297             return;
00298         }
00299 #endif  //(_DEBUG)*/
00300         Actions.push_back(action);
00301     }
00302     IzXomlClassCreator*     findClass(unsigned int name_hash);
00303     IzXomlClassCreator*     findClass(const char* name);
00304     IzXomlAction*           findAction(unsigned int name_hash);
00305     IzXomlAction*           findAction(const char* name);
00306     // Properties end
00307 
00308 protected:
00309     CzList<IzXomlClassCreator*> ClassCreators;
00310     CzList<IzXomlAction*>       Actions;
00311 
00312 public:
00313     void            Init();
00314     void            Release();
00315 
00316     bool            Process(IzXomlResource* parent, const char* filename, bool reset_xml_pools = true);
00317     bool            Process(IzXomlResource* parent, CzXmlNode* node);
00318     IzXomlResource* ProcessNode(IzXomlResource* parent, CzXmlNode* node);
00319 
00320     // Utility
00321     static void     ApplyStyle(CzStyle* style, CzXmlNode* node);
00322 };
00323 
00324 
00325 /// @}
00326 
00327 #endif  // _CZ_XOML_H_