LuaScriptEventInvoker.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../LuaScript/LuaFunction.h"
  23. #include "../LuaScript/LuaScriptEventInvoker.h"
  24. #include "../LuaScript/LuaScriptInstance.h"
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. LuaScriptEventInvoker::LuaScriptEventInvoker(Context* context) :
  29. Object(context),
  30. instance_(0)
  31. {
  32. }
  33. LuaScriptEventInvoker::LuaScriptEventInvoker(LuaScriptInstance* instance) :
  34. Object(instance->GetContext()),
  35. instance_(instance)
  36. {
  37. }
  38. LuaScriptEventInvoker::~LuaScriptEventInvoker()
  39. {
  40. }
  41. void LuaScriptEventInvoker::AddEventHandler(Object* sender, const String& eventName, WeakPtr<LuaFunction> function)
  42. {
  43. EventTypeToLuaFunctionVectorMap& eventTypeToFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  44. StringHash eventType(eventName);
  45. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToFunctionVectorMap.Find(eventType);
  46. if (i == eventTypeToFunctionVectorMap.End())
  47. {
  48. eventTypeToFunctionVectorMap[eventType].Push(function);
  49. if (!sender)
  50. SubscribeToEvent(eventType, HANDLER(LuaScriptEventInvoker, HandleLuaScriptEvent));
  51. else
  52. SubscribeToEvent(sender, eventType, HANDLER(LuaScriptEventInvoker, HandleLuaScriptEvent));
  53. }
  54. else
  55. {
  56. if (!i->second_.Contains(function))
  57. i->second_.Push(function);
  58. }
  59. }
  60. void LuaScriptEventInvoker::RemoveEventHandler(Object* sender, const String& eventName, WeakPtr<LuaFunction> function)
  61. {
  62. EventTypeToLuaFunctionVectorMap& eventTypeToLuaFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  63. StringHash eventType(eventName);
  64. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToLuaFunctionVectorMap.Find(eventType);
  65. if (i == eventTypeToLuaFunctionVectorMap.End())
  66. return;
  67. if (function)
  68. i->second_.Remove(function);
  69. else
  70. i->second_.Clear();
  71. if (i->second_.Empty())
  72. {
  73. eventTypeToLuaFunctionVectorMap.Erase(i);
  74. if (!sender)
  75. UnsubscribeFromEvent(eventType);
  76. else
  77. UnsubscribeFromEvent(sender, eventType);
  78. }
  79. }
  80. void LuaScriptEventInvoker::RemoveAllEventHandlers(Object* sender)
  81. {
  82. if (!sender)
  83. {
  84. UnsubscribeFromAllEvents();
  85. eventTypeToLuaFunctionVectorMap.Clear();
  86. senderEventTypeToLuaFunctionVectorMap.Clear();
  87. }
  88. else
  89. {
  90. UnsubscribeFromEvents(sender);
  91. senderEventTypeToLuaFunctionVectorMap.Erase(sender);
  92. }
  93. }
  94. void LuaScriptEventInvoker::RemoveEventHandlersExcept(const Vector<String>& exceptionNames)
  95. {
  96. PODVector<StringHash> exceptionTypes(exceptionNames.Size());
  97. for (unsigned i = 0; i < exceptionTypes.Size(); ++i)
  98. {
  99. StringHash eventType(exceptionNames[i]);
  100. exceptionTypes[i] = eventType;
  101. eventTypeToLuaFunctionVectorMap.Erase(eventType);
  102. }
  103. UnsubscribeFromAllEventsExcept(exceptionTypes, false);
  104. }
  105. void LuaScriptEventInvoker::HandleLuaScriptEvent(StringHash eventType, VariantMap& eventData)
  106. {
  107. Object* sender = GetEventHandler()->GetSender();
  108. EventTypeToLuaFunctionVectorMap& eventTypeToLuaFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  109. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToLuaFunctionVectorMap.Find(eventType);
  110. if (i == eventTypeToLuaFunctionVectorMap.End())
  111. return;
  112. // Create a copy
  113. LuaFunctionVector luaFunctionVector = i->second_;
  114. if (instance_)
  115. {
  116. instance_->AddRef();
  117. for (unsigned i = 0; i < luaFunctionVector.Size(); ++i)
  118. {
  119. WeakPtr<LuaFunction>& function = luaFunctionVector[i];
  120. if (function && function->BeginCall(instance_))
  121. {
  122. function->PushUserType(eventType, "StringHash");
  123. function->PushUserType(eventData, "VariantMap");
  124. function->EndCall();
  125. }
  126. }
  127. instance_->ReleaseRef();
  128. }
  129. else
  130. {
  131. for (unsigned i = 0; i < luaFunctionVector.Size(); ++i)
  132. {
  133. WeakPtr<LuaFunction>& function = luaFunctionVector[i];
  134. if (function && function->BeginCall())
  135. {
  136. function->PushUserType(eventType, "StringHash");
  137. function->PushUserType(eventData, "VariantMap");
  138. function->EndCall();
  139. }
  140. }
  141. }
  142. }
  143. }