History

use ScriptEngine in Particle::collideWithXXX() methods so that we get all script interfaces

Brad Hefta-Gaub committed 12 years ago

Commit 0df02611 parent 3efc1ea5 files · +671 −658Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 5 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+86−66
@@ -19,11 +19,14 @@ · 18 unmodified lines
1919 #include <PacketHeaders.h>
2020 #include <UUID.h>
2121 #include <VoxelConstants.h>
22+#include <ParticlesScriptingInterface.h>
2223
2324 #include <Sound.h>
2425
2526 #include "ScriptEngine.h"
2627
28+const unsigned int VISUAL_DATA_CALLBACK_USECS = (1.0 / 60.0) * 1000 * 1000;
29+
2730 int ScriptEngine::_scriptNumber = 1;
2831 VoxelsScriptingInterface ScriptEngine::_voxelsScriptingInterface;
2932 ParticlesScriptingInterface ScriptEngine::_particlesScriptingInterface;
@@ -31,7 +34,7 @@ ParticlesScriptingInterface ScriptEngine::_particlesScriptingInterface; · 1 unmodified line
3134 static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* engine) {
3235 QUrl soundURL = QUrl(context->argument(0).toString());
3336 QScriptValue soundScriptValue = engine->newQObject(new Sound(soundURL), QScriptEngine::ScriptOwnership);
34−
37+
3538 return soundScriptValue;
3639 }
3740
@@ -41,7 +44,8 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, bool wantMenuItems, · 3 unmodified lines
4144 _scriptContents = scriptContents;
4245 _isFinished = false;
4346 _isRunning = false;
44−
47+ _isInitialized = false;
48+
4549 // some clients will use these menu features
4650 _wantMenuItems = wantMenuItems;
4751 if (scriptMenuName) {
@@ -54,15 +58,6 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, bool wantMenuItems, · 6 unmodified lines
5458 }
5559 _menu = menu;
5660 _controllerScriptingInterface = controllerScriptingInterface;
57−
58− // hook up our interfaces
59− if (!Particle::getVoxelsScriptingInterface()) {
60− Particle::setVoxelsScriptingInterface(getVoxelsScriptingInterface());
61− }
62−
63− if (!Particle::getParticlesScriptingInterface()) {
64− Particle::setParticlesScriptingInterface(getParticlesScriptingInterface());
65− }
6661 }
6762
6863 ScriptEngine::~ScriptEngine() {
@@ -92,63 +87,89 @@ bool ScriptEngine::setScriptContents(const QString& scriptContents) { · 23 unmodified lines
9287
9388 Q_SCRIPT_DECLARE_QMETAOBJECT(AudioInjectorOptions, QObject*)
9489
95−void ScriptEngine::run() {
96− _isRunning = true;
97− QScriptEngine engine;
98−
90+void ScriptEngine::init() {
91+ if (_isInitialized) {
92+ return; // only initialize once
93+ }
94+
95+ _isInitialized = true;
96+
9997 _voxelsScriptingInterface.init();
10098 _particlesScriptingInterface.init();
101−
99+
102100 // register meta-type for glm::vec3 conversions
103− registerMetaTypes(&engine);
104−
105− QScriptValue agentValue = engine.newQObject(this);
106− engine.globalObject().setProperty("Agent", agentValue);
107−
108− QScriptValue voxelScripterValue = engine.newQObject(&_voxelsScriptingInterface);
109− engine.globalObject().setProperty("Voxels", voxelScripterValue);
110−
111− QScriptValue particleScripterValue = engine.newQObject(&_particlesScriptingInterface);
112− engine.globalObject().setProperty("Particles", particleScripterValue);
113−
114−
115− QScriptValue soundConstructorValue = engine.newFunction(soundConstructor);
116− QScriptValue soundMetaObject = engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue);
117− engine.globalObject().setProperty("Sound", soundMetaObject);
118−
119− QScriptValue injectionOptionValue = engine.scriptValueFromQMetaObject<AudioInjectorOptions>();
120− engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
121−
122− QScriptValue audioScriptingInterfaceValue = engine.newQObject(&_audioScriptingInterface);
123− engine.globalObject().setProperty("Audio", audioScriptingInterfaceValue);
124−
101+ registerMetaTypes(&_engine);
102+
103+ QScriptValue agentValue = _engine.newQObject(this);
104+ _engine.globalObject().setProperty("Agent", agentValue);
105+
106+ QScriptValue voxelScripterValue = _engine.newQObject(&_voxelsScriptingInterface);
107+ _engine.globalObject().setProperty("Voxels", voxelScripterValue);
108+
109+ QScriptValue particleScripterValue = _engine.newQObject(&_particlesScriptingInterface);
110+ _engine.globalObject().setProperty("Particles", particleScripterValue);
111+
112+ QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor);
113+ QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue);
114+ _engine.globalObject().setProperty("Sound", soundMetaObject);
115+
116+ QScriptValue injectionOptionValue = _engine.scriptValueFromQMetaObject<AudioInjectorOptions>();
117+ _engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
118+
119+ QScriptValue audioScriptingInterfaceValue = _engine.newQObject(&_audioScriptingInterface);
120+ _engine.globalObject().setProperty("Audio", audioScriptingInterfaceValue);
121+
125122 if (_controllerScriptingInterface) {
126− QScriptValue controllerScripterValue = engine.newQObject(_controllerScriptingInterface);
127− engine.globalObject().setProperty("Controller", controllerScripterValue);
123+ QScriptValue controllerScripterValue = _engine.newQObject(_controllerScriptingInterface);
124+ _engine.globalObject().setProperty("Controller", controllerScripterValue);
128125 }
129−
130− QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE));
131− engine.globalObject().setProperty("TREE_SCALE", treeScaleValue);
132−
133− const unsigned int VISUAL_DATA_CALLBACK_USECS = (1.0 / 60.0) * 1000 * 1000;
134−
126+
127+ QScriptValue treeScaleValue = _engine.newVariant(QVariant(TREE_SCALE));
128+ _engine.globalObject().setProperty("TREE_SCALE", treeScaleValue);
129+
135130 // let the VoxelPacketSender know how frequently we plan to call it
136131 _voxelsScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS);
137132 _particlesScriptingInterface.getParticlePacketSender()->setProcessCallIntervalHint(VISUAL_DATA_CALLBACK_USECS);
138133
139134 //qDebug() << "Script:\n" << _scriptContents << "\n";
140−
141− QScriptValue result = engine.evaluate(_scriptContents);
135+}
136+
137+void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {
138+ QScriptValue value = _engine.newQObject(object);
139+ _engine.globalObject().setProperty(name, value);
140+}
141+
142+void ScriptEngine::evaluate() {
143+ if (!_isInitialized) {
144+ init();
145+ }
146+
147+ QScriptValue result = _engine.evaluate(_scriptContents);
142148 qDebug() << "Evaluated script.\n";
143−
144− if (engine.hasUncaughtException()) {
145− int line = engine.uncaughtExceptionLineNumber();
149+
150+ if (_engine.hasUncaughtException()) {
151+ int line = _engine.uncaughtExceptionLineNumber();
146152 qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
147153 }
148−
154+}
155+
156+void ScriptEngine::run() {
157+ if (!_isInitialized) {
158+ init();
159+ }
160+ _isRunning = true;
161+
162+ QScriptValue result = _engine.evaluate(_scriptContents);
163+ qDebug() << "Evaluated script.\n";
164+
165+ if (_engine.hasUncaughtException()) {
166+ int line = _engine.uncaughtExceptionLineNumber();
167+ qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
168+ }
169+
149170 timeval startTime;
150171 gettimeofday(&startTime, NULL);
151−
172+
152173 int thisFrame = 0;
153174
154175 while (!_isFinished) {
@@ -166,15 +187,15 @@ void ScriptEngine::run() { · 11 unmodified lines
166187 if (_isFinished) {
167188 break;
168189 }
169−
190+
170191 bool willSendVisualDataCallBack = false;
171− if (_voxelsScriptingInterface.getVoxelPacketSender()->serversExist()) {
192+ if (_voxelsScriptingInterface.getVoxelPacketSender()->serversExist()) {
172193 // allow the scripter's call back to setup visual data
173194 willSendVisualDataCallBack = true;
174−
195+
175196 // release the queue of edit voxel messages.
176197 _voxelsScriptingInterface.getVoxelPacketSender()->releaseQueuedMessages();
177−
198+
178199 // since we're in non-threaded mode, call process so that the packets are sent
179200 if (!_voxelsScriptingInterface.getVoxelPacketSender()->isThreaded()) {
180201 _voxelsScriptingInterface.getVoxelPacketSender()->process();
@@ -184,23 +205,23 @@ void ScriptEngine::run() { · 3 unmodified lines
184205 if (_particlesScriptingInterface.getParticlePacketSender()->serversExist()) {
185206 // allow the scripter's call back to setup visual data
186207 willSendVisualDataCallBack = true;
187−
208+
188209 // release the queue of edit voxel messages.
189210 _particlesScriptingInterface.getParticlePacketSender()->releaseQueuedMessages();
190−
211+
191212 // since we're in non-threaded mode, call process so that the packets are sent
192213 if (!_particlesScriptingInterface.getParticlePacketSender()->isThreaded()) {
193214 _particlesScriptingInterface.getParticlePacketSender()->process();
194215 }
195216 }
196−
217+
197218 if (willSendVisualDataCallBack) {
198219 emit willSendVisualDataCallback();
199220 }
200221
201− if (engine.hasUncaughtException()) {
202− int line = engine.uncaughtExceptionLineNumber();
203− qDebug() << "Uncaught exception at line" << line << ":" << engine.uncaughtException().toString() << "\n";
222+ if (_engine.hasUncaughtException()) {
223+ int line = _engine.uncaughtExceptionLineNumber();
224+ qDebug() << "Uncaught exception at line" << line << ":" << _engine.uncaughtException().toString() << "\n";
204225 }
205226 }
206227 cleanMenuItems();
@@ -213,9 +234,8 @@ void ScriptEngine::run() { · 6 unmodified lines
213234 emit finished();
214235 _isRunning = false;
215236 }
216−
217−void ScriptEngine::stop() {
218− _isFinished = true;
237+void ScriptEngine::stop() {
238+ _isFinished = true;
219239 }
220240
221241