3
0

SoundAssetRef.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <AzCore/Asset/AssetSerializer.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <MiniAudio/SoundAssetRef.h>
  12. namespace MiniAudio
  13. {
  14. void SoundAssetRef::Reflect(AZ::ReflectContext* context)
  15. {
  16. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  17. {
  18. serializeContext->Class<SoundAssetRef>()->Version(0)->EventHandler<SerializationEvents>()->Field(
  19. "asset", &SoundAssetRef::m_asset);
  20. serializeContext->RegisterGenericType<AZStd::vector<SoundAssetRef>>();
  21. serializeContext->RegisterGenericType<AZStd::unordered_map<AZStd::string, SoundAssetRef>>();
  22. serializeContext->RegisterGenericType<AZStd::unordered_map<double, SoundAssetRef>>(); // required to support Map<Number,
  23. // SoundAssetRef> in Script Canvas
  24. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  25. {
  26. editContext
  27. ->Class<SoundAssetRef>(
  28. "SoundAssetRef", "A wrapper around MiniAudio SoundAsset to be used as a variable in Script Canvas.")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  31. // m_asset
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &SoundAssetRef::m_asset, "asset", "")
  33. ->Attribute(AZ::Edit::Attributes::ShowProductAssetFileName, false)
  34. ->Attribute(AZ::Edit::Attributes::HideProductFilesInAssetPicker, true)
  35. ->Attribute(AZ::Edit::Attributes::AssetPickerTitle, "MiniAudio Sound Asset")
  36. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &SoundAssetRef::OnSpawnAssetChanged);
  37. }
  38. }
  39. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  40. {
  41. behaviorContext->Class<SoundAssetRef>("SoundAssetRef")
  42. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  43. ->Attribute(AZ::Script::Attributes::EnableAsScriptEventParamType, true)
  44. ->Attribute(AZ::Script::Attributes::Category, "MiniAudio")
  45. ->Attribute(AZ::Script::Attributes::Module, "miniaudio")
  46. ->Constructor()
  47. ->Method("GetAsset", &SoundAssetRef::GetAsset)
  48. ->Method("SetAsset", &SoundAssetRef::SetAsset);
  49. }
  50. }
  51. SoundAssetRef::~SoundAssetRef()
  52. {
  53. AZ::Data::AssetBus::Handler::BusDisconnect();
  54. }
  55. SoundAssetRef::SoundAssetRef(const SoundAssetRef& rhs)
  56. {
  57. SetAsset(rhs.m_asset);
  58. }
  59. SoundAssetRef::SoundAssetRef(SoundAssetRef&& rhs)
  60. {
  61. SetAsset(AZStd::move(rhs.m_asset));
  62. }
  63. SoundAssetRef& SoundAssetRef::operator=(const SoundAssetRef& rhs)
  64. {
  65. if (this != &rhs)
  66. {
  67. SetAsset(rhs.m_asset);
  68. }
  69. return *this;
  70. }
  71. SoundAssetRef& SoundAssetRef::operator=(SoundAssetRef&& rhs)
  72. {
  73. if (this != &rhs)
  74. {
  75. SetAsset(AZStd::move(rhs.m_asset));
  76. }
  77. return *this;
  78. }
  79. void SoundAssetRef::SetAsset(const AZ::Data::Asset<SoundAsset>& asset)
  80. {
  81. AZ::Data::AssetBus::Handler::BusDisconnect();
  82. m_asset = asset;
  83. if (m_asset.GetId().IsValid())
  84. {
  85. AZ::Data::AssetBus::Handler::BusConnect(m_asset.GetId());
  86. }
  87. }
  88. AZ::Data::Asset<SoundAsset> SoundAssetRef::GetAsset() const
  89. {
  90. return m_asset;
  91. }
  92. void SoundAssetRef::OnSpawnAssetChanged()
  93. {
  94. SetAsset(m_asset);
  95. }
  96. void SoundAssetRef::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  97. {
  98. m_asset = asset;
  99. }
  100. } // namespace MiniAudio