CCActionCatmullRom.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2008 Radu Gruian
  3. * Copyright (c) 2011 Vit Valentin
  4. * Copyright (c) 2012 cocos2d-x.org
  5. * Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. *
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. *
  28. * Original code by Radu Gruian: http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio.So
  29. *
  30. * Adapted to cocos2d-x by Vit Valentin
  31. *
  32. * Adapted from cocos2d-x to cocos2d-iphone by Ricardo Quesada
  33. */
  34. #ifndef __CCACTION_CATMULLROM_H__
  35. #define __CCACTION_CATMULLROM_H__
  36. #include <vector>
  37. #include "2d/CCActionInterval.h"
  38. #include "math/CCGeometry.h"
  39. NS_CC_BEGIN;
  40. class Node;
  41. /**
  42. * @addtogroup actions
  43. * @{
  44. */
  45. /** An Array that contain control points.
  46. * Used by CardinalSplineTo and (By) and CatmullRomTo (and By) actions.
  47. * @ingroup Actions
  48. * @js NA
  49. */
  50. class CC_DLL PointArray : public Ref, public Clonable
  51. {
  52. public:
  53. /** Creates and initializes a Points array with capacity.
  54. * @js NA
  55. * @param capacity The size of the array.
  56. */
  57. static PointArray* create(ssize_t capacity);
  58. /**
  59. * @js NA
  60. * @lua NA
  61. */
  62. virtual ~PointArray();
  63. /**
  64. * @js NA
  65. * @lua NA
  66. */
  67. PointArray();
  68. /** Initializes a Catmull Rom config with a capacity hint.
  69. *
  70. * @js NA
  71. * @param capacity The size of the array.
  72. * @return True.
  73. */
  74. bool initWithCapacity(ssize_t capacity);
  75. /** Appends a control point.
  76. *
  77. * @js NA
  78. * @param controlPoint A control point.
  79. */
  80. void addControlPoint(const Vec2& controlPoint);
  81. /** Inserts a controlPoint at index.
  82. *
  83. * @js NA
  84. * @param controlPoint A control point.
  85. * @param index Insert the point to array in index.
  86. */
  87. void insertControlPoint(const Vec2& controlPoint, ssize_t index);
  88. /** Replaces an existing controlPoint at index.
  89. *
  90. * @js NA
  91. * @param controlPoint A control point.
  92. * @param index Replace the point to array in index.
  93. */
  94. void replaceControlPoint(const Vec2& controlPoint, ssize_t index);
  95. /** Get the value of a controlPoint at a given index.
  96. *
  97. * @js NA
  98. * @param index Get the point in index.
  99. * @return A Vec2.
  100. */
  101. const Vec2& getControlPointAtIndex(ssize_t index) const;
  102. /** Deletes a control point at a given index
  103. *
  104. * @js NA
  105. * @param index Remove the point in index.
  106. */
  107. void removeControlPointAtIndex(ssize_t index);
  108. /** Returns the number of objects of the control point array.
  109. *
  110. * @js NA
  111. * @return The number of objects of the control point array.
  112. */
  113. ssize_t count() const;
  114. /** Returns a new copy of the array reversed. User is responsible for releasing this copy.
  115. *
  116. * @js NA
  117. * @return A new copy of the array reversed.
  118. */
  119. PointArray* reverse() const;
  120. /** Reverse the current control point array inline, without generating a new one.
  121. * @js NA
  122. */
  123. void reverseInline();
  124. /**
  125. * @js NA
  126. * @lua NA
  127. */
  128. virtual PointArray* clone() const;
  129. /**
  130. * @js NA
  131. */
  132. const std::vector<Vec2>& getControlPoints() const;
  133. /**
  134. * @js NA
  135. */
  136. void setControlPoints(std::vector<Vec2> controlPoints);
  137. private:
  138. /** Array that contains the control points. */
  139. std::vector<Vec2> _controlPoints;
  140. };
  141. /** @class CardinalSplineTo
  142. * Cardinal Spline path.
  143. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
  144. * @ingroup Actions
  145. */
  146. class CC_DLL CardinalSplineTo : public ActionInterval
  147. {
  148. public:
  149. /** Creates an action with a Cardinal Spline array of points and tension.
  150. * @param duration In seconds.
  151. * @param points An PointArray.
  152. * @param tension Goodness of fit.
  153. * @code
  154. * When this function bound to js or lua,the input params are changed.
  155. * In js: var create(var t,var table)
  156. * In lua: local create(local t, local table)
  157. * @endcode
  158. */
  159. static CardinalSplineTo* create(float duration, PointArray* points, float tension);
  160. /**
  161. * @js NA
  162. * @lua NA
  163. */
  164. virtual ~CardinalSplineTo();
  165. /**
  166. * @js ctor
  167. * @lua NA
  168. */
  169. CardinalSplineTo();
  170. /**
  171. * Initializes the action with a duration and an array of points.
  172. *
  173. * @param duration In seconds.
  174. * @param points An PointArray.
  175. * @param tension Goodness of fit.
  176. */
  177. bool initWithDuration(float duration, PointArray* points, float tension);
  178. /** It will update the target position and change the _previousPosition to newPos
  179. *
  180. * @param newPos The new position.
  181. */
  182. virtual void updatePosition(const Vec2 &newPos);
  183. /** Return a PointArray.
  184. *
  185. * @return A PointArray.
  186. */
  187. PointArray* getPoints() { return _points; }
  188. /**
  189. * @js NA
  190. * @lua NA
  191. */
  192. void setPoints(PointArray* points)
  193. {
  194. CC_SAFE_RETAIN(points);
  195. CC_SAFE_RELEASE(_points);
  196. _points = points;
  197. }
  198. // Overrides
  199. virtual CardinalSplineTo *clone() const override;
  200. virtual CardinalSplineTo* reverse() const override;
  201. virtual void startWithTarget(Node *target) override;
  202. /**
  203. * @param time In seconds.
  204. */
  205. virtual void update(float time) override;
  206. protected:
  207. /** Array of control points */
  208. PointArray *_points;
  209. float _deltaT;
  210. float _tension;
  211. Vec2 _previousPosition;
  212. Vec2 _accumulatedDiff;
  213. };
  214. /** @class CardinalSplineBy
  215. * Cardinal Spline path.
  216. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
  217. * @ingroup Actions
  218. */
  219. class CC_DLL CardinalSplineBy : public CardinalSplineTo
  220. {
  221. public:
  222. /** Creates an action with a Cardinal Spline array of points and tension.
  223. * @code
  224. * When this function bound to js or lua,the input params are changed.
  225. * In js: var create(var t,var table).
  226. * In lua: local create(local t, local table).
  227. * @param duration In seconds.
  228. * @param point An PointArray.
  229. * @param tension Goodness of fit.
  230. * @endcode
  231. */
  232. static CardinalSplineBy* create(float duration, PointArray* points, float tension);
  233. CardinalSplineBy();
  234. // Overrides
  235. virtual void startWithTarget(Node *target) override;
  236. virtual void updatePosition(const Vec2 &newPos) override;
  237. virtual CardinalSplineBy *clone() const override;
  238. virtual CardinalSplineBy* reverse() const override;
  239. protected:
  240. Vec2 _startPosition;
  241. };
  242. /** @class CatmullRomTo
  243. * An action that moves the target with a CatmullRom curve to a destination point.
  244. * A Catmull Rom is a Cardinal Spline with a tension of 0.5.
  245. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
  246. * @ingroup Actions
  247. */
  248. class CC_DLL CatmullRomTo : public CardinalSplineTo
  249. {
  250. public:
  251. /** Creates an action with a Cardinal Spline array of points and tension.
  252. * @param dt In seconds.
  253. * @param points An PointArray.
  254. * @code
  255. * When this function bound to js or lua,the input params are changed.
  256. * In js: var create(var dt,var table).
  257. * In lua: local create(local dt, local table).
  258. * @endcode
  259. */
  260. static CatmullRomTo* create(float dt, PointArray* points);
  261. /**
  262. * Initializes the action with a duration and an array of points.
  263. *
  264. * @param dt In seconds.
  265. * @param points An PointArray.
  266. */
  267. bool initWithDuration(float dt, PointArray* points);
  268. // Override
  269. virtual CatmullRomTo *clone() const override;
  270. virtual CatmullRomTo *reverse() const override;
  271. };
  272. /** @class CatmullRomBy
  273. * An action that moves the target with a CatmullRom curve by a certain distance.
  274. * A Catmull Rom is a Cardinal Spline with a tension of 0.5.
  275. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
  276. * @ingroup Actions
  277. */
  278. class CC_DLL CatmullRomBy : public CardinalSplineBy
  279. {
  280. public:
  281. /** Creates an action with a Cardinal Spline array of points and tension.
  282. * @param dt In seconds.
  283. * @param points An PointArray.
  284. * @code
  285. * When this function bound to js or lua,the input params are changed.
  286. * In js: var create(var dt,var table).
  287. * In lua: local create(local dt, local table).
  288. * @endcode
  289. */
  290. static CatmullRomBy* create(float dt, PointArray* points);
  291. /** Initializes the action with a duration and an array of points.
  292. *
  293. * @param dt In seconds.
  294. * @param points An PointArray.
  295. */
  296. bool initWithDuration(float dt, PointArray* points);
  297. // Override
  298. virtual CatmullRomBy *clone() const override;
  299. virtual CatmullRomBy *reverse() const override;
  300. };
  301. /** Returns the Cardinal Spline position for a given set of control points, tension and time */
  302. extern CC_DLL Vec2 ccCardinalSplineAt(const Vec2 &p0, const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, float tension, float t);
  303. // end of actions group
  304. /// @}
  305. NS_CC_END;
  306. #endif // __CCACTION_CATMULLROM_H__