EventDispatcher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "EventSpecification.h"
  34. namespace Rocket {
  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(EventId id, EventListener* listener, bool in_capture_phase)
  52. {
  53. // See if event type exists already
  54. Events::iterator event_itr = events.find(id);
  55. if (event_itr == events.end())
  56. {
  57. // No, add listener to new event type entry
  58. event_itr = events.emplace(id, Listeners{ Listener(listener, in_capture_phase) }).first;
  59. }
  60. else
  61. {
  62. // Yes, add listener to the existing list of events for the type
  63. (*event_itr).second.emplace_back(listener, in_capture_phase);
  64. }
  65. listener->OnAttach(element);
  66. }
  67. void EventDispatcher::DetachEvent(EventId id, EventListener* listener, bool in_capture_phase)
  68. {
  69. // Look up the event
  70. Events::iterator event_itr = events.find(id);
  71. // Bail if we can't find the event
  72. if (event_itr == events.end())
  73. {
  74. return;
  75. }
  76. // Find the relevant listener and erase it
  77. Listeners::iterator listener_itr = (*event_itr).second.begin();
  78. while (listener_itr != (*event_itr).second.end())
  79. {
  80. if ((*listener_itr).listener == listener && (*listener_itr).in_capture_phase == in_capture_phase)
  81. {
  82. listener_itr = (*event_itr).second.erase(listener_itr);
  83. listener->OnDetach(element);
  84. }
  85. else
  86. ++listener_itr;
  87. }
  88. }
  89. // Detaches all events from this dispatcher and all child dispatchers.
  90. void EventDispatcher::DetachAllEvents()
  91. {
  92. for (Events::iterator event_iterator = events.begin(); event_iterator != events.end(); ++event_iterator)
  93. {
  94. Listeners& listeners = event_iterator->second;
  95. for (size_t i = 0; i < listeners.size(); ++i)
  96. listeners[i].listener->OnDetach(element);
  97. }
  98. events.clear();
  99. for (int i = 0; i < element->GetNumChildren(true); ++i)
  100. element->GetChild(i)->GetEventDispatcher()->DetachAllEvents();
  101. }
  102. bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const Dictionary& parameters)
  103. {
  104. Event* event = Factory::InstanceEvent(target_element, id, parameters);
  105. if (!event)
  106. return false;
  107. const DefaultActionPhase default_action_phase = event->GetDefaultActionPhase();
  108. const bool bubbles = event->GetBubbles();
  109. // Build the element traversal from the tree
  110. typedef std::vector<Element*> Elements;
  111. Elements elements;
  112. Element* walk_element = target_element->GetParentNode();
  113. while (walk_element)
  114. {
  115. elements.push_back(walk_element);
  116. walk_element = walk_element->GetParentNode();
  117. }
  118. event->SetPhase(EventPhase::Capture);
  119. // Capture phase - root, to target (only events that have registered as capture events)
  120. // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
  121. for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
  122. {
  123. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  124. event->SetCurrentElement(elements[i]);
  125. dispatcher->TriggerEvents(event, default_action_phase);
  126. }
  127. // Target phase - direct at the target
  128. if (event->IsPropagating())
  129. {
  130. event->SetPhase(EventPhase::Target);
  131. event->SetCurrentElement(target_element);
  132. TriggerEvents(event, default_action_phase);
  133. }
  134. if (bubbles && event->IsPropagating())
  135. {
  136. event->SetPhase(EventPhase::Bubble);
  137. // Bubble phase - target to root (normal event bindings)
  138. for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
  139. {
  140. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  141. event->SetCurrentElement(elements[i]);
  142. dispatcher->TriggerEvents(event, default_action_phase);
  143. }
  144. }
  145. bool propagating = event->IsPropagating();
  146. event->RemoveReference();
  147. return propagating;
  148. }
  149. String EventDispatcher::ToString() const
  150. {
  151. String result;
  152. for (auto nvp : events)
  153. {
  154. const EventSpecification& specification = EventSpecificationInterface::Get(nvp.first);
  155. result += CreateString(specification.type.size() + 32, "%s (%d), ", specification.type.c_str(), static_cast<int>(nvp.second.size()));
  156. }
  157. if (result.size() > 2)
  158. {
  159. result.resize(result.size() - 2);
  160. }
  161. return result;
  162. }
  163. void EventDispatcher::TriggerEvents(Event* event, DefaultActionPhase default_action_phase)
  164. {
  165. const EventPhase phase = event->GetPhase();
  166. // Look up the event
  167. Events::iterator itr = events.find(event->GetId());
  168. if (itr != events.end())
  169. {
  170. // Dispatch all actions
  171. Listeners& listeners = (*itr).second;
  172. for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
  173. {
  174. if (phase == EventPhase::Target
  175. || (phase == EventPhase::Capture && listeners[i].in_capture_phase)
  176. || (phase == EventPhase::Bubble && !listeners[i].in_capture_phase))
  177. {
  178. listeners[i].listener->ProcessEvent(*event);
  179. }
  180. }
  181. }
  182. const bool do_default_action = ((int)phase & (int)default_action_phase);
  183. if (do_default_action)
  184. {
  185. element->ProcessDefaultAction(*event);
  186. }
  187. }
  188. }
  189. }