SplinePath.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Ptr.h"
  24. #include "../Core/Variant.h"
  25. #include "../Container/Vector.h"
  26. #include "../Core/Spline.h"
  27. #include "../Graphics/DebugRenderer.h"
  28. #include "../Math/MathDefs.h"
  29. #include "../Math/Vector3.h"
  30. #include "../Scene/Component.h"
  31. #include "../Scene/Node.h"
  32. namespace Urho3D
  33. {
  34. /// Spline for creating smooth movement based on Speed along a set of Control Points modified by the Interpolation Mode.
  35. class URHO3D_API SplinePath : public Component
  36. {
  37. URHO3D_OBJECT(SplinePath, Component);
  38. public:
  39. /// Construct an Empty SplinePath.
  40. explicit SplinePath(Context* context);
  41. /// Destructor.
  42. ~SplinePath() override = default;
  43. /// Register object factory.
  44. static void RegisterObject(Context* context);
  45. /// Apply Attributes to the SplinePath.
  46. void ApplyAttributes() override;
  47. /// Draw the Debug Geometry.
  48. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  49. /// Add a Node to the SplinePath as a Control Point.
  50. void AddControlPoint(Node* point, unsigned index = M_MAX_UNSIGNED);
  51. /// Remove a Node Control Point from the SplinePath.
  52. void RemoveControlPoint(Node* point);
  53. /// Clear the Control Points from the SplinePath.
  54. void ClearControlPoints();
  55. /// Set the Interpolation Mode.
  56. /// @property
  57. void SetInterpolationMode(InterpolationMode interpolationMode);
  58. /// Set the movement Speed.
  59. /// @property
  60. void SetSpeed(float speed) { speed_ = speed; }
  61. /// Set the controlled Node's position on the SplinePath.
  62. void SetPosition(float factor);
  63. /// Set the Node to be moved along the SplinePath.
  64. /// @property
  65. void SetControlledNode(Node* controlled);
  66. /// Get the Interpolation Mode.
  67. /// @property
  68. InterpolationMode GetInterpolationMode() const { return spline_.GetInterpolationMode(); }
  69. /// Get the movement Speed.
  70. /// @property
  71. float GetSpeed() const { return speed_; }
  72. /// Get the length of SplinePath.
  73. /// @property
  74. float GetLength() const { return length_; }
  75. /// Get the parent Node's last position on the spline.
  76. Vector3 GetPosition() const { return GetPoint(traveled_); }
  77. /// Get the controlled Node.
  78. /// @property
  79. Node* GetControlledNode() const { return controlledNode_; }
  80. /// Get a point on the SplinePath from 0.f to 1.f where 0 is the start and 1 is the end.
  81. Vector3 GetPoint(float factor) const;
  82. /// Move the controlled Node to the next position along the SplinePath based off the Speed value.
  83. void Move(float timeStep);
  84. /// Reset movement along the path.
  85. void Reset();
  86. /// Returns whether the movement along the SplinePath is complete.
  87. /// @property{get_isFinished}
  88. bool IsFinished() const { return traveled_ >= 1.0f; }
  89. /// Set Control Point Node IDs attribute.
  90. void SetControlPointIdsAttr(const VariantVector& value);
  91. /// Return Control Point Node IDs attribute.
  92. const VariantVector& GetControlPointIdsAttr() const { return controlPointIdsAttr_; }
  93. /// Set Controlled Node ID attribute.
  94. void SetControlledIdAttr(unsigned value);
  95. /// Get Controlled Node ID attribute.
  96. unsigned GetControlledIdAttr() const { return controlledIdAttr_; }
  97. protected:
  98. /// Listener to manage Control Point movement.
  99. void OnMarkedDirty(Node* point) override;
  100. /// Listener to manage Control Point enabling.
  101. void OnNodeSetEnabled(Node* point) override;
  102. private:
  103. /// Update the Node IDs of the Control Points.
  104. void UpdateNodeIds();
  105. /// Calculate the length of the SplinePath. Used for movement calculations.
  106. void CalculateLength();
  107. /// The Control Points of the Spline.
  108. Spline spline_;
  109. /// The Speed of movement along the Spline.
  110. float speed_;
  111. /// Amount of time that has elapsed while moving.
  112. float elapsedTime_;
  113. /// The fraction of the SplinePath covered.
  114. float traveled_;
  115. /// The length of the SplinePath.
  116. float length_;
  117. /// Whether the Control Point IDs are dirty.
  118. bool dirty_;
  119. /// Node to be moved along the SplinePath.
  120. WeakPtr<Node> controlledNode_;
  121. /// Control Points for the SplinePath.
  122. Vector<WeakPtr<Node> > controlPoints_;
  123. /// Control Point ID's for the SplinePath.
  124. mutable VariantVector controlPointIdsAttr_;
  125. /// Controlled ID for the SplinePath.
  126. mutable unsigned controlledIdAttr_;
  127. };
  128. }