Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 6 files). Show all
Unified · Split
| @@ -389,6 +389,30 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 388 unmodified lines | |||
| 389 | } | 389 | } |
| 390 | } | 390 | } |
| 391 | 391 | ||
| 392 | void ScriptEngine::addEntityEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { | ||
| 393 | auto entities = DependencyManager::get<EntityScriptingInterface>(); | ||
| 394 | if (!_registeredHandlers.contains(entityID)) { | ||
| 395 | _registeredHandlers[entityID] = RegisteredEventHandlers(); | ||
| 396 | } | ||
| 397 | _registeredHandlers[entityID][eventName] = handler; | ||
| 398 | if (eventName == "collisionWithEntity") { | ||
| 399 | connect(entities.data(), &EntityScriptingInterface::collisionWithEntity, this, &ScriptEngine::collisionWithEntity); | ||
| 400 | } | ||
| 401 | // FIXME: deletingEntity, changingEntityID | ||
| 402 | } | ||
| 403 | void ScriptEngine::removeEntityEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { | ||
| 404 | // FIXME | ||
| 405 | } | ||
| 406 | void ScriptEngine::collisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision) { | ||
| 407 | if (!_registeredHandlers.contains(idA)) return; | ||
| 408 | const RegisteredEventHandlers& handlersOnEntity = _registeredHandlers[idA]; | ||
| 409 | if (!handlersOnEntity.contains("collisionWithEntity")) return; | ||
| 410 | // FIXME: Need one more level of indirection. We need to allow multiple handlers per event, registered by different scripts. | ||
| 411 | QScriptValue handlerForEvent = handlersOnEntity["collisionWithEntity"]; | ||
| 412 | QScriptValueList args = QScriptValueList () << idA.toScriptValue(this) << idB.toScriptValue(this) << collisionToScriptValue(this, collision); | ||
| 413 | handlerForEvent.call(QScriptValue(), args); | ||
| 414 | } | ||
| 415 | |||
| 392 | void ScriptEngine::evaluate() { | 416 | void ScriptEngine::evaluate() { |
| 393 | if (_stoppingAllScripts) { | 417 | if (_stoppingAllScripts) { |
| 394 | return; // bail early | 418 | return; // bail early |