SplinePath.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/Ptr.h"
  5. #include "../Core/Variant.h"
  6. #include "../Container/Vector.h"
  7. #include "../Core/Spline.h"
  8. #include "../Graphics/DebugRenderer.h"
  9. #include "../Math/MathDefs.h"
  10. #include "../Math/Vector3.h"
  11. #include "../Scene/Component.h"
  12. #include "../Scene/Node.h"
  13. namespace Urho3D
  14. {
  15. /// Spline for creating smooth movement based on Speed along a set of Control Points modified by the Interpolation Mode.
  16. class URHO3D_API SplinePath : public Component
  17. {
  18. URHO3D_OBJECT(SplinePath, Component);
  19. public:
  20. /// Construct an Empty SplinePath.
  21. explicit SplinePath(Context* context);
  22. /// Destructor.
  23. ~SplinePath() override = default;
  24. /// Register object factory.
  25. /// @nobind
  26. static void RegisterObject(Context* context);
  27. /// Apply Attributes to the SplinePath.
  28. void ApplyAttributes() override;
  29. /// Draw the Debug Geometry.
  30. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  31. /// Add a Node to the SplinePath as a Control Point.
  32. void AddControlPoint(Node* point, unsigned index = M_MAX_UNSIGNED);
  33. /// Remove a Node Control Point from the SplinePath.
  34. void RemoveControlPoint(Node* point);
  35. /// Clear the Control Points from the SplinePath.
  36. void ClearControlPoints();
  37. /// Set the Interpolation Mode.
  38. /// @property
  39. void SetInterpolationMode(InterpolationMode interpolationMode);
  40. /// Set the movement Speed.
  41. /// @property
  42. void SetSpeed(float speed) { speed_ = speed; }
  43. /// Set the controlled Node's position on the SplinePath.
  44. void SetPosition(float factor);
  45. /// Set the Node to be moved along the SplinePath.
  46. /// @property
  47. void SetControlledNode(Node* controlled);
  48. /// Get the Interpolation Mode.
  49. /// @property
  50. InterpolationMode GetInterpolationMode() const { return spline_.GetInterpolationMode(); }
  51. /// Get the movement Speed.
  52. /// @property
  53. float GetSpeed() const { return speed_; }
  54. /// Get the length of SplinePath.
  55. /// @property
  56. float GetLength() const { return length_; }
  57. /// Get the parent Node's last position on the spline.
  58. Vector3 GetPosition() const { return GetPoint(traveled_); }
  59. /// Get the controlled Node.
  60. /// @property
  61. Node* GetControlledNode() const { return controlledNode_; }
  62. /// Get a point on the SplinePath from 0.f to 1.f where 0 is the start and 1 is the end.
  63. Vector3 GetPoint(float factor) const;
  64. /// Move the controlled Node to the next position along the SplinePath based off the Speed value.
  65. void Move(float timeStep);
  66. /// Reset movement along the path.
  67. void Reset();
  68. /// Returns whether the movement along the SplinePath is complete.
  69. /// @property{get_isFinished}
  70. bool IsFinished() const { return traveled_ >= 1.0f; }
  71. /// Set Control Point Node IDs attribute.
  72. void SetControlPointIdsAttr(const VariantVector& value);
  73. /// Return Control Point Node IDs attribute.
  74. const VariantVector& GetControlPointIdsAttr() const { return controlPointIdsAttr_; }
  75. /// Set Controlled Node ID attribute.
  76. void SetControlledIdAttr(unsigned value);
  77. /// Get Controlled Node ID attribute.
  78. unsigned GetControlledIdAttr() const { return controlledIdAttr_; }
  79. protected:
  80. /// Listener to manage Control Point movement.
  81. void OnMarkedDirty(Node* point) override;
  82. /// Listener to manage Control Point enabling.
  83. void OnNodeSetEnabled(Node* point) override;
  84. private:
  85. /// Update the Node IDs of the Control Points.
  86. void UpdateNodeIds();
  87. /// Calculate the length of the SplinePath. Used for movement calculations.
  88. void CalculateLength();
  89. /// The Control Points of the Spline.
  90. Spline spline_;
  91. /// The Speed of movement along the Spline.
  92. float speed_;
  93. /// Amount of time that has elapsed while moving.
  94. float elapsedTime_;
  95. /// The fraction of the SplinePath covered.
  96. float traveled_;
  97. /// The length of the SplinePath.
  98. float length_;
  99. /// Whether the Control Point IDs are dirty.
  100. bool dirty_;
  101. /// Node to be moved along the SplinePath.
  102. WeakPtr<Node> controlledNode_;
  103. /// Control Points for the SplinePath.
  104. Vector<WeakPtr<Node>> controlPoints_;
  105. /// Control Point ID's for the SplinePath.
  106. mutable VariantVector controlPointIdsAttr_;
  107. /// Controlled ID for the SplinePath.
  108. mutable unsigned controlledIdAttr_;
  109. };
  110. }