From ea865d0ff9651f8c766515407eae21b0bb34a63c Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 13 Mar 2014 10:28:18 +0000 Subject: [PATCH] Added support for directly invoking scripts using and to enable scripts to interact with each other. --- .../deprecated/SlideShowConstructor | 1 + src/osgPlugins/p3d/ReaderWriterP3D.cpp | 159 +++++++++++++++++- .../deprecated/SlideShowConstructor.cpp | 12 ++ 3 files changed, 169 insertions(+), 3 deletions(-) diff --git a/include/osgPresentation/deprecated/SlideShowConstructor b/include/osgPresentation/deprecated/SlideShowConstructor index 078a9e676..b5c49cccd 100644 --- a/include/osgPresentation/deprecated/SlideShowConstructor +++ b/include/osgPresentation/deprecated/SlideShowConstructor @@ -527,6 +527,7 @@ public: HUDSettings* getHUDSettings() { return _hudSettings.get(); } const HUDSettings* getHUDSettings() const { return _hudSettings.get(); } + osg::ScriptEngine* getOrCreateScriptEngine(const std::string& language); protected: diff --git a/src/osgPlugins/p3d/ReaderWriterP3D.cpp b/src/osgPlugins/p3d/ReaderWriterP3D.cpp index e5e0f44ee..5def0dc23 100644 --- a/src/osgPlugins/p3d/ReaderWriterP3D.cpp +++ b/src/osgPlugins/p3d/ReaderWriterP3D.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -175,6 +176,7 @@ public: bool parsePropertyAnimation(osgDB::XmlNode* root, osgPresentation::PropertyAnimation& pa) const; void parseModel(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; + void parseModelScript(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename, float scale) const; void parseVolume(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; @@ -195,6 +197,9 @@ public: void parsePage (osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; + void parseRunScriptFile (osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; + void parseRunScript (osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; + void parseSlide (osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur, bool parseTitles=true, bool parseLayers=true) const; void parsePdfDocument (osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const; @@ -1269,9 +1274,8 @@ bool ReaderWriterP3DXML::getJumpProperties(osgDB::XmlNode* cur, osgPresentation: void ReaderWriterP3DXML::parseModel(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const { - osgPresentation::SlideShowConstructor::PositionData positionData = constructor.getModelPositionData(); - bool positionRead = getProperties(cur, positionData); + bool positionRead = getProperties(cur, positionData); osgPresentation::SlideShowConstructor::ModelData modelData;// = constructor.getModelData(); getProperties(cur, modelData); @@ -1292,6 +1296,59 @@ void ReaderWriterP3DXML::parseModel(osgPresentation::SlideShowConstructor& const } +void ReaderWriterP3DXML::parseModelScript(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const +{ + osgPresentation::SlideShowConstructor::PositionData positionData = constructor.getModelPositionData(); + bool positionRead = getProperties(cur, positionData); + + osgPresentation::SlideShowConstructor::ModelData modelData;// = constructor.getModelData(); + getProperties(cur, modelData); + + osgPresentation::SlideShowConstructor::ScriptData scriptData; + getProperties(cur, scriptData); + + std::string language = "lua"; + getProperty(cur, "language", language); + + std::string function = ""; + getProperty(cur, "function", function); + + std::string scriptContents = cur->contents; + + if (!scriptContents.empty()) + { + osg::ref_ptr script = new osg::Script; + script->setLanguage(language); + script->setScript(scriptContents); + + osg::ScriptEngine* se = constructor.getOrCreateScriptEngine(language); + if (se) + { + osg::Parameters inputParameters, outputParameters; + se->run(script.get(), function, inputParameters, outputParameters); + + for(osg::Parameters::iterator itr = outputParameters.begin(); + itr != outputParameters.end(); + ++itr) + { + OSG_NOTICE<<"Parsing return object "<<(*itr)->className()<(itr->get()); + if (model) + { + OSG_NOTICE<<"Adding model "<name, "model-script")) + { + parseModelScript(constructor, cur); + return true; + } else if (match(cur->name, "volume")) { parseVolume(constructor, cur); @@ -2144,6 +2206,28 @@ void ReaderWriterP3DXML::parseLayer(osgPresentation::SlideShowConstructor& const OSG_NOTICE<<"key_to_jump failed."<name, "script_file")) + { + std::string name; + getProperty(cur, "name", name); + constructor.addScriptFile(name, cur->contents); + } + else if (match(cur->name, "script")) + { + std::string name; + getProperty(cur, "name", name); + std::string language("lua"); + getProperty(cur, "language", language); + constructor.addScript(name, language, cur->contents); + } + else if (match(cur->name, "run_script_file")) + { + parseRunScriptFile(constructor, cur); + } + else if (match(cur->name, "run_script")) + { + parseRunScript(constructor, cur); + } else { osgPresentation::KeyPosition keyPosition; @@ -2479,6 +2563,28 @@ void ReaderWriterP3DXML::parseSlide (osgPresentation::SlideShowConstructor& cons } } } + else if (match(cur->name, "script_file")) + { + std::string name; + getProperty(cur, "name", name); + constructor.addScriptFile(name, cur->contents); + } + else if (match(cur->name, "script")) + { + std::string name; + getProperty(cur, "name", name); + std::string language("lua"); + getProperty(cur, "language", language); + constructor.addScript(name, language, cur->contents); + } + else if (match(cur->name, "run_script_file")) + { + parseRunScriptFile(constructor, cur); + } + else if (match(cur->name, "run_script")) + { + parseRunScript(constructor, cur); + } else if (getKeyPosition(cur, keyPosition)) { constructor.addSlideKey(keyPosition); @@ -2506,8 +2612,47 @@ void ReaderWriterP3DXML::parseSlide (osgPresentation::SlideShowConstructor& cons return; } -#include +void ReaderWriterP3DXML::parseRunScriptFile(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const +{ + std::string function = ""; + getProperty(cur, "function", function); + osg::ref_ptr script = osgDB::readFile(cur->getTrimmedContents()); + if (script.valid()) + { + osg::ScriptEngine* se = constructor.getOrCreateScriptEngine(script->getLanguage()); + if (se) + { + osg::Parameters inputParameters, outputParameters; + se->run(script.get(), function, inputParameters, outputParameters); + } + } +} + +void ReaderWriterP3DXML::parseRunScript(osgPresentation::SlideShowConstructor& constructor, osgDB::XmlNode* cur) const +{ + std::string language = "lua"; + getProperty(cur, "language", language); + + std::string function = ""; + getProperty(cur, "function", function); + + std::string scriptContents = cur->contents; + + if (!scriptContents.empty()) + { + osg::ref_ptr script = new osg::Script; + script->setLanguage(language); + script->setScript(scriptContents); + + osg::ScriptEngine* se = constructor.getOrCreateScriptEngine(language); + if (se) + { + osg::Parameters inputParameters, outputParameters; + se->run(script.get(), function, inputParameters, outputParameters); + } + } +} struct MyFindFileCallback : public osgDB::FindFileCallback { @@ -3101,6 +3246,14 @@ osg::Node* ReaderWriterP3DXML::parseXmlGraph(osgDB::XmlNode* root, bool readOnly getProperty(cur, "language", language); constructor.addScript(name, language, cur->contents); } + else if (match(cur->name, "run_script_file")) + { + parseRunScriptFile(constructor, cur); + } + else if (match(cur->name, "run_script")) + { + parseRunScript(constructor, cur); + } else if (match(cur->name, "name")) { constructor.setPresentationName(cur->contents); diff --git a/src/osgPresentation/deprecated/SlideShowConstructor.cpp b/src/osgPresentation/deprecated/SlideShowConstructor.cpp index 7046a95b9..ae2749ecd 100644 --- a/src/osgPresentation/deprecated/SlideShowConstructor.cpp +++ b/src/osgPresentation/deprecated/SlideShowConstructor.cpp @@ -299,6 +299,18 @@ void SlideShowConstructor::setPresentationDuration(double duration) } } +osg::ScriptEngine* SlideShowConstructor::getOrCreateScriptEngine(const std::string& language) +{ + ScriptEngineMap::iterator itr = _scriptEngines.find(language); + if (itr==_scriptEngines.end()) + { + addScriptEngine(language); + itr = _scriptEngines.find(language); + } + + return (itr!=_scriptEngines.end()) ? itr->second.get() : 0; +} + void SlideShowConstructor::addScriptEngine(const std::string& scriptEngineName) { if (_scriptEngines.count(scriptEngineName)!=0)