Scroll all the way to the bottom for the header defines. This patch does what I described in the post before. The void* returned by each set function gives a pointer to the datatype to either make reading the variable simpler or so it can be edited discretely without tripping an event, should that be desired.
NOTE: I just noticed that the default return values of the "get" functions need to be moved to outside the first "if" test.
Code:
Index: GameEntity.cpp
===================================================================
--- GameEntity.cpp (revision 110)
+++ GameEntity.cpp (working copy)
@@ -108,6 +108,13 @@
GameEntity::~GameEntity()
{
delete m_privateData;
+ std::map< std::string, std::pair<PropertyType,void*> >::iterator t = m_properties.begin();
+ while(t != m_properties.end())
+ {
+ delete t->second.second;
+ t++;
+ }
+ m_properties.clear();
}
void GameEntity::addListener( GameEvent::EventID eventType, GameComponent* listener )
@@ -208,3 +215,225 @@
}
}
+
+
+void* GameEntity::setProperty(std::string name, int value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::INTEGER)
+ {
+ int* val = static_cast<int*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::INTEGER,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::INTEGER, new int(value));
+ throwPropertyEvent(name,PropertyType::INTEGER, m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+void* GameEntity::setProperty(std::string name, unsigned int value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::UNSIGNED)
+ {
+ unsigned int* val = static_cast<unsigned int*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::UNSIGNED,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::UNSIGNED, new unsigned int(value));
+ throwPropertyEvent(name,PropertyType::UNSIGNED,m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+void* GameEntity::setProperty(std::string name, float value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::FLOAT)
+ {
+ float* val = static_cast<float*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::FLOAT,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::FLOAT, new float(value));
+ throwPropertyEvent(name,PropertyType::FLOAT,m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+void* GameEntity::setProperty(std::string name, double value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::DOUBLE)
+ {
+ double* val = static_cast<double*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::DOUBLE,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::DOUBLE, new double(value));
+ throwPropertyEvent(name,PropertyType::DOUBLE,m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+void* GameEntity::setProperty(std::string name, std::string value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::STRING)
+ {
+ std::string* val = static_cast<std::string*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::STRING,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::STRING, new std::string(value));
+ throwPropertyEvent(name,PropertyType::STRING,m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+void* GameEntity::setProperty(std::string name, Vec3f value, GameComponent* editor)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::VEC)
+ {
+ Vec3f* val = static_cast<Vec3f*>(t->second.second);
+ *val = value;
+ throwPropertyEvent(name,PropertyType::VEC,t->second.second,editor);
+ return t->second.second;
+ }
+ return 0x0;
+ }
+ else
+ {
+ m_properties[name] = std::make_pair(PropertyType::VEC, new Vec3f(value));
+ throwPropertyEvent(name,PropertyType::VEC,m_properties[name].second,editor);
+ return m_properties[name].second;
+ }
+}
+
+int GameEntity::getPropertyInteger(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::INTEGER)
+ {
+ return *(static_cast<int*>(t->second.second));
+ }
+ return 0;
+ }
+}
+unsigned int GameEntity::getPropertyUnsigned(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::UNSIGNED)
+ {
+ return *(static_cast<unsigned int*>(t->second.second));
+ }
+ return 0;
+ }
+}
+float GameEntity::getPropertyFloat(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::FLOAT)
+ {
+ return *(static_cast<float*>(t->second.second));
+ }
+ return 0.0f;
+ }
+}
+double GameEntity::getPropertyDouble(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::DOUBLE)
+ {
+ return *(static_cast<double*>(t->second.second));
+ }
+ return 0.0;
+ }
+}
+std::string GameEntity::getPropertyString(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::STRING)
+ {
+ return *(static_cast<std::string*>(t->second.second));
+ }
+ return "";
+ }
+}
+Vec3f GameEntity::getPropertyVec3f(std::string name)
+{
+ std::map< std::string, std::pair< PropertyType, void*> >::iterator t;
+ t = m_properties.find(name);
+ if(t != m_properties.end())
+ {
+ if(t->second.first == PropertyType::VEC)
+ {
+ return *(static_cast<Vec3f*>(t->second.second));
+ }
+ return Vec3f(0.0f,0.0f,0.0f);
+ }
+}
+void GameEntity::throwPropertyEvent(std::string name, PropertyType type, void* value, GameComponent* editor)
+{
+ std::pair<std::string , std::pair<PropertyType,void*> > data;
+ data.first = name;
+ data.second.first = type;
+ data.second.second = value;
+ GameEvent event(GameEvent::E_SET_PROPERTY,&GameEventData(static_cast<void*>(&data)),editor);
+ checkEvent(&event);
+}
\ No newline at end of file
Index: GameEntity.h
===================================================================
--- GameEntity.h (revision 110)
+++ GameEntity.h (working copy)
@@ -36,6 +36,8 @@
#define GAMEENTITY_H_
#include <string>
+#include <map>
+#include <GameEngine/utMath.h>
#include "GameComponent.h"
@@ -64,6 +66,30 @@
friend struct DeleteEntity;
public:
+ enum PropertyType
+ {
+ INVALID = 0,
+ INTEGER,
+ UNSIGNED,
+ FLOAT,
+ DOUBLE,
+ STRING,
+ VEC,
+ COUNT
+ };
+ void* setProperty(std::string name, int value, GameComponent* editor = 0);
+ void* setProperty(std::string name, unsigned int value, GameComponent* editor = 0);
+ void* setProperty(std::string name, float value, GameComponent* editor = 0);
+ void* setProperty(std::string name, double value, GameComponent* editor = 0);
+ void* setProperty(std::string name, std::string value, GameComponent* editor = 0);
+ void* setProperty(std::string name, Vec3f value, GameComponent* editor = 0);
+
+ int getPropertyInteger(std::string name);
+ unsigned int getPropertyUnsigned(std::string name);
+ float getPropertyFloat(std::string name);
+ double getPropertyDouble(std::string name);
+ std::string getPropertyString(std::string name);
+ Vec3f getPropertyVec3f(std::string name);
/**
* \brief A unique id (string) of the entity within the GameWorld
@@ -184,7 +210,9 @@
/// Index for the entity within the GameWorld
unsigned int m_worldId;
-
+
+ std::map< std::string, std::pair< PropertyType, void* > > m_properties;
+ void throwPropertyEvent(std::string name, PropertyType type, void* value, GameComponent* editor);
};
/*! @}*/