EditorMiniAudioPlaybackComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "EditorMiniAudioPlaybackComponent.h"
  9. #include <AzCore/Asset/AssetSerializer.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Utils/Utils.h>
  12. namespace MiniAudio
  13. {
  14. AZ::ComponentDescriptor* EditorMiniAudioPlaybackComponent_CreateDescriptor()
  15. {
  16. return EditorMiniAudioPlaybackComponent::CreateDescriptor();
  17. }
  18. void EditorMiniAudioPlaybackComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. BaseClass::Reflect(context);
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<EditorMiniAudioPlaybackComponent, BaseClass>()->Version(4);
  24. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  25. {
  26. editContext->Class<EditorMiniAudioPlaybackComponent>("MiniAudio Playback", "")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "MiniAudio")
  29. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  30. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  31. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  32. ->UIElement(AZ::Edit::UIHandlers::Button, "Play Sound", "Plays the assigned sound")
  33. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  34. ->Attribute(AZ::Edit::Attributes::ButtonText, "Play Sound")
  35. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorMiniAudioPlaybackComponent::PlaySoundInEditor)
  36. ->UIElement(AZ::Edit::UIHandlers::Button, "Stop Sound", "Stops playing the sound")
  37. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  38. ->Attribute(AZ::Edit::Attributes::ButtonText, "Stop Sound")
  39. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorMiniAudioPlaybackComponent::StopSoundInEditor)
  40. ->UIElement(AZ::Edit::UIHandlers::Button, "Pause Sound", "Pause playing the sound")
  41. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  42. ->Attribute(AZ::Edit::Attributes::ButtonText, "Pause Sound")
  43. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorMiniAudioPlaybackComponent::PauseSoundInEditor);
  44. editContext->Class<MiniAudioPlaybackComponentController>("MiniAudioPlaybackComponentController", "")
  45. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  46. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  47. ->DataElement(AZ::Edit::UIHandlers::Default, &MiniAudioPlaybackComponentController::m_config, "Configuration", "")
  48. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  49. editContext
  50. ->Class<MiniAudioPlaybackComponentConfig>(
  51. "MiniAudioPlaybackComponent Config", "[Configuration for MiniAudioPlaybackComponent]")
  52. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  53. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  54. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  55. ->DataElement(
  56. AZ::Edit::UIHandlers::Default, &MiniAudioPlaybackComponentConfig::m_sound, "Sound Asset", "Sound asset to play")
  57. ->ClassElement(AZ::Edit::ClassElements::Group, "Configuration")
  58. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  59. ->DataElement(
  60. AZ::Edit::UIHandlers::Default,
  61. &MiniAudioPlaybackComponentConfig::m_autoplayOnActivate,
  62. "Autoplay",
  63. "Plays the sound on activation of the component.")
  64. ->DataElement(AZ::Edit::UIHandlers::Default, &MiniAudioPlaybackComponentConfig::m_loop, "Loop", "Loops the sound.")
  65. ->DataElement(
  66. AZ::Edit::UIHandlers::Slider,
  67. &MiniAudioPlaybackComponentConfig::m_volume,
  68. "Volume",
  69. "The volume of the sound when played, as a percentage.")
  70. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  71. ->Attribute(AZ::Edit::Attributes::Step, 1.0f)
  72. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  73. ->Attribute(AZ::Edit::Attributes::Suffix, " %")
  74. ->DataElement(
  75. AZ::Edit::UIHandlers::Default,
  76. &MiniAudioPlaybackComponentConfig::m_autoFollowEntity,
  77. "Auto-follow",
  78. "The sound's position is updated to match the entity's position.")
  79. ->ClassElement(AZ::Edit::ClassElements::Group, "Spatialization")
  80. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  81. ->DataElement(
  82. AZ::Edit::UIHandlers::Default,
  83. &MiniAudioPlaybackComponentConfig::m_enableSpatialization,
  84. "Spatialization",
  85. "If true the sound will have 3D position in the world and will have effects applied to it based on the distance "
  86. "from a sound listener.")
  87. ->DataElement(
  88. AZ::Edit::UIHandlers::Default,
  89. &MiniAudioPlaybackComponentConfig::m_directionalAttenuationFactor,
  90. "Directional Attenuation Factor",
  91. "Sets the directional attenuation based on the listener's direction.")
  92. ->DataElement(
  93. AZ::Edit::UIHandlers::ComboBox,
  94. &MiniAudioPlaybackComponentConfig::m_attenuationModel,
  95. "Attenuation",
  96. "Attenuation model.")
  97. ->EnumAttribute(AttenuationModel::Inverse, "Inverse")
  98. ->EnumAttribute(AttenuationModel::Exponential, "Exponential")
  99. ->EnumAttribute(AttenuationModel::Linear, "Linear")
  100. ->DataElement(
  101. AZ::Edit::UIHandlers::Default,
  102. &MiniAudioPlaybackComponentConfig::m_fixedDirection,
  103. "Fixed Direction",
  104. "Determines whether the direction of the sound source is fixed to what is entered in the editor or if the forward "
  105. "direction of the entity is used.")
  106. ->DataElement(
  107. AZ::Edit::UIHandlers::Default,
  108. &MiniAudioPlaybackComponentConfig::m_direction,
  109. "Direction",
  110. "Sets the direction that the sound source's inner and out cones point towards.")
  111. ->DataElement(
  112. AZ::Edit::UIHandlers::Default,
  113. &MiniAudioPlaybackComponentConfig::m_minimumDistance,
  114. "Min Distance",
  115. "Minimum distance for attenuation.")
  116. ->DataElement(
  117. AZ::Edit::UIHandlers::Default,
  118. &MiniAudioPlaybackComponentConfig::m_maximumDistance,
  119. "Max Distance",
  120. "Maximum distance for attenuation.")
  121. ->DataElement(
  122. AZ::Edit::UIHandlers::Default,
  123. &MiniAudioPlaybackComponentConfig::m_innerAngleInDegrees,
  124. "Inner Cone Angle",
  125. "Sets the sound source's inner cone angle in Degrees.")
  126. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  127. ->Attribute(AZ::Edit::Attributes::Step, 1.0f)
  128. ->Attribute(AZ::Edit::Attributes::Max, 360.0f)
  129. ->Attribute(AZ::Edit::Attributes::Suffix, " degrees")
  130. ->DataElement(
  131. AZ::Edit::UIHandlers::Default,
  132. &MiniAudioPlaybackComponentConfig::m_outerAngleInDegrees,
  133. "Outer Cone Angle",
  134. "Sets the sound source's outer cone angle in Degrees.")
  135. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  136. ->Attribute(AZ::Edit::Attributes::Step, 1.0f)
  137. ->Attribute(AZ::Edit::Attributes::Max, 360.0f)
  138. ->Attribute(AZ::Edit::Attributes::Suffix, " degrees")
  139. ->DataElement(
  140. AZ::Edit::UIHandlers::Slider,
  141. &MiniAudioPlaybackComponentConfig::m_outerVolume,
  142. "Outer Volume",
  143. "Sets the volume of the sound source outside of the outer cone, as a percentage.")
  144. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  145. ->Attribute(AZ::Edit::Attributes::Step, 1.0f)
  146. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  147. ->Attribute(AZ::Edit::Attributes::Suffix, " %")
  148. ;
  149. }
  150. }
  151. }
  152. EditorMiniAudioPlaybackComponent::EditorMiniAudioPlaybackComponent(const MiniAudioPlaybackComponentConfig& config)
  153. : BaseClass(config)
  154. {
  155. }
  156. void EditorMiniAudioPlaybackComponent::Activate()
  157. {
  158. BaseClass::Activate();
  159. }
  160. void EditorMiniAudioPlaybackComponent::Deactivate()
  161. {
  162. BaseClass::Deactivate();
  163. }
  164. AZ::u32 EditorMiniAudioPlaybackComponent::OnConfigurationChanged()
  165. {
  166. m_controller.OnConfigurationUpdated();
  167. return AZ::Edit::PropertyRefreshLevels::None;
  168. }
  169. AZ::Crc32 EditorMiniAudioPlaybackComponent::PlaySoundInEditor()
  170. {
  171. m_controller.Play();
  172. return AZ::Edit::PropertyRefreshLevels::None;
  173. }
  174. AZ::Crc32 EditorMiniAudioPlaybackComponent::StopSoundInEditor()
  175. {
  176. m_controller.Stop();
  177. return AZ::Edit::PropertyRefreshLevels::None;
  178. }
  179. AZ::Crc32 EditorMiniAudioPlaybackComponent::PauseSoundInEditor()
  180. {
  181. m_controller.Pause();
  182. return AZ::Edit::PropertyRefreshLevels::None;
  183. }
  184. AZ::Crc32 EditorMiniAudioPlaybackComponent::OnVolumeChanged()
  185. {
  186. m_controller.SetVolumePercentage(m_controller.GetConfiguration().m_volume / 100.f);
  187. return AZ::Edit::PropertyRefreshLevels::None;
  188. }
  189. } // namespace MiniAudio