Files
o3de/Gems/WhiteBox/Code/Source/Viewport/WhiteBoxEdgeTranslationModifier.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

376 lines
16 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 "EditorWhiteBoxComponentModeBus.h"
#include "EditorWhiteBoxEdgeModifierBus.h"
#include "SubComponentModes/EditorWhiteBoxDefaultModeBus.h"
#include "Util/WhiteBoxMathUtil.h"
#include "Viewport/WhiteBoxModifierUtil.h"
#include "Viewport/WhiteBoxViewportConstants.h"
#include "WhiteBoxEdgeTranslationModifier.h"
#include "WhiteBoxManipulatorViews.h"
#include <AzToolsFramework/Manipulators/ManipulatorManager.h>
#include <AzToolsFramework/Manipulators/ManipulatorView.h>
#include <AzToolsFramework/Manipulators/PlanarManipulator.h>
#include <WhiteBox/EditorWhiteBoxComponentBus.h>
namespace WhiteBox
{
AZ_CLASS_ALLOCATOR_IMPL(EdgeTranslationModifier, AZ::SystemAllocator, 0)
static bool BeginningExtrude(
const AzToolsFramework::PlanarManipulator::Action& action, const AppendStage appendStage)
{
return action.m_modifiers.Ctrl() && appendStage == AppendStage::None;
}
static bool EndingExtrude(const AzToolsFramework::PlanarManipulator::Action& action, const AppendStage appendStage)
{
return !action.m_modifiers.Ctrl() && appendStage != AppendStage::None;
}
static bool AppendInactive(const AppendStage appendStage)
{
return appendStage == AppendStage::None || appendStage == AppendStage::Complete;
}
AZStd::array<AZ::Vector3, 2> GetEdgeNormalAxes(const AZ::Vector3& start, const AZ::Vector3& end)
{
AZ::Vector3 axis1, axis2;
CalculateOrthonormalBasis((start - end).GetNormalized(), axis1, axis2);
return {axis1, axis2};
}
EdgeTranslationModifier::EdgeTranslationModifier(
const AZ::EntityComponentIdPair& entityComponentIdPair, const Api::EdgeHandle edgeHandle,
[[maybe_unused]] const AZ::Vector3& intersectionPoint)
: m_hoveredEdgeHandle{edgeHandle}
, m_entityComponentIdPair(entityComponentIdPair)
{
CreateManipulator();
}
EdgeTranslationModifier::~EdgeTranslationModifier()
{
DestroyManipulator();
}
// return all vertex handles from a collection of edge handles
// (ensure to remove duplicates as vertices will be shared across edges)
static Api::VertexHandles VertexHandlesForEdges(const WhiteBoxMesh& whiteBox, const Api::EdgeHandles& edgeHandles)
{
auto vertexHandles = std::reduce(
edgeHandles.cbegin(), edgeHandles.cend(), Api::VertexHandles{},
[&whiteBox](Api::VertexHandles vertexHandles, const Api::EdgeHandle edgeHandle)
{
const auto edgeVertexHandles = Api::EdgeVertexHandles(whiteBox, edgeHandle);
vertexHandles.push_back(edgeVertexHandles[0]);
vertexHandles.push_back(edgeVertexHandles[1]);
return vertexHandles;
});
std::sort(vertexHandles.begin(), vertexHandles.end());
vertexHandles.erase(std::unique(vertexHandles.begin(), vertexHandles.end()), vertexHandles.end());
return vertexHandles;
}
// note: edgeHandles is an inout param and will be modified if its size is 1
static Api::EdgeHandle AttemptEdgeAppend(
WhiteBoxMesh& whiteBox, Api::EdgeHandle hoveredEdgeHandle, Api::EdgeHandles& edgeHandles,
const AZ::Vector3& extrudeVector)
{
// only allow edge extrusion with a single edge
if (edgeHandles.size() == 1)
{
edgeHandles = {Api::TranslateEdgeAppend(whiteBox, hoveredEdgeHandle, extrudeVector)};
return edgeHandles.front();
}
// no append occurred, return original edge handle
return hoveredEdgeHandle;
}
void EdgeTranslationModifier::CreateManipulator()
{
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
// calculate edge handle group (will be > 1 if connecting vertices have been hidden)
m_edgeHandles = Api::EdgeGrouping(*whiteBox, m_hoveredEdgeHandle);
const auto vertexPositions = Api::EdgeVertexPositions(*whiteBox, m_hoveredEdgeHandle);
const auto axes = GetEdgeNormalAxes(vertexPositions[0], vertexPositions[1]);
m_translationManipulator = AzToolsFramework::PlanarManipulator::MakeShared(
AzToolsFramework::WorldFromLocalWithUniformScale(m_entityComponentIdPair.GetEntityId()));
m_translationManipulator->AddEntityComponentIdPair(m_entityComponentIdPair);
m_translationManipulator->SetLocalPosition(Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle));
m_translationManipulator->SetAxes(axes[0], axes[1]);
CreateView();
m_translationManipulator->Register(AzToolsFramework::g_mainManipulatorManagerId);
struct SharedState
{
// the previous position when moving the manipulator, used to calculate manipulator delta position
AZ::Vector3 m_prevPosition;
// the midpoint of the edge manipulator
AZ::Vector3 m_edgeMidpoint = AZ::Vector3::CreateZero();
// the position of the manipulator the moment an append is initiated
AZ::Vector3 m_initiateAppendPosition = AZ::Vector3::CreateZero();
// the distance the manipulator has moved from where it started when an append begins
AZ::Vector3 m_activeAppendOffset = AZ::Vector3::CreateZero();
// what state of appending are we currently in
AppendStage m_appendStage = AppendStage::None;
// has the modifier moved during the action
bool m_moved = false;
};
auto sharedState = AZStd::make_shared<SharedState>();
m_translationManipulator->InstallLeftMouseDownCallback(
[this, sharedState](const AzToolsFramework::PlanarManipulator::Action& action)
{
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
// record initial state at mouse down
sharedState->m_prevPosition = action.LocalPosition();
sharedState->m_edgeMidpoint = Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle);
sharedState->m_appendStage = AppendStage::None;
sharedState->m_moved = false;
});
m_translationManipulator->InstallMouseMoveCallback(
[this, sharedState](const AzToolsFramework::PlanarManipulator::Action& action)
{
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
// has the modifier moved during this interaction
sharedState->m_moved = sharedState->m_moved ||
action.LocalPositionOffset().GetLength() >= cl_whiteBoxMouseClickDeltaThreshold;
// reset append
if (EndingExtrude(action, sharedState->m_appendStage))
{
sharedState->m_appendStage = AppendStage::None;
}
// start trying to extrude
if (BeginningExtrude(action, sharedState->m_appendStage))
{
sharedState->m_appendStage = AppendStage::Initiated;
sharedState->m_initiateAppendPosition = action.LocalPosition();
}
const AZ::Vector3 position = action.LocalPosition();
if (sharedState->m_appendStage == AppendStage::Initiated)
{
const AZ::Vector3 extrudeVector = action.LocalPosition() - sharedState->m_initiateAppendPosition;
float extrudeMagnitude = AZ::GetAbs(extrudeVector.Dot(action.m_fixed.m_axis1)) +
AZ::GetAbs(extrudeVector.Dot(action.m_fixed.m_axis2));
// only extrude after having moved a small amount (to prevent overlapping verts
// and normals being calculated incorrectly)
if (extrudeMagnitude > 0)
{
sharedState->m_activeAppendOffset = action.LocalPositionOffset();
const Api::EdgeHandle nextEdgeHandle =
AttemptEdgeAppend(*whiteBox, m_hoveredEdgeHandle, m_edgeHandles, extrudeVector);
sharedState->m_edgeMidpoint = Api::EdgeMidpoint(*whiteBox, nextEdgeHandle);
sharedState->m_appendStage = AppendStage::Complete;
EditorWhiteBoxEdgeModifierNotificationBus::Broadcast(
&EditorWhiteBoxEdgeModifierNotificationBus::Events::OnEdgeModifierUpdatedEdgeHandle,
m_hoveredEdgeHandle, nextEdgeHandle);
m_hoveredEdgeHandle = nextEdgeHandle;
}
}
else if (AppendInactive(sharedState->m_appendStage))
{
// get the distance the manipulator has moved since the last mouse move
const AZ::Vector3 displacement = position - sharedState->m_prevPosition;
// have to make sure we don't move verts more than once
for (const auto vertexHandle : VertexHandlesForEdges(*whiteBox, m_edgeHandles))
{
SetVertexPosition(
*whiteBox, vertexHandle, VertexPosition(*whiteBox, vertexHandle) + displacement);
}
}
sharedState->m_prevPosition = position;
// regular movement/translation of vertices
if (AppendInactive(sharedState->m_appendStage))
{
m_translationManipulator->SetLocalPosition(
sharedState->m_edgeMidpoint + action.LocalPositionOffset() - sharedState->m_activeAppendOffset);
EditorWhiteBoxComponentModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxComponentModeRequestBus::Events::MarkWhiteBoxIntersectionDataDirty);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::RefreshPolygonScaleModifier);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::RefreshEdgeScaleModifier);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::RefreshPolygonTranslationModifier);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::RefreshEdgeTranslationModifier);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::RefreshVertexSelectionModifier);
}
Api::CalculateNormals(*whiteBox);
Api::CalculatePlanarUVs(*whiteBox);
EditorWhiteBoxComponentNotificationBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentNotificationBus::Events::OnWhiteBoxMeshModified);
});
m_translationManipulator->InstallLeftMouseUpCallback(
[this, sharedState]([[maybe_unused]] const AzToolsFramework::PlanarManipulator::Action& action)
{
// we haven't moved, count as a click
if (!sharedState->m_moved)
{
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxDefaultModeRequestBus::Events::CreateEdgeScaleModifier,
m_hoveredEdgeHandle);
EditorWhiteBoxDefaultModeRequestBus::Event(
m_entityComponentIdPair,
&EditorWhiteBoxDefaultModeRequestBus::Events::AssignSelectedEdgeTranslationModifier);
}
else
{
EditorWhiteBoxComponentRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::SerializeWhiteBox);
}
});
}
void EdgeTranslationModifier::DestroyManipulator()
{
m_translationManipulator->Unregister();
m_translationManipulator.reset();
}
bool EdgeTranslationModifier::MouseOver() const
{
return m_translationManipulator->MouseOver();
}
void EdgeTranslationModifier::ForwardMouseOverEvent(
const AzToolsFramework::ViewportInteraction::MouseInteraction& interaction)
{
m_translationManipulator->ForwardMouseOverEvent(interaction);
}
void EdgeTranslationModifier::Refresh()
{
DestroyManipulator();
CreateManipulator();
}
void EdgeTranslationModifier::CreateView()
{
WhiteBoxMesh* whiteBox = nullptr;
EditorWhiteBoxComponentRequestBus::EventResult(
whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
const AZ::Vector3 edgeMidpoint = Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle);
// if the size of the edge handles and views has changed
// we know we need to either add or remove views
if (m_edgeViews.size() != m_edgeHandles.size())
{
m_edgeViews.resize(m_edgeHandles.size());
AZStd::generate(
AZStd::begin(m_edgeViews), AZStd::end(m_edgeViews),
[]
{
return AZStd::make_shared<ManipulatorViewEdge>();
});
}
for (size_t edgeIndex = 0; edgeIndex < m_edgeHandles.size(); ++edgeIndex)
{
auto view = m_edgeViews[edgeIndex];
const auto edgeHandle = m_edgeHandles[edgeIndex];
const auto vertexHandles = Api::EdgeVertexHandles(*whiteBox, edgeHandle);
// vertex positions in the local space of the entity
const auto vertexPositions = Api::EdgeVertexPositions(*whiteBox, edgeHandle);
// transform edge start/end positions to be in manipulator space (see UpdateIntersectionPoint)
// (relative to m_translationManipulator local position)
view->m_start = vertexPositions[0] - edgeMidpoint;
view->m_end = vertexPositions[1] - edgeMidpoint;
// record if start/end handles are hidden to adjust dimensions of manipulator view
view->m_vertexStartEndHidden[0] = Api::VertexIsHidden(*whiteBox, vertexHandles[0]);
view->m_vertexStartEndHidden[1] = Api::VertexIsHidden(*whiteBox, vertexHandles[1]);
// only do selection colors for 'selected/hovered' edge handle
if (edgeHandle == m_hoveredEdgeHandle)
{
view->SetColor(m_color, m_hoverColor);
}
else
{
view->SetColor(cl_whiteBoxEdgeHoveredColor, cl_whiteBoxEdgeHoveredColor);
}
view->SetWidth(m_width, m_hoverWidth);
}
m_translationManipulator->SetViews(
AzToolsFramework::ManipulatorViews{m_edgeViews.cbegin(), m_edgeViews.cend()});
}
void EdgeTranslationModifier::SetColors(const AZ::Color& color, const AZ::Color& hoverColor)
{
m_color = color;
m_hoverColor = hoverColor;
}
void EdgeTranslationModifier::SetWidths(const float width, const float hoverWidth)
{
m_width = width;
m_hoverWidth = hoverWidth;
}
bool EdgeTranslationModifier::PerformingAction() const
{
return m_translationManipulator->PerformingAction();
}
} // namespace WhiteBox