ElementHandle.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "ElementHandle.h"
  30. #include "../../Include/RmlUi/Core/ElementDocument.h"
  31. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include "../../Include/RmlUi/Core/Property.h"
  33. #include "../../Include/RmlUi/Core/Event.h"
  34. namespace Rml {
  35. namespace Core {
  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. {
  46. }
  47. void ElementHandle::OnAttributeChange(const ElementAttributes& changed_attributes)
  48. {
  49. Element::OnAttributeChange(changed_attributes);
  50. // Reset initialised state if the move or size targets have changed.
  51. if (changed_attributes.find("move_target") != changed_attributes.end() ||
  52. changed_attributes.find("size_target") != changed_attributes.end())
  53. {
  54. initialised = false;
  55. move_target = nullptr;
  56. size_target = nullptr;
  57. }
  58. }
  59. void ElementHandle::ProcessDefaultAction(Event& event)
  60. {
  61. Element::ProcessDefaultAction(event);
  62. if (event.GetTargetElement() == this)
  63. {
  64. // Lazy initialisation.
  65. if (!initialised && GetOwnerDocument())
  66. {
  67. String move_target_name = GetAttribute<String>("move_target", "");
  68. if (!move_target_name.empty())
  69. move_target = GetElementById(move_target_name);
  70. String size_target_name = GetAttribute<String>("size_target", "");
  71. if (!size_target_name.empty())
  72. size_target = GetElementById(size_target_name);
  73. initialised = true;
  74. }
  75. if (event == EventId::Dragstart)
  76. {
  77. // Store the drag starting position
  78. drag_start.x = event.GetParameter< int >("mouse_x", 0);
  79. drag_start.y = event.GetParameter< int >("mouse_y", 0);
  80. // Store current element position and size
  81. if (move_target)
  82. {
  83. move_original_position.x = move_target->GetOffsetLeft();
  84. move_original_position.y = move_target->GetOffsetTop();
  85. }
  86. if (size_target)
  87. size_original_size = size_target->GetBox().GetSize(Box::CONTENT);
  88. }
  89. else if (event == EventId::Drag)
  90. {
  91. // Work out the delta
  92. int x = event.GetParameter< int >("mouse_x", 0) - drag_start.x;
  93. int y = event.GetParameter< int >("mouse_y", 0) - drag_start.y;
  94. // Update the move and size objects
  95. if (move_target)
  96. {
  97. move_target->SetProperty(PropertyId::Left, Property(Math::RealToInteger(move_original_position.x + x), Property::PX));
  98. move_target->SetProperty(PropertyId::Top, Property(Math::RealToInteger(move_original_position.y + y), Property::PX));
  99. }
  100. if (size_target)
  101. {
  102. using namespace Style;
  103. const auto& computed = GetComputedValues();
  104. // Check if we have auto-margins; if so, they have to be set to the current margins.
  105. if (computed.margin_top.type == Margin::Auto)
  106. size_target->SetProperty(PropertyId::MarginTop, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::TOP)), Property::PX));
  107. if (computed.margin_right.type == Margin::Auto)
  108. size_target->SetProperty(PropertyId::MarginRight, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::RIGHT)), Property::PX));
  109. if (computed.margin_bottom.type == Margin::Auto)
  110. size_target->SetProperty(PropertyId::MarginBottom, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM)), Property::PX));
  111. if (computed.margin_left.type == Margin::Auto)
  112. size_target->SetProperty(PropertyId::MarginLeft, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::LEFT)), Property::PX));
  113. int new_x = Math::RealToInteger(size_original_size.x + x);
  114. int new_y = Math::RealToInteger(size_original_size.y + y);
  115. size_target->SetProperty(PropertyId::Width, Property(Math::Max< float >((float) new_x, 0), Property::PX));
  116. size_target->SetProperty(PropertyId::Height, Property(Math::Max< float >((float) new_y, 0), Property::PX));
  117. }
  118. Dictionary parameters;
  119. parameters["handle_x"] = x;
  120. parameters["handle_y"] = y;
  121. DispatchEvent(EventId::Handledrag, parameters);
  122. }
  123. }
  124. }
  125. }
  126. }