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
CzModifier.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_MODIFIER_H_)
00015 #define _CZ_MODIFIER_H_
00016 
00017 #include "CzUtil.h"
00018 #include "CzString.h"
00019 #include "CzXoml.h"
00020 
00021 class CzModifierManager;
00022 
00023 //
00024 //
00025 //
00026 //
00027 // IzModifier - A class Modifier is attached to a class to modify its behvaiour
00028 //
00029 //
00030 //
00031 //
00032 class IzModifier
00033 {
00034 public:
00035     // Properties
00036 protected:
00037     CzModifierManager*  Parent;                     // Parent manager
00038     CzString            Params[4];                  // Modifier parameters
00039 #if defined (_DEBUG)
00040     CzString            Name;                       // Modifier name
00041 #endif  // _DEBUG
00042     unsigned int        NameHash;                   // Modifier name hash
00043     bool                Active;                     // Modifiers active state
00044     bool                Initialised;                // Initialised state
00045 public:
00046     CzModifierManager* getParent()                                  { return Parent; }
00047     void                setParent(CzModifierManager* parent)    { Parent = parent; }
00048     void                setName(const char* name)
00049     {
00050 #if defined (_DEBUG)
00051         Name = name;
00052 #endif  // _DEBUG
00053         NameHash = CzString::CalculateHash(name);
00054     }
00055 #if defined (_DEBUG)
00056     CzString&           getName()                       { return Name; }
00057 #endif  // _DEBUG
00058     unsigned int        getNameHash()                   { return NameHash; }
00059     void                setActive(bool active)          { Active = active; }
00060     bool                isActive() const                { return Active; }
00061     void                setInitialised(bool init)       { Initialised = init; }
00062     bool                isInitialised() const           { return Initialised; }
00063     void                setParameter(int index, const CzString& data)           { Params[index] = data; }
00064     void                setParameter(int index, const char* data)               { Params[index] = data; }
00065     // Properties End
00066 public:
00067     IzModifier() : Active(true), Initialised(false), Parent(NULL) {}
00068     virtual void        InitModifier(IzXomlResource* target) = 0;               // Initialise the Modifier
00069     virtual void        ReleaseModifier(IzXomlResource* target) = 0;            // Clean-up the Modifier
00070     virtual bool        UpdateModifier(IzXomlResource* target, float dt) = 0;   // Update the Modifier
00071 };
00072 
00073 // 
00074 // 
00075 //
00076 //
00077 // CzModifierManager - Manages a collection of class modifiers
00078 //
00079 //
00080 //
00081 //
00082 class CzModifierManager : public IzXomlResource
00083 {
00084 public:
00085     // Public access to iteration
00086     typedef CzList<IzModifier*>::iterator _Iterator;
00087     _Iterator               begin()     { return Modifiers.begin(); }
00088     _Iterator               end()       { return Modifiers.end(); }
00089 
00090 protected:
00091     // Properties
00092     CzList<IzModifier*> Modifiers;          // A collection of class modifiers
00093 public:
00094     bool                    addModifier(IzModifier* modifier);
00095     bool                    removeModifier(IzModifier* modifier);
00096     IzModifier*             findModifier(unsigned int name_hash);
00097     IzModifier*             findModifier(const char* name);
00098     void                    clearModifiers();
00099     // Properties end
00100 
00101 public:
00102     CzModifierManager() : IzXomlResource() { setClassType("modifiers"); }
00103     virtual ~CzModifierManager() { clearModifiers(); }
00104 
00105     void                    Execute(IzXomlResource* target, float dt);
00106 
00107     // Implementation of IzXomlResource interface
00108     int                     LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node);
00109 };
00110 
00111 //
00112 //  CzModifierManagerCreator - Creates an instance of a modifier manager
00113 //
00114 class CzModifierManagerCreator : public IzXomlClassCreator
00115 {
00116 public:
00117     CzModifierManagerCreator()
00118     {
00119         setClassName("modifiers");
00120     }
00121     IzXomlResource* CreateInstance(IzXomlResource* parent)  { return new CzModifierManager(); }
00122 };
00123 
00124 // 
00125 // 
00126 //
00127 //
00128 // IzModifierCreator - Base class that is used by classes that create an instance of a class mdofier
00129 //
00130 //
00131 //
00132 //
00133 class IzModifierCreator
00134 {
00135 #if defined(_DEBUG)
00136     CzString                ClassName;              // Name of class
00137 #endif
00138     unsigned int            ClassNameHash;          // Hashed name of class
00139 public:
00140     void                    setClassName(const char* name)
00141     {
00142 #if defined(_DEBUG)
00143         ClassName = name;
00144 #endif
00145         ClassNameHash = CzString::CalculateHash(name);
00146     }
00147     unsigned int            getClassNameHash() const            { return ClassNameHash; }
00148 #if defined(_DEBUG)
00149     CzString&               getClasstName()                     { return ClassName; }
00150 #endif
00151 public:
00152     virtual IzModifier* CreateInstance() = 0;
00153 };
00154 
00155 // 
00156 // 
00157 //
00158 //
00159 // CzMods - CzMods is the main controller responsible for instantiating class modifiers
00160 // 
00161 // 
00162 // 
00163 //
00164 #define CZ_MODS CzMods::getInstance()
00165 class CzMods
00166 {
00167 public:
00168     CDEFINE_SINGLETON(CzMods)
00169 
00170     // Public access to class creator iteration
00171     typedef CzList<IzModifierCreator*>::iterator _Iterator;
00172     _Iterator               begin() { return ModifierCreators.begin(); }
00173     _Iterator               end() { return ModifierCreators.end(); }
00174 
00175 protected:
00176     // Properties
00177 public:
00178     void                    addModifier(IzModifierCreator* creator)
00179     {
00180 #if defined(_DEBUG)
00181         if (findCreator(creator->getClassNameHash()))
00182         {
00183             CzDebug::Log(CZ_DEBUG_CHANNEL_WARNING, "Mods - Modifier creator already exists in modifier creator list - ", creator->getClasstName().c_str());
00184             delete creator;
00185             return;
00186         }
00187 #endif  //(_DEBUG)
00188         ModifierCreators.push_back(creator);
00189     }
00190     IzModifierCreator*      findCreator(unsigned int name_hash);
00191     IzModifierCreator*      findCreator(const char* name);
00192     // Properties end
00193 
00194 protected:
00195     CzList<IzModifierCreator*> ModifierCreators;
00196 
00197 public:
00198     void            Init();
00199     void            Release();
00200 };
00201 
00202 #endif  // _CZ_MODIFIER_H_