ViewportAddGuideInteraction.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "ViewportAddGuideInteraction.h"
  10. #include "CanvasHelpers.h"
  11. #include "GuideHelpers.h"
  12. #include <LyShine/Bus/UiEditorCanvasBus.h>
  13. ViewportAddGuideInteraction::ViewportAddGuideInteraction(
  14. EditorWindow* editorWindow,
  15. AZ::EntityId canvasId,
  16. bool guideIsVertical,
  17. const AZ::Vector2& startDragMousePos
  18. )
  19. : ViewportDragInteraction(startDragMousePos)
  20. , m_editorWindow(editorWindow)
  21. , m_canvasId(canvasId)
  22. , m_guideIsVertical(guideIsVertical)
  23. , m_cursorViewportPos(0.0f, 0.0f)
  24. {
  25. // store whether snapping is enabled for this canvas
  26. UiEditorCanvasBus::EventResult(m_isSnapping, canvasId, &UiEditorCanvasBus::Events::GetIsSnapEnabled);
  27. m_addingGuideAtPosition = CanvasHelpers::GetSnappedCanvasPoint(m_canvasId, startDragMousePos, m_isSnapping);
  28. }
  29. ViewportAddGuideInteraction::~ViewportAddGuideInteraction()
  30. {
  31. }
  32. void ViewportAddGuideInteraction::Update(const AZ::Vector2& mousePos)
  33. {
  34. m_cursorViewportPos = mousePos;
  35. m_addingGuideAtPosition = CanvasHelpers::GetSnappedCanvasPoint(m_canvasId, mousePos, m_isSnapping);
  36. }
  37. void ViewportAddGuideInteraction::Render(Draw2dHelper& draw2d)
  38. {
  39. float pos = (m_guideIsVertical) ? m_addingGuideAtPosition.GetX() : m_addingGuideAtPosition.GetY();
  40. GuideHelpers::DrawGhostGuideLine(draw2d, m_editorWindow->GetCanvas(), m_guideIsVertical, m_editorWindow->GetViewport(), m_addingGuideAtPosition);
  41. GuideHelpers::DrawGuidePosTextDisplay(draw2d, m_guideIsVertical, pos, m_editorWindow->GetViewport());
  42. }
  43. void ViewportAddGuideInteraction::EndInteraction(EndState endState)
  44. {
  45. if (endState == EndState::Inside || (m_guideIsVertical ? endState == EndState::OutsideY : endState == EndState::OutsideX))
  46. {
  47. // The drag was released in the viewport
  48. AZ::EntityId canvasEntityId = m_editorWindow->GetCanvas();
  49. // record canvas state before the change
  50. AZStd::string canvasUndoXml = CanvasHelpers::BeginUndoableCanvasChange(canvasEntityId);
  51. // Add the new guide to the canvas
  52. if (m_guideIsVertical)
  53. {
  54. UiEditorCanvasBus::Event(canvasEntityId, &UiEditorCanvasBus::Events::AddVerticalGuide, m_addingGuideAtPosition.GetX());
  55. }
  56. else
  57. {
  58. UiEditorCanvasBus::Event(canvasEntityId, &UiEditorCanvasBus::Events::AddHorizontalGuide, m_addingGuideAtPosition.GetY());
  59. }
  60. // force guides to be visible so that you can see the added guide
  61. m_editorWindow->GetViewport()->ShowGuides(true);
  62. // Create the undoable command and push it onto the undo stack
  63. CanvasHelpers::EndUndoableCanvasChange(m_editorWindow, "add guide", canvasUndoXml);
  64. }
  65. }