SplinePath.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  33. {
  34. /// Spline for creating smooth movement based on Speed along a set of Control Points modified by the Interpolation Mode.
  35. class ATOMIC_API SplinePath : public Component
  36. {
  37. ATOMIC_OBJECT(SplinePath, Component)
  38. public:
  39. /// Construct an Empty SplinePath.
  40. SplinePath(Context* context);
  41. /// Destructor.
  42. virtual ~SplinePath() { };
  43. /// Register object factory.
  44. static void RegisterObject(Context* context);
  45. /// Apply Attributes to the SplinePath.
  46. virtual void ApplyAttributes();
  47. /// Draw the Debug Geometry.
  48. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  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. void SetInterpolationMode(InterpolationMode interpolationMode);
  57. /// Set the movement Speed.
  58. void SetSpeed(float speed) { speed_ = speed; }
  59. /// Set the controlled Node's position on the SplinePath.
  60. void SetPosition(float factor);
  61. /// Set the Node to be moved along the SplinePath.
  62. void SetControlledNode(Node* controlled);
  63. /// Get the Interpolation Mode.
  64. InterpolationMode GetInterpolationMode() const { return spline_.GetInterpolationMode(); }
  65. /// Get the movement Speed.
  66. float GetSpeed() const { return speed_; }
  67. /// Get the length of SplinePath;
  68. float GetLength() const { return length_; }
  69. /// Get the parent Node's last position on the spline.
  70. Vector3 GetPosition() const { return GetPoint(traveled_); }
  71. /// Get the controlled Node.
  72. Node* GetControlledNode() const { return controlledNode_; }
  73. /// Get a point on the SplinePath from 0.f to 1.f where 0 is the start and 1 is the end.
  74. Vector3 GetPoint(float factor) const;
  75. /// Move the controlled Node to the next position along the SplinePath based off the Speed value.
  76. void Move(float timeStep);
  77. /// Reset movement along the path.
  78. void Reset();
  79. /// Returns whether the movement along the SplinePath is complete.
  80. bool IsFinished() const { return traveled_ >= 1.0f; }
  81. /// Set Control Point Node IDs attribute.
  82. void SetControlPointIdsAttr(const VariantVector& value);
  83. /// Return Control Point Node IDs attribute.
  84. const VariantVector& GetControlPointIdsAttr() const { return controlPointIdsAttr_; }
  85. /// Set Controlled Node ID attribute.
  86. void SetControlledIdAttr(unsigned value);
  87. /// Get Controlled Node ID attribute.
  88. unsigned GetControlledIdAttr() const { return controlledIdAttr_; }
  89. protected:
  90. /// Listener to manage Control Point movement.
  91. virtual void OnMarkedDirty(Node* point);
  92. /// Listener to manage Control Point enabling.
  93. virtual void OnNodeSetEnabled(Node* point);
  94. private:
  95. /// Update the Node IDs of the Control Points.
  96. void UpdateNodeIds();
  97. /// Calculate the length of the SplinePath. Used for movement calculations.
  98. void CalculateLength();
  99. /// The Control Points of the Spline.
  100. Spline spline_;
  101. /// The Speed of movement along the Spline.
  102. float speed_;
  103. /// Amount of time that has elapsed while moving.
  104. float elapsedTime_;
  105. /// The fraction of the SplinePath covered.
  106. float traveled_;
  107. /// The length of the SplinePath.
  108. float length_;
  109. /// Whether the Control Point IDs are dirty.
  110. bool dirty_;
  111. /// Node to be moved along the SplinePath.
  112. WeakPtr<Node> controlledNode_;
  113. /// Control Points for the SplinePath.
  114. Vector<WeakPtr<Node> > controlPoints_;
  115. /// Control Point ID's for the SplinePath.
  116. mutable VariantVector controlPointIdsAttr_;
  117. /// Controlled ID for the SplinePath.
  118. mutable unsigned controlledIdAttr_;
  119. };
  120. }