Event.cpp 4.2 KB

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