Added Vector serialization and support in lua plugin top enable script users to set/get vector properties such as osg::Array, osg::PrimitiveSet and children lists.

This commit is contained in:
Robert Osfield
2014-02-24 10:19:48 +00:00
parent 6d68718fba
commit 4ef5d9eb5f
15 changed files with 1566 additions and 103 deletions

View File

@@ -29,6 +29,56 @@ namespace lua
// forward declare
class LuaScriptEngine;
struct SerializerScratchPad : public osg::Referenced
{
SerializerScratchPad(unsigned int s=256):dataType(osgDB::BaseSerializer::RW_UNDEFINED),dataSize(0) { maxDataSize = s; data = new char[s]; }
unsigned int maxDataSize;
char* data;
osgDB::BaseSerializer::Type dataType;
unsigned int dataSize;
void reset()
{
dataType = osgDB::BaseSerializer::RW_UNDEFINED;
dataSize = 0;
}
template<typename T>
bool set(const T& t)
{
if (sizeof(T)<=maxDataSize)
{
*(reinterpret_cast<T*>(data)) = t;
dataType = osgDB::getTypeEnum<T>();
dataSize = sizeof(T);
return true;
}
else
{
dataSize = 0;
dataType = osgDB::BaseSerializer::RW_UNDEFINED;
return false;
}
}
template<typename T>
bool get(T& t) const
{
if (sizeof(T)==dataSize && dataType == osgDB::getTypeEnum<T>())
{
t = *(reinterpret_cast<T*>(data));
return true;
}
else
{
return false;
}
}
};
class LuaScriptEngine : public osg::ScriptEngine
{
public:
@@ -47,6 +97,9 @@ class LuaScriptEngine : public osg::ScriptEngine
osgDB::PropertyInterface& getPropertyInterface() const { return _pi; }
int pushDataToStack(SerializerScratchPad* ssp) const;
int popDataFromStack(SerializerScratchPad* ssp, osgDB::BaseSerializer::Type type) const;
int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const;
int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const;
@@ -109,6 +162,7 @@ class LuaScriptEngine : public osg::ScriptEngine
bool popParameter(osg::Object* object) const;
osg::Object* popParameterObject() const;
void pushContainer(osg::Object* object, const std::string& propertyName) const;
void createAndPushObject(const std::string& compoundName) const;
void pushObject(osg::Object* object) const;
@@ -133,6 +187,24 @@ class LuaScriptEngine : public osg::ScriptEngine
else return 0;
}
std::string getStringFromTable(int pos, const std::string& field) const
{
std::string result;
if (lua_type(_lua, pos)==LUA_TTABLE)
{
lua_pushstring(_lua, field.c_str());
lua_rawget(_lua, pos);
if (lua_type(_lua, -1)==LUA_TSTRING)
{
result = lua_tostring(_lua, -1);
}
lua_pop(_lua,1);
}
return result;
}
std::string getObjectCompoundClassName(int pos) const
{
if (lua_type(_lua, pos)==LUA_TTABLE)