Files
o3de/Gems/LmbrCentral/Code/Source/Shape/EditorTubeShapeComponentMode.cpp
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

333 lines
14 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 "EditorTubeShapeComponentMode.h"
#include <AzToolsFramework/API/ToolsApplicationAPI.h>
#include <AzToolsFramework/Manipulators/LinearManipulator.h>
#include <AzToolsFramework/Manipulators/ManipulatorManager.h>
#include <AzToolsFramework/Viewport/ActionBus.h>
#include <LmbrCentral/Shape/SplineComponentBus.h>
#include <LmbrCentral/Shape/EditorTubeShapeComponentBus.h>
#include <LmbrCentral/Shape/TubeShapeComponentBus.h>
#include <AzFramework/Viewport/ViewportColors.h>
#include <AzFramework/Viewport/ViewportConstants.h>
namespace LmbrCentral
{
AZ_CLASS_ALLOCATOR_IMPL(EditorTubeShapeComponentMode, AZ::SystemAllocator, 0)
static const AZ::Crc32 s_resetVariableRadii = AZ_CRC("com.o3de.action.tubeshape.reset_radii", 0x0f2ef8e2);
static const char* const s_resetRadiiTitle = "Reset Radii";
static const char* const s_resetRadiiDesc = "Reset all variable radius values to the default";
EditorTubeShapeComponentMode::EditorTubeShapeComponentMode(
const AZ::EntityComponentIdPair& entityComponentIdPair, const AZ::Uuid componentType)
: EditorBaseComponentMode(entityComponentIdPair, componentType)
{
m_currentTransform = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(
m_currentTransform, entityComponentIdPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
AZ::TransformNotificationBus::Handler::BusConnect(entityComponentIdPair.GetEntityId());
ShapeComponentNotificationsBus::Handler::BusConnect(entityComponentIdPair.GetEntityId());
SplineComponentNotificationBus::Handler::BusConnect(entityComponentIdPair.GetEntityId());
EditorSplineComponentNotificationBus::Handler::BusConnect(entityComponentIdPair.GetEntityId());
CreateManipulators();
}
EditorTubeShapeComponentMode::~EditorTubeShapeComponentMode()
{
DestroyManipulators();
EditorSplineComponentNotificationBus::Handler::BusDisconnect();
SplineComponentNotificationBus::Handler::BusDisconnect();
ShapeComponentNotificationsBus::Handler::BusDisconnect();
AZ::TransformNotificationBus::Handler::BusDisconnect();
}
void EditorTubeShapeComponentMode::Refresh()
{
ContainerChanged();
}
AZStd::vector<AzToolsFramework::ActionOverride> EditorTubeShapeComponentMode::PopulateActionsImpl()
{
return AZStd::vector<AzToolsFramework::ActionOverride>
{
AzToolsFramework::ActionOverride()
.SetUri(s_resetVariableRadii)
.SetKeySequence(QKeySequence(Qt::Key_R))
.SetTitle(s_resetRadiiTitle)
.SetTip(s_resetRadiiDesc)
.SetEntityComponentIdPair(GetEntityComponentIdPair())
.SetCallback([this]()
{
const AZ::EntityId entityId = GetEntityId();
// ensure we record undo command for reset
AzToolsFramework::ScopedUndoBatch undoBatch("Reset variable radii");
AzToolsFramework::ScopedUndoBatch::MarkEntityDirty(entityId);
TubeShapeComponentRequestsBus::Event(
entityId, &TubeShapeComponentRequests::SetAllVariableRadii, 0.0f);
RefreshManipulatorsLocal(entityId);
EditorTubeShapeComponentRequestBus::Event(
entityId, &EditorTubeShapeComponentRequests::GenerateVertices);
AzToolsFramework::OnEntityComponentPropertyChanged(GetEntityComponentIdPair());
// ensure property grid values are refreshed
AzToolsFramework::ToolsApplicationNotificationBus::Broadcast(
&AzToolsFramework::ToolsApplicationNotificationBus::Events::InvalidatePropertyDisplay,
AzToolsFramework::Refresh_Values);
})
};
}
void EditorTubeShapeComponentMode::CreateManipulators()
{
const AZ::EntityId entityId = GetEntityId();
bool empty = false;
SplineComponentRequestBus::EventResult(
empty, entityId, &SplineComponentRequests::Empty);
// if we have no vertices, do not attempt to create any manipulators
if (empty)
{
return;
}
AZ::SplinePtr spline;
SplineComponentRequestBus::EventResult(
spline, entityId, &SplineComponentRequests::GetSpline);
const auto tubeManipulatorStates = GenerateTubeManipulatorStates(*spline);
for (size_t manipulatorIndex = 0; manipulatorIndex < tubeManipulatorStates.size(); ++manipulatorIndex)
{
const TubeManipulatorState tubeManipulatorState = tubeManipulatorStates[manipulatorIndex];
const AZ::Vector3 normal = spline->GetNormal(tubeManipulatorState.m_splineAddress);
const AZ::Vector3 position = spline->GetPosition(tubeManipulatorState.m_splineAddress);
float radius = 0.0f;
TubeShapeComponentRequestsBus::EventResult(
radius, entityId, &TubeShapeComponentRequests::GetTotalRadius, tubeManipulatorState.m_splineAddress);
auto linearManipulator = AzToolsFramework::LinearManipulator::MakeShared(m_currentTransform);
linearManipulator->AddEntityComponentIdPair(GetEntityComponentIdPair());
linearManipulator->SetLocalTransform(AZ::Transform::CreateTranslation(position + normal * radius));
linearManipulator->SetAxis(normal);
AzToolsFramework::ManipulatorViews views;
views.emplace_back(AzToolsFramework::CreateManipulatorViewQuadBillboard(
AzFramework::ViewportColors::DefaultManipulatorHandleColor, AzFramework::ViewportConstants::DefaultManipulatorHandleSize));
linearManipulator->SetViews(AZStd::move(views));
linearManipulator->Register(AzToolsFramework::g_mainManipulatorManagerId);
struct SharedState
{
float m_startingVariableRadius = 0.0f;
float m_startingFixedRadius = 0.0f;
};
auto sharedState = AZStd::make_shared<SharedState>();
linearManipulator->InstallLeftMouseDownCallback(
[this, tubeManipulatorState, sharedState]
(const AzToolsFramework::LinearManipulator::Action& /*action*/) mutable
{
const AZ::EntityId entityId = GetEntityId();
float variableRadius = 0.0f;
TubeShapeComponentRequestsBus::EventResult(
variableRadius, entityId,
&TubeShapeComponentRequests::GetVariableRadius, static_cast<int>(tubeManipulatorState.m_vertIndex));
// the base radius of the tube (when no variable radii are applied)
float fixedRadius = 0.0f;
TubeShapeComponentRequestsBus::EventResult(
fixedRadius, entityId, &TubeShapeComponentRequests::GetRadius);
sharedState->m_startingVariableRadius = variableRadius;
sharedState->m_startingFixedRadius = fixedRadius;
});
linearManipulator->InstallMouseMoveCallback(
[this, tubeManipulatorState, manipulatorIndex, spline, sharedState]
(const AzToolsFramework::LinearManipulator::Action& action)
{
const AZ::EntityId entityId = GetEntityId();
const float axisDisplacement = action.LocalPositionOffset().Dot(action.m_fixed.m_axis);
// set clamped variable radius, it can be no more than the inverse
// of the base/fixed radius of the tube
TubeShapeComponentRequestsBus::Event(
entityId, &TubeShapeComponentRequests::SetVariableRadius, static_cast<int>(tubeManipulatorState.m_vertIndex),
AZ::GetMax(sharedState->m_startingVariableRadius + axisDisplacement, -sharedState->m_startingFixedRadius));
bool found = false;
AZ::Vector3 localVertexPosition = AZ::Vector3::CreateZero();
SplineComponentRequestBus::EventResult(
found, entityId, &SplineComponentRequests::GetVertex, tubeManipulatorState.m_vertIndex, localVertexPosition);
const AZ::Vector3 localNormal = spline->GetNormal(tubeManipulatorState.m_splineAddress);
const AZ::Vector3 manipulatorVector = action.LocalPosition() - localVertexPosition;
const float manipulatorDot = manipulatorVector.Dot(localNormal);
// ensure the manipulator position cannot move passed
// the center point of a tube vertex
const AZ::Vector3 localPosition = manipulatorDot >= 0.0f
? action.LocalPosition()
: localVertexPosition;
m_radiusManipulators[manipulatorIndex]->SetLocalTransform(
AZ::Transform::CreateTranslation(localPosition));
m_radiusManipulators[manipulatorIndex]->SetBoundsDirty();
EditorTubeShapeComponentRequestBus::Event(
entityId, &EditorTubeShapeComponentRequests::GenerateVertices);
});
m_radiusManipulators.emplace_back(std::move(linearManipulator));
}
}
void EditorTubeShapeComponentMode::DestroyManipulators()
{
for (auto& linearManipulator : m_radiusManipulators)
{
if (linearManipulator)
{
linearManipulator->Unregister();
linearManipulator.reset();
}
}
m_radiusManipulators.clear();
}
void EditorTubeShapeComponentMode::ContainerChanged()
{
DestroyManipulators();
CreateManipulators();
}
void EditorTubeShapeComponentMode::OnVertexAdded(size_t /*index*/)
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::OnVertexRemoved(size_t /*index*/)
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::OnVerticesSet(const AZStd::vector<AZ::Vector3>& /*vertices*/)
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::OnVerticesCleared()
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::OnShapeChanged(const ShapeChangeReasons /*changeReason*/)
{
RefreshManipulatorsLocal(GetEntityId());
}
void EditorTubeShapeComponentMode::OnSplineChanged()
{
RefreshManipulatorsLocal(GetEntityId());
}
void EditorTubeShapeComponentMode::OnTransformChanged(
const AZ::Transform& /*local*/, const AZ::Transform& world)
{
m_currentTransform = world;
}
void EditorTubeShapeComponentMode::OnSplineTypeChanged()
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::OnOpenCloseChanged(const bool /*closed*/)
{
ContainerChanged();
}
void EditorTubeShapeComponentMode::RefreshManipulatorsLocal(const AZ::EntityId entityId)
{
AZ::SplinePtr spline;
SplineComponentRequestBus::EventResult(
spline, entityId, &SplineComponentRequests::GetSpline);
const auto tubeManipulatorStates = GenerateTubeManipulatorStates(*spline);
AZ_Assert(tubeManipulatorStates.size() == m_radiusManipulators.size(),
"tubeManipulatorStates.size() (%zu) does not equal m_radiusManipulators.size() (%zu), it's likely CreateManipulators has not been called",
tubeManipulatorStates.size(), m_radiusManipulators.size());
for (size_t manipulatorIndex = 0; manipulatorIndex < tubeManipulatorStates.size(); ++manipulatorIndex)
{
const TubeManipulatorState& tubeManipulatorState = tubeManipulatorStates[manipulatorIndex];
const AZ::Vector3 normal = spline->GetNormal(tubeManipulatorState.m_splineAddress);
const AZ::Vector3 position = spline->GetPosition(tubeManipulatorState.m_splineAddress);
float radius = 0.0f;
TubeShapeComponentRequestsBus::EventResult(
radius, entityId, &TubeShapeComponentRequests::GetTotalRadius, tubeManipulatorState.m_splineAddress);
m_radiusManipulators[manipulatorIndex]->SetLocalTransform(
AZ::Transform::CreateTranslation(position + normal * radius));
m_radiusManipulators[manipulatorIndex]->SetAxis(normal);
m_radiusManipulators[manipulatorIndex]->SetBoundsDirty();
}
}
AZStd::vector<EditorTubeShapeComponentMode::TubeManipulatorState> GenerateTubeManipulatorStates(const AZ::Spline& spline)
{
if (spline.GetVertexCount() == 0)
{
return {};
}
const auto segmentCount = spline.GetSegmentCount();
if (segmentCount == 0)
{
return { { AZ::SplineAddress(0), 0 } };
}
const AZ::u64 startVertex = spline.GetAddressByFraction(0.0f).m_segmentIndex;
const AZ::u64 endVertex = startVertex + segmentCount + (spline.IsClosed() ? 0 : 1);
AZStd::vector<EditorTubeShapeComponentMode::TubeManipulatorState> splineAddresses;
for (AZ::u64 vertIndex = startVertex; vertIndex < endVertex; ++vertIndex)
{
if (vertIndex + 1 == endVertex)
{
AZ_Assert(vertIndex > 0, "vertexIndex is 0 and not safe to subtract from")
splineAddresses.push_back({ AZ::SplineAddress(vertIndex - 1, 1.0f), vertIndex });
}
else
{
splineAddresses.push_back({ AZ::SplineAddress(vertIndex), vertIndex });
}
}
return splineAddresses;
}
} // namespace LmbrCentral