[DEV] add capabilities on the creating system

This commit is contained in:
Edouard DUPIN 2013-08-20 18:11:40 +02:00
parent 327367b1a7
commit c741f6e1c8
2 changed files with 121 additions and 13 deletions

View File

@ -111,35 +111,113 @@ void ege::Environement::GetElementNearestFixed(const vec3& _sourcePosition,
}
}
static etk::Hash<ege::createElement_tf>& GetHachTableCreating(void)
class CallBackCreator
{
static etk::Hash<ege::createElement_tf> s_table;
public:
ege::createElement_string_tf m_string;
ege::createElement_ejson_tf m_ejson;
ege::createElement_exml_tf m_exml;
CallBackCreator(void) : m_string(NULL), m_ejson(NULL), m_exml(NULL) {};
};
static etk::Hash<CallBackCreator>& GetHachTableCreating(void)
{
static etk::Hash<CallBackCreator> s_table;
return s_table;
}
void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_tf _creator)
void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_string_tf _creator)
{
if (NULL == _creator) {
EGE_ERROR("Try to add an empty CREATOR ...");
return;
}
GetHachTableCreating().Add(_type, _creator);
if (GetHachTableCreating().Exist(_type)==true) {
GetHachTableCreating()[_type].m_string = _creator;
return;
}
CallBackCreator tmpp;
tmpp.m_string = _creator;
GetHachTableCreating().Add(_type, tmpp);
}
ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, const etk::UString& _description, bool _autoAddElement)
void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_ejson_tf _creator)
{
if (NULL == _creator) {
EGE_ERROR("Try to add an empty CREATOR ...");
return;
}
if (GetHachTableCreating().Exist(_type)==true) {
GetHachTableCreating()[_type].m_ejson = _creator;
return;
}
CallBackCreator tmpp;
tmpp.m_ejson = _creator;
GetHachTableCreating().Add(_type, tmpp);
}
void ege::Environement::AddCreator(const etk::UString& _type, ege::createElement_exml_tf _creator)
{
if (NULL == _creator) {
EGE_ERROR("Try to add an empty CREATOR ...");
return;
}
if (GetHachTableCreating().Exist(_type)==true) {
GetHachTableCreating()[_type].m_exml = _creator;
return;
}
CallBackCreator tmpp;
tmpp.m_exml = _creator;
GetHachTableCreating().Add(_type, tmpp);
}
ege::ElementGame* ege::Environement::CreateElement(
const etk::UString& _type,
const etk::UString* _description,
const ejson::Value* _value,
const exml::Node* _node,
bool _autoAddElement)
{
if (false==GetHachTableCreating().Exist(_type)) {
EGE_ERROR("Request creating of an type that is not known '" << _type << "'");
return NULL;
}
ege::createElement_tf pointerFunction = GetHachTableCreating()[_type];
if (NULL == pointerFunction) {
EGE_ERROR("NULL pointer ==> internal error... '" << _type << "'");
// internal error
return NULL;
CallBackCreator tmpp = GetHachTableCreating()[_type];
ege::ElementGame* tmpElement = NULL;
if (NULL != _value) {
if (NULL == tmpp.m_ejson) {
EGE_ERROR("ejson NULL pointer creator ==> internal error... '" << _type << "'");
// internal error
return NULL;
}
tmpElement = tmpp.m_ejson(*this, _value);
} else if (NULL != _node) {
if (NULL == tmpp.m_exml) {
EGE_ERROR("exml NULL pointer creator ==> internal error... '" << _type << "'");
// internal error
return NULL;
}
tmpElement = tmpp.m_exml(*this, _node);
} else if (NULL != _description) {
if (NULL == tmpp.m_string) {
EGE_ERROR("string NULL pointer creator ==> internal error... '" << _type << "'");
// internal error
return NULL;
}
tmpElement = tmpp.m_string(*this, *_description);
} else {
if (NULL != tmpp.m_ejson) {
tmpElement = tmpp.m_ejson(*this, NULL);
} else if (NULL != tmpp.m_exml) {
tmpElement = tmpp.m_exml(*this, NULL);
} else if (NULL != tmpp.m_string) {
tmpElement = tmpp.m_string(*this, "");
} else {
EGE_ERROR("No callback availlable !!! '" << _type << "'");
return NULL;
}
}
ege::ElementGame* tmpElement = pointerFunction(*this, _description);
if (NULL == tmpElement) {
EGE_ERROR("Sub creator han an error when creating element : '" << _type << "'");
return NULL;
@ -149,6 +227,25 @@ ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, co
}
return tmpElement;
}
ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, bool _autoAddElement)
{
return CreateElement(_type, NULL, NULL, NULL, _autoAddElement);
}
ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, const etk::UString& _description, bool _autoAddElement)
{
return CreateElement(_type, &_description, NULL, NULL, _autoAddElement);
}
ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, const ejson::Value* _value, bool _autoAddElement)
{
return CreateElement(_type, NULL, _value, NULL, _autoAddElement);
}
ege::ElementGame* ege::Environement::CreateElement(const etk::UString& _type, const exml::Node* _node, bool _autoAddElement)
{
return CreateElement(_type, NULL, NULL, _node, _autoAddElement);
}
void ege::Environement::AddElementGame(ege::ElementGame* _newElement)

View File

@ -15,11 +15,15 @@ class btDynamicsWorld;
#include <etk/Vector.h>
#include <etk/math/Vector3D.h>
#include <ejson/ejson.h>
#include <exml/exml.h>
namespace ege {
class ElementGame;
class Environement;
typedef ege::ElementGame* (*createElement_tf)(ege::Environement& _env, const etk::UString& _description);
typedef ege::ElementGame* (*createElement_string_tf)(ege::Environement& _env, const etk::UString& _description);
typedef ege::ElementGame* (*createElement_ejson_tf)(ege::Environement& _env, const ejson::Value* _value);
typedef ege::ElementGame* (*createElement_exml_tf)(ege::Environement& _env, const exml::Node* _node);
class ElementInteraction
{
@ -64,7 +68,9 @@ namespace ege {
* @param[in] _type Type of the element.
* @param[in] _creator Function pointer that reference the element creating.
*/
static void AddCreator(const etk::UString& _type, ege::createElement_tf _creator);
static void AddCreator(const etk::UString& _type, ege::createElement_string_tf _creator);
static void AddCreator(const etk::UString& _type, ege::createElement_ejson_tf _creator);
static void AddCreator(const etk::UString& _type, ege::createElement_exml_tf _creator);
/**
* @brief Create an element on the curent scene.
* @param[in] _type Type of the element that might be created.
@ -73,7 +79,12 @@ namespace ege {
* @return NULL if an error occured OR the pointer on the element and it is already added on the system.
* @note Pointer is return in case of setting properties on it...
*/
ege::ElementGame* CreateElement(const etk::UString& _type, bool _autoAddElement=true);
ege::ElementGame* CreateElement(const etk::UString& _type, const etk::UString& _description, bool _autoAddElement=true);
ege::ElementGame* CreateElement(const etk::UString& _type, const ejson::Value* _value, bool _autoAddElement=true);
ege::ElementGame* CreateElement(const etk::UString& _type, const exml::Node* _node, bool _autoAddElement=true);
protected:
ege::ElementGame* CreateElement(const etk::UString& _type, const etk::UString* _description, const ejson::Value* _value, const exml::Node* _node, bool _autoAddElement);
public:
class ResultNearestElement
{