ViewportAlign.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorCommon.h"
  9. #include "ViewportAlign.h"
  10. #include <LyShine/UiComponentTypes.h>
  11. void ViewportAlign::AlignSelectedElements(EditorWindow* editorWindow, AlignType alignType)
  12. {
  13. QTreeWidgetItemRawPtrQList selectedItems = editorWindow->GetHierarchy()->selectedItems();
  14. LyShine::EntityArray selectedElements = SelectionHelpers::GetTopLevelSelectedElements(editorWindow->GetHierarchy(), selectedItems);
  15. // elements that are controlled by a layout element cannot be moved. So make a list of the elements that
  16. // can be aligned.
  17. AZStd::vector<AZ::EntityId> elementsToAlign;
  18. for (auto element : selectedElements)
  19. {
  20. if (!ViewportHelpers::IsControlledByLayout(element))
  21. {
  22. elementsToAlign.push_back(element->GetId());
  23. }
  24. }
  25. // we have to have at least two elements in order to do the align operation
  26. size_t numElements = elementsToAlign.size();
  27. if (numElements < 2)
  28. {
  29. return;
  30. }
  31. // get mode to see if we are in MOVE or ANCHOR mode, in MOVE mode we modify offsets, in ANCHOR mode we modify anchors
  32. ViewportInteraction::InteractionMode interactionMode = editorWindow->GetViewport()->GetViewportInteraction()->GetMode();
  33. // start the undoable event
  34. SerializeHelpers::SerializedEntryList preChangeState;
  35. HierarchyClipboard::BeginUndoableEntitiesChange(editorWindow, preChangeState);
  36. // get the AABB of each element plus the overall AABB of all the top level selected elements
  37. const float minFloat = std::numeric_limits<float>::min();
  38. const float maxFloat = std::numeric_limits<float>::max();
  39. UiTransformInterface::Rect overallBoundingBox;
  40. overallBoundingBox.Set(maxFloat, minFloat, maxFloat, minFloat);
  41. AZStd::vector<UiTransformInterface::Rect> elementBoundingBoxes(numElements);
  42. for (int i = 0; i < numElements; ++i)
  43. {
  44. AZ::EntityId entityId = elementsToAlign[i];
  45. UiTransformInterface::RectPoints points;
  46. UiTransformBus::Event(entityId, &UiTransformBus::Events::GetCanvasSpacePoints, points);
  47. // setup the AABB for this element
  48. AZ::Vector2 topLeft = points.GetAxisAlignedTopLeft();
  49. AZ::Vector2 bottomRight = points.GetAxisAlignedBottomRight();
  50. elementBoundingBoxes[i].left = topLeft.GetX();
  51. elementBoundingBoxes[i].right = bottomRight.GetX();
  52. elementBoundingBoxes[i].top = topLeft.GetY();
  53. elementBoundingBoxes[i].bottom = bottomRight.GetY();
  54. // update the overall AABB
  55. overallBoundingBox.left = AZStd::GetMin(overallBoundingBox.left, topLeft.GetX());
  56. overallBoundingBox.right = AZStd::GetMax(overallBoundingBox.right, bottomRight.GetX());
  57. overallBoundingBox.top = AZStd::GetMin(overallBoundingBox.top, topLeft.GetY());
  58. overallBoundingBox.bottom = AZStd::GetMax(overallBoundingBox.bottom, bottomRight.GetY());
  59. }
  60. // For each element, compute the delta of where it is from where it should be
  61. // then adjust the offsets to align it
  62. for (int i = 0; i < numElements; ++i)
  63. {
  64. UiTransformInterface::Rect& aabb = elementBoundingBoxes[i];
  65. // the delta to move depends on the align type
  66. AZ::Vector2 deltaInCanvasSpace;
  67. switch (alignType)
  68. {
  69. case AlignType::HorizontalLeft:
  70. deltaInCanvasSpace.Set(overallBoundingBox.left - aabb.left, 0.0f);
  71. break;
  72. case AlignType::HorizontalCenter:
  73. deltaInCanvasSpace.Set(overallBoundingBox.GetCenterX() - aabb.GetCenterX(), 0.0f);
  74. break;
  75. case AlignType::HorizontalRight:
  76. deltaInCanvasSpace.Set(overallBoundingBox.right - aabb.right, 0.0f);
  77. break;
  78. case AlignType::VerticalTop:
  79. deltaInCanvasSpace.Set(0.0f, overallBoundingBox.top - aabb.top);
  80. break;
  81. case AlignType::VerticalCenter:
  82. deltaInCanvasSpace.Set(0.0f, overallBoundingBox.GetCenterY() - aabb.GetCenterY());
  83. break;
  84. case AlignType::VerticalBottom:
  85. deltaInCanvasSpace.Set(0.0f, overallBoundingBox.bottom - aabb.bottom);
  86. break;
  87. }
  88. // If this element needs to move...
  89. if (!deltaInCanvasSpace.IsZero())
  90. {
  91. AZ::EntityId entityId = elementsToAlign[i];
  92. AZ::EntityId parentEntityId = EntityHelpers::GetParentElement(entityId)->GetId();
  93. // compute the delta to move in local space (i.e. relative to the parent)
  94. AZ::Vector2 deltaInLocalSpace = EntityHelpers::TransformDeltaFromCanvasToLocalSpace(parentEntityId, deltaInCanvasSpace);
  95. // do the actual move of the element
  96. if (interactionMode == ViewportInteraction::InteractionMode::MOVE)
  97. {
  98. EntityHelpers::MoveByLocalDeltaUsingOffsets(entityId, deltaInLocalSpace);
  99. }
  100. else if (interactionMode == ViewportInteraction::InteractionMode::ANCHOR)
  101. {
  102. EntityHelpers::MoveByLocalDeltaUsingAnchors(entityId, parentEntityId, deltaInLocalSpace, true);
  103. }
  104. // Let listeners know that the properties on this element have changed
  105. UiElementChangeNotificationBus::Event(entityId, &UiElementChangeNotificationBus::Events::UiElementPropertyChanged);
  106. }
  107. }
  108. // Tell the Properties panel to update
  109. const AZ::Uuid transformComponentType = LyShine::UiTransform2dComponentUuid;
  110. editorWindow->GetProperties()->TriggerRefresh(AzToolsFramework::PropertyModificationRefreshLevel::Refresh_Values, &transformComponentType);
  111. // end the undoable event
  112. HierarchyClipboard::EndUndoableEntitiesChange(editorWindow, "align", preChangeState);
  113. }
  114. bool ViewportAlign::IsAlignAllowed(EditorWindow* editorWindow)
  115. {
  116. // if nothing is selected then it is not allowed
  117. QTreeWidgetItemRawPtrQList selectedItems = editorWindow->GetHierarchy()->selectedItems();
  118. if (selectedItems.size() < 2)
  119. {
  120. return false;
  121. }
  122. LyShine::EntityArray selectedElements = SelectionHelpers::GetTopLevelSelectedElements(editorWindow->GetHierarchy(), selectedItems);
  123. // elements that are controlled by a layout element cannot be moved. So make a list of the elements that
  124. // can be aligned.
  125. AZStd::vector<AZ::EntityId> elementsToAlign;
  126. for (auto element : selectedElements)
  127. {
  128. if (!ViewportHelpers::IsControlledByLayout(element))
  129. {
  130. elementsToAlign.push_back(element->GetId());
  131. }
  132. }
  133. // we have to have at least two elements in order to do the align operation
  134. if (elementsToAlign.size() < 2)
  135. {
  136. return false;
  137. }
  138. return true;
  139. }