ReferenceShapeComponent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/EntityBus.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/std/parallel/shared_mutex.h>
  13. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  14. #include <LmbrCentral/Shape/ReferenceShapeComponentBus.h>
  15. namespace LmbrCentral
  16. {
  17. template<typename, typename>
  18. class EditorWrappedComponentBase;
  19. class ReferenceShapeConfig
  20. : public AZ::ComponentConfig
  21. {
  22. public:
  23. AZ_CLASS_ALLOCATOR(ReferenceShapeConfig, AZ::SystemAllocator);
  24. AZ_RTTI(ReferenceShapeConfig, "{3E49974D-2EE0-4AF9-92B9-229A22B515C3}", AZ::ComponentConfig);
  25. static void Reflect(AZ::ReflectContext* context);
  26. AZ::EntityId m_shapeEntityId;
  27. };
  28. inline constexpr AZ::TypeId ReferenceShapeComponentTypeId{ "{EB9C6DC1-900F-4CE8-AA00-81361127063A}" };
  29. /**
  30. * allows reference and reuse of shape entities
  31. */
  32. class ReferenceShapeComponent
  33. : public AZ::Component
  34. , private AZ::EntityBus::Handler
  35. , private AZ::TransformNotificationBus::Handler
  36. , private LmbrCentral::ShapeComponentNotificationsBus::Handler
  37. , private LmbrCentral::ShapeComponentRequestsBus::Handler
  38. , private ReferenceShapeRequestBus::Handler
  39. {
  40. public:
  41. template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
  42. AZ_COMPONENT(ReferenceShapeComponent, ReferenceShapeComponentTypeId);
  43. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  44. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  45. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  46. static void Reflect(AZ::ReflectContext* context);
  47. ReferenceShapeComponent(const ReferenceShapeConfig& configuration);
  48. ReferenceShapeComponent() = default;
  49. ~ReferenceShapeComponent() = default;
  50. //////////////////////////////////////////////////////////////////////////
  51. // AZ::Component interface implementation
  52. void Activate() override;
  53. void Deactivate() override;
  54. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  55. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  56. ////////////////////////////////////////////////////////////////////////
  57. // EntityEvents
  58. void OnEntityActivated(const AZ::EntityId& entityId) override;
  59. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  60. //////////////////////////////////////////////////////////////////////////
  61. // TransformNotificationBus
  62. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  63. ////////////////////////////////////////////////////////////////////////
  64. // LmbrCentral::ShapeComponentNotificationsBus
  65. void OnShapeChanged(LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons reasons) override;
  66. //////////////////////////////////////////////////////////////////////////
  67. // ShapeComponentRequestsBus
  68. AZ::Crc32 GetShapeType() const override;
  69. AZ::Aabb GetEncompassingAabb() const override;
  70. void GetTransformAndLocalBounds(AZ::Transform& transform, AZ::Aabb& bounds) const override;
  71. bool IsPointInside(const AZ::Vector3& point) const override;
  72. float DistanceFromPoint(const AZ::Vector3& point) const override;
  73. float DistanceSquaredFromPoint(const AZ::Vector3& point) const override;
  74. AZ::Vector3 GenerateRandomPointInside(AZ::RandomDistributionType randomDistribution) const override;
  75. bool IntersectRay(const AZ::Vector3& src, const AZ::Vector3& dir, float& distance) const override;
  76. protected:
  77. //////////////////////////////////////////////////////////////////////////
  78. // ReferenceShapeRequestsBus
  79. AZ::EntityId GetShapeEntityId() const override;
  80. void SetShapeEntityId(AZ::EntityId entityId) override;
  81. private:
  82. ReferenceShapeConfig m_configuration;
  83. mutable AZStd::shared_mutex m_mutex; //!< Mutex to allow multiple readers but single writer for efficient thread safety
  84. bool m_allowNotifications = true; //!< temporarily disable sending notifications to avoid redundancies
  85. bool AllowRequest() const;
  86. bool AllowNotification() const;
  87. void SetupDependencies();
  88. };
  89. }