axmol/core/2d/ActionCatmullRom.h

348 lines
9.8 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +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.
* Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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/ActionInterval.h"
#include "math/Math.h"
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
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 AX_DLL PointArray : public Object, public Clonable
2019-11-23 20:27:39 +08:00
{
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);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CardinalSplineTo : public ActionInterval
2019-11-23 20:27:39 +08:00
{
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();
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* 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.
*/
2021-12-25 10:04:45 +08:00
virtual void updatePosition(const Vec2& newPos);
2019-11-23 20:27:39 +08:00
/** Return a PointArray.
*
* @return A PointArray.
*/
PointArray* getPoints() { return _points; }
/**
* @js NA
* @lua NA
*/
void setPoints(PointArray* points)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(points);
AX_SAFE_RELEASE(_points);
2019-11-23 20:27:39 +08:00
_points = points;
}
// Overrides
2021-12-25 10:04:45 +08:00
virtual CardinalSplineTo* clone() const override;
2019-11-23 20:27:39 +08:00
virtual CardinalSplineTo* reverse() const override;
2021-12-25 10:04:45 +08:00
virtual void startWithTarget(Node* target) override;
2019-11-23 20:27:39 +08:00
/**
* @param time In seconds.
*/
virtual void update(float time) override;
protected:
/** Array of control points */
2021-12-25 10:04:45 +08:00
PointArray* _points;
2019-11-23 20:27:39 +08:00
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
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CardinalSplineBy : public CardinalSplineTo
2019-11-23 20:27:39 +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: 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
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;
2019-11-23 20:27:39 +08:00
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
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CatmullRomTo : public CardinalSplineTo
2019-11-23 20:27:39 +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).
* In lua: local create(local dt, local table).
* @endcode
*/
static CatmullRomTo* create(float dt, PointArray* points);
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
* 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 CatmullRomTo* clone() const override;
virtual CatmullRomTo* reverse() const override;
2019-11-23 20:27:39 +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
*/
2022-07-16 10:43:05 +08:00
class AX_DLL CatmullRomBy : public CardinalSplineBy
2019-11-23 20:27:39 +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).
* 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;
2019-11-23 20:27:39 +08:00
};
/** Returns the Cardinal Spline position for a given set of control points, tension and time */
2022-07-16 10:43:05 +08:00
extern AX_DLL Vec2
2021-12-25 10:04:45 +08:00
ccCardinalSplineAt(const Vec2& p0, const Vec2& p1, const Vec2& p2, const Vec2& p3, float tension, float t);
2019-11-23 20:27:39 +08:00
// end of actions group
/// @}
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif // __CCACTION_CATMULLROM_H__