EditorWhiteBoxMeshAsset.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #pragma once
  9. #include "Asset/WhiteBoxMeshAsset.h"
  10. #include "Asset/WhiteBoxMeshAssetBus.h"
  11. #include <AzCore/Component/ComponentBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <IEditor.h>
  14. namespace WhiteBox
  15. {
  16. //! Handle creating, loading and setting White Box mesh assets.
  17. //! @note Used by the EditorWhiteBoxComponent to delegate asset handling responsibilities.
  18. class EditorWhiteBoxMeshAsset
  19. : private AZ::Data::AssetBus::Handler
  20. , private WhiteBoxMeshAssetNotificationBus::Handler
  21. , private IEditorNotifyListener
  22. , private AZ::TickBus::Handler
  23. {
  24. public:
  25. AZ_CLASS_ALLOCATOR_DECL
  26. AZ_TYPE_INFO(EditorWhiteBoxMeshAsset, "{4A9D9B10-9E60-4D59-A308-966829DC2B76}")
  27. static void Reflect(AZ::ReflectContext* context);
  28. EditorWhiteBoxMeshAsset() = default;
  29. ~EditorWhiteBoxMeshAsset();
  30. //! Release the stored WhiteBoxMeshAsset but retain the AssetId to be loaded again in future.
  31. void Release();
  32. //! Release the stored WhiteBoxMeshAsset and invalidate the AssetId to full clear the asset from use.
  33. //! @note m_entityComponentIdPair is not reset.
  34. void Reset();
  35. //! Associate an EditorWhiteBoxMeshAsset with a specific entity/component id pair that
  36. //! changes will be propagated to.
  37. void Associate(const AZ::EntityComponentIdPair& entityComponentIdPair);
  38. //! Transfer ownership of an in-memory White Box mesh and create an asset at the specified relative path.
  39. void TakeOwnershipOfWhiteBoxMesh(const AZStd::string& relativeAssetPath, Api::WhiteBoxMeshPtr whiteBoxMesh);
  40. //! Returns the WhiteBoxMesh stored on this asset.
  41. //! @note If an asset has not been set or loaded this value will be null.
  42. //! @note Do not hold onto a reference to this pointer for longer than a function call.
  43. WhiteBoxMesh* GetWhiteBoxMesh();
  44. //! Returns the AssetId of the White Box mesh asset.
  45. //! @note If an asset has not been set or loaded this value will be invalid.
  46. AZ::Data::AssetId GetWhiteBoxMeshAssetId() const;
  47. //! Returns the WhiteBoxMeshAsset.
  48. //! @note If an asset has not been set or loaded the asset will be empty/invalid.
  49. AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> GetWhiteBoxMeshAsset();
  50. //! Helper to return if the White Box mesh asset id is valid or not (if it is valid, the asset is in use).
  51. bool InUse() const;
  52. //! Request a load of the stored asset.
  53. //! @note If the asset is not in use (asset id is invalid) the call will be a noop.
  54. void Load();
  55. //! Write the asset to disk at its existing location.
  56. void Save();
  57. //! Write the asset to disk at an arbitrary location.
  58. void Save(const AZStd::string& absolutePath);
  59. //! Returns if the asset is both set and loaded.
  60. //! @note An asset may be in use, but not yet loaded (InUse() != Loaded()).
  61. bool Loaded() const;
  62. //! Store the in-memory representation of the WhiteBoxMesh to the current WhiteBoxMeshAsset.
  63. void Serialize();
  64. private:
  65. // Edit context callbacks
  66. void AssetChanged();
  67. void AssetCleared();
  68. // AZ::Data::AssetBus ...
  69. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  70. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  71. void OnAssetError(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  72. void OnAssetReloadError(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  73. // WhiteBoxMeshAssetNotificationBus ...
  74. void OnWhiteBoxMeshAssetModified(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  75. // IEditorNotifyListener ...
  76. void OnEditorNotifyEvent(EEditorNotifyEvent editorEvent) override;
  77. //! Disconnect from buses/listeners before either releasing or destroying the asset.
  78. void Disconnect();
  79. // AZ::TickBus overrides ...
  80. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  81. // Listeners for legacy editor events when the level is saved.
  82. void RegisterForEditorEvents();
  83. void UnregisterForEditorEvents();
  84. //! A reference to White Box mesh data stored in an asset.
  85. AZ::Data::Asset<Pipeline::WhiteBoxMeshAsset> m_meshAsset;
  86. AZ::EntityComponentIdPair m_entityComponentIdPair; //!< The entity/component this asset is associated with.
  87. };
  88. } // namespace WhiteBox