openscenegraph
osg/ScriptEngine
Go to the documentation of this file.
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
14#ifndef OSG_SCRIPTENGINE
15#define OSG_SCRIPTENGINE 1
16
17#include <osg/Object>
18#include <osg/Callback>
19#include <osg/NodeVisitor>
20#include <osg/UserDataContainer>
21
22namespace osg
23{
24
25// forward declare
26class ScriptEngine;
27
28/* Script class for wrapping a script and the language used in the script.*/
29class Script : public osg::Object
30{
31 public:
32 Script():_modifiedCount(0) {}
33 Script(const std::string& language, const std::string& str): _language(language), _script(str), _modifiedCount(0) {}
34 Script(const Script& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop), _language(rhs._language), _script(rhs._script), _modifiedCount(0) {}
35
36 META_Object(osg, Script)
37
38 void setLanguage(const std::string& language) { _language = language; dirty(); }
39 const std::string& getLanguage() const{ return _language; }
40
41 void setScript(const std::string& str) { _script = str; dirty(); }
42 const std::string& getScript() const { return _script; }
43
44 void dirty() { ++_modifiedCount; }
45 unsigned int getModifiedCount() const { return _modifiedCount; }
46
47 protected:
48
49 virtual ~Script() {}
50
51 std::string _language;
52 std::string _script;
53 unsigned int _modifiedCount;
54};
55
56
57/** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/
58class OSG_EXPORT ScriptNodeCallback : public osg::NodeCallback
59{
60 public:
61 ScriptNodeCallback(Script* script=0, const std::string& entryPoint="") : _script(script), _entryPoint(entryPoint) {}
62 ScriptNodeCallback(const ScriptNodeCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
63 osg::Object(rhs,copyop),
64 osg::Callback(rhs,copyop),
65 osg::NodeCallback(rhs,copyop), _script(rhs._script) {}
66
67 META_Object(osg, ScriptNodeCallback)
68
69 /** Set the script to call.*/
70 void setScript(osg::Script* script) { _script = script; }
71
72 /** Get the script to call.*/
73 osg::Script* getScript() { return _script.get(); }
74
75 /** Get the script to call.*/
76 const osg::Script* getScript() const { return _script.get(); }
77
78 /** Set the entry point to call.*/
79 void setEntryPoint(const std::string& script) { _entryPoint = script; }
80
81 /** Get the script to call.*/
82 const std::string& getEntryPoint() const { return _entryPoint; }
83
84 /** find the ScriptEngine from looking at the UserDataContainers of nodes in scene graph above the ScriptCallback.*/
85 osg::ScriptEngine* getScriptEngine(osg::NodePath& nodePath);
86
87 /** NodeCallback method, calls the Script.*/
88 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
89
90 protected:
91
92 virtual ~ScriptNodeCallback() {}
93
94 osg::ref_ptr<Script> _script;
95 std::string _entryPoint;
96};
97
98/** ScriptEngine base class for integrating different scripting languages.
99 * Concrete ScriptEngine's are provided by osgDB::readFile<ScriptEngine> */
100class ScriptEngine : public osg::Object
101{
102 public:
103
104 /** get the scripting language supported by the ScriptEngine.*/
105 inline const std::string& getLanguage() const { return _language; }
106
107 /** run a Script.*/
108 bool run(osg::Script* script)
109 {
110 // assumpt empty input and output parameters lists
111 Parameters inputParameters, outputParameters;
112 return run(script, "", inputParameters, outputParameters);
113 }
114
115 /** run a Script.*/
116 virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters) = 0;
117
118 protected:
119
120 ScriptEngine(const std::string& language):_language(language) { setName(language); }
121 virtual ~ScriptEngine() {}
122
123 std::string _language;
124};
125
126}
127
128#endif