Event.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "../../Include/RmlUi/Core/Event.h"
  30. #include "../../Include/RmlUi/Core/EventInstancer.h"
  31. namespace Rml {
  32. namespace Core {
  33. Event::Event() : id(EventId::Invalid)
  34. {
  35. id = EventId::Invalid;
  36. phase = EventPhase::None;
  37. interruptible = false;
  38. interrupted = false;
  39. interrupted_immediate = false;
  40. default_prevented = false;
  41. current_element = nullptr;
  42. target_element = nullptr;
  43. has_mouse_position = false;
  44. mouse_screen_position = Vector2f(0, 0);
  45. }
  46. Event::Event(Element* _target_element, EventId id, const String& type, const Dictionary& _parameters, bool interruptible)
  47. : parameters(_parameters), target_element(_target_element), type(type), id(id), interruptible(interruptible)
  48. {
  49. phase = EventPhase::None;
  50. interrupted = false;
  51. interrupted_immediate = false;
  52. default_prevented = false;
  53. current_element = nullptr;
  54. has_mouse_position = false;
  55. mouse_screen_position = Vector2f(0, 0);
  56. const Variant* mouse_x = GetIf(parameters, "mouse_x");
  57. const Variant* mouse_y = GetIf(parameters, "mouse_y");
  58. if (mouse_x && mouse_y)
  59. {
  60. has_mouse_position = true;
  61. mouse_x->GetInto(mouse_screen_position.x);
  62. mouse_y->GetInto(mouse_screen_position.y);
  63. }
  64. }
  65. Event::~Event()
  66. {
  67. }
  68. void Event::SetCurrentElement(Element* element)
  69. {
  70. current_element = element;
  71. if(has_mouse_position)
  72. {
  73. ProjectMouse(element);
  74. }
  75. }
  76. Element* Event::GetCurrentElement() const
  77. {
  78. return current_element;
  79. }
  80. Element* Event::GetTargetElement() const
  81. {
  82. return target_element;
  83. }
  84. const String& Event::GetType() const
  85. {
  86. return type;
  87. }
  88. bool Event::operator==(const String& _type) const
  89. {
  90. return type == _type;
  91. }
  92. bool Event::operator==(EventId _id) const
  93. {
  94. return id == _id;
  95. }
  96. void Event::SetPhase(EventPhase _phase)
  97. {
  98. phase = _phase;
  99. }
  100. EventPhase Event::GetPhase() const
  101. {
  102. return phase;
  103. }
  104. bool Event::IsPropagating() const
  105. {
  106. return !interrupted;
  107. }
  108. void Event::StopPropagation()
  109. {
  110. // Set interrupted to true if we can be interrupted
  111. if (interruptible)
  112. {
  113. interrupted = true;
  114. }
  115. }
  116. bool Event::IsImmediatePropagating() const
  117. {
  118. return !interrupted_immediate;
  119. }
  120. bool Event::IsDefaultPrevented() const
  121. {
  122. return default_prevented;
  123. }
  124. bool Event::IsInterruptible() const
  125. {
  126. return interruptible;
  127. }
  128. void Event::StopImmediatePropagation()
  129. {
  130. if(interruptible)
  131. {
  132. interrupted_immediate = true;
  133. interrupted = true;
  134. }
  135. }
  136. void Event::PreventDefault()
  137. {
  138. default_prevented = true;
  139. }
  140. const Dictionary& Event::GetParameters() const
  141. {
  142. return parameters;
  143. }
  144. const Vector2f& Event::GetUnprojectedMouseScreenPos() const
  145. {
  146. return mouse_screen_position;
  147. }
  148. void Event::Release()
  149. {
  150. instancer->ReleaseEvent(this);
  151. }
  152. EventId Event::GetId() const
  153. {
  154. return id;
  155. }
  156. void Event::ProjectMouse(Element* element)
  157. {
  158. if(!element)
  159. {
  160. parameters["mouse_x"] = mouse_screen_position.x;
  161. parameters["mouse_y"] = mouse_screen_position.y;
  162. return;
  163. }
  164. // Only need to project mouse position if element has a transform state
  165. if (element->GetTransformState())
  166. {
  167. // Project mouse from parent (previous 'mouse_x/y' property) to child (element)
  168. Variant *mouse_x = GetIf(parameters, "mouse_x");
  169. Variant *mouse_y = GetIf(parameters, "mouse_y");
  170. if (!mouse_x || !mouse_y)
  171. {
  172. RMLUI_ERROR;
  173. return;
  174. }
  175. Vector2f projected_position = mouse_screen_position;
  176. // Not sure how best to handle the case where the projection fails.
  177. if (element->Project(projected_position))
  178. {
  179. *mouse_x = projected_position.x;
  180. *mouse_y = projected_position.y;
  181. }
  182. else
  183. StopPropagation();
  184. }
  185. }
  186. }
  187. }