axmol/cocos/2d/CCActionCatmullRom.h

286 lines
8.1 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-2014 Chukong Technologies Inc.
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.
*
*
* 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>
2012-06-08 13:55:28 +08:00
#include "CCActionInterval.h"
#include "CCNode.h"
#include "CCGeometry.h"
2012-06-08 13:55:28 +08:00
NS_CC_BEGIN;
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.
2012-06-20 18:09:11 +08:00
@ingroup Actions
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
*/
2013-12-27 15:06:16 +08:00
static PointArray* create(ssize_t capacity);
/**
* @js NA
* @lua NA
*/
virtual ~PointArray();
/**
* @js NA
* @lua NA
*/
PointArray();
2012-06-08 13:55:28 +08:00
/** initializes a Catmull Rom config with a capacity hint
* @js NA
*/
2013-12-27 15:06:16 +08:00
bool initWithCapacity(ssize_t capacity);
2012-06-08 13:55:28 +08:00
/** appends a control point
* @js NA
*/
2014-04-15 18:08:29 +08:00
void addControlPoint(Vector2 controlPoint);
2012-06-08 13:55:28 +08:00
/** inserts a controlPoint at index
* @js NA
*/
2014-04-15 18:08:29 +08:00
void insertControlPoint(Vector2 &controlPoint, ssize_t index);
2012-06-08 13:55:28 +08:00
/** replaces an existing controlPoint at index
* @js NA
*/
2014-04-15 18:08:29 +08:00
void replaceControlPoint(Vector2 &controlPoint, ssize_t index);
2012-06-08 13:55:28 +08:00
/** get the value of a controlPoint at a given index
* @js NA
*/
2014-04-15 18:08:29 +08:00
Vector2 getControlPointAtIndex(ssize_t index);
2012-06-08 13:55:28 +08:00
/** deletes a control point at a given index
* @js NA
*/
2013-12-27 15:06:16 +08:00
void removeControlPointAtIndex(ssize_t index);
2012-06-08 13:55:28 +08:00
/** returns the number of objects of the control point array
* @js NA
*/
2013-12-27 15:06:16 +08:00
ssize_t count() const;
2012-06-08 13:55:28 +08:00
/** returns a new copy of the array reversed. User is responsible for releasing this copy
* @js NA
*/
PointArray* reverse() const;
2012-06-08 13:55:28 +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
*/
2014-04-15 18:08:29 +08:00
const std::vector<Vector2*>* getControlPoints() const;
/**
* @js NA
*/
2014-04-15 18:08:29 +08:00
void setControlPoints(std::vector<Vector2*> *controlPoints);
2012-06-08 13:55:28 +08:00
private:
/** Array that contains the control points */
2014-04-15 18:08:29 +08:00
std::vector<Vector2*> *_controlPoints;
2012-06-08 13:55:28 +08:00
};
/** Cardinal Spline path.
http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
2012-06-20 18:09:11 +08:00
@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
* @code
* when this function bound to js or lua,the input params are changed
* in js: var create(var t,var table)
* in lua: lcaol create(local t, local table)
* @endcode
*/
static CardinalSplineTo* create(float duration, PointArray* points, float tension);
/**
* @js NA
* @lua NA
*/
virtual ~CardinalSplineTo();
/**
* @js NA
* @lua NA
*/
CardinalSplineTo();
2012-06-08 13:55:28 +08:00
/** initializes the action with a duration and an array of points */
bool initWithDuration(float duration, PointArray* points, float tension);
2014-04-15 18:08:29 +08:00
virtual void updatePosition(Vector2 &newPos);
inline PointArray* getPoints() { return _points; }
/**
* @js NA
* @lua NA
*/
inline 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
virtual CardinalSplineTo *clone() const override;
virtual CardinalSplineTo* reverse() const override;
virtual void startWithTarget(Node *target) override;
virtual void update(float time) override;
2012-06-08 13:55:28 +08:00
protected:
/** Array of control points */
PointArray *_points;
float _deltaT;
float _tension;
2014-04-15 18:08:29 +08:00
Vector2 _previousPosition;
Vector2 _accumulatedDiff;
2012-06-08 13:55:28 +08:00
};
/** Cardinal Spline path.
http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
2012-06-20 18:09:11 +08:00
@ingroup Actions
2012-06-08 13:55:28 +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)
* in lua: lcaol create(local t, local table)
* @endcode
*/
static CardinalSplineBy* create(float duration, PointArray* points, float tension);
CardinalSplineBy();
2012-06-08 13:55:28 +08:00
// Overrides
virtual void startWithTarget(Node *target) override;
2014-04-15 18:08:29 +08:00
virtual void updatePosition(Vector2 &newPos) override;
virtual CardinalSplineBy *clone() const override;
virtual CardinalSplineBy* reverse() const override;
2012-06-08 13:55:28 +08:00
protected:
2014-04-15 18:08:29 +08:00
Vector2 _startPosition;
2012-06-08 13:55:28 +08:00
};
/** 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
2012-06-20 18:09:11 +08:00
@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
* @code
* when this function bound to js or lua,the input params are changed
* in js: var create(var dt,var table)
* in lua: lcaol create(local dt, local table)
* @endcode
*/
static CatmullRomTo* create(float dt, PointArray* points);
2012-06-08 13:55:28 +08:00
/** initializes the action with a duration and an array of points */
bool initWithDuration(float dt, PointArray* points);
// Override
virtual CatmullRomTo *clone() const override;
virtual CatmullRomTo *reverse() const override;
2012-06-08 13:55:28 +08:00
};
/** 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
2012-06-20 18:09:11 +08:00
@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
* @code
* when this function bound to js or lua,the input params are changed
* in js: var create(var dt,var table)
* in lua: lcaol create(local dt, local table)
* @endcode
*/
static CatmullRomBy* create(float dt, PointArray* points);
2012-06-08 13:55:28 +08:00
/** initializes the action with a duration and an array of points */
bool initWithDuration(float dt, PointArray* points);
// Override
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 */
2014-04-15 18:08:29 +08:00
extern CC_DLL Vector2 ccCardinalSplineAt(Vector2 &p0, Vector2 &p1, Vector2 &p2, Vector2 &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
/// @}
2012-06-08 13:55:28 +08:00
NS_CC_END;
#endif // __CCACTION_CATMULLROM_H__