EventDispatcher.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. bool operator==(EventListenerEntry a, EventListenerEntry b) { return a.id == b.id && a.in_capture_phase == b.in_capture_phase && a.listener == b.listener; }
  37. bool operator!=(EventListenerEntry a, EventListenerEntry b) { return !(a == b); }
  38. struct CompareId {
  39. bool operator()(EventListenerEntry a, EventListenerEntry b) const { return a.id < b.id; }
  40. };
  41. struct CompareIdPhase {
  42. bool operator()(EventListenerEntry a, EventListenerEntry b) const { return std::tie(a.id, a.in_capture_phase) < std::tie(b.id, b.in_capture_phase); }
  43. };
  44. EventDispatcher::EventDispatcher(Element* _element)
  45. {
  46. element = _element;
  47. }
  48. EventDispatcher::~EventDispatcher()
  49. {
  50. // Detach from all event dispatchers
  51. for (const auto& event : listeners)
  52. event.listener->OnDetach(element);
  53. }
  54. void EventDispatcher::AttachEvent(EventId id, EventListener* listener, bool in_capture_phase)
  55. {
  56. EventListenerEntry entry(id, listener, in_capture_phase);
  57. // The entries are sorted by (id,phase). Find the bounds of this sort, then find the entry.
  58. auto range = std::equal_range(listeners.begin(), listeners.end(), entry, CompareIdPhase());
  59. auto it = std::find(range.first, range.second, entry);
  60. if(it == range.second)
  61. {
  62. // No existing entry found, add it to the end of the (id, phase) range
  63. listeners.emplace(it, entry);
  64. listener->OnAttach(element);
  65. }
  66. }
  67. void EventDispatcher::DetachEvent(EventId id, EventListener* listener, bool in_capture_phase)
  68. {
  69. EventListenerEntry entry(id, listener, in_capture_phase);
  70. // The entries are sorted by (id,phase). Find the bounds of this sort, then find the entry.
  71. // We could also just do a linear search over all the entries, which might be faster for low number of entries.
  72. auto range = std::equal_range(listeners.begin(), listeners.end(), entry, CompareIdPhase());
  73. auto it = std::find(range.first, range.second, entry);
  74. if (it != range.second)
  75. {
  76. // We found our listener, remove it
  77. listeners.erase(it);
  78. listener->OnDetach(element);
  79. }
  80. }
  81. // Detaches all events from this dispatcher and all child dispatchers.
  82. void EventDispatcher::DetachAllEvents()
  83. {
  84. for (const auto& event : listeners)
  85. event.listener->OnDetach(element);
  86. listeners.clear();
  87. for (int i = 0; i < element->GetNumChildren(true); ++i)
  88. element->GetChild(i)->GetEventDispatcher()->DetachAllEvents();
  89. }
  90. bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  91. {
  92. Event* event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible);
  93. if (!event)
  94. return false;
  95. // Build the element traversal from the tree
  96. typedef std::vector<Element*> Elements;
  97. Elements elements;
  98. Element* walk_element = target_element->GetParentNode();
  99. while (walk_element)
  100. {
  101. elements.push_back(walk_element);
  102. walk_element = walk_element->GetParentNode();
  103. }
  104. event->SetPhase(EventPhase::Capture);
  105. // Capture phase - root, to target (only events that have registered as capture events)
  106. // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
  107. for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
  108. {
  109. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  110. event->SetCurrentElement(elements[i]);
  111. dispatcher->TriggerEvents(*event, default_action_phase);
  112. }
  113. // Target phase - direct at the target
  114. if (event->IsPropagating())
  115. {
  116. event->SetPhase(EventPhase::Target);
  117. event->SetCurrentElement(target_element);
  118. TriggerEvents(*event, default_action_phase);
  119. }
  120. // Bubble phase - target to root (normal event bindings)
  121. if (bubbles && event->IsPropagating())
  122. {
  123. event->SetPhase(EventPhase::Bubble);
  124. for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
  125. {
  126. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  127. event->SetCurrentElement(elements[i]);
  128. dispatcher->TriggerEvents(*event, default_action_phase);
  129. }
  130. }
  131. bool propagating = event->IsPropagating();
  132. event->RemoveReference();
  133. return propagating;
  134. }
  135. String EventDispatcher::ToString() const
  136. {
  137. String result;
  138. if (listeners.empty())
  139. return result;
  140. auto add_to_result = [&result](EventId id, int count) {
  141. const EventSpecification& specification = EventSpecificationInterface::Get(id);
  142. result += CreateString(specification.type.size() + 32, "%s (%d), ", specification.type.c_str(), count);
  143. };
  144. EventId previous_id = listeners[0].id;
  145. int count = 0;
  146. for (const auto& listener : listeners)
  147. {
  148. if (listener.id != previous_id)
  149. {
  150. add_to_result(previous_id, count);
  151. previous_id = listener.id;
  152. count = 0;
  153. }
  154. count++;
  155. }
  156. if (count > 0)
  157. add_to_result(previous_id, count);
  158. if (result.size() > 2)
  159. result.resize(result.size() - 2);
  160. return result;
  161. }
  162. void EventDispatcher::TriggerEvents(Event& event, DefaultActionPhase default_action_phase)
  163. {
  164. const EventPhase phase = event.GetPhase();
  165. // Find the range of entries with matching id and phase, given that listeners are sorted by (id,phase).
  166. // In the case of target phase we will match any listener phase.
  167. Listeners::iterator begin, end;
  168. if (phase == EventPhase::Capture)
  169. std::tie(begin, end) = std::equal_range(listeners.begin(), listeners.end(), EventListenerEntry(event.GetId(), nullptr, true), CompareIdPhase());
  170. else if (phase == EventPhase::Target)
  171. std::tie(begin, end) = std::equal_range(listeners.begin(), listeners.end(), EventListenerEntry(event.GetId(), nullptr, false), CompareId());
  172. else if (phase == EventPhase::Bubble)
  173. std::tie(begin, end) = std::equal_range(listeners.begin(), listeners.end(), EventListenerEntry(event.GetId(), nullptr, false), CompareIdPhase());
  174. for (auto it = begin; it != end; ++it)
  175. {
  176. it->listener->ProcessEvent(event);
  177. }
  178. const bool do_default_action = ((unsigned int)phase & (unsigned int)default_action_phase);
  179. // Do the default action unless we have been cancelled.
  180. if (do_default_action && event.IsPropagating())
  181. {
  182. element->ProcessDefaultAction(event);
  183. }
  184. }
  185. }
  186. }