Files
o3de/Gems/WhiteBox/Code/Source/SubComponentModes/EditorWhiteBoxEdgeRestoreMode.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

219 lines
8.7 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include "EditorWhiteBoxComponentModeCommon.h"
#include "EditorWhiteBoxEdgeRestoreMode.h"
#include "Viewport/WhiteBoxModifierUtil.h"
#include "Viewport/WhiteBoxViewportConstants.h"
#include <AzCore/Component/ComponentBus.h>
#include <AzCore/Component/TransformBus.h>
#include <AzCore/std/sort.h>
#include <AzToolsFramework/ComponentMode/EditorComponentModeBus.h>
#include <AzToolsFramework/Maths/TransformUtils.h>
#include <AzToolsFramework/Viewport/ViewportTypes.h>
#include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
#include <WhiteBox/EditorWhiteBoxComponentBus.h>
namespace WhiteBox
{
static const char* const FlipEdgeUndoRedoDesc = "Flip an edge to divide quad across different diagonal";
static const char* const RestoreEdgeUndoRedoDesc = "Restore an edge to split two connected polygons";
static const char* const RestoreVertexUndoRedoDesc = "Restore a vertex to split two connected edges";
AZ_CLASS_ALLOCATOR_IMPL(EdgeRestoreMode, AZ::SystemAllocator, 0)
void EdgeRestoreMode::Refresh()
{
// noop
}
AZStd::vector<AzToolsFramework::ActionOverride> EdgeRestoreMode::PopulateActions(
[[maybe_unused]] const AZ::EntityComponentIdPair& entityComponentIdPair)
{
return {};
}
bool EdgeRestoreMode::HandleMouseInteraction(
const AzToolsFramework::ViewportInteraction::MouseInteractionEvent& mouseInteraction,
const AZ::EntityComponentIdPair& entityComponentIdPair,
const AZStd::optional<EdgeIntersection>& edgeIntersection,
[[maybe_unused]] const AZStd::optional<PolygonIntersection>& polygonIntersection,
const AZStd::optional<VertexIntersection>& vertexIntersection)
{
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
const auto closestIntersection =
FindClosestGeometryIntersection(edgeIntersection, polygonIntersection, vertexIntersection);
// clear for each event/update
m_edgeIntersection.reset();
m_vertexIntersection.reset();
// update stored edge and vertex intersection
switch (closestIntersection)
{
case GeometryIntersection::Edge:
m_edgeIntersection = edgeIntersection;
break;
case GeometryIntersection::Vertex:
m_vertexIntersection = vertexIntersection;
break;
default:
// do nothing
break;
}
if (InputRestore(mouseInteraction))
{
switch (closestIntersection)
{
// ensure we were actually hovering over an edge when clicking
case GeometryIntersection::Edge:
{
// attempt to restore an edge
// (an optional<> is returned potentially containing two split polygons if successful)
if (Api::RestoreEdge(
*whiteBox, edgeIntersection->m_closestEdgeWithHandle.m_handle, m_edgeHandlesBeingRestored))
{
RecordWhiteBoxAction(*whiteBox, entityComponentIdPair, RestoreEdgeUndoRedoDesc);
return true;
}
}
break;
// ensure we were actually hovering over a vertex when clicking
case GeometryIntersection::Vertex:
{
// note: operation may fail if the vertex is isolated
if (Api::TryRestoreVertex(*whiteBox, vertexIntersection->m_closestVertexWithHandle.m_handle))
{
RecordWhiteBoxAction(*whiteBox, entityComponentIdPair, RestoreVertexUndoRedoDesc);
}
return true;
}
break;
default:
return false;
}
}
if (InputFlipEdge(mouseInteraction))
{
switch (closestIntersection)
{
// ensure we were actually hovering over an edge when clicking
case GeometryIntersection::Edge:
{
// attempt to flip an edge
if (Api::FlipEdge(*whiteBox, edgeIntersection->m_closestEdgeWithHandle.m_handle))
{
RecordWhiteBoxAction(*whiteBox, entityComponentIdPair, FlipEdgeUndoRedoDesc);
return true;
}
}
break;
default:
return false;
}
}
return false;
}
void EdgeRestoreMode::Display(
const AZ::EntityComponentIdPair& entityComponentIdPair, const AZ::Transform& worldFromLocal,
const IntersectionAndRenderData& renderData, const AzFramework::ViewportInfo& viewportInfo,
AzFramework::DebugDisplayRequests& debugDisplay)
{
// clang-format off
const Api::EdgeHandles interactiveEdgeHandles = m_edgeIntersection.has_value()
? Api::EdgeHandles{ m_edgeIntersection->m_closestEdgeWithHandle.m_handle }
: Api::EdgeHandles{};
// clang-format on
debugDisplay.PushMatrix(worldFromLocal);
// draw all 'user' and 'mesh' edges
DrawEdges(
debugDisplay, cl_whiteBoxEdgeUserColor, renderData.m_whiteBoxEdgeRenderData.m_bounds.m_user,
interactiveEdgeHandles);
DrawEdges(
debugDisplay, cl_whiteBoxEdgeMeshColor, renderData.m_whiteBoxEdgeRenderData.m_bounds.m_mesh,
interactiveEdgeHandles);
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
// special handling for edges in the process of being restored - an edge may be clicked
// and remain 'orphaned' from a polygon until another connection (loop) can be made.
for (const Api::EdgeHandle edgeHandleRestore : m_edgeHandlesBeingRestored)
{
if (AZStd::any_of(
interactiveEdgeHandles.begin(), interactiveEdgeHandles.end(),
[edgeHandleRestore](const Api::EdgeHandle edgeHandle)
{
return edgeHandle == edgeHandleRestore;
}))
{
continue;
}
debugDisplay.SetColor(cl_whiteBoxEdgeHoveredColor);
const auto edgeGeom = Api::EdgeVertexPositions(*whiteBox, edgeHandleRestore);
debugDisplay.DrawLine(edgeGeom[0], edgeGeom[1]);
}
// draw the hovered highlighted edge
if (!interactiveEdgeHandles.empty() && m_edgeIntersection.has_value())
{
debugDisplay.SetColor(cl_whiteBoxSelectedModifierColor);
debugDisplay.DrawLine(
m_edgeIntersection->m_closestEdgeWithHandle.m_bound.m_start,
m_edgeIntersection->m_closestEdgeWithHandle.m_bound.m_end);
}
debugDisplay.PopMatrix();
const AzFramework::CameraState cameraState = AzToolsFramework::GetCameraState(viewportInfo.m_viewportId);
const Api::VertexHandle hoveredVertexHandle =
m_vertexIntersection.value_or(VertexIntersection{}).m_closestVertexWithHandle.m_handle;
for (const auto& vertex : renderData.m_whiteBoxIntersectionData.m_vertexBounds)
{
if (hoveredVertexHandle == vertex.m_handle)
{
debugDisplay.SetColor(cl_whiteBoxVertexSelectedModifierColor);
}
else if (Api::VertexIsHidden(*whiteBox, vertex.m_handle))
{
debugDisplay.SetColor(cl_whiteBoxVertexHiddenRestoreColor);
}
else
{
debugDisplay.SetColor(cl_whiteBoxVertexRestoreColor);
}
// calculate the radius of the manipulator based
// on the distance from the camera
const float radius = cl_whiteBoxVertexManipulatorSize *
AzToolsFramework::CalculateScreenToWorldMultiplier(
worldFromLocal.TransformPoint(vertex.m_bound.m_center), cameraState);
// note: we must manually transform position to world space to avoid the size
// of the sphere being drawn incorrectly when scale is applied
debugDisplay.DrawBall(worldFromLocal.TransformPoint(vertex.m_bound.m_center), radius);
}
}
} // namespace WhiteBox