Added osg::CallbackObject to be used to extend C++ class from scripting languages by providing callback objects assigned to the osg::Object UserDataContainer, with the CallbackObject's Name used to map the "method" provided by the CallbackObject. The CallbackObject is implemented by the script engine to provide the neccessary glue to invoking the script with the appropriate input parameters and handling the output parameters.
To the Lua plugin added support for assigned lua functions to C++ osg::Objects via the new osg::CallbackObject mechanism. To invoke the scripts function from C++ one must get the CallbackObject and call run on it. Renamed ScriptCallback to ScriptNodeCallback to avoid possibly confusion between osg::CallbackObject and the ScriptNodeCallback.
This commit is contained in:
@@ -18,6 +18,53 @@
|
||||
|
||||
using namespace lua;
|
||||
|
||||
class LuaCallbackObject : public osg::CallbackObject
|
||||
{
|
||||
public:
|
||||
LuaCallbackObject(const std::string& methodName, const LuaScriptEngine* lse, int ref):_lse(lse),_ref(ref) { setName(methodName); }
|
||||
|
||||
virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const
|
||||
{
|
||||
int topBeforeCall = lua_gettop(_lse->getLuaState());
|
||||
|
||||
lua_rawgeti(_lse->getLuaState(), LUA_REGISTRYINDEX, _ref);
|
||||
|
||||
int numInputs = 1;
|
||||
_lse->pushParameter(object);
|
||||
|
||||
for(osg::Parameters::iterator itr = inputParameters.begin();
|
||||
itr != inputParameters.end();
|
||||
++itr)
|
||||
{
|
||||
_lse->pushParameter(itr->get());
|
||||
++numInputs;
|
||||
}
|
||||
|
||||
if (lua_pcall(_lse->getLuaState(), numInputs, LUA_MULTRET,0)!=0)
|
||||
{
|
||||
OSG_NOTICE<<"Lua error : "<<lua_tostring(_lse->getLuaState(), -1)<<std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
int topAfterCall = lua_gettop(_lse->getLuaState());
|
||||
int numReturns = topAfterCall-topBeforeCall;
|
||||
for(int i=1; i<=numReturns; ++i)
|
||||
{
|
||||
outputParameters.insert(outputParameters.begin(), _lse->popParameterObject());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int getRef() const { return _ref; }
|
||||
|
||||
protected:
|
||||
|
||||
osg::ref_ptr<const LuaScriptEngine> _lse;
|
||||
int _ref;
|
||||
};
|
||||
|
||||
|
||||
static int getProperty(lua_State * _lua)
|
||||
{
|
||||
const LuaScriptEngine* lse = reinterpret_cast<const LuaScriptEngine*>(lua_topointer(_lua, lua_upvalueindex(1)));
|
||||
@@ -439,6 +486,8 @@ bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, os
|
||||
}
|
||||
}
|
||||
|
||||
int topBeforeCall = lua_gettop(_lua);
|
||||
|
||||
if (entryPoint.empty())
|
||||
{
|
||||
ScriptMap::iterator itr = _loadedScripts.find(script);
|
||||
@@ -460,7 +509,6 @@ bool LuaScriptEngine::run(osg::Script* script, const std::string& entryPoint, os
|
||||
pushParameter(itr->get());
|
||||
}
|
||||
|
||||
int topBeforeCall = lua_gettop(_lua);
|
||||
|
||||
if (lua_pcall(_lua, inputParameters.size(), LUA_MULTRET,0)!=0)
|
||||
{
|
||||
@@ -566,6 +614,15 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string&
|
||||
return 1;
|
||||
}
|
||||
|
||||
osg::CallbackObject* co = osg::getCallbackObject(object, propertyName);
|
||||
LuaCallbackObject* lco = dynamic_cast<LuaCallbackObject*>(co);
|
||||
if (lco)
|
||||
{
|
||||
lua_rawgeti(_lua, LUA_REGISTRYINDEX, lco->getRef());
|
||||
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") has callback object method need to call it, ref="<<lco->getRef()<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") no property found."<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -740,6 +797,16 @@ int LuaScriptEngine::setPropertyFromStack(osg::Object* object, const std::string
|
||||
osgDB::BaseSerializer::Type type;
|
||||
if (!_pi.getPropertyType(object, propertyName, type))
|
||||
{
|
||||
if (lua_type(_lua,-1)==LUA_TFUNCTION)
|
||||
{
|
||||
int ref = luaL_ref(_lua, LUA_REGISTRYINDEX);
|
||||
|
||||
OSG_NOTICE<<"LuaScriptEngine::setPropertyFromStack("<<object<<", "<<propertyName<<") need to handle lua function assigment, ref="<<ref<<std::endl;
|
||||
osg::ref_ptr<LuaCallbackObject> lco = new LuaCallbackObject(propertyName, this, ref);
|
||||
object->getOrCreateUserDataContainer()->addUserObject(lco.get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
type = LuaScriptEngine::getType();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user