ViewportMoveGuideInteraction.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "ViewportMoveGuideInteraction.h"
  10. #include "CanvasHelpers.h"
  11. #include "GuideHelpers.h"
  12. #include <LyShine/Bus/UiEditorCanvasBus.h>
  13. ViewportMoveGuideInteraction::ViewportMoveGuideInteraction(
  14. EditorWindow* editorWindow,
  15. AZ::EntityId canvasId,
  16. bool guideIsVertical,
  17. int guideIndex,
  18. const AZ::Vector2& startDragMousePos
  19. )
  20. : ViewportDragInteraction(startDragMousePos)
  21. , m_editorWindow(editorWindow)
  22. , m_canvasId(canvasId)
  23. , m_guideIsVertical(guideIsVertical)
  24. , m_guideIndex(guideIndex)
  25. , m_cursorViewportPos(0.0f, 0.0f)
  26. {
  27. // store whether snapping is enabled for this canvas
  28. UiEditorCanvasBus::EventResult(m_isSnapping, canvasId, &UiEditorCanvasBus::Events::GetIsSnapEnabled);
  29. m_startingPosition = GuideHelpers::GetGuidePosition(canvasId, m_guideIsVertical, m_guideIndex);
  30. // store the state before anything is moved
  31. m_canvasUndoXml = CanvasHelpers::BeginUndoableCanvasChange(m_canvasId);
  32. }
  33. ViewportMoveGuideInteraction::~ViewportMoveGuideInteraction()
  34. {
  35. }
  36. void ViewportMoveGuideInteraction::Update(const AZ::Vector2& mousePos)
  37. {
  38. // remember mouse position for render
  39. m_cursorViewportPos = mousePos;
  40. // Move the guide
  41. MoveGuideToMousePos(mousePos);
  42. }
  43. void ViewportMoveGuideInteraction::Render(Draw2dHelper& draw2d)
  44. {
  45. // We don't need to render the guide since its position has been updated and the normal canvas render will draw it
  46. // What we draw is the "visual aid" which in this case is a text display of the ruler position
  47. float pos = GuideHelpers::GetGuidePosition(m_canvasId, m_guideIsVertical, m_guideIndex);
  48. GuideHelpers::DrawGuidePosTextDisplay(draw2d, m_guideIsVertical, pos, m_editorWindow->GetViewport());
  49. }
  50. void ViewportMoveGuideInteraction::EndInteraction(EndState endState)
  51. {
  52. // If the interaction ended inside the viewport then do a move, else do a delete
  53. const char* commandName = "move guide";
  54. if (endState == EndState::OutsideXY || (m_guideIsVertical ? endState == EndState::OutsideX : endState == EndState::OutsideY))
  55. {
  56. GuideHelpers::RemoveGuide(m_canvasId, m_guideIsVertical, m_guideIndex);
  57. commandName = "delete guide";
  58. }
  59. CanvasHelpers::EndUndoableCanvasChange(m_editorWindow, commandName, m_canvasUndoXml);
  60. }
  61. void ViewportMoveGuideInteraction::MoveGuideToMousePos(const AZ::Vector2& viewportPos)
  62. {
  63. AZ::Vector2 snappedPos = CanvasHelpers::GetSnappedCanvasPoint(m_canvasId, viewportPos, m_isSnapping);
  64. GuideHelpers::SetGuidePosition(m_canvasId, m_guideIsVertical, m_guideIndex, snappedPos);
  65. }