From 838c71b135976de2fd13d7b9f3f14e4cbd07f2a4 Mon Sep 17 00:00:00 2001 From: andyque Date: Mon, 17 Feb 2014 12:33:48 +0800 Subject: [PATCH] refactor EaseIn with CCTweenFunction --- cocos/2d/CCActionEase.cpp | 5 +- cocos/2d/CCActionEase.h | 3 +- cocos/2d/CCTweenFunction.cpp | 16 ++++ cocos/2d/CCTweenFunction.h | 101 +++++++++++--------- cocos/editor-support/cocostudio/CCTween.cpp | 3 +- 5 files changed, 78 insertions(+), 50 deletions(-) diff --git a/cocos/2d/CCActionEase.cpp b/cocos/2d/CCActionEase.cpp index 28c6399d70..6cac31a29d 100644 --- a/cocos/2d/CCActionEase.cpp +++ b/cocos/2d/CCActionEase.cpp @@ -32,6 +32,7 @@ THE SOFTWARE. */ #include "CCActionEase.h" +#include "CCTweenFunction.h" NS_CC_BEGIN @@ -137,7 +138,9 @@ EaseIn* EaseIn::clone() const void EaseIn::update(float time) { - _inner->update(powf(time, _rate)); + TweenFunction funObj(_rate); + _tweenFunction = &TweenFunction::easeIn; + _inner->update(_tweenFunction(funObj,time)); } EaseIn* EaseIn::reverse() const diff --git a/cocos/2d/CCActionEase.h b/cocos/2d/CCActionEase.h index b656df3c6e..67b37c3bd4 100644 --- a/cocos/2d/CCActionEase.h +++ b/cocos/2d/CCActionEase.h @@ -32,6 +32,7 @@ THE SOFTWARE. NS_CC_BEGIN class Object; +class TweenFunction; /** * @addtogroup actions @@ -65,7 +66,7 @@ protected: /** The inner action */ ActionInterval *_inner; - std::function tweenFunction; + std::function _tweenFunction; private: CC_DISALLOW_COPY_AND_ASSIGN(ActionEase); diff --git a/cocos/2d/CCTweenFunction.cpp b/cocos/2d/CCTweenFunction.cpp index 81d2717de6..8bd670a1bc 100644 --- a/cocos/2d/CCTweenFunction.cpp +++ b/cocos/2d/CCTweenFunction.cpp @@ -30,6 +30,18 @@ NS_CC_BEGIN #define M_PI_X_2 (float)M_PI * 2.0f #endif +TweenFunction::TweenFunction() +:_rate(0.0f) +{ + +} + +TweenFunction::TweenFunction(float rate) +:_rate(rate) +{ + +} + float TweenFunction::tweenTo(float time, TweenType type, float *easingParam) { @@ -475,6 +487,10 @@ float TweenFunction::customEase(float time, float *easingParam) return time; } +float TweenFunction::easeIn(float time) const +{ + return powf(time, _rate); +} NS_CC_END diff --git a/cocos/2d/CCTweenFunction.h b/cocos/2d/CCTweenFunction.h index f7125e6131..36b218b025 100644 --- a/cocos/2d/CCTweenFunction.h +++ b/cocos/2d/CCTweenFunction.h @@ -26,6 +26,7 @@ THE SOFTWARE. #define __CCTWEENFUNCTION_H__ #include "CCPlatformMacros.h" +#include "ccMacros.h" #include NS_CC_BEGIN @@ -86,55 +87,61 @@ enum TweenType class TweenFunction { public: - - static float tweenTo(float time, TweenType type, float *easingParam); - - static float linear(float time); + TweenFunction(float rate); + TweenFunction(); + + float tweenTo(float time, TweenType type, float *easingParam); + + float linear(float time); //tween functions for CCActionEase - static float easeIn(float time); - - static float sineEaseIn(float time); - static float sineEaseOut(float time); - static float sineEaseInOut(float time); - - static float quadEaseIn(float time); - static float quadEaseOut(float time); - static float quadEaseInOut(float time); - - static float cubicEaseIn(float time); - static float cubicEaseOut(float time); - static float cubicEaseInOut(float time); - - static float quartEaseIn(float time); - static float quartEaseOut(float time); - static float quartEaseInOut(float time); - - static float quintEaseIn(float time); - static float quintEaseOut(float time); - static float quintEaseInOut(float time); - - static float expoEaseIn(float time); - static float expoEaseOut(float time); - static float expoEaseInOut(float time); - - static float circEaseIn(float time); - static float circEaseOut(float time); - static float circEaseInOut(float time); - - static float elasticEaseIn(float time, float *easingParam); - static float elasticEaseOut(float time, float *easingParam); - static float elasticEaseInOut(float time, float *easingParam); - - static float backEaseIn(float time); - static float backEaseOut(float time); - static float backEaseInOut(float time); - - static float bounceEaseIn(float time); - static float bounceEaseOut(float time); - static float bounceEaseInOut(float time); - - static float customEase(float time, float *easingParam); + float easeIn(float time) const; + + float sineEaseIn(float time); + float sineEaseOut(float time); + float sineEaseInOut(float time); + + float quadEaseIn(float time); + float quadEaseOut(float time); + float quadEaseInOut(float time); + + float cubicEaseIn(float time); + float cubicEaseOut(float time); + float cubicEaseInOut(float time); + + float quartEaseIn(float time); + float quartEaseOut(float time); + float quartEaseInOut(float time); + + float quintEaseIn(float time); + float quintEaseOut(float time); + float quintEaseInOut(float time); + + float expoEaseIn(float time); + float expoEaseOut(float time); + float expoEaseInOut(float time); + + float circEaseIn(float time); + float circEaseOut(float time); + float circEaseInOut(float time); + + float elasticEaseIn(float time, float *easingParam); + float elasticEaseOut(float time, float *easingParam); + float elasticEaseInOut(float time, float *easingParam); + + float backEaseIn(float time); + float backEaseOut(float time); + float backEaseInOut(float time); + + float bounceEaseIn(float time); + float bounceEaseOut(float time); + float bounceEaseInOut(float time); + + float customEase(float time, float *easingParam); + +private: + DISALLOW_COPY_AND_ASSIGN(TweenFunction); + float _rate; }; diff --git a/cocos/editor-support/cocostudio/CCTween.cpp b/cocos/editor-support/cocostudio/CCTween.cpp index ea1e74e674..7757b50b26 100644 --- a/cocos/editor-support/cocostudio/CCTween.cpp +++ b/cocos/editor-support/cocostudio/CCTween.cpp @@ -478,7 +478,8 @@ float Tween::updateFrameData(float currentPercent) TweenType tweenType = (_frameTweenEasing != Linear) ? _frameTweenEasing : _tweenEasing; if (tweenType != cocos2d::TWEEN_EASING_MAX && tweenType != Linear && !_passLastFrame) { - currentPercent = TweenFunction::tweenTo(currentPercent, tweenType, _from->easingParams); + TweenFunction funcObj; + currentPercent = funcObj.tweenTo(currentPercent, tweenType, _from->easingParams); } return currentPercent;