Event.cpp 4.5 KB

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