EditorAudioAreaEnvironmentComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "EditorAudioAreaEnvironmentComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. namespace LmbrCentral
  12. {
  13. //=========================================================================
  14. namespace ClassConverters
  15. {
  16. static bool UpgradeEditorAudioAreaEnvironmentComponent(AZ::SerializeContext&, AZ::SerializeContext::DataElementNode&);
  17. } // namespace ClassConverters
  18. //=========================================================================
  19. void EditorAudioAreaEnvironmentComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<EditorAudioAreaEnvironmentComponent, EditorComponentBase>()
  24. ->Version(2, &ClassConverters::UpgradeEditorAudioAreaEnvironmentComponent)
  25. ->Field("Broad-phase Trigger Area entity", &EditorAudioAreaEnvironmentComponent::m_broadPhaseTriggerArea)
  26. ->Field("Environment name", &EditorAudioAreaEnvironmentComponent::m_environmentName)
  27. ->Field("Environment fade distance", &EditorAudioAreaEnvironmentComponent::m_environmentFadeDistance)
  28. ;
  29. if (auto editContext = serializeContext->GetEditContext())
  30. {
  31. editContext->Class<EditorAudioAreaEnvironmentComponent>("Audio Area Environment", "The Audio Area Environment component enables entities that are moving around and throughout a shape to have environment effects applied to any sounds that they trigger")
  32. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  33. ->Attribute(AZ::Edit::Attributes::Category, "Audio")
  34. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/AudioAreaEnvironment.svg")
  35. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/AudioAreaEnvironment.svg")
  36. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  37. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  38. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/audio/area-environment/")
  39. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAudioAreaEnvironmentComponent::m_broadPhaseTriggerArea,
  40. "Broad-phase trigger area", "The entity that contains a Trigger Area component for broad-phase checks")
  41. ->Attribute(AZ::Edit::Attributes::RequiredService, AZ_CRC_CE("ProximityTriggerService"))
  42. ->DataElement("AudioControl", &EditorAudioAreaEnvironmentComponent::m_environmentName,
  43. "Environment name", "The name of the ATL Environment to use")
  44. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAudioAreaEnvironmentComponent::m_environmentFadeDistance,
  45. "Fade distance", "Distance around the area shape that the environment amounts will fade")
  46. ->Attribute(AZ::Edit::Attributes::Min, 0.01f)
  47. ;
  48. }
  49. }
  50. }
  51. //=========================================================================
  52. EditorAudioAreaEnvironmentComponent::EditorAudioAreaEnvironmentComponent()
  53. {
  54. m_environmentName.m_propertyType = AzToolsFramework::AudioPropertyType::Environment;
  55. }
  56. //=========================================================================
  57. void EditorAudioAreaEnvironmentComponent::BuildGameEntity(AZ::Entity* gameEntity)
  58. {
  59. if (auto audioAreaEnvironmentComponent = gameEntity->CreateComponent<AudioAreaEnvironmentComponent>())
  60. {
  61. audioAreaEnvironmentComponent->m_broadPhaseTriggerArea = m_broadPhaseTriggerArea;
  62. audioAreaEnvironmentComponent->m_environmentName = m_environmentName.m_controlName;
  63. audioAreaEnvironmentComponent->m_environmentFadeDistance = m_environmentFadeDistance;
  64. }
  65. }
  66. //=========================================================================
  67. namespace ClassConverters
  68. {
  69. static bool UpgradeEditorAudioAreaEnvironmentComponent(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  70. {
  71. if (classElement.GetVersion() == 1)
  72. {
  73. // upgrade V1 to V2
  74. AZStd::string oldEnvironmentName;
  75. int environmentIndex = classElement.FindElement(AZ::Crc32("Environment name"));
  76. if (environmentIndex == -1)
  77. {
  78. AZ_Error("Serialization", false, "Failed to find old Environment name.");
  79. return false;
  80. }
  81. auto environmentNode = classElement.GetSubElement(environmentIndex);
  82. if (!environmentNode.GetData<AZStd::string>(oldEnvironmentName))
  83. {
  84. AZ_Error("Serialization", false, "Failed to retrieve old Environment name.");
  85. return false;
  86. }
  87. classElement.RemoveElement(environmentIndex);
  88. AzToolsFramework::CReflectedVarAudioControl newEnvironment;
  89. newEnvironment.m_propertyType = AzToolsFramework::AudioPropertyType::Environment;
  90. newEnvironment.m_controlName = oldEnvironmentName;
  91. if (classElement.AddElementWithData(context, "Environment name", newEnvironment) == -1)
  92. {
  93. AZ_Error("Serialization", false, "Failed to replace Environment name with new version.");
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. } // namespace ClassConverters
  100. } // namespace LmbrCentral