History

Make handler generators more readable

Atlante45 committed 11 years ago

Commit 4e33aa81 parent 5cf936f2 files · +23 −26Browse files

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

Unified · Split

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