Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 2 files). Show all
Unified · Split
| @@ -508,26 +508,29 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 507 unmodified lines | ||
| 508 | 508 | // Connect up ALL the handlers to the global entities object's signals. |
| 509 | 509 | // (We could go signal by signal, or even handler by handler, but I don't think the efficiency is worth the complexity.) |
| 510 | 510 | auto entities = DependencyManager::get<EntityScriptingInterface>(); |
| 511 | − connect(entities.data(), &EntityScriptingInterface::deletingEntity, this, | |
| 512 | − [=](const EntityItemID& entityID) { | |
| 513 | − _registeredHandlers.remove(entityID); | |
| 514 | − }); | |
| 515 | − | |
| 511 | + connect(entities.data(), &EntityScriptingInterface::deletingEntity, this, [this](const EntityItemID& entityID) { | |
| 512 | + _registeredHandlers.remove(entityID); | |
| 513 | + }); | |
| 514 | + | |
| 516 | 515 | // Two common cases of event handler, differing only in argument signature. |
| 517 | − auto makeSingleEntityHandler = [=](const QString& eventName) -> std::function<void(const EntityItemID&)> { | |
| 518 | − return [=](const EntityItemID& entityItemID) -> void { | |
| 519 | − generalHandler(entityItemID, eventName, [=]() -> QScriptValueList { | |
| 520 | − return QScriptValueList() << entityItemID.toScriptValue(this); | |
| 521 | − }); | |
| 516 | + auto makeSingleEntityHandler = [&](QString eventName) { | |
| 517 | + return [this, eventName](const EntityItemID& entityItemID) { | |
| 518 | + forwardHandlerCall(entityItemID, eventName, { entityItemID.toScriptValue(this) }); | |
| 519 | + }; | |
| 520 | + }; | |
| 521 | + auto makeMouseHandler = [&](QString eventName) { | |
| 522 | + return [this, eventName](const EntityItemID& entityItemID, const MouseEvent& event) { | |
| 523 | + forwardHandlerCall(entityItemID, eventName, { entityItemID.toScriptValue(this), event.toScriptValue(this) }); | |
| 522 | 524 | }; |
| 523 | 525 | }; |
| 524 | − auto makeMouseHandler = [=](const QString& eventName) -> std::function<void(const EntityItemID&, const MouseEvent&)> { | |
| 525 | − return [=](const EntityItemID& entityItemID, const MouseEvent& event) -> void { | |
| 526 | − generalHandler(entityItemID, eventName, [=]() -> QScriptValueList { | |
| 527 | − return QScriptValueList() << entityItemID.toScriptValue(this) << event.toScriptValue(this); | |
| 528 | − }); | |
| 526 | + | |
| 527 | + auto makeCollisionHandler = [&](QString eventName) { | |
| 528 | + return [this, eventName](const EntityItemID& idA, const EntityItemID& idB, const Collision& collision) { | |
| 529 | + forwardHandlerCall(idA, eventName, { idA.toScriptValue(this), idB.toScriptValue(this), | |
| 530 | + collisionToScriptValue(this, collision) }); | |
| 529 | 531 | }; |
| 530 | 532 | }; |
| 533 | + | |
| 531 | 534 | connect(entities.data(), &EntityScriptingInterface::enterEntity, this, makeSingleEntityHandler("enterEntity")); |
| 532 | 535 | connect(entities.data(), &EntityScriptingInterface::leaveEntity, this, makeSingleEntityHandler("leaveEntity")); |
| 533 | 536 | |
| @@ -543,12 +546,7 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 9 unmodified lines | ||
| 543 | 546 | connect(entities.data(), &EntityScriptingInterface::hoverOverEntity, this, makeMouseHandler("hoverOverEntity")); |
| 544 | 547 | connect(entities.data(), &EntityScriptingInterface::hoverLeaveEntity, this, makeMouseHandler("hoverLeaveEntity")); |
| 545 | 548 | |
| 546 | − connect(entities.data(), &EntityScriptingInterface::collisionWithEntity, this, | |
| 547 | − [=](const EntityItemID& idA, const EntityItemID& idB, const Collision& collision) { | |
| 548 | − generalHandler(idA, "collisionWithEntity", [=]() { | |
| 549 | − return QScriptValueList () << idA.toScriptValue(this) << idB.toScriptValue(this) << collisionToScriptValue(this, collision); | |
| 550 | − }); | |
| 551 | − }); | |
| 549 | + connect(entities.data(), &EntityScriptingInterface::collisionWithEntity, this, makeCollisionHandler("collisionWithEntity")); | |
| 552 | 550 | } |
| 553 | 551 | if (!_registeredHandlers.contains(entityID)) { |
| 554 | 552 | _registeredHandlers[entityID] = RegisteredEventHandlers(); |
| @@ -899,9 +897,9 @@ void ScriptEngine::load(const QString& loadFile) { · 344 unmodified lines | ||
| 899 | 897 | } |
| 900 | 898 | |
| 901 | 899 | // Look up the handler associated with eventName and entityID. If found, evalute the argGenerator thunk and call the handler with those args |
| 902 | −void ScriptEngine::generalHandler(const EntityItemID& entityID, const QString& eventName, std::function<QScriptValueList()> argGenerator) { | |
| 900 | +void ScriptEngine::forwardHandlerCall(const EntityItemID& entityID, const QString& eventName, QScriptValueList eventHanderArgs) { | |
| 903 | 901 | if (QThread::currentThread() != thread()) { |
| 904 | − qDebug() << "*** ERROR *** ScriptEngine::generalHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]"; | |
| 902 | + qDebug() << "*** ERROR *** ScriptEngine::forwardHandlerCall() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "]"; | |
| 905 | 903 | assert(false); |
| 906 | 904 | return; |
| 907 | 905 | } |
| @@ -915,9 +913,8 @@ void ScriptEngine::generalHandler(const EntityItemID& entityID, const QString& e · 7 unmodified lines | ||
| 915 | 913 | } |
| 916 | 914 | QScriptValueList handlersForEvent = handlersOnEntity[eventName]; |
| 917 | 915 | if (!handlersForEvent.isEmpty()) { |
| 918 | − QScriptValueList args = argGenerator(); | |
| 919 | 916 | for (int i = 0; i < handlersForEvent.count(); ++i) { |
| 920 | − handlersForEvent[i].call(QScriptValue(), args); | |
| 917 | + handlersForEvent[i].call(QScriptValue(), eventHanderArgs); | |
| 921 | 918 | } |
| 922 | 919 | } |
| 923 | 920 | } |