Event.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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()
  34. {
  35. phase = PHASE_UNKNOWN;
  36. interruped = false;
  37. interruptible = false;
  38. current_element = NULL;
  39. target_element = NULL;
  40. }
  41. Event::Event(Element* _target_element, const String& _type, const Dictionary& _parameters, bool _interruptible) : type(_type), parameters(_parameters), target_element(_target_element), parameters_backup(_parameters), interruptible(_interruptible)
  42. {
  43. phase = PHASE_UNKNOWN;
  44. interruped = false;
  45. current_element = NULL;
  46. }
  47. Event::~Event()
  48. {
  49. }
  50. void Event::SetCurrentElement(Element* element)
  51. {
  52. ProjectMouse(element);
  53. current_element = element;
  54. }
  55. Element* Event::GetCurrentElement() const
  56. {
  57. return current_element;
  58. }
  59. Element* Event::GetTargetElement() const
  60. {
  61. return target_element;
  62. }
  63. const String& Event::GetType() const
  64. {
  65. return type;
  66. }
  67. bool Event::operator==(const String& _type) const
  68. {
  69. return type == _type;
  70. }
  71. void Event::SetPhase(EventPhase _phase)
  72. {
  73. phase = _phase;
  74. }
  75. Event::EventPhase Event::GetPhase() const
  76. {
  77. return phase;
  78. }
  79. bool Event::IsPropagating() const
  80. {
  81. return !interruped;
  82. }
  83. void Event::StopPropagation()
  84. {
  85. // Set interruped to true if we can be interruped
  86. if (interruptible)
  87. {
  88. interruped = true;
  89. }
  90. }
  91. const Dictionary* Event::GetParameters() const
  92. {
  93. return &parameters;
  94. }
  95. void Event::OnReferenceDeactivate()
  96. {
  97. instancer->ReleaseEvent(this);
  98. }
  99. void Event::ProjectMouse(Element* element)
  100. {
  101. if (element)
  102. {
  103. Variant *old_mouse_x = parameters_backup.Get("mouse_x");
  104. Variant *old_mouse_y = parameters_backup.Get("mouse_y");
  105. if (!old_mouse_x || !old_mouse_y)
  106. {
  107. // This is not a mouse event.
  108. return;
  109. }
  110. Variant *mouse_x = parameters.Get("mouse_x");
  111. Variant *mouse_y = parameters.Get("mouse_y");
  112. if (!mouse_x || !mouse_y)
  113. {
  114. // This should not happen.
  115. return;
  116. }
  117. Vector2f old_mouse(
  118. old_mouse_x->Get< float >(),
  119. old_mouse_y->Get< float >()
  120. );
  121. Vector2f mouse = element->Project(old_mouse);
  122. mouse_x->Reset(mouse.x);
  123. mouse_y->Reset(mouse.y);
  124. }
  125. else
  126. {
  127. parameters = parameters_backup;
  128. }
  129. }
  130. }
  131. }