From 11d5009f466d394b4f71a3f791f7073e2b65a00f Mon Sep 17 00:00:00 2001 From: Chris Galvan Date: Thu, 12 Aug 2021 16:31:39 -0500 Subject: [PATCH] Fixed creating entities in the viewport logic to use hit test detection. Signed-off-by: Chris Galvan --- Code/Editor/EditorViewportWidget.cpp | 3 +- .../SandboxIntegration.cpp | 2 +- Code/Editor/Viewport.cpp | 42 +++++++++++-------- Code/Editor/Viewport.h | 2 + 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/Code/Editor/EditorViewportWidget.cpp b/Code/Editor/EditorViewportWidget.cpp index 9ef20e02fb..28e8cce33e 100644 --- a/Code/Editor/EditorViewportWidget.cpp +++ b/Code/Editor/EditorViewportWidget.cpp @@ -2697,8 +2697,7 @@ void EditorViewportWidget::RestoreViewportAfterGameMode() QString( tr("When leaving \" Game Mode \" the engine will automatically restore your camera position to the default position before you " "had entered Game mode.

If you dislike this setting you can always change this anytime in the global " - "preferences.

")) - .arg(EditorPreferencesGeneralRestoreViewportCameraSettingName); + "preferences.

")); QString restoreOnExitGameModePopupDisabledRegKey("Editor/AutoHide/ViewportCameraRestoreOnExitGameMode"); // Read the popup disabled registry value diff --git a/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp b/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp index 2ed2a30f08..66c57361be 100644 --- a/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp +++ b/Code/Editor/Plugins/ComponentEntityEditorPlugin/SandboxIntegration.cpp @@ -1452,7 +1452,7 @@ void SandboxIntegrationManager::ContextMenu_NewEntity() if (view) { const QPoint viewPoint(m_contextMenuViewPoint.GetX(), m_contextMenuViewPoint.GetY()); - worldPosition = LYVec3ToAZVec3(view->SnapToGrid(view->ViewToWorld(viewPoint))); + worldPosition = view->GetHitLocation(viewPoint); } CreateNewEntityAtPosition(worldPosition); diff --git a/Code/Editor/Viewport.cpp b/Code/Editor/Viewport.cpp index 2bc1b21216..873f555c80 100644 --- a/Code/Editor/Viewport.cpp +++ b/Code/Editor/Viewport.cpp @@ -46,24 +46,7 @@ void QtViewport::BuildDragDropContext(AzQtComponents::ViewportDragContext& conte PreWidgetRendering(); // required so that the current render cam is set. - Vec3 pos = Vec3(ZERO); - HitContext hit; - if (HitTest(pt, hit)) - { - pos = hit.raySrc + hit.rayDir * hit.dist; - pos = SnapToGrid(pos); - } - else - { - bool hitTerrain; - pos = ViewToWorld(pt, &hitTerrain); - if (hitTerrain) - { - pos.z = GetIEditor()->GetTerrainElevation(pos.x, pos.y); - } - pos = SnapToGrid(pos); - } - context.m_hitLocation = AZ::Vector3(pos.x, pos.y, pos.z); + context.m_hitLocation = GetHitLocation(pt); PostWidgetRendering(); } @@ -1154,6 +1137,29 @@ bool QtViewport::HitTest(const QPoint& point, HitContext& hitInfo) return false; } +AZ::Vector3 QtViewport::GetHitLocation(const QPoint& point) +{ + Vec3 pos = Vec3(ZERO); + HitContext hit; + if (HitTest(point, hit)) + { + pos = hit.raySrc + hit.rayDir * hit.dist; + pos = SnapToGrid(pos); + } + else + { + bool hitTerrain; + pos = ViewToWorld(point, &hitTerrain); + if (hitTerrain) + { + pos.z = GetIEditor()->GetTerrainElevation(pos.x, pos.y); + } + pos = SnapToGrid(pos); + } + + return AZ::Vector3(pos.x, pos.y, pos.z); +} + ////////////////////////////////////////////////////////////////////////// void QtViewport::SetZoomFactor(float fZoomFactor) { diff --git a/Code/Editor/Viewport.h b/Code/Editor/Viewport.h index 823b8c77b1..6b5bfb5c34 100644 --- a/Code/Editor/Viewport.h +++ b/Code/Editor/Viewport.h @@ -201,6 +201,7 @@ public: //! Performs hit testing of 2d point in view to find which object hit. virtual bool HitTest(const QPoint& point, HitContext& hitInfo) = 0; + virtual AZ::Vector3 GetHitLocation(const QPoint& point) = 0; virtual void MakeConstructionPlane(int axis) = 0; @@ -436,6 +437,7 @@ public: //! Performs hit testing of 2d point in view to find which object hit. bool HitTest(const QPoint& point, HitContext& hitInfo) override; + AZ::Vector3 GetHitLocation(const QPoint& point) override; //! Do 2D hit testing of line in world space. // pToCameraDistance is an optional output parameter in which distance from the camera to the line is returned.