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