EditorWhiteBoxMeshAsset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "Asset/WhiteBoxMeshAssetHandler.h"
  9. #include "Asset/WhiteBoxMeshAssetUndoCommand.h"
  10. #include "EditorWhiteBoxMeshAsset.h"
  11. #include "Util/WhiteBoxEditorUtil.h"
  12. #include <AzCore/Asset/AssetSerializer.h>
  13. #include <AzCore/IO/FileIO.h>
  14. #include <AzCore/Serialization/EditContext.h>
  15. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  16. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  17. #include <WhiteBox/EditorWhiteBoxComponentBus.h>
  18. namespace WhiteBox
  19. {
  20. AZ_CLASS_ALLOCATOR_IMPL(EditorWhiteBoxMeshAsset, AZ::SystemAllocator)
  21. static constexpr const char* const AssetModifiedUndoRedoDesc = "White Box Mesh asset was updated";
  22. static bool MeshAssetValid(AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset)
  23. {
  24. return meshAsset.GetId().IsValid();
  25. }
  26. static bool MeshAssetLoaded(AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset)
  27. {
  28. return MeshAssetValid(meshAsset) && meshAsset.IsReady();
  29. }
  30. static AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> CreateOrFindMeshAsset(
  31. const AZStd::string& assetPath, AZ::Data::AssetLoadBehavior loadBehavior)
  32. {
  33. AZ::Data::AssetId generatedAssetId;
  34. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  35. generatedAssetId, &AZ::Data::AssetCatalogRequests::GenerateAssetIdTEMP, assetPath.c_str());
  36. AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> meshAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset(
  37. generatedAssetId, azrtti_typeid<Pipeline::WhiteBoxMeshAsset>(), loadBehavior);
  38. return meshAsset;
  39. }
  40. static AZStd::optional<AZStd::string> AbsolutePathForSourceAsset(
  41. AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>& asset)
  42. {
  43. AZStd::string relativeAssetPath;
  44. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  45. relativeAssetPath, &AZ::Data::AssetCatalogRequests::GetAssetPathById, asset.GetId());
  46. AZStd::string absoluteAssetPath;
  47. bool foundAbsolutePath = false;
  48. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
  49. foundAbsolutePath,
  50. &AzToolsFramework::AssetSystem::AssetSystemRequest::GetFullSourcePathFromRelativeProductPath,
  51. relativeAssetPath, absoluteAssetPath);
  52. if (foundAbsolutePath)
  53. {
  54. return absoluteAssetPath;
  55. }
  56. return AZStd::nullopt;
  57. }
  58. static bool SaveAsset(
  59. const AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>& meshAsset, const AZStd::string& absoluteFilePath)
  60. {
  61. bool success = false;
  62. const auto assetType = AZ::AzTypeInfo<Pipeline::WhiteBoxMeshAsset>::Uuid();
  63. if (auto assetHandler = AZ::Data::AssetManager::Instance().GetHandler(assetType))
  64. {
  65. if (AZ::IO::FileIOStream fileStream(absoluteFilePath.c_str(), AZ::IO::OpenMode::ModeWrite);
  66. fileStream.IsOpen())
  67. {
  68. success = assetHandler->SaveAssetData(meshAsset, &fileStream);
  69. AZ_Printf(
  70. "EditorWhiteBoxMeshAsset", "Save %s. Location: %s", success ? "succeeded" : "failed",
  71. absoluteFilePath.c_str());
  72. }
  73. }
  74. return success;
  75. }
  76. void EditorWhiteBoxMeshAsset::Reflect(AZ::ReflectContext* context)
  77. {
  78. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  79. {
  80. serializeContext->Class<EditorWhiteBoxMeshAsset>()->Version(1)->Field(
  81. "MeshAsset", &EditorWhiteBoxMeshAsset::m_meshAsset);
  82. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  83. {
  84. editContext->Class<EditorWhiteBoxMeshAsset>("Editor White Box Mesh Asset", "White Box Mesh Asset")
  85. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  86. ->DataElement(
  87. AZ::Edit::UIHandlers::Default, &EditorWhiteBoxMeshAsset::m_meshAsset, "Mesh Asset",
  88. "Mesh Asset")
  89. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorWhiteBoxMeshAsset::AssetChanged)
  90. ->Attribute(AZ::Edit::Attributes::ClearNotify, &EditorWhiteBoxMeshAsset::AssetCleared);
  91. }
  92. }
  93. }
  94. EditorWhiteBoxMeshAsset::~EditorWhiteBoxMeshAsset()
  95. {
  96. if (InUse())
  97. {
  98. Reset();
  99. }
  100. }
  101. bool EditorWhiteBoxMeshAsset::InUse() const
  102. {
  103. return MeshAssetValid(m_meshAsset);
  104. }
  105. bool EditorWhiteBoxMeshAsset::Loaded() const
  106. {
  107. // it is possible that we've switched to use an asset but it isn't
  108. // ready yet, in this case return that the asset isn't yet loaded
  109. return MeshAssetLoaded(m_meshAsset);
  110. }
  111. void EditorWhiteBoxMeshAsset::Serialize()
  112. {
  113. AzToolsFramework::ScopedUndoBatch undoBatch(AssetModifiedUndoRedoDesc);
  114. // create undo command to record changes to the asset
  115. auto* command = aznew WhiteBoxMeshAssetUndoCommand();
  116. command->SetAsset(m_meshAsset);
  117. command->SetUndoState(m_meshAsset->GetWhiteBoxData());
  118. m_meshAsset->Serialize();
  119. command->SetRedoState(m_meshAsset->GetWhiteBoxData());
  120. command->SetParent(undoBatch.GetUndoBatch());
  121. }
  122. void EditorWhiteBoxMeshAsset::Load()
  123. {
  124. if (!InUse())
  125. {
  126. return;
  127. }
  128. Disconnect();
  129. if (m_meshAsset.GetStatus() == AZ::Data::AssetData::AssetStatus::Error ||
  130. m_meshAsset.GetStatus() == AZ::Data::AssetData::AssetStatus::NotLoaded)
  131. {
  132. m_meshAsset.QueueLoad();
  133. }
  134. RegisterForEditorEvents();
  135. AZ::Data::AssetBus::Handler::BusConnect(m_meshAsset.GetId());
  136. WhiteBoxMeshAssetNotificationBus::Handler::BusConnect(m_meshAsset.GetId());
  137. }
  138. void EditorWhiteBoxMeshAsset::Associate(const AZ::EntityComponentIdPair& entityComponentIdPair)
  139. {
  140. m_entityComponentIdPair = entityComponentIdPair;
  141. }
  142. void EditorWhiteBoxMeshAsset::Disconnect()
  143. {
  144. UnregisterForEditorEvents();
  145. // disconnect from any previously connected asset id
  146. AZ::Data::AssetBus::Handler::BusDisconnect();
  147. WhiteBoxMeshAssetNotificationBus::Handler::BusDisconnect();
  148. // ensure we're disconnected from the tick bus
  149. AZ::TickBus::Handler::BusDisconnect();
  150. }
  151. void EditorWhiteBoxMeshAsset::Release()
  152. {
  153. Disconnect();
  154. m_meshAsset.Release();
  155. }
  156. void EditorWhiteBoxMeshAsset::Reset()
  157. {
  158. Disconnect();
  159. m_meshAsset = AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset>{};
  160. }
  161. WhiteBoxMesh* EditorWhiteBoxMeshAsset::GetWhiteBoxMesh()
  162. {
  163. return Loaded() ? m_meshAsset->GetMesh() : nullptr;
  164. }
  165. AZ::Data::AssetId EditorWhiteBoxMeshAsset::GetWhiteBoxMeshAssetId() const
  166. {
  167. return m_meshAsset.GetId();
  168. }
  169. AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> EditorWhiteBoxMeshAsset::GetWhiteBoxMeshAsset()
  170. {
  171. return m_meshAsset;
  172. }
  173. void EditorWhiteBoxMeshAsset::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  174. {
  175. if (asset == m_meshAsset)
  176. {
  177. m_meshAsset = asset;
  178. // defer rebuilding the mesh by a frame by connecting to the tick bus - this prevents issues
  179. // with reentrancy when rebuilding the white box mesh
  180. AZ::TickBus::Handler::BusConnect();
  181. }
  182. }
  183. void EditorWhiteBoxMeshAsset::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  184. {
  185. // after rebuilding the white box mesh, immediately disconnect from the tick bus (we only use it for deferred rebuilding)
  186. EditorWhiteBoxComponentRequestBus::Event(m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
  187. AZ::TickBus::Handler::BusDisconnect();
  188. }
  189. void EditorWhiteBoxMeshAsset::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  190. {
  191. OnAssetReady(asset);
  192. }
  193. void EditorWhiteBoxMeshAsset::OnAssetError(AZ::Data::Asset<AZ::Data::AssetData> asset)
  194. {
  195. if (asset == m_meshAsset)
  196. {
  197. AZ_Warning("EditorWhiteBoxMeshAsset", false, "OnAssetError: %s", asset.GetHint().c_str());
  198. }
  199. }
  200. void EditorWhiteBoxMeshAsset::OnAssetReloadError(AZ::Data::Asset<AZ::Data::AssetData> asset)
  201. {
  202. if (asset == m_meshAsset)
  203. {
  204. AZ_Warning("EditorWhiteBoxMeshAsset", false, "OnAssetReloadError: %s", asset.GetHint().c_str());
  205. }
  206. }
  207. void EditorWhiteBoxMeshAsset::OnWhiteBoxMeshAssetModified(AZ::Data::Asset<AZ::Data::AssetData> asset)
  208. {
  209. if (asset == m_meshAsset)
  210. {
  211. EditorWhiteBoxComponentRequestBus::Event(
  212. m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::RebuildWhiteBox);
  213. }
  214. }
  215. void EditorWhiteBoxMeshAsset::RegisterForEditorEvents()
  216. {
  217. if (auto* editor = GetIEditor())
  218. {
  219. editor->RegisterNotifyListener(this);
  220. }
  221. }
  222. void EditorWhiteBoxMeshAsset::UnregisterForEditorEvents()
  223. {
  224. if (auto* editor = GetIEditor())
  225. {
  226. editor->UnregisterNotifyListener(this);
  227. }
  228. }
  229. void EditorWhiteBoxMeshAsset::OnEditorNotifyEvent(const EEditorNotifyEvent editorEvent)
  230. {
  231. switch (editorEvent)
  232. {
  233. case eNotify_OnEndSceneSave:
  234. if (InUse())
  235. {
  236. Save();
  237. }
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. void EditorWhiteBoxMeshAsset::TakeOwnershipOfWhiteBoxMesh(
  244. const AZStd::string& relativeAssetPath, Api::WhiteBoxMeshPtr whiteBoxMesh)
  245. {
  246. m_meshAsset = CreateOrFindMeshAsset(relativeAssetPath, m_meshAsset.GetAutoLoadBehavior());
  247. m_meshAsset->SetMesh(AZStd::move(whiteBoxMesh));
  248. // make sure the new asset has an up to date serialized state (for use in undo/redo)
  249. m_meshAsset->Serialize();
  250. AZ::Data::AssetBus::Handler::BusDisconnect();
  251. WhiteBoxMeshAssetNotificationBus::Handler::BusDisconnect();
  252. AZ::Data::AssetBus::Handler::BusConnect(m_meshAsset.GetId());
  253. WhiteBoxMeshAssetNotificationBus::Handler::BusConnect(m_meshAsset.GetId());
  254. }
  255. void EditorWhiteBoxMeshAsset::Save()
  256. {
  257. if (const auto absolutePath = AbsolutePathForSourceAsset(m_meshAsset))
  258. {
  259. Save(absolutePath.value());
  260. }
  261. }
  262. void EditorWhiteBoxMeshAsset::Save(const AZStd::string& absolutePath)
  263. {
  264. // save the asset to disk in the project folder
  265. if (WhiteBox::SaveAsset(m_meshAsset, absolutePath))
  266. {
  267. RequestEditSourceControl(absolutePath.c_str());
  268. }
  269. }
  270. void EditorWhiteBoxMeshAsset::AssetChanged()
  271. {
  272. EditorWhiteBoxComponentRequestBus::Event(
  273. m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::DeserializeWhiteBox);
  274. }
  275. void EditorWhiteBoxMeshAsset::AssetCleared()
  276. {
  277. // when hitting 'clear' on the asset widget, the asset data is written locally to the component
  278. EditorWhiteBoxComponentRequestBus::Event(
  279. m_entityComponentIdPair, &EditorWhiteBoxComponentRequestBus::Events::WriteAssetToComponent);
  280. }
  281. } // namespace WhiteBox