From 5f8e2bda2f9b71110c2b57a83b5594b839abeb5e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 31 Jan 2014 16:20:29 +0000 Subject: [PATCH] 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. --- examples/osgpresentation/osgpresentation.cpp | 4 +- include/osg/ScriptEngine | 39 +++++++++-- src/osg/ScriptEngine.cpp | 11 ++- src/osgPlugins/lua/LuaScriptEngine.cpp | 69 ++++++++++++++++++- .../deprecated/SlideShowConstructor.cpp | 4 +- 5 files changed, 115 insertions(+), 12 deletions(-) diff --git a/examples/osgpresentation/osgpresentation.cpp b/examples/osgpresentation/osgpresentation.cpp index e150c90ce..94f8d24cf 100644 --- a/examples/osgpresentation/osgpresentation.cpp +++ b/examples/osgpresentation/osgpresentation.cpp @@ -58,7 +58,7 @@ int main(int argc, char** argv) itr != scripts.end(); ++itr) { - model->addUpdateCallback(new osg::ScriptCallback(itr->get())); + model->addUpdateCallback(new osg::ScriptNodeCallback(itr->get())); } std::string str; @@ -441,7 +441,7 @@ int main(int argc, char** argv) osg::ref_ptr script = osgDB::readFile(str); if (script.valid()) { - presentation->addUpdateCallback(new osg::ScriptCallback(script.get(),"update")); + presentation->addUpdateCallback(new osg::ScriptNodeCallback(script.get(),"update")); } } diff --git a/include/osg/ScriptEngine b/include/osg/ScriptEngine index 5ba15bf6b..85e5b97f5 100644 --- a/include/osg/ScriptEngine +++ b/include/osg/ScriptEngine @@ -17,6 +17,7 @@ #include #include #include +#include namespace osg { @@ -27,6 +28,7 @@ typedef std::vector< osg::ref_ptr > Parameters; // forward declare class ScriptEngine; + /* Script class for wrapping a script and the language used in the script.*/ class Script : public osg::Object { @@ -55,14 +57,41 @@ class Script : public osg::Object unsigned int _modifiedCount; }; +/** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/ +class OSG_EXPORT CallbackObject : public osg::Object +{ +public: + CallbackObject() {} + CallbackObject(const std::string& name) { setName(name); } + CallbackObject(const CallbackObject& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):osg::Object(rhs,copyop) {} + META_Object(osg, CallbackObject); + + inline bool run(osg::Object* object) const + { + osg::Parameters inputParameters; + osg::Parameters outputParameters; + return run(object, inputParameters, outputParameters); + } + + virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const; + +}; + +/** Convinience function for getting the CallbackObject associated with specificed name from an Object's UserDataContainer.*/ +inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name) +{ + osg::UserDataContainer* udc = object->getUserDataContainer(); + return udc ? dynamic_cast(udc->getUserObject(name)) : 0; +} + /** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/ -class OSG_EXPORT ScriptCallback : public osg::NodeCallback +class OSG_EXPORT ScriptNodeCallback : public osg::NodeCallback { public: - ScriptCallback(Script* script=0, const std::string& entryPoint="") : _script(script), _entryPoint(entryPoint) {} - ScriptCallback(const ScriptCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::NodeCallback(rhs,copyop), _script(rhs._script) {} + ScriptNodeCallback(Script* script=0, const std::string& entryPoint="") : _script(script), _entryPoint(entryPoint) {} + ScriptNodeCallback(const ScriptNodeCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::NodeCallback(rhs,copyop), _script(rhs._script) {} - META_Object(osg, ScriptCallback) + META_Object(osg, ScriptNodeCallback) /** Set the script to call.*/ void setScript(osg::Script* script) { _script = script; } @@ -81,7 +110,7 @@ class OSG_EXPORT ScriptCallback : public osg::NodeCallback protected: - virtual ~ScriptCallback() {} + virtual ~ScriptNodeCallback() {} osg::ref_ptr