ElementHandle.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-2023 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 "ElementHandle.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/ElementDocument.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include "../../Include/RmlUi/Core/Event.h"
  33. #include "../../Include/RmlUi/Core/Property.h"
  34. namespace Rml {
  35. ElementHandle::ElementHandle(const String& tag) : Element(tag), drag_start(0, 0)
  36. {
  37. // Make sure we can be dragged!
  38. SetProperty(PropertyId::Drag, Property(Style::Drag::Drag));
  39. move_target = nullptr;
  40. size_target = nullptr;
  41. initialised = false;
  42. }
  43. ElementHandle::~ElementHandle() {}
  44. void ElementHandle::OnAttributeChange(const ElementAttributes& changed_attributes)
  45. {
  46. Element::OnAttributeChange(changed_attributes);
  47. // Reset initialised state if the move or size targets have changed.
  48. if (changed_attributes.find("move_target") != changed_attributes.end() || changed_attributes.find("size_target") != changed_attributes.end())
  49. {
  50. initialised = false;
  51. move_target = nullptr;
  52. size_target = nullptr;
  53. }
  54. }
  55. void ElementHandle::ProcessDefaultAction(Event& event)
  56. {
  57. Element::ProcessDefaultAction(event);
  58. if (event.GetTargetElement() == this)
  59. {
  60. // Lazy initialisation.
  61. if (!initialised && GetOwnerDocument())
  62. {
  63. String move_target_name = GetAttribute<String>("move_target", "");
  64. if (!move_target_name.empty())
  65. move_target = GetElementById(move_target_name);
  66. String size_target_name = GetAttribute<String>("size_target", "");
  67. if (!size_target_name.empty())
  68. size_target = GetElementById(size_target_name);
  69. initialised = true;
  70. }
  71. auto GetSize = [](const Box& box, const ComputedValues& computed) {
  72. return box.GetSize((computed.box_sizing() == Style::BoxSizing::BorderBox) ? Box::BORDER : Box::CONTENT);
  73. };
  74. // Set any auto margins to their current value, since auto-margins may affect the size and position of an element.
  75. auto SetDefiniteMargins = [](Element* element, const ComputedValues& computed) {
  76. auto SetDefiniteMargin = [](Element* element, PropertyId margin_id, Box::Edge edge) {
  77. element->SetProperty(margin_id, Property(Math::Round(element->GetBox().GetEdge(Box::MARGIN, edge)), Property::PX));
  78. };
  79. using Style::Margin;
  80. if (computed.margin_top().type == Margin::Auto)
  81. SetDefiniteMargin(element, PropertyId::MarginTop, Box::TOP);
  82. if (computed.margin_right().type == Margin::Auto)
  83. SetDefiniteMargin(element, PropertyId::MarginRight, Box::RIGHT);
  84. if (computed.margin_bottom().type == Margin::Auto)
  85. SetDefiniteMargin(element, PropertyId::MarginBottom, Box::BOTTOM);
  86. if (computed.margin_left().type == Margin::Auto)
  87. SetDefiniteMargin(element, PropertyId::MarginLeft, Box::LEFT);
  88. };
  89. if (event == EventId::Dragstart)
  90. {
  91. // Store the drag starting position
  92. drag_start = event.GetUnprojectedMouseScreenPos();
  93. // Store current element position and size
  94. if (move_target)
  95. {
  96. using namespace Style;
  97. const Box& box = move_target->GetBox();
  98. const auto& computed = move_target->GetComputedValues();
  99. // Store the initial margin edge position, since top/left properties determine the margin position.
  100. move_original_position.x = move_target->GetOffsetLeft() - box.GetEdge(Box::MARGIN, Box::LEFT);
  101. move_original_position.y = move_target->GetOffsetTop() - box.GetEdge(Box::MARGIN, Box::TOP);
  102. // Check if we have auto-size together with definite right/bottom; if so, the size needs to be fixed to the current size.
  103. if (computed.width().type == Width::Auto && computed.right().type != Right::Auto)
  104. move_target->SetProperty(PropertyId::Width, Property(Math::Round(GetSize(box, computed).x), Property::PX));
  105. if (computed.height().type == Height::Auto && computed.bottom().type != Bottom::Auto)
  106. move_target->SetProperty(PropertyId::Height, Property(Math::Round(GetSize(box, computed).y), Property::PX));
  107. SetDefiniteMargins(move_target, computed);
  108. }
  109. if (size_target)
  110. {
  111. using namespace Style;
  112. const Box& box = size_target->GetBox();
  113. const auto& computed = size_target->GetComputedValues();
  114. size_original_size = GetSize(box, computed);
  115. SetDefiniteMargins(size_target, computed);
  116. }
  117. }
  118. else if (event == EventId::Drag)
  119. {
  120. const Vector2f delta = event.GetUnprojectedMouseScreenPos() - drag_start;
  121. if (move_target)
  122. {
  123. const Vector2f new_position = (move_original_position + delta).Round();
  124. move_target->SetProperty(PropertyId::Left, Property(new_position.x, Property::PX));
  125. move_target->SetProperty(PropertyId::Top, Property(new_position.y, Property::PX));
  126. }
  127. if (size_target)
  128. {
  129. const Vector2f new_size = Math::Max((size_original_size + delta).Round(), Vector2f(0.f));
  130. size_target->SetProperty(PropertyId::Width, Property(new_size.x, Property::PX));
  131. size_target->SetProperty(PropertyId::Height, Property(new_size.y, Property::PX));
  132. }
  133. Dictionary parameters;
  134. parameters["handle_x"] = delta.x;
  135. parameters["handle_y"] = delta.y;
  136. DispatchEvent(EventId::Handledrag, parameters);
  137. }
  138. }
  139. }
  140. } // namespace Rml