History

more work on exposing device inputs as JavaScript named constants

Brad Hefta-Gaub committed 11 years ago

Commit d3ee9b01 parent 37f530f5 files · +67 −11Browse 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+44−7
@@ -277,10 +277,6 @@ void ScriptEngine::init() { · 276 unmodified lines
277 registerAvatarTypes(this);277 registerAvatarTypes(this);
278 registerAudioMetaTypes(this);278 registerAudioMetaTypes(this);
279 279
280 if (_controllerScriptingInterface) {
281 _controllerScriptingInterface->registerControllerTypes(this);
282 }
283
284 qScriptRegisterMetaType(this, EntityPropertyFlagsToScriptValue, EntityPropertyFlagsFromScriptValue);280 qScriptRegisterMetaType(this, EntityPropertyFlagsToScriptValue, EntityPropertyFlagsFromScriptValue);
285 qScriptRegisterMetaType(this, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValueHonorReadOnly);281 qScriptRegisterMetaType(this, EntityItemPropertiesToScriptValue, EntityItemPropertiesFromScriptValueHonorReadOnly);
286 qScriptRegisterMetaType(this, EntityItemIDtoScriptValue, EntityItemIDfromScriptValue);282 qScriptRegisterMetaType(this, EntityItemIDtoScriptValue, EntityItemIDfromScriptValue);
@@ -323,6 +319,43 @@ void ScriptEngine::init() { · 36 unmodified lines
323 319
324 // constants320 // constants
325 globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE)));321 globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE)));
322
323 if (_controllerScriptingInterface) {
324 _controllerScriptingInterface->registerControllerTypes(this);
325 }
326
327
328}
329
330void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) {
331 if (QThread::currentThread() != thread()) {
332#ifdef THREAD_DEBUGGING
333 qDebug() << "*** WARNING *** ScriptEngine::registerValue() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name;
334#endif
335 QMetaObject::invokeMethod(this, "registerValue",
336 Q_ARG(const QString&, valueName),
337 Q_ARG(QScriptValue, value));
338 return;
339 }
340
341 QStringList pathToValue = valueName.split(".");
342 int partsToGo = pathToValue.length();
343 QScriptValue partObject = globalObject();
344
345 for (const auto& pathPart : pathToValue) {
346 partsToGo--;
347 if (!partObject.property(pathPart).isValid()) {
348 if (partsToGo > 0) {
349 //QObject *object = new QObject;
350 QScriptValue partValue = newArray(); //newQObject(object, QScriptEngine::ScriptOwnership);
351 qDebug() << "partValue[" << pathPart<<"].isArray() :" << partValue.isArray();
352 partObject.setProperty(pathPart, partValue);
353 } else {
354 partObject.setProperty(pathPart, value);
355 }
356 }
357 partObject = partObject.property(pathPart);
358 }
326}359}
327 360
328void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {361void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) {
@@ -339,9 +372,13 @@ void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { · 10 unmodified lines
339 qDebug() << "ScriptEngine::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;372 qDebug() << "ScriptEngine::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name;
340 #endif373 #endif
341 374
342 if (object) {375 if (!globalObject().property(name).isValid()) {
343 QScriptValue value = newQObject(object);376 if (object) {
344 globalObject().setProperty(name, value);377 QScriptValue value = newQObject(object);
378 globalObject().setProperty(name, value);
379 } else {
380 globalObject().setProperty(name, QScriptValue());
381 }
345 }382 }
346}383}
347 384