EventDispatcher.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "EventDispatcher.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/Event.h"
  32. #include "../../Include/RmlUi/Core/EventListener.h"
  33. #include "../../Include/RmlUi/Core/Factory.h"
  34. namespace Rml {
  35. namespace Core {
  36. EventDispatcher::EventDispatcher(Element* _element)
  37. {
  38. element = _element;
  39. }
  40. EventDispatcher::~EventDispatcher()
  41. {
  42. // Detach from all event dispatchers
  43. for (Events::iterator event_itr = events.begin(); event_itr != events.end(); ++event_itr)
  44. {
  45. for (Listeners::iterator listener_itr = (*event_itr).second.begin(); listener_itr != (*event_itr).second.end(); ++listener_itr)
  46. {
  47. (*listener_itr).listener->OnDetach(element);
  48. }
  49. }
  50. }
  51. void EventDispatcher::AttachEvent(const String& type, EventListener* listener, bool in_capture_phase)
  52. {
  53. // Look up the event
  54. Events::iterator event_itr = events.find(type);
  55. // Ensure the event is in the event list
  56. if (event_itr == events.end())
  57. {
  58. event_itr = events.insert(std::pair< String, Listeners >(type, Listeners())).first;
  59. }
  60. // Add the action to the events
  61. (*event_itr).second.push_back(Listener(listener, in_capture_phase));
  62. listener->OnAttach(element);
  63. }
  64. void EventDispatcher::DetachEvent(const String& type, EventListener* listener, bool in_capture_phase)
  65. {
  66. // Look up the event
  67. Events::iterator event_itr = events.find(type);
  68. // Bail if we can't find the event
  69. if (event_itr == events.end())
  70. {
  71. return;
  72. }
  73. // Find the relevant listener and erase it
  74. Listeners::iterator listener_itr = (*event_itr).second.begin();
  75. while (listener_itr != (*event_itr).second.end())
  76. {
  77. if ((*listener_itr).listener == listener && (*listener_itr).in_capture_phase == in_capture_phase)
  78. {
  79. listener_itr = (*event_itr).second.erase(listener_itr);
  80. listener->OnDetach(element);
  81. }
  82. else
  83. ++listener_itr;
  84. }
  85. }
  86. // Detaches all events from this dispatcher and all child dispatchers.
  87. void EventDispatcher::DetachAllEvents()
  88. {
  89. for (Events::iterator event_iterator = events.begin(); event_iterator != events.end(); ++event_iterator)
  90. {
  91. Listeners& listeners = event_iterator->second;
  92. for (size_t i = 0; i < listeners.size(); ++i)
  93. listeners[i].listener->OnDetach(element);
  94. }
  95. events.clear();
  96. for (int i = 0; i < element->GetNumChildren(true); ++i)
  97. element->GetChild(i)->GetEventDispatcher()->DetachAllEvents();
  98. }
  99. bool EventDispatcher::DispatchEvent(Element* target_element, const String& name, const Dictionary& parameters, bool interruptible)
  100. {
  101. //Event event(target_element, name, parameters, interruptible);
  102. Event* event = Factory::InstanceEvent(target_element, name, parameters, interruptible);
  103. if (event == NULL)
  104. return false;
  105. // Build the element traversal from the tree
  106. typedef std::vector<Element*> Elements;
  107. Elements elements;
  108. Element* walk_element = target_element->GetParentNode();
  109. while (walk_element)
  110. {
  111. elements.push_back(walk_element);
  112. walk_element = walk_element->GetParentNode();
  113. }
  114. event->SetPhase(Event::PHASE_CAPTURE);
  115. // Capture phase - root, to target (only events that have registered as capture events)
  116. // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
  117. for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
  118. {
  119. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  120. event->SetCurrentElement(elements[i]);
  121. dispatcher->TriggerEvents(event);
  122. }
  123. // Target phase - direct at the target
  124. if (event->IsPropagating())
  125. {
  126. event->SetPhase(Event::PHASE_TARGET);
  127. event->SetCurrentElement(target_element);
  128. TriggerEvents(event);
  129. }
  130. if (event->IsPropagating())
  131. {
  132. event->SetPhase(Event::PHASE_BUBBLE);
  133. // Bubble phase - target to root (normal event bindings)
  134. for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
  135. {
  136. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  137. event->SetCurrentElement(elements[i]);
  138. dispatcher->TriggerEvents(event);
  139. }
  140. }
  141. bool propagating = event->IsPropagating();
  142. event->RemoveReference();
  143. return propagating;
  144. }
  145. String EventDispatcher::ToString() const
  146. {
  147. String result;
  148. for (auto nvp : events)
  149. {
  150. result += String(nvp.first.Length() + 32, "%s (%d), ", nvp.first.CString(), static_cast<int>(nvp.second.size()));
  151. }
  152. if (result.Length() > 2)
  153. {
  154. result.Resize(result.Length() - 2);
  155. }
  156. return result;
  157. }
  158. void EventDispatcher::TriggerEvents(Event* event)
  159. {
  160. // Look up the event
  161. Events::iterator itr = events.find(event->GetType());
  162. if (itr != events.end())
  163. {
  164. // Dispatch all actions
  165. Listeners& listeners = (*itr).second;
  166. if (event->GetPhase() == Event::PHASE_TARGET)
  167. {
  168. // Fire all listeners waiting for bubble events before we send the event to the target itself.
  169. for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
  170. {
  171. if (!listeners[i].in_capture_phase)
  172. {
  173. listeners[i].listener->ProcessEvent(*event);
  174. }
  175. }
  176. // Send the event to the target element itself.
  177. if (event->IsPropagating())
  178. element->ProcessEvent(*event);
  179. // Fire all listeners waiting for capture events.
  180. for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
  181. {
  182. if (listeners[i].in_capture_phase)
  183. listeners[i].listener->ProcessEvent(*event);
  184. }
  185. return;
  186. }
  187. else
  188. {
  189. bool in_capture_phase = event->GetPhase() == Event::PHASE_CAPTURE;
  190. for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
  191. {
  192. // If we're in the correct phase, fire the event
  193. if (listeners[i].in_capture_phase == in_capture_phase)
  194. listeners[i].listener->ProcessEvent(*event);
  195. }
  196. }
  197. }
  198. if (event->GetPhase() != Event::PHASE_CAPTURE)
  199. {
  200. // Send the event to the target element.
  201. element->ProcessEvent(*event);
  202. }
  203. }
  204. }
  205. }