axmol/cocos/2d/CCTweenFunction.cpp

553 lines
11 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
2013-06-06 12:02:54 +08:00
http://www.cocos2d-x.org
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.
****************************************************************************/
#include "2d/CCTweenFunction.h"
2014-08-27 15:04:22 +08:00
#define _USE_MATH_DEFINES // needed for M_PI and M_PI2
2014-08-26 18:19:28 +08:00
#include <math.h> // M_PI
2014-08-27 15:04:22 +08:00
#undef _USE_MATH_DEFINES
NS_CC_BEGIN
2013-06-06 12:02:54 +08:00
namespace tweenfunc {
#ifndef M_PI_X_2
#define M_PI_X_2 (float)M_PI * 2.0f
#endif
2014-02-17 12:33:48 +08:00
2013-06-07 10:52:32 +08:00
float tweenTo(float time, TweenType type, float *easingParam)
2013-06-06 12:02:54 +08:00
{
float delta = 0;
2013-06-07 10:52:32 +08:00
switch (type)
2013-06-07 10:52:32 +08:00
{
case CUSTOM_EASING:
delta = customEase(time, easingParam);
break;
case Linear:
delta = linear(time);
break;
case Sine_EaseIn:
delta = sineEaseIn(time);
break;
case Sine_EaseOut:
delta = sineEaseOut(time);
break;
case Sine_EaseInOut:
delta = sineEaseInOut(time);
break;
case Quad_EaseIn:
delta = quadEaseIn(time);
break;
case Quad_EaseOut:
delta = quadEaseOut(time);
break;
case Quad_EaseInOut:
delta = quadEaseInOut(time);
break;
case Cubic_EaseIn:
delta = cubicEaseIn(time);
break;
case Cubic_EaseOut:
delta = cubicEaseOut(time);
break;
case Cubic_EaseInOut:
delta = cubicEaseInOut(time);
break;
case Quart_EaseIn:
delta = quartEaseIn(time);
break;
case Quart_EaseOut:
delta = quartEaseOut(time);
break;
case Quart_EaseInOut:
delta = quartEaseInOut(time);
break;
case Quint_EaseIn:
delta = quintEaseIn(time);
break;
case Quint_EaseOut:
delta = quintEaseOut(time);
break;
case Quint_EaseInOut:
delta = quintEaseInOut(time);
break;
case Expo_EaseIn:
delta = expoEaseIn(time);
break;
case Expo_EaseOut:
delta = expoEaseOut(time);
break;
case Expo_EaseInOut:
delta = expoEaseInOut(time);
break;
case Circ_EaseIn:
delta = circEaseIn(time);
break;
case Circ_EaseOut:
delta = circEaseOut(time);
break;
case Circ_EaseInOut:
delta = circEaseInOut(time);
break;
case Elastic_EaseIn:
2014-03-13 19:53:06 +08:00
{
float period = 0.3f;
if (nullptr != easingParam) {
period = easingParam[0];
}
delta = elasticEaseIn(time, period);
}
break;
case Elastic_EaseOut:
2014-03-13 19:53:06 +08:00
{
float period = 0.3f;
if (nullptr != easingParam) {
period = easingParam[0];
}
delta = elasticEaseOut(time, period);
}
break;
case Elastic_EaseInOut:
2014-03-13 19:53:06 +08:00
{
float period = 0.3f;
if (nullptr != easingParam) {
period = easingParam[0];
}
delta = elasticEaseInOut(time, period);
}
break;
case Back_EaseIn:
delta = backEaseIn(time);
break;
case Back_EaseOut:
delta = backEaseOut(time);
break;
case Back_EaseInOut:
delta = backEaseInOut(time);
break;
case Bounce_EaseIn:
delta = bounceEaseIn(time);
break;
case Bounce_EaseOut:
delta = bounceEaseOut(time);
break;
case Bounce_EaseInOut:
delta = bounceEaseInOut(time);
break;
default:
delta = sineEaseInOut(time);
break;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
return delta;
}
// Linear
float linear(float time)
2013-06-06 12:02:54 +08:00
{
return time;
2013-06-06 12:02:54 +08:00
}
// Sine Ease
float sineEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return -1 * cosf(time * (float)M_PI_2) + 1;
2013-06-06 12:02:54 +08:00
}
float sineEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
return sinf(time * (float)M_PI_2);
2013-06-06 12:02:54 +08:00
}
float sineEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
return -0.5f * (cosf((float)M_PI * time) - 1);
2013-06-06 12:02:54 +08:00
}
// Quad Ease
float quadEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return time * time;
2013-06-06 12:02:54 +08:00
}
float quadEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
return -1 * time * (time - 2);
2013-06-06 12:02:54 +08:00
}
float quadEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time*2;
if (time < 1)
return 0.5f * time * time;
--time;
return -0.5f * (time * (time - 2) - 1);
2013-06-06 12:02:54 +08:00
}
// Cubic Ease
float cubicEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return time * time * time;
2013-06-06 12:02:54 +08:00
}
float cubicEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
time -= 1;
return (time * time * time + 1);
2013-06-06 12:02:54 +08:00
}
float cubicEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time*2;
if (time < 1)
return 0.5f * time * time * time;
time -= 2;
return 0.5f * (time * time * time + 2);
2013-06-06 12:02:54 +08:00
}
// Quart Ease
float quartEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return time * time * time * time;
2013-06-06 12:02:54 +08:00
}
float quartEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
time -= 1;
return -(time * time * time * time - 1);
2013-06-06 12:02:54 +08:00
}
float quartEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time*2;
if (time < 1)
return 0.5f * time * time * time * time;
time -= 2;
return -0.5f * (time * time * time * time - 2);
2013-06-06 12:02:54 +08:00
}
// Quint Ease
float quintEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return time * time * time * time * time;
2013-06-06 12:02:54 +08:00
}
float quintEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
time -=1;
return (time * time * time * time * time + 1);
2013-06-06 12:02:54 +08:00
}
float quintEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time*2;
if (time < 1)
return 0.5f * time * time * time * time * time;
time -= 2;
return 0.5f * (time * time * time * time * time + 2);
2013-06-06 12:02:54 +08:00
}
// Expo Ease
float expoEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return time == 0 ? 0 : powf(2, 10 * (time/1 - 1)) - 1 * 0.001f;
2013-06-06 12:02:54 +08:00
}
float expoEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
return time == 1 ? 1 : (-powf(2, -10 * time / 1) + 1);
2013-06-06 12:02:54 +08:00
}
float expoEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time /= 0.5f;
if (time < 1)
{
time = 0.5f * powf(2, 10 * (time - 1));
}
else
{
time = 0.5f * (-powf(2, -10 * (time - 1)) + 2);
}
return time;
2013-06-06 12:02:54 +08:00
}
// Circ Ease
float circEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
return -1 * (sqrt(1 - time * time) - 1);
2013-06-06 12:02:54 +08:00
}
float circEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time - 1;
return sqrt(1 - time * time);
2013-06-06 12:02:54 +08:00
}
float circEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
time = time * 2;
if (time < 1)
return -0.5f * (sqrt(1 - time * time) - 1);
time -= 2;
return 0.5f * (sqrt(1 - time * time) + 1);
2013-06-06 12:02:54 +08:00
}
// Elastic Ease
2014-03-08 21:48:44 +08:00
float elasticEaseIn(float time, float period)
2013-06-06 12:02:54 +08:00
{
float newT = 0;
if (time == 0 || time == 1)
2013-06-07 10:52:32 +08:00
{
newT = time;
2013-06-07 10:52:32 +08:00
}
else
{
float s = period / 4;
time = time - 1;
newT = -powf(2, 10 * time) * sinf((time - s) * M_PI_X_2 / period);
}
return newT;
}
2014-03-08 21:48:44 +08:00
float elasticEaseOut(float time, float period)
{
float newT = 0;
if (time == 0 || time == 1)
{
newT = time;
2013-06-07 10:52:32 +08:00
}
else
{
float s = period / 4;
newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) + 1;
}
return newT;
}
2014-03-08 21:48:44 +08:00
float elasticEaseInOut(float time, float period)
{
float newT = 0;
if (time == 0 || time == 1)
{
newT = time;
2013-06-07 10:52:32 +08:00
}
else
2013-09-13 18:07:37 +08:00
{
time = time * 2;
if (! period)
{
period = 0.3f * 1.5f;
}
float s = period / 4;
time = time - 1;
if (time < 0)
{
newT = -0.5f * powf(2, 10 * time) * sinf((time -s) * M_PI_X_2 / period);
}
else
{
newT = powf(2, -10 * time) * sinf((time - s) * M_PI_X_2 / period) * 0.5f + 1;
}
2013-09-13 18:07:37 +08:00
}
return newT;
2013-06-06 12:02:54 +08:00
}
// Back Ease
float backEaseIn(float time)
2013-06-06 12:02:54 +08:00
{
float overshoot = 1.70158f;
return time * time * ((overshoot + 1) * time - overshoot);
2013-06-06 12:02:54 +08:00
}
float backEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
float overshoot = 1.70158f;
time = time - 1;
return time * time * ((overshoot + 1) * time + overshoot) + 1;
2013-06-06 12:02:54 +08:00
}
float backEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
float overshoot = 1.70158f * 1.525f;
time = time * 2;
if (time < 1)
{
return (time * time * ((overshoot + 1) * time - overshoot)) / 2;
}
else
2013-09-13 18:07:37 +08:00
{
time = time - 2;
return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1;
2013-09-13 18:07:37 +08:00
}
}
2013-09-13 18:07:37 +08:00
// Bounce Ease
float bounceTime(float time)
{
if (time < 1 / 2.75f)
{
return 7.5625f * time * time;
}
else if (time < 2 / 2.75f)
{
time -= 1.5f / 2.75f;
return 7.5625f * time * time + 0.75f;
}
else if(time < 2.5f / 2.75f)
{
time -= 2.25f / 2.75f;
return 7.5625f * time * time + 0.9375f;
}
time -= 2.625f / 2.75f;
return 7.5625f * time * time + 0.984375f;
}
float bounceEaseIn(float time)
{
return 1 - bounceTime(1 - time);
2013-06-06 12:02:54 +08:00
}
float bounceEaseOut(float time)
2013-06-06 12:02:54 +08:00
{
return bounceTime(time);
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
float bounceEaseInOut(float time)
2013-06-06 12:02:54 +08:00
{
float newT = 0;
if (time < 0.5f)
2013-06-07 10:52:32 +08:00
{
time = time * 2;
newT = (1 - bounceTime(1 - time)) * 0.5f;
2013-06-07 10:52:32 +08:00
}
else
{
newT = bounceTime(time * 2 - 1) * 0.5f + 0.5f;
2013-06-06 12:02:54 +08:00
}
return newT;
2013-06-06 12:02:54 +08:00
}
2013-06-07 10:52:32 +08:00
// Custom Ease
float customEase(float time, float *easingParam)
2013-06-06 12:02:54 +08:00
{
if (easingParam)
{
float tt = 1-time;
return easingParam[1]*tt*tt*tt + 3*easingParam[3]*time*tt*tt + 3*easingParam[5]*time*time*tt + easingParam[7]*time*time*time;
}
return time;
2013-06-06 12:02:54 +08:00
}
float easeIn(float time, float rate)
2014-02-17 12:33:48 +08:00
{
return powf(time, rate);
}
float easeOut(float time, float rate)
{
return powf(time, 1 / rate);
}
float easeInOut(float time, float rate)
{
time *= 2;
if (time < 1)
{
return 0.5f * powf(time, rate);
}
else
{
return (1.0f - 0.5f * powf(2 - time, rate));
}
}
float quadraticIn(float time)
{
return powf(time,2);
}
float quadraticOut(float time)
{
return -time*(time-2);
}
float quadraticInOut(float time)
{
float resultTime = time;
time = time*2;
if (time < 1)
{
resultTime = time * time * 0.5f;
}
else
{
--time;
resultTime = -0.5f * (time * (time - 2) - 1);
}
return resultTime;
}
float bezieratFunction( float a, float b, float c, float d, float t )
{
return (powf(1-t,3) * a + 3*t*(powf(1-t,2))*b + 3*powf(t,2)*(1-t)*c + powf(t,3)*d );
}
2014-02-17 12:33:48 +08:00
}
2013-06-06 12:02:54 +08:00
NS_CC_END