3
0

SoundAssetRef.cpp 4.0 KB

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