From afab78ed40517c492d71a874d4e9ece4b81714d6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 25 Dec 2013 17:36:32 +0000 Subject: [PATCH] Preliminary support for wrapping methods in the lua script plugin. --- examples/osgpresentation/osgpresentation.cpp | 4 +- include/osg/ScriptEngine | 5 +- include/osg/ValueObject | 4 +- src/osg/ScriptEngine.cpp | 4 +- src/osgPlugins/lua/CMakeLists.txt | 4 + src/osgPlugins/lua/GeodeMethods.cpp | 82 ++++++++++++ src/osgPlugins/lua/GroupMethods.cpp | 91 +++++++++++++ src/osgPlugins/lua/LuaScriptEngine.cpp | 128 +++++++++++++++---- src/osgPlugins/lua/LuaScriptEngine.h | 20 ++- src/osgPlugins/lua/MethodObject.cpp | 85 ++++++++++++ src/osgPlugins/lua/MethodObject.h | 72 +++++++++++ src/osgPlugins/lua/ReaderWriterLua.cpp | 6 +- 12 files changed, 466 insertions(+), 39 deletions(-) create mode 100644 src/osgPlugins/lua/GeodeMethods.cpp create mode 100644 src/osgPlugins/lua/GroupMethods.cpp create mode 100644 src/osgPlugins/lua/MethodObject.cpp create mode 100644 src/osgPlugins/lua/MethodObject.h diff --git a/examples/osgpresentation/osgpresentation.cpp b/examples/osgpresentation/osgpresentation.cpp index de13312f3..e150c90ce 100644 --- a/examples/osgpresentation/osgpresentation.cpp +++ b/examples/osgpresentation/osgpresentation.cpp @@ -451,8 +451,8 @@ int main(int argc, char** argv) osg::ref_ptr script = osgDB::readFile(str); if (script.valid()) { - osg::ScriptEngine::Parameters inputParameters; - osg::ScriptEngine::Parameters outputParameters; + osg::Parameters inputParameters; + osg::Parameters outputParameters; inputParameters.push_back(new osg::StringValueObject("string","my very first string input")); inputParameters.push_back(new osg::DoubleValueObject("double",1.234)); diff --git a/include/osg/ScriptEngine b/include/osg/ScriptEngine index 22e6b27d0..5ba15bf6b 100644 --- a/include/osg/ScriptEngine +++ b/include/osg/ScriptEngine @@ -21,6 +21,9 @@ namespace osg { +typedef std::vector< osg::ref_ptr > Parameters; + + // forward declare class ScriptEngine; @@ -93,8 +96,6 @@ class ScriptEngine : public osg::Object /** get the scripting language supported by the ScriptEngine.*/ inline const std::string& getLanguage() const { return _language; } - typedef std::vector< osg::ref_ptr > Parameters; - /** run a Script.*/ bool run(osg::Script* script) { diff --git a/include/osg/ValueObject b/include/osg/ValueObject index ed8ff2df2..8861d2a1c 100644 --- a/include/osg/ValueObject +++ b/include/osg/ValueObject @@ -95,7 +95,8 @@ class ValueObject : public Object virtual bool get(GetValueVisitor& /*gvv*/) const { return false; } virtual bool set(SetValueVisitor& /*gvv*/) { return false; } - protected: + +protected: virtual ~ValueObject() {} }; @@ -207,6 +208,7 @@ void osg::Object::setUserValue(const std::string& name, const T& value) else udc->addUserObject(new UserValueObject(name,value)); } + } #endif diff --git a/src/osg/ScriptEngine.cpp b/src/osg/ScriptEngine.cpp index 80d1a5e55..6c8e2e58b 100644 --- a/src/osg/ScriptEngine.cpp +++ b/src/osg/ScriptEngine.cpp @@ -45,12 +45,12 @@ void ScriptCallback::operator()(Node* node, NodeVisitor* nv) ref_ptr ref_nv(nv); { - ScriptEngine::Parameters inputParameters; + Parameters inputParameters; inputParameters.push_back(node); inputParameters.push_back(nv); // empty outputParameters - ScriptEngine::Parameters outputParameters; + Parameters outputParameters; engine->run(_script.get(), _entryPoint, inputParameters, outputParameters); } diff --git a/src/osgPlugins/lua/CMakeLists.txt b/src/osgPlugins/lua/CMakeLists.txt index da94edf0a..3ec9e5816 100644 --- a/src/osgPlugins/lua/CMakeLists.txt +++ b/src/osgPlugins/lua/CMakeLists.txt @@ -1,7 +1,11 @@ SET(TARGET_H + MethodObject.h LuaScriptEngine.h ) SET(TARGET_SRC + MethodObject.cpp + GeodeMethods.cpp + GroupMethods.cpp LuaScriptEngine.cpp ReaderWriterLua.cpp ) diff --git a/src/osgPlugins/lua/GeodeMethods.cpp b/src/osgPlugins/lua/GeodeMethods.cpp new file mode 100644 index 000000000..4247afd1a --- /dev/null +++ b/src/osgPlugins/lua/GeodeMethods.cpp @@ -0,0 +1,82 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include +#include "MethodObject.h" + +struct GeodeGetNumDrawables : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + osg::Geode* geode = reinterpret_cast(objectPtr); + outputParameters.push_back(new osg::UIntValueObject("return", geode->getNumDrawables())); + return true; + } +}; + + +struct GeodeGetDrawable : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Object* indexObject = inputParameters[0].get(); + osg::UIntValueObject* uivo = dynamic_cast(indexObject); + if (!uivo) return false; + + osg::Geode* geode = reinterpret_cast(objectPtr); + outputParameters.push_back(geode->getDrawable(uivo->getValue())); + + return true; + } +}; + +struct GeodeAddDrawable : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Drawable* child = dynamic_cast(inputParameters[0].get()); + if (!child) return false; + + osg::Geode* geode = reinterpret_cast(objectPtr); + geode->addDrawable(child); + + return true; + } +}; + + +struct GeodeRemoveDrawable : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Drawable* child = dynamic_cast(inputParameters[0].get()); + if (!child) return false; + + osg::Geode* geode = reinterpret_cast(objectPtr); + geode->removeDrawable(child); + + return true; + } +}; + +static osgDB::RegisterMethodObjectProxy s_getNumDrawables("osg::Geode","getNumDrawables",new GeodeGetNumDrawables()); +static osgDB::RegisterMethodObjectProxy s_getNumDrawable("osg::Geode","getDrawable",new GeodeGetDrawable()); +static osgDB::RegisterMethodObjectProxy s_getAddDrawable("osg::Geode","addDrawable",new GeodeAddDrawable()); +static osgDB::RegisterMethodObjectProxy s_getRemoveDrawable("osg::Geode","removeDrawable",new GeodeRemoveDrawable()); diff --git a/src/osgPlugins/lua/GroupMethods.cpp b/src/osgPlugins/lua/GroupMethods.cpp new file mode 100644 index 000000000..c1130ed51 --- /dev/null +++ b/src/osgPlugins/lua/GroupMethods.cpp @@ -0,0 +1,91 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include + +#include "MethodObject.h" + +using namespace osgDB; + +struct GroupGetNumChildren : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + osg::Group* group = reinterpret_cast(objectPtr); + outputParameters.push_back(new osg::UIntValueObject("return", group->getNumChildren())); + return true; + } +}; + +struct GroupGetChild : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Object* indexObject = inputParameters[0].get(); + OSG_NOTICE<<"GroupGetChild "<className()<(indexObject); + if (dvo) index = static_cast(dvo->getValue()); + else + { + osg::UIntValueObject* uivo = dynamic_cast(indexObject); + if (uivo) index = uivo->getValue(); + } + osg::Group* group = reinterpret_cast(objectPtr); + outputParameters.push_back(group->getChild(index)); + + return true; + } +}; + +struct GroupAddChild : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Node* child = dynamic_cast(inputParameters[0].get()); + if (!child) return false; + + osg::Group* group = reinterpret_cast(objectPtr); + group->addChild(child); + + return true; + } +}; + + +struct GroupRemoveChild : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Node* child = dynamic_cast(inputParameters[0].get()); + if (!child) return false; + + osg::Group* group = reinterpret_cast(objectPtr); + group->removeChild(child); + + return true; + } +}; + +static osgDB::RegisterMethodObjectProxy s_getNumChildren("osg::Group","getNumChildren",new GroupGetNumChildren()); +static osgDB::RegisterMethodObjectProxy s_getNumChild("osg::Group","getChild",new GroupGetChild()); +static osgDB::RegisterMethodObjectProxy s_getAddChild("osg::Group","addChild",new GroupAddChild()); +static osgDB::RegisterMethodObjectProxy s_getRemoveChild("osg::Group","removeChild",new GroupRemoveChild()); diff --git a/src/osgPlugins/lua/LuaScriptEngine.cpp b/src/osgPlugins/lua/LuaScriptEngine.cpp index f813f5deb..1b073c080 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.cpp +++ b/src/osgPlugins/lua/LuaScriptEngine.cpp @@ -12,6 +12,7 @@ */ #include "LuaScriptEngine.h" +#include "MethodObject.h" #include #include @@ -77,10 +78,45 @@ static int setProperty(lua_State* _lua) } } - OSG_NOTICE<<"Warning: Lua getProperty() not matched"<(lua_topointer(_lua, lua_upvalueindex(1))); + std::string methodName = lua_tostring(_lua, lua_upvalueindex(2)); + int n = lua_gettop(_lua); /* number of arguments */ + OSG_NOTICE<<"callClassMethod(), n = "<=1 && lua_type(_lua, 1)==LUA_TTABLE) + { + osg::Object* object = lse->getObjectFromTable(1); + OSG_NOTICE<<"callClassMethod() on "<className()<<" method name "<popParameterObject()); + } + + if (osgDB::MethodsObjectManager::instance()->run(object, object->getCompoundClassName(), methodName, inputParameters, outputParameters)) + { + for(osg::Parameters::iterator itr = outputParameters.begin(); + itr != outputParameters.end(); + ++itr) + { + OSG_NOTICE<<" pushing return "<<(*itr)->className()<pushParameter(itr->get()); + } + return outputParameters.size(); + } + } + return 0; +} + +#if 0 static int getChild(lua_State * _lua) { const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); @@ -112,16 +148,11 @@ static int getChild(lua_State * _lua) return 0; } - static int setChild(lua_State* _lua) { const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); - int n = lua_gettop(_lua); /* number of arguments */ - if (n==3 && - lua_type(_lua, 1)==LUA_TTABLE && - lua_type(_lua, 2)==LUA_TNUMBER && - lua_type(_lua, 3)==LUA_TTABLE) + if (lse->matchLuaParameters(LUA_TTABLE, LUA_TNUMBER, LUA_TTABLE)) { osg::Group* group = lse->getObjectFromTable(1); @@ -150,15 +181,13 @@ static int addChild(lua_State* _lua) { const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); - int n = lua_gettop(_lua); /* number of arguments */ - if (n==2 && - lua_type(_lua, 1)==LUA_TTABLE && - lua_type(_lua, 2)==LUA_TTABLE) + if (lse->matchLuaParameters(LUA_TTABLE, LUA_TTABLE)) { osg::Group* group = lse->getObjectFromTable(1); osg::Node* child = lse->getObjectFromTable(2); if (group && child) { + OSG_NOTICE<<"addChild("<className()<<") ptr = "<addChild(child); return 0; } @@ -172,8 +201,7 @@ static int getNumChildren(lua_State * _lua) { const LuaScriptEngine* lse = reinterpret_cast(lua_topointer(_lua, lua_upvalueindex(1))); - int n = lua_gettop(_lua); /* number of arguments */ - if (n==1 && (lua_type(_lua, 1)==LUA_TTABLE)) + if (lse->matchLuaParameters(LUA_TTABLE)) { osg::Group* group = lse->getObjectFromTable(1); if (group) @@ -186,6 +214,7 @@ static int getNumChildren(lua_State * _lua) OSG_NOTICE<<"Warning: Lua Group::getNumChildren() not matched"<getLuaState(); } + PushStackValueVisitor(const LuaScriptEngine* lsg) : _lsg(lsg) { _lua = const_cast(lsg)->getLuaState(); } virtual void apply(bool value) { lua_pushboolean(_lua, value ? 0 : 1); } virtual void apply(char value) { lua_pushnumber(_lua, value); } @@ -492,12 +521,12 @@ class GetStackValueVisitor : public osg::ValueObject::SetValueVisitor { public: - LuaScriptEngine* _lsg; + const LuaScriptEngine* _lsg; lua_State* _lua; int _index; int _numberToPop; - GetStackValueVisitor(LuaScriptEngine* lsg, int index) : _lsg(lsg), _lua(0), _index(index), _numberToPop(0) { _lua = lsg->getLuaState(); } + GetStackValueVisitor(const LuaScriptEngine* lsg, int index) : _lsg(lsg), _lua(0), _index(index), _numberToPop(0) { _lua = const_cast(lsg)->getLuaState(); } virtual void apply(bool& value) { if (lua_isboolean(_lua, _index)) { value = (lua_toboolean(_lua, _index)!=0); _numberToPop = 1; } } @@ -527,6 +556,16 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& osgDB::BaseSerializer::Type type; if (!_pi.getPropertyType(object, propertyName, type)) { + if (osgDB::MethodsObjectManager::instance()->hasMethod(object->getCompoundClassName(), propertyName)) + { + OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<(this)); + lua_pushstring(_lua, propertyName.c_str()); + lua_pushcclosure(_lua, callClassMethod, 2); + + return 1; + } + OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<(object); if (vo) @@ -1291,7 +1340,7 @@ bool LuaScriptEngine::pushParameter(osg::Object* object) return false; } -bool LuaScriptEngine::popParameter(osg::Object* object) +bool LuaScriptEngine::popParameter(osg::Object* object) const { osg::ValueObject* vo = dynamic_cast(object); if (vo) @@ -1308,7 +1357,7 @@ bool LuaScriptEngine::popParameter(osg::Object* object) return false; } -osg::Object* LuaScriptEngine::popParameterObject() +osg::Object* LuaScriptEngine::popParameterObject() const { osg::ref_ptr object = 0; @@ -1412,6 +1461,11 @@ osg::Object* LuaScriptEngine::popParameterObject() if (getValue(value)) object = new osg::MatrixdValueObject("", value); break; } + case(osgDB::BaseSerializer::RW_LIST): + { + OSG_NOTICE<<"Need to implement RW_LIST support"<className()); lua_settable(_lua, -3); osg::Group* group = dynamic_cast(object); + osg::Geode* geode = dynamic_cast(object); if (group) { +#if 0 + assignClosure("getChild", getChild); + assignClosure("setChild", setChild); + assignClosure("addChild", addChild); + assignClosure("getNumChildren", getNumChildren); +#endif +#if 0 lua_pushstring(_lua, "getChild"); lua_pushlightuserdata(_lua, const_cast(this)); lua_pushcclosure(_lua, getChild, 1); @@ -1505,7 +1567,18 @@ void LuaScriptEngine::pushObject(osg::Object* object) const lua_pushlightuserdata(_lua, const_cast(this)); lua_pushcclosure(_lua, getNumChildren, 1); lua_settable(_lua, -3); - +#endif + luaL_getmetatable(_lua, "LuaScriptEngine.Object"); + lua_setmetatable(_lua, -2); + } + else if (geode) + { +#if 0 + assignClosure("getDrawable",getDrawable,1); + assignClosure("setDrawable",setDrawable,1); + assignClosure("addDrawable",addDrawable,1); + assignClosure("getNumDrawables",getNumDrawables,1); +#endif luaL_getmetatable(_lua, "LuaScriptEngine.Object"); lua_setmetatable(_lua, -2); } @@ -1521,3 +1594,10 @@ void LuaScriptEngine::pushObject(osg::Object* object) const } } +void LuaScriptEngine::assignClosure(const char* name, lua_CFunction fn) const +{ + lua_pushstring(_lua, name); + lua_pushlightuserdata(_lua, const_cast(this)); + lua_pushcclosure(_lua, fn, 1); + lua_settable(_lua, -3); +} diff --git a/src/osgPlugins/lua/LuaScriptEngine.h b/src/osgPlugins/lua/LuaScriptEngine.h index fba5c5347..661b08e8d 100644 --- a/src/osgPlugins/lua/LuaScriptEngine.h +++ b/src/osgPlugins/lua/LuaScriptEngine.h @@ -26,6 +26,9 @@ extern "C" { namespace lua { +// forward declare +class LuaScriptEngine; + class LuaScriptEngine : public osg::ScriptEngine { public: @@ -37,10 +40,10 @@ class LuaScriptEngine : public osg::ScriptEngine virtual const std::string& getLanguage() const { return _language; } /** run a Script.*/ - virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters); + virtual bool run(osg::Script* script, const std::string& entryPoint, osg::Parameters& inputParameters, osg::Parameters& outputParameters); /** get the lua_State object.*/ - lua_State* getLuaState() { return _lua; } + lua_State* getLuaState() const { return _lua; } int pushPropertyToStack(osg::Object* object, const std::string& propertyName) const; int setPropertyFromStack(osg::Object* object, const std::string& propertyName) const; @@ -83,9 +86,9 @@ class LuaScriptEngine : public osg::ScriptEngine void pushValue(const osg::Plane& value) const; void pushValue(const osg::Matrixd& value) const; - bool pushParameter(osg::Object* object); - bool popParameter(osg::Object* object); - osg::Object* popParameterObject(); + bool pushParameter(osg::Object* object) const; + bool popParameter(osg::Object* object) const; + osg::Object* popParameterObject() const; void createAndPushObject(const std::string& compoundName) const; @@ -110,6 +113,13 @@ class LuaScriptEngine : public osg::ScriptEngine else return 0; } + void assignClosure(const char* name, lua_CFunction fn) const; + + bool matchLuaParameters(int luaType1) const { return ((lua_gettop(_lua)==1) && (lua_type(_lua, 1)==luaType1)); } + bool matchLuaParameters(int luaType1, int luaType2) const { return ((lua_gettop(_lua)==2) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2)); } + bool matchLuaParameters(int luaType1, int luaType2, int luaType3) const { return ((lua_gettop(_lua)==3) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2) && (lua_type(_lua, 3)==luaType3)); } + bool matchLuaParameters(int luaType1, int luaType2, int luaType3, int luaType4) const { return ((lua_gettop(_lua)==4) && (lua_type(_lua, 1)==luaType1) && (lua_type(_lua, 2)==luaType2) && (lua_type(_lua, 3)==luaType3) && (lua_type(_lua, 4)==luaType4)); } + protected: void initialize(); diff --git a/src/osgPlugins/lua/MethodObject.cpp b/src/osgPlugins/lua/MethodObject.cpp new file mode 100644 index 000000000..218c28d05 --- /dev/null +++ b/src/osgPlugins/lua/MethodObject.cpp @@ -0,0 +1,85 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include "MethodObject.h" +#include + +using namespace osgDB; + + +void MethodsObject::add(const std::string& methodName, MethodObject* mo) +{ + methodMap.insert(MethodMap::value_type(methodName, mo)); +} + +bool MethodsObject::run(void* objectPtr, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const +{ + MethodMap::const_iterator itr = methodMap.find(methodName); + while (itr != methodMap.end()) + { + if (itr->first==methodName) + { + OSG_NOTICE<<"Calling methodObject for "<second->run(objectPtr, inputParameters, outputParameters)) return true; + } + ++itr; + } + OSG_NOTICE<<"No matching methodObject found for "<& MethodsObjectManager::instance() +{ + static osg::ref_ptr s_MethodsObjectManager = new MethodsObjectManager; + return s_MethodsObjectManager; +} + +void MethodsObjectManager::add(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo) +{ + MethodsObjectMap::iterator itr = methodsObjects.find( compoundClassName ); + if (itr==methodsObjects.end()) + { + methodsObjects[compoundClassName] = new MethodsObject; + itr = methodsObjects.find( compoundClassName ); + } + itr->second->add(methodName, mo); +} + +bool MethodsObjectManager::run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const +{ + MethodsObjectMap::const_iterator itr = methodsObjects.find( compoundClassName ); + if (itr!=methodsObjects.end()) + { + return itr->second->run(objectPtr, methodName, inputParameters, outputParameters); + } + OSG_NOTICE<<"No matching methodObject found for class "<second->hasMethod(methodName); + } + return false; + +} diff --git a/src/osgPlugins/lua/MethodObject.h b/src/osgPlugins/lua/MethodObject.h new file mode 100644 index 000000000..e2b5ca482 --- /dev/null +++ b/src/osgPlugins/lua/MethodObject.h @@ -0,0 +1,72 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef METHODSOBJECT_H +#define METHODSOBJECT_H + +#include + +namespace osgDB +{ + +struct MethodObject : public osg::Referenced +{ + typedef std::vector< osg::ref_ptr > Parameters; + + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const = 0; + virtual ~MethodObject() {} +}; + +struct MethodsObject : public osg::Referenced +{ + MethodsObject() {} + virtual ~MethodsObject() {} + + void add(const std::string& methodName, MethodObject* mo); + + bool run(void* objectPtr, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const; + + bool hasMethod(const std::string& methodName) const; + + typedef std::multimap< std::string, osg::ref_ptr > MethodMap; + MethodMap methodMap; +}; + +struct MethodsObjectManager : public osg::Referenced +{ + MethodsObjectManager() {} + virtual ~MethodsObjectManager() {} + + static osg::ref_ptr& instance(); + + void add(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo); + + bool run(void* objectPtr, const std::string& compoundClassName, const std::string& methodName, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const; + + bool hasMethod(const std::string& compoundClassName, const std::string& methodName) const; + + typedef std::map< std::string, osg::ref_ptr > MethodsObjectMap; + MethodsObjectMap methodsObjects; +}; + +struct RegisterMethodObjectProxy +{ + RegisterMethodObjectProxy(const std::string& compoundClassName, const std::string& methodName, MethodObject* mo) + { + MethodsObjectManager::instance()->add(compoundClassName, methodName, mo); + } +}; + +} + +#endif diff --git a/src/osgPlugins/lua/ReaderWriterLua.cpp b/src/osgPlugins/lua/ReaderWriterLua.cpp index a1c66a8a8..849a0bd10 100644 --- a/src/osgPlugins/lua/ReaderWriterLua.cpp +++ b/src/osgPlugins/lua/ReaderWriterLua.cpp @@ -73,8 +73,8 @@ class ReaderWriterLua : public osgDB::ReaderWriter if (!script) return ReadResult::ERROR_IN_READING_FILE; std::string entryPoint = ""; - osg::ScriptEngine::Parameters inputParameters; - osg::ScriptEngine::Parameters outputParameters; + osg::Parameters inputParameters; + osg::Parameters outputParameters; osg::ref_ptr se = new lua::LuaScriptEngine(); if (!se->run(script.get(), entryPoint, inputParameters, outputParameters)) return 0; @@ -84,7 +84,7 @@ class ReaderWriterLua : public osgDB::ReaderWriter typedef std::vector< osg::ref_ptr > Objects; Objects objects; - for(osg::ScriptEngine::Parameters::iterator itr = outputParameters.begin(); + for(osg::Parameters::iterator itr = outputParameters.begin(); itr != outputParameters.end(); ++itr) {