EventDispatcher.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "EventSpecification.h"
  35. namespace Rml {
  36. namespace Core {
  37. bool operator==(EventListenerEntry a, EventListenerEntry b) { return a.id == b.id && a.in_capture_phase == b.in_capture_phase && a.listener == b.listener; }
  38. bool operator!=(EventListenerEntry a, EventListenerEntry b) { return !(a == b); }
  39. struct CompareId {
  40. bool operator()(EventListenerEntry a, EventListenerEntry b) const { return a.id < b.id; }
  41. };
  42. struct CompareIdPhase {
  43. bool operator()(EventListenerEntry a, EventListenerEntry b) const { return std::tie(a.id, a.in_capture_phase) < std::tie(b.id, b.in_capture_phase); }
  44. };
  45. EventDispatcher::EventDispatcher(Element* _element)
  46. {
  47. element = _element;
  48. }
  49. EventDispatcher::~EventDispatcher()
  50. {
  51. // Detach from all event dispatchers
  52. for (const auto& event : listeners)
  53. event.listener->OnDetach(element);
  54. }
  55. void EventDispatcher::AttachEvent(EventId id, EventListener* listener, bool in_capture_phase)
  56. {
  57. EventListenerEntry entry(id, listener, in_capture_phase);
  58. // The entries are sorted by (id,phase). Find the bounds of this sort, then find the entry.
  59. auto range = std::equal_range(listeners.begin(), listeners.end(), entry, CompareIdPhase());
  60. auto it = std::find(range.first, range.second, entry);
  61. if(it == range.second)
  62. {
  63. // No existing entry found, add it to the end of the (id, phase) range
  64. listeners.emplace(it, entry);
  65. listener->OnAttach(element);
  66. }
  67. }
  68. void EventDispatcher::DetachEvent(EventId id, EventListener* listener, bool in_capture_phase)
  69. {
  70. EventListenerEntry entry(id, listener, in_capture_phase);
  71. // The entries are sorted by (id,phase). Find the bounds of this sort, then find the entry.
  72. // We could also just do a linear search over all the entries, which might be faster for low number of entries.
  73. auto range = std::equal_range(listeners.begin(), listeners.end(), entry, CompareIdPhase());
  74. auto it = std::find(range.first, range.second, entry);
  75. if (it != range.second)
  76. {
  77. // We found our listener, remove it
  78. listeners.erase(it);
  79. listener->OnDetach(element);
  80. }
  81. }
  82. // Detaches all events from this dispatcher and all child dispatchers.
  83. void EventDispatcher::DetachAllEvents()
  84. {
  85. for (const auto& event : listeners)
  86. event.listener->OnDetach(element);
  87. listeners.clear();
  88. for (int i = 0; i < element->GetNumChildren(true); ++i)
  89. element->GetChild(i)->GetEventDispatcher()->DetachAllEvents();
  90. }
  91. bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const String& type, const Dictionary& parameters, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  92. {
  93. EventPtr event = Factory::InstanceEvent(target_element, id, type, parameters, interruptible);
  94. if (!event)
  95. return false;
  96. // Build the element traversal from the tree
  97. typedef std::vector<Element*> Elements;
  98. Elements elements;
  99. Element* walk_element = target_element->GetParentNode();
  100. while (walk_element)
  101. {
  102. elements.push_back(walk_element);
  103. walk_element = walk_element->GetParentNode();
  104. }
  105. event->SetPhase(EventPhase::Capture);
  106. // Capture phase - root, to target (only events that have registered as capture events)
  107. // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
  108. for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
  109. {
  110. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  111. event->SetCurrentElement(elements[i]);
  112. dispatcher->TriggerEvents(*event, default_action_phase);
  113. }
  114. // Target phase - direct at the target
  115. if (event->IsPropagating())
  116. {
  117. event->SetPhase(EventPhase::Target);
  118. event->SetCurrentElement(target_element);
  119. TriggerEvents(*event, default_action_phase);
  120. }
  121. // Bubble phase - target to root (normal event bindings)
  122. if (bubbles && event->IsPropagating())
  123. {
  124. event->SetPhase(EventPhase::Bubble);
  125. for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
  126. {
  127. EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
  128. event->SetCurrentElement(elements[i]);
  129. dispatcher->TriggerEvents(*event, default_action_phase);
  130. }
  131. }
  132. bool propagating = event->IsPropagating();
  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. // Convert to indices. If any listeners are added or removed during ProcessEvent, this guards against iterator invalidation.
  175. // Note: The result of listener add/remove may then instead be that a listener is skipped or repeated.
  176. const size_t i_end = (size_t)(end - listeners.begin());
  177. for (size_t i = (size_t)(begin - listeners.begin()); i < i_end && i < listeners.size(); ++i)
  178. {
  179. listeners[i].listener->ProcessEvent(event);
  180. }
  181. const bool do_default_action = ((unsigned int)phase & (unsigned int)default_action_phase);
  182. // Do the default action unless we have been cancelled.
  183. if (do_default_action && event.IsPropagating())
  184. {
  185. element->ProcessDefaultAction(event);
  186. }
  187. }
  188. }
  189. }