ElementHandle.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/Context.h"
  31. #include "../../Include/RmlUi/Core/ElementDocument.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/Event.h"
  34. #include "../../Include/RmlUi/Core/Property.h"
  35. namespace Rml {
  36. ElementHandle::ElementHandle(const String& tag) : Element(tag), drag_start(0, 0)
  37. {
  38. // Make sure we can be dragged!
  39. SetProperty(PropertyId::Drag, Property(Style::Drag::Drag));
  40. move_target = nullptr;
  41. size_target = nullptr;
  42. initialised = false;
  43. }
  44. ElementHandle::~ElementHandle() {}
  45. void ElementHandle::OnAttributeChange(const ElementAttributes& changed_attributes)
  46. {
  47. Element::OnAttributeChange(changed_attributes);
  48. // Reset initialised state if the move or size targets have changed.
  49. if (changed_attributes.find("move_target") != changed_attributes.end() || changed_attributes.find("size_target") != changed_attributes.end())
  50. {
  51. initialised = false;
  52. move_target = nullptr;
  53. size_target = nullptr;
  54. }
  55. }
  56. void ElementHandle::ProcessDefaultAction(Event& event)
  57. {
  58. Element::ProcessDefaultAction(event);
  59. if (event.GetTargetElement() == this)
  60. {
  61. // Lazy initialisation.
  62. if (!initialised && GetOwnerDocument())
  63. {
  64. String move_target_name = GetAttribute<String>("move_target", "");
  65. if (!move_target_name.empty())
  66. move_target = GetElementById(move_target_name);
  67. String size_target_name = GetAttribute<String>("size_target", "");
  68. if (!size_target_name.empty())
  69. size_target = GetElementById(size_target_name);
  70. initialised = true;
  71. }
  72. // Set any auto margins to their current value, since auto-margins may affect the size and position of an element.
  73. auto SetDefiniteMargins = [](Element* element, const ComputedValues& computed) {
  74. auto SetDefiniteMargin = [](Element* element, PropertyId margin_id, BoxEdge edge) {
  75. element->SetProperty(margin_id, Property(Math::Round(element->GetBox().GetEdge(BoxArea::Margin, edge)), Unit::PX));
  76. };
  77. using Style::Margin;
  78. if (computed.margin_top().type == Margin::Auto)
  79. SetDefiniteMargin(element, PropertyId::MarginTop, BoxEdge::Top);
  80. if (computed.margin_right().type == Margin::Auto)
  81. SetDefiniteMargin(element, PropertyId::MarginRight, BoxEdge::Right);
  82. if (computed.margin_bottom().type == Margin::Auto)
  83. SetDefiniteMargin(element, PropertyId::MarginBottom, BoxEdge::Bottom);
  84. if (computed.margin_left().type == Margin::Auto)
  85. SetDefiniteMargin(element, PropertyId::MarginLeft, BoxEdge::Left);
  86. };
  87. // The following table lists the expected behavior for each combination of definite (non-auto) properties:
  88. //
  89. // Definite properties | Move | Size
  90. // ---------------------|-------------------------|--------------------------
  91. // (none) | left += dx | width += dx
  92. // left | left += dx | width += dx
  93. // right | right -= dx | right -= dx; width += dx
  94. // width | left += dx | width += dx
  95. // left & right | left += dx; right -= dx | right -= dx
  96. // left & width | left += dx; | width += dx
  97. // right & width | right -= dx | right -= dx; width += dx
  98. // right & width | right -= dx | right -= dx; width += dx
  99. // left & right & width | left += dx; | width += dx
  100. //
  101. // For simplicity, this table only specifies the horizontal direction. The same behavior applies for the
  102. // corresponding properties in the vertical direction. For now, we assume that the handle is anchored to the
  103. // bottom-right corner of the target element.
  104. if (event == EventId::Dragstart)
  105. {
  106. Context* context = GetContext();
  107. drag_start = event.GetUnprojectedMouseScreenPos();
  108. if (move_target && context)
  109. {
  110. using namespace Style;
  111. const Box& parent_box =
  112. move_target->GetOffsetParent() ? move_target->GetOffsetParent()->GetBox() : context->GetRootElement()->GetBox();
  113. const Vector2f containing_block = move_target->GetContainingBlock();
  114. const Box& box = move_target->GetBox();
  115. const auto& computed = move_target->GetComputedValues();
  116. const Position position = computed.position();
  117. SetDefiniteMargins(move_target, computed);
  118. auto ResolveValueOrInvoke = [](const LengthPercentageAuto& value, float containing_block, Position position, auto&& fallback_func) {
  119. if (value.type != LengthPercentageAuto::Auto)
  120. return ResolveValue(value, containing_block);
  121. if (position == Position::Relative)
  122. return 0.0f;
  123. return fallback_func();
  124. };
  125. // The following is derived at by solving the expressions in 'Element::UpdateOffset' for the computed top/left values.
  126. move_original_position_top_left.x = ResolveValueOrInvoke(computed.left(), containing_block.x, position, [&] {
  127. return move_target->GetOffsetLeft() -
  128. (box.GetEdge(BoxArea::Margin, BoxEdge::Left) + parent_box.GetEdge(BoxArea::Border, BoxEdge::Left));
  129. });
  130. move_original_position_top_left.y = ResolveValueOrInvoke(computed.top(), containing_block.y, position, [&] {
  131. return move_target->GetOffsetTop() -
  132. (box.GetEdge(BoxArea::Margin, BoxEdge::Top) + parent_box.GetEdge(BoxArea::Border, BoxEdge::Top));
  133. });
  134. // For the bottom-right positions, we don't need to find the 'auto'-invoked expressions like above,
  135. // since the following value is only used when bottom-right is non-auto.
  136. move_original_position_bottom_right.x = ResolveValue(computed.right(), containing_block.x);
  137. move_original_position_bottom_right.y = ResolveValue(computed.bottom(), containing_block.y);
  138. move_bottom_right.x = (computed.right().type != Right::Auto);
  139. move_bottom_right.y = (computed.bottom().type != Bottom::Auto);
  140. move_top_left.x = (!move_bottom_right.x || computed.left().type != Left::Auto);
  141. move_top_left.y = (!move_bottom_right.y || computed.top().type != Top::Auto);
  142. }
  143. if (size_target && context)
  144. {
  145. using namespace Style;
  146. const Box& box = size_target->GetBox();
  147. const auto& computed = size_target->GetComputedValues();
  148. const Vector2f containing_block = size_target->GetContainingBlock();
  149. SetDefiniteMargins(size_target, computed);
  150. size_original_size = box.GetSize(computed.box_sizing() == BoxSizing::BorderBox ? BoxArea::Border : BoxArea::Content);
  151. size_original_position_bottom_right.x = ResolveValue(computed.right(), containing_block.x);
  152. size_original_position_bottom_right.y = ResolveValue(computed.bottom(), containing_block.y);
  153. size_bottom_right.x = (computed.right().type != Right::Auto);
  154. size_bottom_right.y = (computed.bottom().type != Bottom::Auto);
  155. size_width_height.x =
  156. (computed.left().type == Left::Auto || computed.right().type == Right::Auto || computed.width().type != Width::Auto);
  157. size_width_height.y =
  158. (computed.top().type == Top::Auto || computed.bottom().type == Bottom::Auto || computed.height().type != Height::Auto);
  159. }
  160. }
  161. else if (event == EventId::Drag)
  162. {
  163. const Vector2f delta = event.GetUnprojectedMouseScreenPos() - drag_start;
  164. if (move_target)
  165. {
  166. const Vector2f new_position_top_left = (move_original_position_top_left + delta).Round();
  167. const Vector2f new_position_bottom_right = (move_original_position_bottom_right - delta).Round();
  168. if (move_top_left.x)
  169. move_target->SetProperty(PropertyId::Left, Property(new_position_top_left.x, Unit::PX));
  170. if (move_top_left.y)
  171. move_target->SetProperty(PropertyId::Top, Property(new_position_top_left.y, Unit::PX));
  172. if (move_bottom_right.x)
  173. move_target->SetProperty(PropertyId::Right, Property(new_position_bottom_right.x, Unit::PX));
  174. if (move_bottom_right.y)
  175. move_target->SetProperty(PropertyId::Bottom, Property(new_position_bottom_right.y, Unit::PX));
  176. }
  177. if (size_target)
  178. {
  179. const Vector2f new_size = Math::Max((size_original_size + delta).Round(), Vector2f(0.f));
  180. const Vector2f new_position_bottom_right = (size_original_position_bottom_right - delta).Round();
  181. if (size_width_height.x)
  182. size_target->SetProperty(PropertyId::Width, Property(new_size.x, Unit::PX));
  183. if (size_width_height.y)
  184. size_target->SetProperty(PropertyId::Height, Property(new_size.y, Unit::PX));
  185. if (size_bottom_right.x)
  186. size_target->SetProperty(PropertyId::Right, Property(new_position_bottom_right.x, Unit::PX));
  187. if (size_bottom_right.y)
  188. size_target->SetProperty(PropertyId::Bottom, Property(new_position_bottom_right.y, Unit::PX));
  189. }
  190. Dictionary parameters;
  191. parameters["handle_x"] = delta.x;
  192. parameters["handle_y"] = delta.y;
  193. DispatchEvent(EventId::Handledrag, parameters);
  194. }
  195. }
  196. }
  197. } // namespace Rml