History

Merge branch 'master' of https://github.com/highfidelity/hifi into virtualEntities

ZappoMan committed 12 years ago

Commit 805a88b2 parents 30aee70 f438ae2184 files · +7608 −3041Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+93−79
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <AudioInjector.h>
2121 #include <AudioRingBuffer.h>
2222 #include <AvatarData.h>
23+#include <Bitstream.h>
2324 #include <CollisionInfo.h>
2425 #include <EntityScriptingInterface.h>
2526 #include <NetworkAccessManager.h>
@@ -71,6 +72,14 @@ void injectorFromScriptValue(const QScriptValue &object, AudioInjector* &out) { · 45 unmodified lines
7172 out = qobject_cast<AudioInjector*>(object.toQObject());
7273 }
7374
75+QScriptValue injectorToScriptValueInputController(QScriptEngine *engine, AbstractInputController* const &in) {
76+ return engine->newQObject(in);
77+}
78+
79+void injectorFromScriptValueInputController(const QScriptValue &object, AbstractInputController* &out) {
80+ out = qobject_cast<AbstractInputController*>(object.toQObject());
81+}
82+
7483 ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString,
7584 AbstractControllerScriptingInterface* controllerScriptingInterface) :
7685
@@ -78,7 +87,6 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 1 unmodified line
7887 _isFinished(false),
7988 _isRunning(false),
8089 _isInitialized(false),
81− _engine(),
8290 _isAvatar(false),
8391 _avatarIdentityTimer(NULL),
8492 _avatarBillboardTimer(NULL),
@@ -104,7 +112,6 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 19 unmodified lines
104112 _isFinished(false),
105113 _isRunning(false),
106114 _isInitialized(false),
107− _engine(),
108115 _isAvatar(false),
109116 _avatarIdentityTimer(NULL),
110117 _avatarBillboardTimer(NULL),
@@ -185,7 +192,7 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 74 unmodified lines
185192 _avatarData = avatarData;
186193
187194 // remove the old Avatar property, if it exists
188− _engine.globalObject().setProperty(objectName, QScriptValue());
195+ globalObject().setProperty(objectName, QScriptValue());
189196
190197 // give the script engine the new Avatar script property
191198 registerGlobalObject(objectName, _avatarData);
@@ -193,7 +200,7 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 1 unmodified line
193200
194201 void ScriptEngine::setAvatarHashMap(AvatarHashMap* avatarHashMap, const QString& objectName) {
195202 // remove the old Avatar property, if it exists
196− _engine.globalObject().setProperty(objectName, QScriptValue());
203+ globalObject().setProperty(objectName, QScriptValue());
197204
198205 // give the script engine the new avatar hash map
199206 registerGlobalObject(objectName, avatarHashMap);
@@ -222,46 +229,55 @@ void ScriptEngine::init() { · 22 unmodified lines
222229 _particlesScriptingInterface.init();
223230
224231 // register various meta-types
225− registerMetaTypes(&_engine);
226− registerMIDIMetaTypes(&_engine);
227− registerVoxelMetaTypes(&_engine);
228− registerEventTypes(&_engine);
229− registerMenuItemProperties(&_engine);
230− registerAnimationTypes(&_engine);
231− registerAvatarTypes(&_engine);
232−
233− qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue);
234− qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue);
235− qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine);
236−
237− qScriptRegisterMetaType(&_engine, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValue);
238− qScriptRegisterMetaType(&_engine, EntityItemIDtoScriptValue, EntityItemIDfromScriptValue);
239− qScriptRegisterMetaType(&_engine, RayToEntityIntersectionResultToScriptValue, RayToEntityIntersectionResultFromScriptValue);
240− qScriptRegisterSequenceMetaType<QVector<EntityItemID> >(&_engine);
241−
242− qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(&_engine);
243− qScriptRegisterSequenceMetaType<QVector<glm::quat> >(&_engine);
244− qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine);
245−
246− QScriptValue xmlHttpRequestConstructorValue = _engine.newFunction(XMLHttpRequestClass::constructor);
247− _engine.globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue);
248−
249− QScriptValue printConstructorValue = _engine.newFunction(debugPrint);
250− _engine.globalObject().setProperty("print", printConstructorValue);
251−
252− QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor);
253− QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue);
254− _engine.globalObject().setProperty("Sound", soundMetaObject);
255−
256− QScriptValue injectionOptionValue = _engine.scriptValueFromQMetaObject<AudioInjectorOptions>();
257− _engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
258−
259− QScriptValue localVoxelsValue = _engine.scriptValueFromQMetaObject<LocalVoxels>();
260− _engine.globalObject().setProperty("LocalVoxels", localVoxelsValue);
232+ registerMetaTypes(this);
233+ registerMIDIMetaTypes(this);
234+ registerVoxelMetaTypes(this);
235+ registerEventTypes(this);
236+ registerMenuItemProperties(this);
237+ registerAnimationTypes(this);
238+ registerAvatarTypes(this);
239+ Bitstream::registerTypes(this);
240+
241+ qScriptRegisterMetaType(this, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue);
242+ qScriptRegisterMetaType(this, ParticleIDtoScriptValue, ParticleIDfromScriptValue);
243+ qScriptRegisterSequenceMetaType<QVector<ParticleID> >(this);
244+
245+ /**
246+ qScriptRegisterMetaType(this, ModelItemPropertiesToScriptValue, ModelItemPropertiesFromScriptValue);
247+ qScriptRegisterMetaType(this, ModelItemIDtoScriptValue, ModelItemIDfromScriptValue);
248+ qScriptRegisterMetaType(this, RayToModelIntersectionResultToScriptValue, RayToModelIntersectionResultFromScriptValue);
249+ qScriptRegisterSequenceMetaType<QVector<ModelItemID> >(this);
250+ **/
251+
252+ qScriptRegisterMetaType(this, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValue);
253+ qScriptRegisterMetaType(this, EntityItemIDtoScriptValue, EntityItemIDfromScriptValue);
254+ qScriptRegisterMetaType(this, RayToEntityIntersectionResultToScriptValue, RayToEntityIntersectionResultFromScriptValue);
255+ qScriptRegisterSequenceMetaType<QVector<EntityItemID> >(this);
256+
257+ qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(this);
258+ qScriptRegisterSequenceMetaType<QVector<glm::quat> >(this);
259+ qScriptRegisterSequenceMetaType<QVector<QString> >(this);
260+
261+ QScriptValue xmlHttpRequestConstructorValue = newFunction(XMLHttpRequestClass::constructor);
262+ globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue);
263+
264+ QScriptValue printConstructorValue = newFunction(debugPrint);
265+ globalObject().setProperty("print", printConstructorValue);
266+
267+ QScriptValue soundConstructorValue = newFunction(soundConstructor);
268+ QScriptValue soundMetaObject = newQMetaObject(&Sound::staticMetaObject, soundConstructorValue);
269+ globalObject().setProperty("Sound", soundMetaObject);
270+
271+ QScriptValue injectionOptionValue = scriptValueFromQMetaObject<AudioInjectorOptions>();
272+ globalObject().setProperty("AudioInjectionOptions", injectionOptionValue);
273+
274+ QScriptValue localVoxelsValue = scriptValueFromQMetaObject<LocalVoxels>();
275+ globalObject().setProperty("LocalVoxels", localVoxelsValue);
261276
262− qScriptRegisterMetaType(&_engine, injectorToScriptValue, injectorFromScriptValue);
277+ qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue);
278+ qScriptRegisterMetaType( this, injectorToScriptValueInputController, injectorFromScriptValueInputController);
263279
264− qScriptRegisterMetaType(&_engine, animationDetailsToScriptValue, animationDetailsFromScriptValue);
280+ qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue);
265281
266282 registerGlobalObject("Script", this);
267283 registerGlobalObject("Audio", &_audioScriptingInterface);
@@ -276,15 +292,14 @@ void ScriptEngine::init() { · 8 unmodified lines
276292 registerGlobalObject("Voxels", &_voxelsScriptingInterface);
277293
278294 // constants
279− QScriptValue globalObject = _engine.globalObject();
280− globalObject.setProperty("TREE_SCALE", _engine.newVariant(QVariant(TREE_SCALE)));
281− globalObject.setProperty("COLLISION_GROUP_ENVIRONMENT", _engine.newVariant(QVariant(COLLISION_GROUP_ENVIRONMENT)));
282− globalObject.setProperty("COLLISION_GROUP_AVATARS", _engine.newVariant(QVariant(COLLISION_GROUP_AVATARS)));
283− globalObject.setProperty("COLLISION_GROUP_VOXELS", _engine.newVariant(QVariant(COLLISION_GROUP_VOXELS)));
284− globalObject.setProperty("COLLISION_GROUP_PARTICLES", _engine.newVariant(QVariant(COLLISION_GROUP_PARTICLES)));
295+ globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE)));
296+ globalObject().setProperty("COLLISION_GROUP_ENVIRONMENT", newVariant(QVariant(COLLISION_GROUP_ENVIRONMENT)));
297+ globalObject().setProperty("COLLISION_GROUP_AVATARS", newVariant(QVariant(COLLISION_GROUP_AVATARS)));
298+ globalObject().setProperty("COLLISION_GROUP_VOXELS", newVariant(QVariant(COLLISION_GROUP_VOXELS)));
299+ globalObject().setProperty("COLLISION_GROUP_PARTICLES", newVariant(QVariant(COLLISION_GROUP_PARTICLES)));
285300
286− globalObject.setProperty("AVATAR_MOTION_OBEY_LOCAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_LOCAL_GRAVITY)));
287− globalObject.setProperty("AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY)));
301+ globalObject().setProperty("AVATAR_MOTION_OBEY_LOCAL_GRAVITY", newVariant(QVariant(AVATAR_MOTION_OBEY_LOCAL_GRAVITY)));
302+ globalObject().setProperty("AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY", newVariant(QVariant(AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY)));
288303
289304 // let the VoxelPacketSender know how frequently we plan to call it
290305 _voxelsScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(SCRIPT_DATA_CALLBACK_USECS);
@@ -293,8 +308,8 @@ void ScriptEngine::init() { · 2 unmodified lines
293308
294309 QScriptValue ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {
295310 if (object) {
296− QScriptValue value = _engine.newQObject(object);
297− _engine.globalObject().setProperty(name, value);
311+ QScriptValue value = newQObject(object);
312+ globalObject().setProperty(name, value);
298313 return value;
299314 }
300315 return QScriptValue::NullValue;
@@ -302,15 +317,15 @@ QScriptValue ScriptEngine::registerGlobalObject(const QString& name, QObject* ob · 1 unmodified line
302317
303318 void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter,
304319 QScriptEngine::FunctionSignature setter, QScriptValue object) {
305− QScriptValue setterFunction = _engine.newFunction(setter, 1);
306− QScriptValue getterFunction = _engine.newFunction(getter);
320+ QScriptValue setterFunction = newFunction(setter, 1);
321+ QScriptValue getterFunction = newFunction(getter);
307322
308323 if (!object.isNull()) {
309324 object.setProperty(name, setterFunction, QScriptValue::PropertySetter);
310325 object.setProperty(name, getterFunction, QScriptValue::PropertyGetter);
311326 } else {
312− _engine.globalObject().setProperty(name, setterFunction, QScriptValue::PropertySetter);
313− _engine.globalObject().setProperty(name, getterFunction, QScriptValue::PropertyGetter);
327+ globalObject().setProperty(name, setterFunction, QScriptValue::PropertySetter);
328+ globalObject().setProperty(name, getterFunction, QScriptValue::PropertyGetter);
314329 }
315330 }
316331
@@ -319,25 +334,24 @@ void ScriptEngine::evaluate() { · 2 unmodified lines
319334 init();
320335 }
321336
322− QScriptValue result = _engine.evaluate(_scriptContents);
337+ QScriptValue result = evaluate(_scriptContents);
323338
324− if (_engine.hasUncaughtException()) {
325− int line = _engine.uncaughtExceptionLineNumber();
339+ if (hasUncaughtException()) {
340+ int line = uncaughtExceptionLineNumber();
326341 qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString();
327342 emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString());
328− _engine.clearExceptions();
343+ clearExceptions();
329344 }
330345 }
331346
332347 QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {
333− QScriptValue result = _engine.evaluate(program, fileName, lineNumber);
334− bool hasUncaughtException = _engine.hasUncaughtException();
335− if (hasUncaughtException) {
336− int line = _engine.uncaughtExceptionLineNumber();
348+ QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
349+ if (hasUncaughtException()) {
350+ int line = uncaughtExceptionLineNumber();
337351 qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString();
338352 }
339− emit evaluationFinished(result, hasUncaughtException);
340− _engine.clearExceptions();
353+ emit evaluationFinished(result, hasUncaughtException());
354+ clearExceptions();
341355 return result;
342356 }
343357
@@ -361,12 +375,12 @@ void ScriptEngine::run() { · 17 unmodified lines
361375 _isFinished = false;
362376 emit runningStateChanged();
363377
364− QScriptValue result = _engine.evaluate(_scriptContents);
365− if (_engine.hasUncaughtException()) {
366− int line = _engine.uncaughtExceptionLineNumber();
378+ QScriptValue result = evaluate(_scriptContents);
379+ if (hasUncaughtException()) {
380+ int line = uncaughtExceptionLineNumber();
367381 qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString();
368382 emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString());
369− _engine.clearExceptions();
383+ clearExceptions();
370384 }
371385
372386 QElapsedTimer startTime;
@@ -521,11 +535,11 @@ void ScriptEngine::run() { · 148 unmodified lines
521535 qint64 now = usecTimestampNow();
522536 float deltaTime = (float) (now - lastUpdate) / (float) USECS_PER_SECOND;
523537
524− if (_engine.hasUncaughtException()) {
525− int line = _engine.uncaughtExceptionLineNumber();
526− qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << _engine.uncaughtException().toString();
527− emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + _engine.uncaughtException().toString());
528− _engine.clearExceptions();
538+ if (hasUncaughtException()) {
539+ int line = uncaughtExceptionLineNumber();
540+ qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << uncaughtException().toString();
541+ emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + uncaughtException().toString());
542+ clearExceptions();
529543 }
530544
531545 emit update(deltaTime);
@@ -683,12 +697,12 @@ void ScriptEngine::include(const QString& includeFile) { · 151 unmodified lines
683697 }
684698 }
685699
686− QScriptValue result = _engine.evaluate(includeContents);
687− if (_engine.hasUncaughtException()) {
688− int line = _engine.uncaughtExceptionLineNumber();
700+ QScriptValue result = evaluate(includeContents);
701+ if (hasUncaughtException()) {
702+ int line = uncaughtExceptionLineNumber();
689703 qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString();
690704 emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString());
691− _engine.clearExceptions();
705+ clearExceptions();
692706 }
693707 }
694708