~ubuntu-branches/ubuntu/lucid/structure-synth/lucid

« back to all changes in this revision

Viewing changes to StructureSynth/JavaScriptSupport/JavaScriptParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2009-04-13 13:28:45 UTC
  • Revision ID: james.westby@ubuntu.com-20090413132845-d7d42t4llxjxq0ez
Tags: upstream-0.9
ImportĀ upstreamĀ versionĀ 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "JavaScriptParser.h"
 
2
 
 
3
#include <QScriptEngine>
 
4
#include <QMetaType>
 
5
#include "SyntopiaCore/Logging/Logging.h"
 
6
#include "SyntopiaCore/GLEngine/Sphere.h"
 
7
 
 
8
#include "Debug.h"
 
9
 
 
10
using namespace SyntopiaCore::Logging;
 
11
 
 
12
//Q_DECLARE_METATYPE(StructureSynth::JavaScriptSupport::Vector3)
 
13
 
 
14
 
 
15
namespace StructureSynth {
 
16
        namespace JavaScriptSupport {   
 
17
 
 
18
                namespace {
 
19
                        QScriptValue constructVector3(QScriptContext * context, QScriptEngine *engine)
 
20
                        {
 
21
                                //INFO("constructVector3");
 
22
                                StructureSynth::JavaScriptSupport::Vector3* bar = new StructureSynth::JavaScriptSupport::Vector3();
 
23
                                // initialize from arguments in context, if desired
 
24
                                //QScriptValue v =  engine->toScriptValue(bar);
 
25
                                QScriptValue v =  engine->newQObject(bar, QScriptEngine::ScriptOwnership);
 
26
                                //INFO("Old X: " + v.property("x").toString());
 
27
                                if (context->argumentCount() == 3) {
 
28
                                        v.setProperty("x", QScriptValue(engine,context->argument(0).toNumber()));
 
29
                                        v.setProperty("y", QScriptValue(engine,context->argument(1).toNumber()));
 
30
                                        v.setProperty("z", QScriptValue(engine,context->argument(2).toNumber()));
 
31
                                }
 
32
                                //INFO("constructVector3 - end");
 
33
                                
 
34
                                return v;
 
35
                        }
 
36
 
 
37
                        QScriptValue vector3ToScriptValue(QScriptEngine *engine, const StructureSynth::JavaScriptSupport::Vector3 &s)
 
38
                        {
 
39
                                //INFO("<-toScriptValue: From " + s.toString());
 
40
                                QScriptValue obj = engine->newObject();
 
41
                                /*
 
42
                                Vector3 v;
 
43
                                v.writeX(12);
 
44
                                v.writeY(22);
 
45
                                v.writeZ(32);
 
46
                                obj.setPrototype(engine->newQObject(&v));
 
47
                                */
 
48
                        
 
49
                                obj.setProperty("x", QScriptValue(engine, s.readX()));
 
50
                                obj.setProperty("y", QScriptValue(engine, s.readY()));
 
51
                                obj.setProperty("z", QScriptValue(engine, s.readZ()));
 
52
                                return obj;
 
53
                        }
 
54
 
 
55
                        void vector3FromScriptValue(const QScriptValue &obj, StructureSynth::JavaScriptSupport::Vector3 &s)
 
56
                        {
 
57
                                //INFO("<-fromScriptValue");
 
58
                                s.writeX( obj.property("x").toNumber() );
 
59
                                s.writeY( obj.property("y").toNumber() );
 
60
                                s.writeZ( obj.property("z").toNumber() );
 
61
                        } 
 
62
 
 
63
                        QScriptValue addSphereStatic(QScriptContext* context, QScriptEngine* /*engine*/)
 
64
                        {
 
65
                                QScriptValue thisObject = context->thisObject();
 
66
                                QObject* w = thisObject.toQObject();
 
67
                                
 
68
                                if (context->argumentCount() == 2) {
 
69
                                        Vector3 v = qScriptValueToValue<Vector3>(context->argument(0));
 
70
                                        double radius = context->argument(1).toNumber();
 
71
                                        //INFO("Got: " + v.toString() + " and " + QString::number(radius));
 
72
                                        SyntopiaCore::GLEngine::Object3D* o = new SyntopiaCore::GLEngine::Sphere( v.getObj(), radius);
 
73
                                        o->setColor(((World*)w)->getRgb(), ((World*)w)->getAlpha());
 
74
                                        ((World*)w)->getEngine()->addObject(o);
 
75
                                } else {
 
76
                                        WARNING("addSphere expected two arguments..");
 
77
                                }
 
78
                                return QScriptValue();
 
79
                        }
 
80
                                
 
81
                        QScriptValue setColorStatic(QScriptContext* context, QScriptEngine* /*engine*/)
 
82
                        {
 
83
                                QScriptValue thisObject = context->thisObject();
 
84
                                QObject* w = thisObject.toQObject();
 
85
                                
 
86
                                if (context->argumentCount() == 2) {
 
87
                                        Vector3 v = qScriptValueToValue<Vector3>(context->argument(0));
 
88
                                        double alpha = context->argument(1).toNumber();
 
89
                                        ((World*)w)->setColor2(v, alpha);
 
90
                                } else {
 
91
                                        WARNING("setColor expected two arguments..");
 
92
                                }
 
93
                                return QScriptValue();
 
94
                        }
 
95
                        
 
96
 
 
97
                }
 
98
 
 
99
                JavaScriptParser::JavaScriptParser(SyntopiaCore::GLEngine::EngineWidget* engine3D) : engine3D(engine3D) {
 
100
                }
 
101
 
 
102
                JavaScriptParser::~JavaScriptParser() {
 
103
                }
 
104
                        
 
105
                void JavaScriptParser::parse(QString input) {
 
106
 
 
107
                        INFO("Initializing JavaScript environment.");
 
108
                        QScriptEngine engine;
 
109
 
 
110
                        // Setup the global objects...
 
111
                        Debug debugObject;
 
112
                        engine.globalObject().setProperty("Debug", engine.newQObject(&debugObject)); 
 
113
 
 
114
                        World world(engine3D);
 
115
                        QScriptValue w = engine.newQObject(&world);
 
116
                        w.setProperty("addSphere", engine.newFunction(addSphereStatic));
 
117
                        w.setProperty("setColor", engine.newFunction(setColorStatic));
 
118
                        engine.globalObject().setProperty("World", w); 
 
119
                         
 
120
 
 
121
                        Vector3 *v3PrototypeObject = new Vector3();
 
122
                        QScriptValue vProto = engine.newQObject(v3PrototypeObject);
 
123
                        int id = qMetaTypeId<StructureSynth::JavaScriptSupport::Vector3>();
 
124
                        engine.setDefaultPrototype(id, vProto);
 
125
                        QScriptValue vectorCtor = engine.newFunction(constructVector3, vProto);
 
126
                        engine.globalObject().setProperty("Vector3", vectorCtor); 
 
127
 
 
128
                        qScriptRegisterMetaType(&engine, vector3ToScriptValue, vector3FromScriptValue, vProto); 
 
129
                        //qScriptRegisterMetaType(&engine, vector3ToScriptValue, vector3FromScriptValue); 
 
130
 
 
131
                        // Execute and catch exceptions.
 
132
                        QScriptValue result = engine.evaluate(input);
 
133
                        if (engine.hasUncaughtException()) {
 
134
                                int line = engine.uncaughtExceptionLineNumber();
 
135
                                QString error =  QString("Uncaught exception at line %1:%2").arg(line).arg(result.toString());
 
136
                                WARNING(error);
 
137
                        } else {
 
138
                                INFO(result.toString());
 
139
                        }
 
140
 
 
141
                }
 
142
                
 
143
        }
 
144
}