EventDispatcher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  103. {
  104. Event* event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible);
  105. if (!event)
  106. return false;
  107. // Build the element traversal from the tree
  108. typedef std::vector<Element*> Elements;
  109. Elements elements;
  110. Element* walk_element = target_element->GetParentNode();
  111. while (walk_element)
  112. {
  113. elements.push_back(walk_element);
  114. walk_element = walk_element->GetParentNode();
  115. }
  116. event->SetPhase(EventPhase::Capture);
  117. // Capture phase - root, to target (only events that have registered as capture events)
  118. // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
  119. for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
  120. {
  121. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  122. event->SetCurrentElement(elements[i]);
  123. dispatcher->TriggerEvents(event, default_action_phase);
  124. }
  125. // Target phase - direct at the target
  126. if (event->IsPropagating())
  127. {
  128. event->SetPhase(EventPhase::Target);
  129. event->SetCurrentElement(target_element);
  130. TriggerEvents(event, default_action_phase);
  131. }
  132. if (bubbles && event->IsPropagating())
  133. {
  134. event->SetPhase(EventPhase::Bubble);
  135. // Bubble phase - target to root (normal event bindings)
  136. for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
  137. {
  138. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  139. event->SetCurrentElement(elements[i]);
  140. dispatcher->TriggerEvents(event, default_action_phase);
  141. }
  142. }
  143. bool propagating = event->IsPropagating();
  144. event->RemoveReference();
  145. return propagating;
  146. }
  147. String EventDispatcher::ToString() const
  148. {
  149. String result;
  150. for (auto nvp : events)
  151. {
  152. const EventSpecification& specification = EventSpecificationInterface::Get(nvp.first);
  153. result += CreateString(specification.type.size() + 32, "%s (%d), ", specification.type.c_str(), static_cast<int>(nvp.second.size()));
  154. }
  155. if (result.size() > 2)
  156. {
  157. result.resize(result.size() - 2);
  158. }
  159. return result;
  160. }
  161. void EventDispatcher::TriggerEvents(Event* event, DefaultActionPhase default_action_phase)
  162. {
  163. const EventPhase phase = event->GetPhase();
  164. // Look up the event
  165. Events::iterator itr = events.find(event->GetId());
  166. if (itr != events.end())
  167. {
  168. // Dispatch all actions
  169. Listeners& listeners = (*itr).second;
  170. for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
  171. {
  172. if (phase == EventPhase::Target
  173. || (phase == EventPhase::Capture && listeners[i].in_capture_phase)
  174. || (phase == EventPhase::Bubble && !listeners[i].in_capture_phase))
  175. {
  176. listeners[i].listener->ProcessEvent(*event);
  177. }
  178. }
  179. }
  180. const bool do_default_action = ((int)phase & (int)default_action_phase);
  181. if (do_default_action)
  182. {
  183. element->ProcessDefaultAction(*event);
  184. }
  185. }
  186. }
  187. }