ScriptEngine 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_SCRIPTENGINE
  14. #define OSG_SCRIPTENGINE 1
  15. #include <osg/Object>
  16. #include <osg/Callback>
  17. #include <osg/NodeVisitor>
  18. #include <osg/UserDataContainer>
  19. namespace osg
  20. {
  21. // forward declare
  22. class ScriptEngine;
  23. /* Script class for wrapping a script and the language used in the script.*/
  24. class Script : public osg::Object
  25. {
  26. public:
  27. Script():_modifiedCount(0) {}
  28. Script(const std::string& language, const std::string& str): _language(language), _script(str), _modifiedCount(0) {}
  29. Script(const Script& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop), _language(rhs._language), _script(rhs._script), _modifiedCount(0) {}
  30. META_Object(osg, Script)
  31. void setLanguage(const std::string& language) { _language = language; dirty(); }
  32. const std::string& getLanguage() const{ return _language; }
  33. void setScript(const std::string& str) { _script = str; dirty(); }
  34. const std::string& getScript() const { return _script; }
  35. void dirty() { ++_modifiedCount; }
  36. unsigned int getModifiedCount() const { return _modifiedCount; }
  37. protected:
  38. virtual ~Script() {}
  39. std::string _language;
  40. std::string _script;
  41. unsigned int _modifiedCount;
  42. };
  43. /** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/
  44. class OSG_EXPORT ScriptNodeCallback : public osg::NodeCallback
  45. {
  46. public:
  47. ScriptNodeCallback(Script* script=0, const std::string& entryPoint="") : _script(script), _entryPoint(entryPoint) {}
  48. ScriptNodeCallback(const ScriptNodeCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  49. osg::Object(rhs,copyop),
  50. osg::Callback(rhs,copyop),
  51. osg::NodeCallback(rhs,copyop), _script(rhs._script) {}
  52. META_Object(osg, ScriptNodeCallback)
  53. /** Set the script to call.*/
  54. void setScript(osg::Script* script) { _script = script; }
  55. /** Get the script to call.*/
  56. osg::Script* getScript() { return _script.get(); }
  57. /** Get the script to call.*/
  58. const osg::Script* getScript() const { return _script.get(); }
  59. /** Set the entry point to call.*/
  60. void setEntryPoint(const std::string& script) { _entryPoint = script; }
  61. /** Get the script to call.*/
  62. const std::string& getEntryPoint() const { return _entryPoint; }
  63. /** find the ScriptEngine from looking at the UserDataContainers of nodes in scene graph above the ScriptCallback.*/
  64. osg::ScriptEngine* getScriptEngine(osg::NodePath& nodePath);
  65. /** NodeCallback method, calls the Script.*/
  66. virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
  67. protected:
  68. virtual ~ScriptNodeCallback() {}
  69. osg::ref_ptr<Script> _script;
  70. std::string _entryPoint;
  71. };
  72. /** ScriptEngine base class for integrating different scripting languages.
  73. * Concrete ScriptEngine's are provided by osgDB::readFile<ScriptEngine> */
  74. class ScriptEngine : public osg::Object
  75. {
  76. public:
  77. /** get the scripting language supported by the ScriptEngine.*/
  78. inline const std::string& getLanguage() const { return _language; }
  79. /** run a Script.*/
  80. bool run(osg::Script* script)
  81. {
  82. // assumpt empty input and output parameters lists
  83. Parameters inputParameters, outputParameters;
  84. return run(script, "", inputParameters, outputParameters);
  85. }
  86. /** run a Script.*/
  87. virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters) = 0;
  88. protected:
  89. ScriptEngine(const std::string& language):_language(language) { setName(language); }
  90. virtual ~ScriptEngine() {}
  91. std::string _language;
  92. };
  93. }
  94. #endif