3
0

EditorPostFxSystemComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <PostProcess/EditorPostFxSystemComponent.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. void EditorPostFxSystemComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. EditorPostFxLayerCategoriesAsset::Reflect(context);
  19. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serialize->Class<EditorPostFxSystemComponent, AzToolsFramework::Components::EditorComponentBase>()
  22. ->Version(0)
  23. ;
  24. if (AZ::EditContext* ec = serialize->GetEditContext())
  25. {
  26. ec->Class<EditorPostFxSystemComponent>("Editor PostFx System", "Manages discovery of PostFx layer categories asset")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  29. ;
  30. }
  31. }
  32. }
  33. void EditorPostFxSystemComponent::Init()
  34. {
  35. AzToolsFramework::Components::EditorComponentBase::Init();
  36. }
  37. void EditorPostFxSystemComponent::Activate()
  38. {
  39. RegisterAssethandlers();
  40. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  41. AzToolsFramework::Components::EditorComponentBase::Activate();
  42. PostFxLayerCategoriesProviderRequestBus::Handler::BusConnect();
  43. }
  44. void EditorPostFxSystemComponent::Deactivate()
  45. {
  46. PostFxLayerCategoriesProviderRequestBus::Handler::BusDisconnect();
  47. AzToolsFramework::Components::EditorComponentBase::Deactivate();
  48. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  49. UnregisterAssethandlers();
  50. }
  51. void EditorPostFxSystemComponent::RegisterAssethandlers()
  52. {
  53. m_postFxLayerCategoriesAsset = aznew AzFramework::GenericAssetHandler<EditorPostFxLayerCategoriesAsset>("PostFx Layer Categories", "Other", "postFxLayerCategories");
  54. m_postFxLayerCategoriesAsset->Register();
  55. }
  56. void EditorPostFxSystemComponent::UnregisterAssethandlers()
  57. {
  58. m_layerCategoriesAssetMap.clear();
  59. if (m_postFxLayerCategoriesAsset)
  60. {
  61. m_postFxLayerCategoriesAsset->Unregister();
  62. delete m_postFxLayerCategoriesAsset;
  63. m_postFxLayerCategoriesAsset = nullptr;
  64. }
  65. }
  66. void EditorPostFxSystemComponent::GetLayerCategories(PostFx::LayerCategoriesMap& layerCategories) const
  67. {
  68. // use layer category definitions from all postfxlayercategories asset files
  69. for (const auto& assetPair : m_layerCategoriesAssetMap)
  70. {
  71. const auto& asset = assetPair.second;
  72. if (asset.IsReady())
  73. {
  74. const auto& priorityLayerPairs = asset.Get()->m_layerCategories;
  75. layerCategories.insert(priorityLayerPairs.begin(), priorityLayerPairs.end());
  76. }
  77. }
  78. // insert default layer
  79. layerCategories[PostFx::DefaultLayerCategory] = PostFx::DefaultLayerCategoryValue;
  80. }
  81. void EditorPostFxSystemComponent::OnCatalogLoaded(const char* catalogFile)
  82. {
  83. AZ_UNUSED(catalogFile);
  84. // automatically register all layer categories assets
  85. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets,
  86. nullptr,
  87. [this](const AZ::Data::AssetId assetId, const AZ::Data::AssetInfo& assetInfo) {
  88. const auto assetType = azrtti_typeid<EditorPostFxLayerCategoriesAsset>();
  89. if (assetInfo.m_assetType == assetType)
  90. {
  91. m_layerCategoriesAssetMap[assetId] = AZ::Data::AssetManager::Instance().GetAsset(assetId, assetType, AZ::Data::AssetLoadBehavior::PreLoad);
  92. m_layerCategoriesAssetMap[assetId].QueueLoad();
  93. }
  94. },
  95. nullptr);
  96. }
  97. void EditorPostFxSystemComponent::OnCatalogAssetChanged(const AZ::Data::AssetId& assetId)
  98. {
  99. UpdateLayerCategoriesAssetMap(assetId);
  100. }
  101. void EditorPostFxSystemComponent::OnCatalogAssetAdded(const AZ::Data::AssetId& assetId)
  102. {
  103. UpdateLayerCategoriesAssetMap(assetId);
  104. }
  105. void EditorPostFxSystemComponent::UpdateLayerCategoriesAssetMap(const AZ::Data::AssetId& assetId)
  106. {
  107. AZ::Data::AssetInfo assetInfo;
  108. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequests::GetAssetInfoById, assetId);
  109. const auto assetType = azrtti_typeid<EditorPostFxLayerCategoriesAsset>();
  110. if (assetInfo.m_assetType == assetType)
  111. {
  112. m_layerCategoriesAssetMap[assetId] = AZ::Data::AssetManager::Instance().GetAsset(assetId, assetType, AZ::Data::AssetLoadBehavior::PreLoad);
  113. m_layerCategoriesAssetMap[assetId].BlockUntilLoadComplete();
  114. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestRefresh, AzToolsFramework::PropertyModificationRefreshLevel::Refresh_AttributesAndValues);
  115. }
  116. }
  117. void EditorPostFxSystemComponent::OnCatalogAssetRemoved(const AZ::Data::AssetId& assetId, const AZ::Data::AssetInfo&)
  118. {
  119. m_layerCategoriesAssetMap.erase(assetId);
  120. }
  121. }
  122. }