EditorCommonFeaturesSystemComponent.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <EditorCommonFeaturesSystemComponent.h>
  9. #include <SkinnedMesh/SkinnedMeshDebugDisplay.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include <AzFramework/API/ApplicationAPI.h>
  14. #include <AzToolsFramework/API/EditorCameraBus.h>
  15. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  16. #include <IEditor.h>
  17. namespace AZ
  18. {
  19. namespace Render
  20. {
  21. static IEditor* GetLegacyEditor()
  22. {
  23. IEditor* editor = nullptr;
  24. AzToolsFramework::EditorRequestBus::BroadcastResult(editor, &AzToolsFramework::EditorRequestBus::Events::GetEditor);
  25. return editor;
  26. }
  27. EditorCommonFeaturesSystemComponent::EditorCommonFeaturesSystemComponent() = default;
  28. EditorCommonFeaturesSystemComponent::~EditorCommonFeaturesSystemComponent() = default;
  29. //! Main system component for the Atom Common Feature Gem's editor/tools module.
  30. void EditorCommonFeaturesSystemComponent::Reflect(AZ::ReflectContext* context)
  31. {
  32. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  33. {
  34. serialize->Class<EditorCommonFeaturesSystemComponent, AZ::Component>()
  35. ->Version(1)
  36. ->Field("Atom Level Default Asset Path", &EditorCommonFeaturesSystemComponent::m_atomLevelDefaultAssetPath);
  37. if (AZ::EditContext* ec = serialize->GetEditContext())
  38. {
  39. ec->Class<EditorCommonFeaturesSystemComponent>("AtomEditorCommonFeaturesSystemComponent",
  40. "Configures editor- and tool-specific functionality for common render features.")
  41. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  42. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System", 0xc94d118b))
  43. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  44. ->DataElement(nullptr, &EditorCommonFeaturesSystemComponent::m_atomLevelDefaultAssetPath, "Atom Level Default Asset Path",
  45. "path to the slice the instantiate for a new Atom level")
  46. ;
  47. }
  48. }
  49. }
  50. void EditorCommonFeaturesSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  51. {
  52. provided.push_back(AZ_CRC("EditorCommonFeaturesService", 0x94945c0c));
  53. }
  54. void EditorCommonFeaturesSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  55. {
  56. incompatible.push_back(AZ_CRC("EditorCommonFeaturesService", 0x94945c0c));
  57. }
  58. void EditorCommonFeaturesSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  59. {
  60. AZ_UNUSED(required);
  61. }
  62. void EditorCommonFeaturesSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  63. {
  64. AZ_UNUSED(dependent);
  65. }
  66. void EditorCommonFeaturesSystemComponent::Init()
  67. {
  68. }
  69. void EditorCommonFeaturesSystemComponent::Activate()
  70. {
  71. m_renderer = AZStd::make_unique<AZ::LyIntegration::Thumbnails::CommonThumbnailRenderer>();
  72. m_previewerFactory = AZStd::make_unique <LyIntegration::CommonPreviewerFactory>();
  73. m_skinnedMeshDebugDisplay = AZStd::make_unique<SkinnedMeshDebugDisplay>();
  74. AzToolsFramework::EditorLevelNotificationBus::Handler::BusConnect();
  75. AzToolsFramework::AssetBrowser::PreviewerRequestBus::Handler::BusConnect();
  76. AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusConnect();
  77. }
  78. void EditorCommonFeaturesSystemComponent::Deactivate()
  79. {
  80. AzToolsFramework::EditorLevelNotificationBus::Handler::BusDisconnect();
  81. AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusDisconnect();
  82. AzToolsFramework::AssetBrowser::PreviewerRequestBus::Handler::BusDisconnect();
  83. m_skinnedMeshDebugDisplay.reset();
  84. m_previewerFactory.reset();
  85. m_renderer.reset();
  86. }
  87. void EditorCommonFeaturesSystemComponent::OnNewLevelCreated()
  88. {
  89. bool isPrefabSystemEnabled = false;
  90. AzFramework::ApplicationRequests::Bus::BroadcastResult(
  91. isPrefabSystemEnabled, &AzFramework::ApplicationRequests::IsPrefabSystemEnabled);
  92. if (!isPrefabSystemEnabled)
  93. {
  94. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  95. m_levelDefaultSliceAssetId, &AZ::Data::AssetCatalogRequests::GetAssetIdByPath, m_atomLevelDefaultAssetPath.c_str(),
  96. azrtti_typeid<AZ::SliceAsset>(), false);
  97. if (m_levelDefaultSliceAssetId.IsValid())
  98. {
  99. AZ::Data::Asset<AZ::Data::AssetData> asset = AZ::Data::AssetManager::Instance().GetAsset<AZ::SliceAsset>(
  100. m_levelDefaultSliceAssetId, AZ::Data::AssetLoadBehavior::Default);
  101. asset.BlockUntilLoadComplete();
  102. if (asset)
  103. {
  104. AZ::Vector3 cameraPosition = AZ::Vector3::CreateZero();
  105. bool activeCameraFound = false;
  106. Camera::EditorCameraRequestBus::BroadcastResult(
  107. activeCameraFound, &Camera::EditorCameraRequestBus::Events::GetActiveCameraPosition, cameraPosition);
  108. if (activeCameraFound)
  109. {
  110. AZ::Transform worldTransform = AZ::Transform::CreateTranslation(cameraPosition);
  111. AzToolsFramework::SliceEditorEntityOwnershipServiceNotificationBus::Handler::BusConnect();
  112. if (IEditor* editor = GetLegacyEditor(); !editor->IsUndoSuspended())
  113. {
  114. editor->SuspendUndo();
  115. }
  116. AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::Broadcast(
  117. &AzToolsFramework::SliceEditorEntityOwnershipServiceRequests::InstantiateEditorSlice, asset,
  118. worldTransform);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. void EditorCommonFeaturesSystemComponent::OnSliceInstantiated(const AZ::Data::AssetId& sliceAssetId, AZ::SliceComponent::SliceInstanceAddress& sliceAddress, const AzFramework::SliceInstantiationTicket& /*ticket*/)
  125. {
  126. if (m_levelDefaultSliceAssetId == sliceAssetId)
  127. {
  128. const AZ::SliceComponent::EntityList& entities = sliceAddress.GetInstance()->GetInstantiated()->m_entities;
  129. AZStd::vector<AZ::EntityId> entityIds;
  130. entityIds.reserve(entities.size());
  131. for (const Entity* entity : entities)
  132. {
  133. entityIds.push_back(entity->GetId());
  134. }
  135. //Detach instantiated env probe entities from engine slice
  136. AzToolsFramework::SliceEditorEntityOwnershipServiceRequestBus::Broadcast(
  137. &AzToolsFramework::SliceEditorEntityOwnershipServiceRequests::DetachSliceEntities, entityIds);
  138. sliceAddress.SetInstance(nullptr);
  139. sliceAddress.SetReference(nullptr);
  140. AzToolsFramework::SliceEditorEntityOwnershipServiceNotificationBus::Handler::BusDisconnect();
  141. IEditor* editor = GetLegacyEditor();
  142. //save after level default slice fully instantiated
  143. editor->SaveDocument();
  144. if (editor->IsUndoSuspended())
  145. {
  146. editor->ResumeUndo();
  147. }
  148. }
  149. }
  150. void EditorCommonFeaturesSystemComponent::OnSliceInstantiationFailed(const AZ::Data::AssetId& sliceAssetId, const AzFramework::SliceInstantiationTicket& /*ticket*/)
  151. {
  152. if (m_levelDefaultSliceAssetId == sliceAssetId)
  153. {
  154. GetLegacyEditor()->ResumeUndo();
  155. AzToolsFramework::SliceEditorEntityOwnershipServiceNotificationBus::Handler::BusDisconnect();
  156. AZ_Warning("EditorCommonFeaturesSystemComponent", false, "Failed to instantiate default Atom environment slice.");
  157. }
  158. }
  159. const AzToolsFramework::AssetBrowser::PreviewerFactory* EditorCommonFeaturesSystemComponent::GetPreviewerFactory(
  160. const AzToolsFramework::AssetBrowser::AssetBrowserEntry* entry) const
  161. {
  162. return m_previewerFactory->IsEntrySupported(entry) ? m_previewerFactory.get() : nullptr;
  163. }
  164. void EditorCommonFeaturesSystemComponent::OnApplicationAboutToStop()
  165. {
  166. m_renderer.reset();
  167. }
  168. } // namespace Render
  169. } // namespace AZ