LuaScriptEventInvoker.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. class EventHandlerCommand
  29. {
  30. public:
  31. EventHandlerCommand(int type) : type_(type) { }
  32. virtual ~EventHandlerCommand() { }
  33. int type_;
  34. };
  35. struct AddOrRemoveEventHandlerCommand : public EventHandlerCommand
  36. {
  37. enum { Type = 1 };
  38. AddOrRemoveEventHandlerCommand(bool add, Object* sender, const StringHash& eventType, WeakPtr<LuaFunction> function) :
  39. EventHandlerCommand(Type), add_(add), sender_(sender), eventType_(eventType), function_(function)
  40. {
  41. }
  42. bool add_;
  43. Object* sender_;
  44. StringHash eventType_;
  45. WeakPtr<LuaFunction> function_;
  46. };
  47. struct RemoveAllEventHandlersCommand : public EventHandlerCommand
  48. {
  49. enum { Type = 2 };
  50. RemoveAllEventHandlersCommand(Object* sender) : EventHandlerCommand(Type), sender_(sender)
  51. {
  52. }
  53. Object* sender_;
  54. };
  55. struct RemoveEventHandlersExceptCommand : public EventHandlerCommand
  56. {
  57. enum { Type = 3 };
  58. RemoveEventHandlersExceptCommand(const PODVector<StringHash>& exceptionTypes) : EventHandlerCommand(Type), exceptionTypes_(exceptionTypes)
  59. {
  60. }
  61. PODVector<StringHash> exceptionTypes_;
  62. };
  63. LuaScriptEventInvoker::LuaScriptEventInvoker(Context* context) :
  64. Object(context),
  65. instance_(0),
  66. invoking_(false)
  67. {
  68. }
  69. LuaScriptEventInvoker::LuaScriptEventInvoker(LuaScriptInstance* instance) :
  70. Object(instance->GetContext()),
  71. instance_(instance),
  72. invoking_(false)
  73. {
  74. }
  75. LuaScriptEventInvoker::~LuaScriptEventInvoker()
  76. {
  77. }
  78. void LuaScriptEventInvoker::AddEventHandler(Object* sender, const StringHash& eventType, WeakPtr<LuaFunction> function)
  79. {
  80. if (invoking_)
  81. {
  82. eventHandlerCommands_.Push(new AddOrRemoveEventHandlerCommand(true, sender, eventType, function));
  83. return;
  84. }
  85. EventTypeToLuaFunctionVectorMap& eventTypeToFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  86. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToFunctionVectorMap.Find(eventType);
  87. if (i == eventTypeToFunctionVectorMap.End())
  88. {
  89. eventTypeToFunctionVectorMap[eventType].Push(function);
  90. if (!sender)
  91. SubscribeToEvent(eventType, HANDLER(LuaScriptEventInvoker, HandleLuaScriptEvent));
  92. else
  93. SubscribeToEvent(sender, eventType, HANDLER(LuaScriptEventInvoker, HandleLuaScriptEvent));
  94. }
  95. else
  96. {
  97. if (!i->second_.Contains(function))
  98. i->second_.Push(function);
  99. }
  100. }
  101. void LuaScriptEventInvoker::RemoveEventHandler(Object* sender, const StringHash& eventType, WeakPtr<LuaFunction> function)
  102. {
  103. if (invoking_)
  104. {
  105. eventHandlerCommands_.Push(new AddOrRemoveEventHandlerCommand(false, sender, eventType, function));
  106. return;
  107. }
  108. EventTypeToLuaFunctionVectorMap& eventTypeToLuaFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  109. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToLuaFunctionVectorMap.Find(eventType);
  110. if (i == eventTypeToLuaFunctionVectorMap.End())
  111. return;
  112. if (function)
  113. i->second_.Remove(function);
  114. else
  115. i->second_.Clear();
  116. if (i->second_.Empty())
  117. {
  118. eventTypeToLuaFunctionVectorMap.Erase(i);
  119. if (!sender)
  120. UnsubscribeFromEvent(eventType);
  121. else
  122. UnsubscribeFromEvent(sender, eventType);
  123. }
  124. }
  125. void LuaScriptEventInvoker::RemoveAllEventHandlers(Object* sender)
  126. {
  127. if (invoking_)
  128. {
  129. eventHandlerCommands_.Push(new RemoveAllEventHandlersCommand(sender));
  130. return;
  131. }
  132. if (!sender)
  133. {
  134. UnsubscribeFromAllEvents();
  135. eventTypeToLuaFunctionVectorMap.Clear();
  136. senderEventTypeToLuaFunctionVectorMap.Clear();
  137. }
  138. else
  139. {
  140. UnsubscribeFromEvents(sender);
  141. senderEventTypeToLuaFunctionVectorMap.Erase(sender);
  142. }
  143. }
  144. void LuaScriptEventInvoker::RemoveEventHandlersExcept(const PODVector<StringHash>& exceptionTypes)
  145. {
  146. if (invoking_)
  147. {
  148. eventHandlerCommands_.Push(new RemoveEventHandlersExceptCommand(exceptionTypes));
  149. return;
  150. }
  151. for (unsigned i = 0; i < exceptionTypes.Size(); ++i)
  152. eventTypeToLuaFunctionVectorMap.Erase(exceptionTypes[i]);
  153. UnsubscribeFromAllEventsExcept(exceptionTypes, false);
  154. }
  155. void LuaScriptEventInvoker::HandleLuaScriptEvent(StringHash eventType, VariantMap& eventData)
  156. {
  157. Object* sender = GetEventHandler()->GetSender();
  158. EventTypeToLuaFunctionVectorMap& eventTypeToLuaFunctionVectorMap = GetEventTypeToLuaFunctionVectorMap(sender);
  159. EventTypeToLuaFunctionVectorMap::Iterator i = eventTypeToLuaFunctionVectorMap.Find(eventType);
  160. if (i == eventTypeToLuaFunctionVectorMap.End())
  161. return;
  162. invoking_ = true;
  163. LuaFunctionVector& luaFunctionVector = i->second_;
  164. if (instance_)
  165. {
  166. instance_->AddRef();
  167. for (unsigned i = 0; i < luaFunctionVector.Size(); ++i)
  168. {
  169. WeakPtr<LuaFunction>& function = luaFunctionVector[i];
  170. if (function && function->BeginCall(instance_))
  171. {
  172. function->PushUserType(eventType, "StringHash");
  173. function->PushUserType(eventData, "VariantMap");
  174. function->EndCall();
  175. }
  176. }
  177. instance_->ReleaseRef();
  178. }
  179. else
  180. {
  181. for (unsigned i = 0; i < luaFunctionVector.Size(); ++i)
  182. {
  183. WeakPtr<LuaFunction>& function = luaFunctionVector[i];
  184. if (function && function->BeginCall())
  185. {
  186. function->PushUserType(eventType, "StringHash");
  187. function->PushUserType(eventData, "VariantMap");
  188. function->EndCall();
  189. }
  190. }
  191. }
  192. invoking_ = false;
  193. if (!eventHandlerCommands_.Empty())
  194. {
  195. for (unsigned i = 0; i < eventHandlerCommands_.Size(); ++i)
  196. ExecuteThenDestroyCommand(eventHandlerCommands_[i]);
  197. eventHandlerCommands_.Clear();
  198. }
  199. }
  200. void LuaScriptEventInvoker::ExecuteThenDestroyCommand(EventHandlerCommand* command)
  201. {
  202. if (!command)
  203. return;
  204. switch (command->type_)
  205. {
  206. case AddOrRemoveEventHandlerCommand::Type:
  207. {
  208. AddOrRemoveEventHandlerCommand* theCommand = (AddOrRemoveEventHandlerCommand*)command;
  209. if (theCommand->add_)
  210. AddEventHandler(theCommand->sender_, theCommand->eventType_, theCommand->function_);
  211. else
  212. RemoveEventHandler(theCommand->sender_, theCommand->eventType_, theCommand->function_);
  213. }
  214. break;
  215. case RemoveAllEventHandlersCommand::Type:
  216. {
  217. RemoveAllEventHandlersCommand* theCommand = (RemoveAllEventHandlersCommand*)command;
  218. RemoveAllEventHandlers(theCommand->sender_);
  219. }
  220. break;
  221. case RemoveEventHandlersExceptCommand::Type:
  222. {
  223. RemoveEventHandlersExceptCommand* theCommand = (RemoveEventHandlersExceptCommand*)command;
  224. RemoveEventHandlersExcept(theCommand->exceptionTypes_);
  225. }
  226. break;
  227. }
  228. delete command;
  229. }
  230. }