AudioSwitchComponent.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "AudioSwitchComponent.h"
  9. #include <ISystem.h>
  10. #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Debug/Trace.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. namespace LmbrCentral
  16. {
  17. void AudioSwitchComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<AudioSwitchComponent, AZ::Component>()
  23. ->Version(1)
  24. ->Field("Switch name", &AudioSwitchComponent::m_defaultSwitchName)
  25. ->Field("State name", &AudioSwitchComponent::m_defaultStateName)
  26. ;
  27. }
  28. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  29. {
  30. behaviorContext->EBus<AudioSwitchComponentRequestBus>("AudioSwitchComponentRequestBus")
  31. ->Event("SetState", &AudioSwitchComponentRequestBus::Events::SetState)
  32. ->Event("SetSwitchState", &AudioSwitchComponentRequestBus::Events::SetSwitchState)
  33. ;
  34. }
  35. }
  36. //=========================================================================
  37. void AudioSwitchComponent::Activate()
  38. {
  39. OnDefaultSwitchChanged();
  40. OnDefaultStateChanged();
  41. // set the default switch state, if valid IDs were found.
  42. if (m_defaultSwitchID != INVALID_AUDIO_CONTROL_ID && m_defaultStateID != INVALID_AUDIO_SWITCH_STATE_ID)
  43. {
  44. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetSwitchState, m_defaultSwitchID, m_defaultStateID);
  45. }
  46. AudioSwitchComponentRequestBus::Handler::BusConnect(GetEntityId());
  47. }
  48. //=========================================================================
  49. void AudioSwitchComponent::Deactivate()
  50. {
  51. AudioSwitchComponentRequestBus::Handler::BusDisconnect(GetEntityId());
  52. }
  53. //=========================================================================
  54. AudioSwitchComponent::AudioSwitchComponent(const AZStd::string& switchName, const AZStd::string& stateName)
  55. : m_defaultSwitchName(switchName)
  56. , m_defaultStateName(stateName)
  57. {
  58. }
  59. //=========================================================================
  60. void AudioSwitchComponent::SetState(const char* stateName)
  61. {
  62. if (m_defaultSwitchID != INVALID_AUDIO_CONTROL_ID)
  63. {
  64. // only allowed if there's a default switch that is known.
  65. if (stateName && stateName[0] != '\0')
  66. {
  67. Audio::TAudioSwitchStateID stateID = INVALID_AUDIO_SWITCH_STATE_ID;
  68. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  69. {
  70. stateID = audioSystem->GetAudioSwitchStateID(m_defaultSwitchID, stateName);
  71. }
  72. if (stateID != INVALID_AUDIO_SWITCH_STATE_ID)
  73. {
  74. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetSwitchState, m_defaultSwitchID, stateID);
  75. }
  76. }
  77. }
  78. }
  79. //=========================================================================
  80. void AudioSwitchComponent::SetSwitchState(const char* switchName, const char* stateName)
  81. {
  82. Audio::TAudioControlID switchID = INVALID_AUDIO_CONTROL_ID;
  83. Audio::TAudioSwitchStateID stateID = INVALID_AUDIO_SWITCH_STATE_ID;
  84. // lookup switch...
  85. if (switchName && switchName[0] != '\0')
  86. {
  87. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  88. {
  89. switchID = audioSystem->GetAudioSwitchID(switchName);
  90. }
  91. }
  92. // using the switchID (if found), lookup the state...
  93. if (switchID != INVALID_AUDIO_CONTROL_ID && stateName && stateName[0] != '\0')
  94. {
  95. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  96. {
  97. stateID = audioSystem->GetAudioSwitchStateID(switchID, stateName);
  98. }
  99. }
  100. // if both IDs found, make the call...
  101. if (switchID != INVALID_AUDIO_CONTROL_ID && stateID != INVALID_AUDIO_SWITCH_STATE_ID)
  102. {
  103. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetSwitchState, switchID, stateID);
  104. }
  105. }
  106. //=========================================================================
  107. void AudioSwitchComponent::OnDefaultSwitchChanged()
  108. {
  109. m_defaultSwitchID = INVALID_AUDIO_CONTROL_ID;
  110. if (!m_defaultSwitchName.empty())
  111. {
  112. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  113. {
  114. m_defaultSwitchID = audioSystem->GetAudioSwitchID(m_defaultSwitchName.c_str());
  115. }
  116. }
  117. }
  118. //=========================================================================
  119. void AudioSwitchComponent::OnDefaultStateChanged()
  120. {
  121. m_defaultStateID = INVALID_AUDIO_SWITCH_STATE_ID;
  122. if (!m_defaultStateName.empty() && m_defaultSwitchID != INVALID_AUDIO_CONTROL_ID)
  123. {
  124. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  125. {
  126. m_defaultStateID = audioSystem->GetAudioSwitchStateID(m_defaultSwitchID, m_defaultStateName.c_str());
  127. }
  128. }
  129. }
  130. } // namespace LmbrCentral