Event.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../../Include/RmlUi/Core/Event.h"
  2. #include "../../Include/RmlUi/Core/Element.h"
  3. #include "../../Include/RmlUi/Core/EventInstancer.h"
  4. namespace Rml {
  5. Event::Event() {}
  6. Event::Event(Element* _target_element, EventId id, const String& type, const Dictionary& _parameters, bool interruptible) :
  7. parameters(_parameters), target_element(_target_element), type(type), id(id), interruptible(interruptible)
  8. {
  9. const Variant* mouse_x = GetIf(parameters, "mouse_x");
  10. const Variant* mouse_y = GetIf(parameters, "mouse_y");
  11. if (mouse_x && mouse_y)
  12. {
  13. has_mouse_position = true;
  14. mouse_x->GetInto(mouse_screen_position.x);
  15. mouse_y->GetInto(mouse_screen_position.y);
  16. }
  17. }
  18. Event::~Event() {}
  19. void Event::SetCurrentElement(Element* element)
  20. {
  21. current_element = element;
  22. if (has_mouse_position)
  23. {
  24. ProjectMouse(element);
  25. }
  26. }
  27. Element* Event::GetCurrentElement() const
  28. {
  29. return current_element;
  30. }
  31. Element* Event::GetTargetElement() const
  32. {
  33. return target_element;
  34. }
  35. const String& Event::GetType() const
  36. {
  37. return type;
  38. }
  39. bool Event::operator==(const String& _type) const
  40. {
  41. return type == _type;
  42. }
  43. bool Event::operator==(EventId _id) const
  44. {
  45. return id == _id;
  46. }
  47. void Event::SetPhase(EventPhase _phase)
  48. {
  49. phase = _phase;
  50. }
  51. EventPhase Event::GetPhase() const
  52. {
  53. return phase;
  54. }
  55. bool Event::IsPropagating() const
  56. {
  57. return !interrupted;
  58. }
  59. void Event::StopPropagation()
  60. {
  61. if (interruptible)
  62. {
  63. interrupted = true;
  64. }
  65. }
  66. bool Event::IsImmediatePropagating() const
  67. {
  68. return !interrupted_immediate;
  69. }
  70. bool Event::IsInterruptible() const
  71. {
  72. return interruptible;
  73. }
  74. void Event::StopImmediatePropagation()
  75. {
  76. if (interruptible)
  77. {
  78. interrupted_immediate = true;
  79. interrupted = true;
  80. }
  81. }
  82. const Dictionary& Event::GetParameters() const
  83. {
  84. return parameters;
  85. }
  86. Vector2f Event::GetUnprojectedMouseScreenPos() const
  87. {
  88. return mouse_screen_position;
  89. }
  90. void Event::Release()
  91. {
  92. if (instancer)
  93. instancer->ReleaseEvent(this);
  94. else
  95. Log::Message(Log::LT_WARNING, "Leak detected: Event %s not instanced via RmlUi Factory. Unable to release.", type.c_str());
  96. }
  97. EventId Event::GetId() const
  98. {
  99. return id;
  100. }
  101. void Event::ProjectMouse(Element* element)
  102. {
  103. if (!element)
  104. {
  105. parameters["mouse_x"] = mouse_screen_position.x;
  106. parameters["mouse_y"] = mouse_screen_position.y;
  107. return;
  108. }
  109. // Only need to project mouse position if element has a transform state
  110. if (element->GetTransformState())
  111. {
  112. // Project mouse from parent (previous 'mouse_x/y' property) to child (element)
  113. Variant* mouse_x = GetIf(parameters, "mouse_x");
  114. Variant* mouse_y = GetIf(parameters, "mouse_y");
  115. if (!mouse_x || !mouse_y)
  116. {
  117. RMLUI_ERROR;
  118. return;
  119. }
  120. Vector2f projected_position = mouse_screen_position;
  121. // Not sure how best to handle the case where the projection fails.
  122. if (element->Project(projected_position))
  123. {
  124. *mouse_x = projected_position.x;
  125. *mouse_y = projected_position.y;
  126. }
  127. else
  128. StopPropagation();
  129. }
  130. }
  131. } // namespace Rml