SphereShapeComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "SphereShapeComponent.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <Shape/ShapeComponentConverters.h>
  12. #include <Shape/ShapeDisplay.h>
  13. namespace LmbrCentral
  14. {
  15. void SphereShapeComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  16. {
  17. provided.push_back(AZ_CRC_CE("ShapeService"));
  18. provided.push_back(AZ_CRC_CE("SphereShapeService"));
  19. }
  20. void SphereShapeComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  21. {
  22. incompatible.push_back(AZ_CRC_CE("ShapeService"));
  23. incompatible.push_back(AZ_CRC_CE("SphereShapeService"));
  24. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  25. }
  26. void SphereShapeComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  27. {
  28. required.push_back(AZ_CRC_CE("TransformService"));
  29. }
  30. void SphereShapeDebugDisplayComponent::Reflect(AZ::ReflectContext* context)
  31. {
  32. EntityDebugDisplayComponent::Reflect(context);
  33. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  34. {
  35. serializeContext->Class<SphereShapeDebugDisplayComponent, EntityDebugDisplayComponent>()
  36. ->Version(1)
  37. ->Field("Configuration", &SphereShapeDebugDisplayComponent::m_sphereShapeConfig)
  38. ;
  39. }
  40. }
  41. void SphereShapeDebugDisplayComponent::Activate()
  42. {
  43. EntityDebugDisplayComponent::Activate();
  44. ShapeComponentNotificationsBus::Handler::BusConnect(GetEntityId());
  45. }
  46. void SphereShapeDebugDisplayComponent::Deactivate()
  47. {
  48. ShapeComponentNotificationsBus::Handler::BusDisconnect();
  49. EntityDebugDisplayComponent::Deactivate();
  50. }
  51. void SphereShapeDebugDisplayComponent::Draw(AzFramework::DebugDisplayRequests& debugDisplay)
  52. {
  53. DrawSphereShape(m_sphereShapeConfig.GetDrawParams(), m_sphereShapeConfig, debugDisplay);
  54. }
  55. bool SphereShapeDebugDisplayComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  56. {
  57. if (const auto config = azrtti_cast<const SphereShapeConfig*>(baseConfig))
  58. {
  59. m_sphereShapeConfig = *config;
  60. return true;
  61. }
  62. return false;
  63. }
  64. void SphereShapeDebugDisplayComponent::OnShapeChanged(ShapeChangeReasons changeReason)
  65. {
  66. if (changeReason == ShapeChangeReasons::ShapeChanged)
  67. {
  68. SphereShapeComponentRequestsBus::EventResult(m_sphereShapeConfig, GetEntityId(), &SphereShapeComponentRequests::GetSphereConfiguration);
  69. }
  70. }
  71. bool SphereShapeDebugDisplayComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  72. {
  73. if (auto outConfig = azrtti_cast<SphereShapeConfig*>(outBaseConfig))
  74. {
  75. *outConfig = m_sphereShapeConfig;
  76. return true;
  77. }
  78. return false;
  79. }
  80. namespace ClassConverters
  81. {
  82. static bool DeprecateSphereColliderConfiguration(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
  83. static bool DeprecateSphereColliderComponent(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
  84. }
  85. void SphereShapeConfig::Reflect(AZ::ReflectContext* context)
  86. {
  87. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  88. {
  89. // Deprecate: SphereColliderConfiguration -> SphereShapeConfig
  90. serializeContext->ClassDeprecate(
  91. "SphereColliderConfiguration",
  92. AZ::Uuid("{0319AE62-3355-4C98-873D-3139D0427A53}"),
  93. &ClassConverters::DeprecateSphereColliderConfiguration)
  94. ;
  95. serializeContext->Class<SphereShapeConfig, ShapeComponentConfig>()
  96. ->Version(2)
  97. ->Field("Radius", &SphereShapeConfig::m_radius)
  98. ->Field("TranslationOffset", &SphereShapeConfig::m_translationOffset)
  99. ;
  100. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  101. {
  102. editContext->Class<SphereShapeConfig>("Configuration", "Sphere shape configuration parameters")
  103. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  104. ->DataElement(AZ::Edit::UIHandlers::Default, &SphereShapeConfig::m_radius, "Radius", "Radius of sphere")
  105. ->Attribute(AZ::Edit::Attributes::Min, 0.f)
  106. ->Attribute(AZ::Edit::Attributes::Suffix, " m")
  107. ->Attribute(AZ::Edit::Attributes::Step, 0.05f)
  108. ->DataElement(
  109. AZ::Edit::UIHandlers::Default,
  110. &SphereShapeConfig::m_translationOffset,
  111. "Translation Offset",
  112. "Translation offset of shape relative to its entity")
  113. ->Attribute(AZ::Edit::Attributes::Suffix, " m")
  114. ->Attribute(AZ::Edit::Attributes::Step, 0.05f);
  115. }
  116. }
  117. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  118. {
  119. behaviorContext->Class<SphereShapeConfig>()
  120. ->Constructor()
  121. ->Constructor<float>()
  122. ->Property("Radius", BehaviorValueProperty(&SphereShapeConfig::m_radius))
  123. ;
  124. }
  125. }
  126. void SphereShapeComponent::Reflect(AZ::ReflectContext* context)
  127. {
  128. SphereShape::Reflect(context);
  129. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  130. {
  131. // Deprecate: SphereColliderComponent -> SphereShapeComponent
  132. serializeContext->ClassDeprecate(
  133. "SphereColliderComponent",
  134. AZ::Uuid("{99F33E4A-4EFB-403C-8918-9171D47A03A4}"),
  135. &ClassConverters::DeprecateSphereColliderComponent)
  136. ;
  137. serializeContext->Class<SphereShapeComponent, AZ::Component>()
  138. ->Version(2, &ClassConverters::UpgradeSphereShapeComponent)
  139. ->Field("SphereShape", &SphereShapeComponent::m_sphereShape)
  140. ;
  141. }
  142. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  143. {
  144. behaviorContext->Constant("SphereShapeComponentTypeId", BehaviorConstant(SphereShapeComponentTypeId));
  145. behaviorContext->EBus<SphereShapeComponentRequestsBus>("SphereShapeComponentRequestsBus")
  146. ->Event("GetSphereConfiguration", &SphereShapeComponentRequestsBus::Events::GetSphereConfiguration)
  147. ->Event("SetRadius", &SphereShapeComponentRequestsBus::Events::SetRadius)
  148. ;
  149. }
  150. }
  151. void SphereShapeComponent::Activate()
  152. {
  153. m_sphereShape.Activate(GetEntityId());
  154. }
  155. void SphereShapeComponent::Deactivate()
  156. {
  157. m_sphereShape.Deactivate();
  158. }
  159. bool SphereShapeComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  160. {
  161. if (const auto config = azrtti_cast<const SphereShapeConfig*>(baseConfig))
  162. {
  163. m_sphereShape.SetSphereConfiguration(*config);
  164. return true;
  165. }
  166. return false;
  167. }
  168. bool SphereShapeComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  169. {
  170. if (auto outConfig = azrtti_cast<SphereShapeConfig*>(outBaseConfig))
  171. {
  172. *outConfig = m_sphereShape.GetSphereConfiguration();
  173. return true;
  174. }
  175. return false;
  176. }
  177. namespace ClassConverters
  178. {
  179. static bool DeprecateSphereColliderConfiguration(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  180. {
  181. /*
  182. Old:
  183. <Class name="SphereColliderConfiguration" field="Configuration" version="1" type="{0319AE62-3355-4C98-873D-3139D0427A53}">
  184. <Class name="float" field="Radius" value="1.0000000" type="{EA2C3E90-AFBE-44D4-A90D-FAAF79BAF93D}"/>
  185. </Class>
  186. New:
  187. <Class name="SphereShapeConfig" field="Configuration" version="1" type="{4AADFD75-48A7-4F31-8F30-FE4505F09E35}">
  188. <Class name="float" field="Radius" value="1.0000000" type="{EA2C3E90-AFBE-44D4-A90D-FAAF79BAF93D}"/>
  189. </Class>
  190. */
  191. // Cache the Radius
  192. float oldRadius = 0.f;
  193. const int oldIndex = classElement.FindElement(AZ_CRC_CE("Radius"));
  194. if (oldIndex != -1)
  195. {
  196. classElement.GetSubElement(oldIndex).GetData<float>(oldRadius);
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. // Convert to SphereShapeConfig
  203. const bool result = classElement.Convert<SphereShapeConfig>(context);
  204. if (result)
  205. {
  206. const int newIndex = classElement.AddElement<float>(context, "Radius");
  207. if (newIndex != -1)
  208. {
  209. classElement.GetSubElement(newIndex).SetData<float>(context, oldRadius);
  210. return true;
  211. }
  212. return false;
  213. }
  214. return false;
  215. }
  216. static bool DeprecateSphereColliderComponent(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  217. {
  218. /*
  219. Old:
  220. <Class name="SphereColliderComponent" version="1" type="{99F33E4A-4EFB-403C-8918-9171D47A03A4}">
  221. <Class name="SphereColliderConfiguration" field="Configuration" version="1" type="{0319AE62-3355-4C98-873D-3139D0427A53}">
  222. <Class name="float" field="Radius" value="1.0000000" type="{EA2C3E90-AFBE-44D4-A90D-FAAF79BAF93D}"/>
  223. </Class>
  224. </Class>
  225. New:
  226. <Class name="SphereShapeComponent" version="1" type="{E24CBFF0-2531-4F8D-A8AB-47AF4D54BCD2}">
  227. <Class name="SphereShapeConfig" field="Configuration" version="1" type="{4AADFD75-48A7-4F31-8F30-FE4505F09E35}">
  228. <Class name="float" field="Radius" value="1.0000000" type="{EA2C3E90-AFBE-44D4-A90D-FAAF79BAF93D}"/>
  229. </Class>
  230. </Class>
  231. */
  232. // Cache the Configuration
  233. SphereShapeConfig configuration;
  234. int configIndex = classElement.FindElement(AZ_CRC_CE("Configuration"));
  235. if (configIndex != -1)
  236. {
  237. classElement.GetSubElement(configIndex).GetData<SphereShapeConfig>(configuration);
  238. }
  239. else
  240. {
  241. return false;
  242. }
  243. // Convert to SphereShapeComponent
  244. const bool result = classElement.Convert<SphereShapeComponent>(context);
  245. if (result)
  246. {
  247. configIndex = classElement.AddElement<SphereShapeConfig>(context, "Configuration");
  248. if (configIndex != -1)
  249. {
  250. classElement.GetSubElement(configIndex).SetData<SphereShapeConfig>(context, configuration);
  251. return true;
  252. }
  253. return false;
  254. }
  255. return false;
  256. }
  257. } // namespace ClassConverters
  258. } // namespace LmbrCentral