axmol/core/2d/CCActionCatmullRom.h

346 lines
9.7 KiB
C
Raw Normal View History

2012-06-08 13:55:28 +08:00
/*
* 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.
2012-06-08 13:55:28 +08:00
*
*
* 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.
*
*
2021-12-25 10:04:45 +08:00
* Original code by Radu Gruian:
* http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio.So
2012-06-08 13:55:28 +08:00
*
* 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"
2021-10-23 23:27:14 +08:00
#include "math/CCMath.h"
2012-06-08 13:55:28 +08:00
NS_AX_BEGIN;
2012-06-08 13:55:28 +08:00
2014-08-26 18:19:28 +08:00
class Node;
2012-06-20 18:09:11 +08:00
/**
* @addtogroup actions
* @{
*/
2012-06-08 13:55:28 +08:00
/** An Array that contain control points.
* Used by CardinalSplineTo and (By) and CatmullRomTo (and By) actions.
* @ingroup Actions
* @js NA
2012-06-08 13:55:28 +08:00
*/
class CC_DLL PointArray : public Ref, public Clonable
2012-06-08 13:55:28 +08:00
{
public:
/** Creates and initializes a Points array with capacity.
* @js NA
* @param capacity The size of the array.
*/
2013-12-27 15:06:16 +08:00
static PointArray* create(ssize_t capacity);
2014-11-18 10:11:17 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~PointArray();
/**
* @js NA
* @lua NA
*/
PointArray();
2014-11-18 10:11:17 +08:00
/** Initializes a Catmull Rom config with a capacity hint.
*
* @js NA
* @param capacity The size of the array.
* @return True.
*/
2013-12-27 15:06:16 +08:00
bool initWithCapacity(ssize_t capacity);
2014-11-18 10:11:17 +08:00
/** Appends a control point.
*
* @js NA
* @param controlPoint A control point.
*/
2016-05-17 12:17:56 +08:00
void addControlPoint(const Vec2& controlPoint);
2014-11-18 10:11:17 +08:00
/** 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);
2014-11-18 10:11:17 +08:00
/** 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);
2014-11-18 10:11:17 +08:00
/** 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;
2014-11-18 10:11:17 +08:00
/** Deletes a control point at a given index
*
* @js NA
* @param index Remove the point in index.
*/
2013-12-27 15:06:16 +08:00
void removeControlPointAtIndex(ssize_t index);
2014-11-18 10:11:17 +08:00
/** Returns the number of objects of the control point array.
*
* @js NA
* @return The number of objects of the control point array.
*/
2013-12-27 15:06:16 +08:00
ssize_t count() const;
2014-11-18 10:11:17 +08:00
/** 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;
2014-11-18 10:11:17 +08:00
/** Reverse the current control point array inline, without generating a new one.
* @js NA
*/
2012-06-08 13:55:28 +08:00
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);
2021-12-25 10:04:45 +08:00
2012-06-08 13:55:28 +08:00
private:
/** Array that contains the control points. */
std::vector<Vec2> _controlPoints;
2012-06-08 13:55:28 +08:00
};
/** @class CardinalSplineTo
* Cardinal Spline path.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
* @ingroup Actions
2012-06-08 13:55:28 +08:00
*/
class CC_DLL CardinalSplineTo : public ActionInterval
2012-06-08 13:55:28 +08:00
{
public:
/** Creates an action with a Cardinal Spline array of points and tension.
* @param duration In seconds.
2015-03-27 11:39:31 +08:00
* @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)
2015-12-03 20:00:51 +08:00
* 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();
2014-11-18 10:11:17 +08:00
2021-12-25 10:04:45 +08:00
/**
* Initializes the action with a duration and an array of points.
*
* @param duration In seconds.
2015-03-27 11:39:31 +08:00
* @param points An PointArray.
* @param tension Goodness of fit.
2015-01-20 15:41:59 +08:00
*/
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.
*/
2021-12-25 10:04:45 +08:00
virtual void updatePosition(const Vec2& newPos);
/** Return a PointArray.
*
* @return A PointArray.
*/
PointArray* getPoints() { return _points; }
/**
* @js NA
* @lua NA
*/
void setPoints(PointArray* points)
2012-06-08 13:55:28 +08:00
{
CC_SAFE_RETAIN(points);
CC_SAFE_RELEASE(_points);
_points = points;
2012-06-08 13:55:28 +08:00
}
// Overrides
2021-12-25 10:04:45 +08:00
virtual CardinalSplineTo* clone() const override;
virtual CardinalSplineTo* reverse() const override;
2021-12-25 10:04:45 +08:00
virtual void startWithTarget(Node* target) override;
2015-01-20 15:41:59 +08:00
/**
* @param time In seconds.
2015-01-20 15:41:59 +08:00
*/
virtual void update(float time) override;
2012-06-08 13:55:28 +08:00
protected:
/** Array of control points */
2021-12-25 10:04:45 +08:00
PointArray* _points;
float _deltaT;
float _tension;
Vec2 _previousPosition;
Vec2 _accumulatedDiff;
2012-06-08 13:55:28 +08:00
};
/** @class CardinalSplineBy
* Cardinal Spline path.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
* @ingroup Actions
2012-06-08 13:55:28 +08:00
*/
2014-11-18 10:11:17 +08:00
class CC_DLL CardinalSplineBy : public CardinalSplineTo
2012-06-08 13:55:28 +08:00
{
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).
2015-12-03 20:00:51 +08:00
* 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();
2014-11-18 10:11:17 +08:00
// Overrides
2021-12-25 10:04:45 +08:00
virtual void startWithTarget(Node* target) override;
virtual void updatePosition(const Vec2& newPos) override;
virtual CardinalSplineBy* clone() const override;
virtual CardinalSplineBy* reverse() const override;
2012-06-08 13:55:28 +08:00
protected:
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 _startPosition;
2012-06-08 13:55:28 +08:00
};
/** @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
2012-06-08 13:55:28 +08:00
*/
class CC_DLL CatmullRomTo : public CardinalSplineTo
2012-06-08 13:55:28 +08:00
{
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).
2015-12-03 20:00:51 +08:00
* In lua: local create(local dt, local table).
* @endcode
*/
static CatmullRomTo* create(float dt, PointArray* points);
2021-12-25 10:04:45 +08:00
/**
* Initializes the action with a duration and an array of points.
*
* @param dt In seconds.
* @param points An PointArray.
2015-01-20 15:41:59 +08:00
*/
bool initWithDuration(float dt, PointArray* points);
// Override
2021-12-25 10:04:45 +08:00
virtual CatmullRomTo* clone() const override;
virtual CatmullRomTo* reverse() const override;
2012-06-08 13:55:28 +08:00
};
/** @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
2012-06-08 13:55:28 +08:00
*/
class CC_DLL CatmullRomBy : public CardinalSplineBy
2012-06-08 13:55:28 +08:00
{
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).
2015-12-03 20:00:51 +08:00
* 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
2021-12-25 10:04:45 +08:00
virtual CatmullRomBy* clone() const override;
virtual CatmullRomBy* reverse() const override;
2012-06-08 13:55:28 +08:00
};
/** Returns the Cardinal Spline position for a given set of control points, tension and time */
2021-12-25 10:04:45 +08:00
extern CC_DLL Vec2
ccCardinalSplineAt(const Vec2& p0, const Vec2& p1, const Vec2& p2, const Vec2& p3, float tension, float t);
2012-06-08 13:55:28 +08:00
2012-06-20 18:09:11 +08:00
// end of actions group
/// @}
NS_AX_END;
2012-06-08 13:55:28 +08:00
2021-12-25 10:04:45 +08:00
#endif // __CCACTION_CATMULLROM_H__