SplineComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/TransformBus.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <LmbrCentral/Shape/SplineComponentBus.h>
  13. namespace LmbrCentral
  14. {
  15. /// Common functionality and data for the SplineComponent.
  16. class SplineCommon
  17. {
  18. public:
  19. AZ_CLASS_ALLOCATOR(SplineCommon, AZ::SystemAllocator);
  20. AZ_RTTI(SplineCommon, "{91A31D7E-F63A-4AA8-BC50-909B37F0AD8B}");
  21. SplineCommon();
  22. virtual ~SplineCommon() = default;
  23. static void Reflect(AZ::ReflectContext* context);
  24. void ChangeSplineType(SplineType splineType);
  25. /// Override callbacks to be used when spline changes/is modified.
  26. void SetCallbacks(
  27. const AZ::IndexFunction& OnAddVertex, const AZ::IndexFunction& OnRemoveVertex,
  28. const AZ::IndexFunction& OnUpdateVertex, const AZ::VoidFunction& OnSetVertices,
  29. const AZ::VoidFunction& OnClearVertices, const AZ::VoidFunction& OnChangeType,
  30. const AZ::BoolFunction& OnOpenClose);
  31. AZ::SplinePtr m_spline; ///< Reference to the underlying spline data.
  32. private:
  33. AZ::u32 OnChangeSplineType();
  34. SplineType m_splineType = SplineType::LINEAR; ///< The currently set spline type (default to Linear).
  35. AZ::IndexFunction m_onAddVertex = nullptr;
  36. AZ::IndexFunction m_onRemoveVertex = nullptr;
  37. AZ::IndexFunction m_onUpdateVertex = nullptr;
  38. AZ::VoidFunction m_onSetVertices = nullptr;
  39. AZ::VoidFunction m_onClearVertices = nullptr;
  40. AZ::VoidFunction m_onChangeType = nullptr;
  41. AZ::BoolFunction m_onOpenCloseChange = nullptr;
  42. };
  43. /// Component interface to core spline implementation.
  44. class SplineComponent
  45. : public AZ::Component
  46. , private SplineComponentRequestBus::Handler
  47. , private AZ::TransformNotificationBus::Handler
  48. {
  49. public:
  50. friend class EditorSplineComponent;
  51. AZ_COMPONENT(SplineComponent, "{F0905297-1E24-4044-BFDA-BDE3583F1E57}");
  52. // AZ::Component
  53. void Activate() override;
  54. void Deactivate() override;
  55. // SplineComponentRequestBus
  56. AZ::SplinePtr GetSpline() override;
  57. void ChangeSplineType(SplineType splineType) override;
  58. void SetClosed(bool closed) override;
  59. // SplineComponentRequestBus/VertexContainerInterface
  60. bool GetVertex(size_t index, AZ::Vector3& vertex) const override;
  61. void AddVertex(const AZ::Vector3& vertex) override;
  62. bool UpdateVertex(size_t index, const AZ::Vector3& vertex) override;
  63. bool InsertVertex(size_t index, const AZ::Vector3& vertex) override;
  64. bool RemoveVertex(size_t index) override;
  65. void SetVertices(const AZStd::vector<AZ::Vector3>& vertices) override;
  66. void ClearVertices() override;
  67. size_t Size() const override;
  68. bool Empty() const override;
  69. // TransformNotificationBus
  70. void OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world) override;
  71. protected:
  72. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  73. {
  74. provided.push_back(AZ_CRC_CE("SplineService"));
  75. provided.push_back(AZ_CRC_CE("VariableVertexContainerService"));
  76. provided.push_back(AZ_CRC_CE("FixedVertexContainerService"));
  77. }
  78. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  79. {
  80. incompatible.push_back(AZ_CRC_CE("SplineService"));
  81. incompatible.push_back(AZ_CRC_CE("VariableVertexContainerService"));
  82. incompatible.push_back(AZ_CRC_CE("FixedVertexContainerService"));
  83. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  84. }
  85. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  86. {
  87. required.push_back(AZ_CRC_CE("TransformService"));
  88. }
  89. static void Reflect(AZ::ReflectContext* context);
  90. private:
  91. SplineCommon m_splineCommon; ///< Stores common spline functionality and properties.
  92. AZ::Transform m_currentTransform; ///< Caches the current transform for the entity on which this component lives.
  93. };
  94. } // namespace LmbrCentral