EventDispatcher.cpp 6.6 KB

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