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

336 lines
11 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 "Asset/WhiteBoxMeshAssetHandler.h"
#include "Asset/WhiteBoxMeshAssetUndoCommand.h"
#include "EditorWhiteBoxMeshAsset.h"
#include "Util/WhiteBoxEditorUtil.h"
#include <AzCore/IO/FileIO.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzToolsFramework/API/EditorAssetSystemAPI.h>
#include <AzToolsFramework/API/ToolsApplicationAPI.h>
#include <WhiteBox/EditorWhiteBoxComponentBus.h>
namespace WhiteBox
{
AZ_CLASS_ALLOCATOR_IMPL(EditorWhiteBoxMeshAsset, AZ::SystemAllocator, 0)
static constexpr const char* const AssetModifiedUndoRedoDesc = "White Box Mesh asset was updated";
static bool MeshAssetValid(AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset)
{
return meshAsset.GetId().IsValid();
}
static bool MeshAssetLoaded(AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset)
{
return MeshAssetValid(meshAsset) && meshAsset.IsReady();
}
static AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> CreateOrFindMeshAsset(
const AZStd::string& assetPath, AZ::Data::AssetLoadBehavior loadBehavior)
{
AZ::Data::AssetId generatedAssetId;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
generatedAssetId, &AZ::Data::AssetCatalogRequests::GenerateAssetIdTEMP, assetPath.c_str());
AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset(
generatedAssetId, azrtti_typeid<Pipeline::WhiteBoxMeshAsset>(), loadBehavior);
return meshAsset;
}
static AZStd::optional<AZStd::string> AbsolutePathForSourceAsset(
AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>& asset)
{
AZStd::string relativeAssetPath;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
relativeAssetPath, &AZ::Data::AssetCatalogRequests::GetAssetPathById, asset.GetId());
AZStd::string absoluteAssetPath;
bool foundAbsolutePath = false;
AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
foundAbsolutePath,
&AzToolsFramework::AssetSystem::AssetSystemRequest::GetFullSourcePathFromRelativeProductPath,
relativeAssetPath, absoluteAssetPath);
if (foundAbsolutePath)
{
return absoluteAssetPath;
}
return AZStd::nullopt;
}
static bool SaveAsset(
const AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>& meshAsset, const AZStd::string& absoluteFilePath)
{
bool success = false;
const auto assetType = AZ::AzTypeInfo<Pipeline::WhiteBoxMeshAsset>::Uuid();
if (auto assetHandler = AZ::Data::AssetManager::Instance().GetHandler(assetType))
{
if (AZ::IO::FileIOStream fileStream(absoluteFilePath.c_str(), AZ::IO::OpenMode::ModeWrite);
fileStream.IsOpen())
{
success = assetHandler->SaveAssetData(meshAsset, &fileStream);
AZ_Printf(
"EditorWhiteBoxMeshAsset", "Save %s. Location: %s", success ? "succeeded" : "failed",
absoluteFilePath.c_str());
}
}
return success;
}
void EditorWhiteBoxMeshAsset::Reflect(AZ::ReflectContext* context)
{
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<EditorWhiteBoxMeshAsset>()->Version(1)->Field(
"MeshAsset", &EditorWhiteBoxMeshAsset::m_meshAsset);
if (AZ::EditContext* editContext = serializeContext->GetEditContext())
{
editContext->Class<EditorWhiteBoxMeshAsset>("Editor White Box Mesh Asset", "White Box Mesh Asset")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->DataElement(
AZ::Edit::UIHandlers::Default, &EditorWhiteBoxMeshAsset::m_meshAsset, "Mesh Asset",
"Mesh Asset")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorWhiteBoxMeshAsset::AssetChanged)
->Attribute(AZ::Edit::Attributes::ClearNotify, &EditorWhiteBoxMeshAsset::AssetCleared);
}
}
}
EditorWhiteBoxMeshAsset::~EditorWhiteBoxMeshAsset()
{
if (InUse())
{
Reset();
}
}
bool EditorWhiteBoxMeshAsset::InUse() const
{
return MeshAssetValid(m_meshAsset);
}
bool EditorWhiteBoxMeshAsset::Loaded() const
{
// it is possible that we've switched to use an asset but it isn't
// ready yet, in this case return that the asset isn't yet loaded
return MeshAssetLoaded(m_meshAsset);
}
void EditorWhiteBoxMeshAsset::Serialize()
{
AzToolsFramework::ScopedUndoBatch undoBatch(AssetModifiedUndoRedoDesc);
// create undo command to record changes to the asset
auto* command = aznew WhiteBoxMeshAssetUndoCommand();
command->SetAsset(m_meshAsset);
command->SetUndoState(m_meshAsset->GetWhiteBoxData());
m_meshAsset->Serialize();
command->SetRedoState(m_meshAsset->GetWhiteBoxData());
command->SetParent(undoBatch.GetUndoBatch());
}
void EditorWhiteBoxMeshAsset::Load()
{
if (!InUse())
{
return;
}
Disconnect();
if (m_meshAsset.GetStatus() == AZ::Data::AssetData::AssetStatus::Error ||
m_meshAsset.GetStatus() == AZ::Data::AssetData::AssetStatus::NotLoaded)
{
m_meshAsset.QueueLoad();
}
RegisterForEditorEvents();
AZ::Data::AssetBus::Handler::BusConnect(m_meshAsset.GetId());
WhiteBoxMeshAssetNotificationBus::Handler::BusConnect(m_meshAsset.GetId());
}
void EditorWhiteBoxMeshAsset::Associate(const AZ::EntityComponentIdPair& entityComponentIdPair)
{
m_entityComponentIdPair = entityComponentIdPair;
}
void EditorWhiteBoxMeshAsset::Disconnect()
{
UnregisterForEditorEvents();
// disconnect from any previously connected asset id
AZ::Data::AssetBus::Handler::BusDisconnect();
WhiteBoxMeshAssetNotificationBus::Handler::BusDisconnect();
// ensure we're disconnected from the tick bus
AZ::TickBus::Handler::BusDisconnect();
}
void EditorWhiteBoxMeshAsset::Release()
{
Disconnect();
m_meshAsset.Release();
}
void EditorWhiteBoxMeshAsset::Reset()
{
Disconnect();
m_meshAsset = AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>{};
}
WhiteBoxMesh* EditorWhiteBoxMeshAsset::GetWhiteBoxMesh()
{
return Loaded() ? m_meshAsset->GetMesh() : nullptr;
}
AZ::Data::AssetId EditorWhiteBoxMeshAsset::GetWhiteBoxMeshAssetId() const
{
return m_meshAsset.GetId();
}
AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> EditorWhiteBoxMeshAsset::GetWhiteBoxMeshAsset()
{
return m_meshAsset;
}
void EditorWhiteBoxMeshAsset::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
if (asset == m_meshAsset)
{
m_meshAsset = asset;
// defer rebuilding the mesh by a frame by connecting to the tick bus - this prevents issues
// with reentrancy when rebuilding the white box mesh
AZ::TickBus::Handler::BusConnect();
}
}
void EditorWhiteBoxMeshAsset::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
// after rebuilding the white box mesh, immediately disconnect from the tick bus (we only use it for deferred rebuilding)
EditorWhiteBoxComponentRequestBus::Event(m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
AZ::TickBus::Handler::BusDisconnect();
}
void EditorWhiteBoxMeshAsset::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
OnAssetReady(asset);
}
void EditorWhiteBoxMeshAsset::OnAssetError(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
if (asset == m_meshAsset)
{
AZ_Warning("EditorWhiteBoxMeshAsset", false, "OnAssetError: %s", asset.GetHint().c_str());
}
}
void EditorWhiteBoxMeshAsset::OnAssetReloadError(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
if (asset == m_meshAsset)
{
AZ_Warning("EditorWhiteBoxMeshAsset", false, "OnAssetReloadError: %s", asset.GetHint().c_str());
}
}
void EditorWhiteBoxMeshAsset::OnWhiteBoxMeshAssetModified(AZ::Data::Asset<AZ::Data::AssetData> asset)
{
if (asset == m_meshAsset)
{
EditorWhiteBoxComponentRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
}
}
void EditorWhiteBoxMeshAsset::RegisterForEditorEvents()
{
if (auto* editor = GetIEditor())
{
editor->RegisterNotifyListener(this);
}
}
void EditorWhiteBoxMeshAsset::UnregisterForEditorEvents()
{
if (auto* editor = GetIEditor())
{
editor->UnregisterNotifyListener(this);
}
}
void EditorWhiteBoxMeshAsset::OnEditorNotifyEvent(const EEditorNotifyEvent editorEvent)
{
switch (editorEvent)
{
case eNotify_OnEndSceneSave:
if (InUse())
{
Save();
}
break;
default:
break;
}
}
void EditorWhiteBoxMeshAsset::TakeOwnershipOfWhiteBoxMesh(
const AZStd::string& relativeAssetPath, Api::WhiteBoxMeshPtr whiteBoxMesh)
{
m_meshAsset = CreateOrFindMeshAsset(relativeAssetPath, m_meshAsset.GetAutoLoadBehavior());
m_meshAsset->SetMesh(AZStd::move(whiteBoxMesh));
// make sure the new asset has an up to date serialized state (for use in undo/redo)
m_meshAsset->Serialize();
AZ::Data::AssetBus::Handler::BusDisconnect();
WhiteBoxMeshAssetNotificationBus::Handler::BusDisconnect();
AZ::Data::AssetBus::Handler::BusConnect(m_meshAsset.GetId());
WhiteBoxMeshAssetNotificationBus::Handler::BusConnect(m_meshAsset.GetId());
}
void EditorWhiteBoxMeshAsset::Save()
{
if (const auto absolutePath = AbsolutePathForSourceAsset(m_meshAsset))
{
Save(absolutePath.value());
}
}
void EditorWhiteBoxMeshAsset::Save(const AZStd::string& absolutePath)
{
// save the asset to disk in the project folder
if (WhiteBox::SaveAsset(m_meshAsset, absolutePath))
{
RequestEditSourceControl(absolutePath.c_str());
}
}
void EditorWhiteBoxMeshAsset::AssetChanged()
{
EditorWhiteBoxComponentRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::DeserializeWhiteBox);
}
void EditorWhiteBoxMeshAsset::AssetCleared()
{
// when hitting 'clear' on the asset widget, the asset data is written locally to the component
EditorWhiteBoxComponentRequestBus::Event(
m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::WriteAssetToComponent);
}
} // namespace WhiteBox