File: ScriptEngine

package info (click to toggle)
openscenegraph-3.4 3.4.1%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,912 kB
  • sloc: cpp: 392,492; ansic: 23,625; java: 1,020; yacc: 548; makefile: 449; objc: 288; xml: 155; lex: 151
file content (122 lines) | stat: -rw-r--r-- 4,311 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_SCRIPTENGINE
#define OSG_SCRIPTENGINE 1

#include <osg/Object>
#include <osg/Callback>
#include <osg/NodeVisitor>
#include <osg/UserDataContainer>

namespace osg
{

// forward declare
class ScriptEngine;

/* Script class for wrapping a script and the language used in the script.*/
class Script : public osg::Object
{
    public:
        Script():_modifiedCount(0) {}
        Script(const std::string& language, const std::string& str): _language(language), _script(str), _modifiedCount(0)  {}
        Script(const Script& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop), _language(rhs._language), _script(rhs._script), _modifiedCount(0) {}

        META_Object(osg, Script)

        void setLanguage(const std::string& language) { _language = language; dirty(); }
        const std::string& getLanguage() { return _language; }

        void setScript(const std::string& str) { _script = str; dirty(); }
        const std::string& getScript() const { return _script; }

        void dirty() { ++_modifiedCount; }
        unsigned int getModifiedCount() const { return _modifiedCount; }

    protected:

        virtual ~Script() {}

        std::string _language;
        std::string _script;
        unsigned int _modifiedCount;
};


/** NodeCallback for attaching a script to a NodeCallback so that it can be called as an update or event callback.*/
class OSG_EXPORT ScriptNodeCallback : public osg::NodeCallback
{
    public:
        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::Object(rhs,copyop),
            osg::Callback(rhs,copyop),
            osg::NodeCallback(rhs,copyop), _script(rhs._script) {}

        META_Object(osg, ScriptNodeCallback)

        /** Set the script to call.*/
        void setScript(osg::Script* script) { _script  = script; }

        /** Get the script to call.*/
        osg::Script* getScript() { return _script.get(); }

        /** Get the script to call.*/
        const osg::Script* getScript() const { return _script.get(); }

        /** find the ScriptEngine from looking at the UserDataContainers of nodes in scene graph above the ScriptCallback.*/
        osg::ScriptEngine* getScriptEngine(osg::NodePath& nodePath);

        /** NodeCallback method, calls the Script.*/
        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);

    protected:

        virtual ~ScriptNodeCallback() {}

        osg::ref_ptr<Script>    _script;
        std::string             _entryPoint;
};

/** ScriptEngine base class for integrating different scripting languages.
 *  Concrete ScriptEngine's are provided by osgDB::readFile<ScriptEngine> */
class ScriptEngine : public osg::Object
{
    public:

        /** get the scripting language supported by the ScriptEngine.*/
        inline const std::string& getLanguage() const { return _language; }

        /** run a Script.*/
        bool run(osg::Script* script)
        {
            // assumpt empty input and output parameters lists
            Parameters inputParameters, outputParameters;
            return run(script, "", inputParameters, outputParameters);
        }

        /** run a Script.*/
        virtual bool run(osg::Script* script, const std::string& entryPoint, Parameters& inputParameters, Parameters& outputParameters) = 0;

    protected:

        ScriptEngine(const std::string& language):_language(language) { setName(language); }
        virtual ~ScriptEngine() {}

        std::string _language;
};

}

#endif