ConstantGradientComponent.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <GradientSignal/Components/ConstantGradientComponent.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <LmbrCentral/Dependency/DependencyMonitor.h>
  13. namespace GradientSignal
  14. {
  15. void ConstantGradientConfig::Reflect(AZ::ReflectContext* context)
  16. {
  17. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  18. if (serialize)
  19. {
  20. serialize->Class<ConstantGradientConfig, AZ::ComponentConfig>()
  21. ->Version(0)
  22. ->Field("Value", &ConstantGradientConfig::m_value)
  23. ;
  24. AZ::EditContext* edit = serialize->GetEditContext();
  25. if (edit)
  26. {
  27. edit->Class<ConstantGradientConfig>(
  28. "Constant Gradient", "")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  31. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  32. ->DataElement(AZ::Edit::UIHandlers::Slider, &ConstantGradientConfig::m_value, "Value", "Value always returned by this gradient.")
  33. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  34. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  35. ;
  36. }
  37. }
  38. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  39. {
  40. behaviorContext->Class<ConstantGradientConfig>()
  41. ->Constructor()
  42. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  43. ->Property("constantValue", BehaviorValueProperty(&ConstantGradientConfig::m_value))
  44. ;
  45. }
  46. }
  47. void ConstantGradientComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  48. {
  49. services.push_back(AZ_CRC_CE("GradientService"));
  50. }
  51. void ConstantGradientComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  52. {
  53. services.push_back(AZ_CRC_CE("GradientService"));
  54. services.push_back(AZ_CRC_CE("GradientTransformService"));
  55. }
  56. void ConstantGradientComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  57. {
  58. }
  59. void ConstantGradientComponent::Reflect(AZ::ReflectContext* context)
  60. {
  61. ConstantGradientConfig::Reflect(context);
  62. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  63. if (serialize)
  64. {
  65. serialize->Class<ConstantGradientComponent, AZ::Component>()
  66. ->Version(0)
  67. ->Field("Configuration", &ConstantGradientComponent::m_configuration)
  68. ;
  69. }
  70. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  71. {
  72. behaviorContext->Constant("ConstantGradientComponentTypeId", BehaviorConstant(ConstantGradientComponentTypeId));
  73. behaviorContext->Class<ConstantGradientComponent>()->RequestBus("ConstantGradientRequestBus");
  74. behaviorContext->EBus<ConstantGradientRequestBus>("ConstantGradientRequestBus")
  75. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  76. ->Event("GetConstantValue", &ConstantGradientRequestBus::Events::GetConstantValue)
  77. ->Event("SetConstantValue", &ConstantGradientRequestBus::Events::SetConstantValue)
  78. ->VirtualProperty("ConstantValue", "GetConstantValue", "SetConstantValue")
  79. ;
  80. }
  81. }
  82. ConstantGradientComponent::ConstantGradientComponent(const ConstantGradientConfig& configuration)
  83. : m_configuration(configuration)
  84. {
  85. }
  86. void ConstantGradientComponent::Activate()
  87. {
  88. ConstantGradientRequestBus::Handler::BusConnect(GetEntityId());
  89. // Connect to GradientRequestBus last so that everything is initialized before listening for gradient queries.
  90. GradientRequestBus::Handler::BusConnect(GetEntityId());
  91. }
  92. void ConstantGradientComponent::Deactivate()
  93. {
  94. // Disconnect from GradientRequestBus first to ensure no queries are in process when deactivating.
  95. GradientRequestBus::Handler::BusDisconnect();
  96. ConstantGradientRequestBus::Handler::BusDisconnect();
  97. }
  98. bool ConstantGradientComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  99. {
  100. if (auto config = azrtti_cast<const ConstantGradientConfig*>(baseConfig))
  101. {
  102. m_configuration = *config;
  103. return true;
  104. }
  105. return false;
  106. }
  107. bool ConstantGradientComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  108. {
  109. if (auto config = azrtti_cast<ConstantGradientConfig*>(outBaseConfig))
  110. {
  111. *config = m_configuration;
  112. return true;
  113. }
  114. return false;
  115. }
  116. float ConstantGradientComponent::GetValue([[maybe_unused]] const GradientSampleParams& sampleParams) const
  117. {
  118. AZStd::shared_lock lock(m_queryMutex);
  119. return m_configuration.m_value;
  120. }
  121. void ConstantGradientComponent::GetValues(
  122. [[maybe_unused]] AZStd::span<const AZ::Vector3> positions, AZStd::span<float> outValues) const
  123. {
  124. if (positions.size() != outValues.size())
  125. {
  126. AZ_Assert(false, "input and output lists are different sizes (%zu vs %zu).", positions.size(), outValues.size());
  127. return;
  128. }
  129. AZStd::shared_lock lock(m_queryMutex);
  130. AZStd::fill(outValues.begin(), outValues.end(), m_configuration.m_value);
  131. }
  132. float ConstantGradientComponent::GetConstantValue() const
  133. {
  134. return m_configuration.m_value;
  135. }
  136. void ConstantGradientComponent::SetConstantValue(float constant)
  137. {
  138. // Only hold the lock while we're changing the data. Don't hold onto it during the OnCompositionChanged call, because that can
  139. // execute an arbitrary amount of logic, including calls back to this component.
  140. {
  141. AZStd::unique_lock lock(m_queryMutex);
  142. m_configuration.m_value = constant;
  143. }
  144. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  145. }
  146. }