From 4a8cde4dadedd2f8fbe2b15e81c44dd9a4af4817 Mon Sep 17 00:00:00 2001 From: yinkaile <501251991@qq.com> Date: Mon, 8 Jul 2013 23:25:11 +0800 Subject: [PATCH] Change Tween Type to Frame Index --- extensions/CCArmature/animation/CCTween.cpp | 56 +++++++++---------- extensions/CCArmature/animation/CCTween.h | 1 - extensions/CCArmature/datas/CCDatas.cpp | 5 +- extensions/CCArmature/datas/CCDatas.h | 1 + .../CCArmature/utils/CCDataReaderHelper.cpp | 23 +++++++- 5 files changed, 53 insertions(+), 33 deletions(-) diff --git a/extensions/CCArmature/animation/CCTween.cpp b/extensions/CCArmature/animation/CCTween.cpp index a43ff4ad24..1717119bee 100644 --- a/extensions/CCArmature/animation/CCTween.cpp +++ b/extensions/CCArmature/animation/CCTween.cpp @@ -54,7 +54,6 @@ CCTween::CCTween() , m_pFrom(NULL) , m_pTo(NULL) , m_pBetween(NULL) - , m_pCurrentKeyFrame(NULL) , m_pBone(NULL) , m_eFrameTweenEasing(Linear) @@ -101,11 +100,10 @@ void CCTween::play(CCMovementBoneData *movementBoneData, int durationTo, int dur m_eLoopType = (AnimationType)loop; - m_pCurrentKeyFrame = NULL; - m_iTotalDuration = 0; m_BetweenDuration = 0; - m_iToIndex = 0; + m_iFromIndex = m_iToIndex = 0; + bool difMovement = movementBoneData != m_pMovementBoneData; @@ -126,7 +124,6 @@ void CCTween::play(CCMovementBoneData *movementBoneData, int durationTo, int dur } m_eFrameTweenEasing = Linear; m_iRawDuration = m_pMovementBoneData->duration; - m_iFromIndex = m_iToIndex = 0; } else if (m_pMovementBoneData->frameList.count() > 1) { @@ -217,8 +214,6 @@ void CCTween::updateHandler() // m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex; m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex; - - } else { @@ -266,10 +261,6 @@ void CCTween::updateHandler() { tweenNodeTo(percent); } - else if(m_pCurrentKeyFrame) - { - tweenNodeTo(0); - } } void CCTween::setBetween(CCFrameData *from, CCFrameData *to) @@ -328,7 +319,7 @@ void CCTween::arriveKeyFrame(CCFrameData *keyFrameData) } // if(keyFrameData->m_strSound.length() != 0) // { - // //soundManager.dispatchEventWith(Event.SOUND_FRAME, m_pCurrentKeyFrame->sound); + // //soundManager.dispatchEventWith(Event.SOUND_FRAME, keyFrameData->sound); // } } } @@ -366,26 +357,33 @@ void CCTween::tweenColorTo(float percent, CCFrameData *node, bool dirty) float CCTween::updateFrameData(float currentPrecent, bool activeFrame) { - + if (currentPrecent > 1) + { + currentPrecent = fmodf(currentPrecent,1); + } + float playedTime = (float)m_iRawDuration * currentPrecent; - CCFrameData *from; - CCFrameData *to; - bool isListEnd; - //! If play to current frame's front or back, then find current frame again - if (playedTime >= m_iTotalDuration || playedTime < m_iTotalDuration - m_BetweenDuration) + if (playedTime < m_iTotalDuration || playedTime >= m_iTotalDuration + m_BetweenDuration) { /* * Get frame length, if m_iToIndex >= _length, then set m_iToIndex to 0, start anew. * m_iToIndex is next index will play */ int length = m_pMovementBoneData->frameList.count(); + + CCFrameData *from = NULL; + CCFrameData *to = NULL; + bool isListEnd; + CCFrameData **frames = (CCFrameData**)m_pMovementBoneData->frameList.data->arr; + do { - m_BetweenDuration = m_pMovementBoneData->getFrameData(m_iToIndex)->duration; - m_iTotalDuration += m_BetweenDuration; + from = frames[m_iFromIndex]; + to = frames[m_iToIndex]; + m_iTotalDuration = from->frameID; m_iFromIndex = m_iToIndex; if (++m_iToIndex >= length) @@ -393,27 +391,29 @@ float CCTween::updateFrameData(float currentPrecent, bool activeFrame) m_iToIndex = 0; } } - while (playedTime >= m_iTotalDuration); + while (playedTime < from->frameID || playedTime>=to->frameID); + + m_BetweenDuration = to->frameID - from->frameID; isListEnd = m_eLoopType == ANIMATION_MAX && m_iToIndex == 0; if(isListEnd) { - to = from = m_pMovementBoneData->getFrameData(m_iFromIndex); - } - else - { - from = m_pMovementBoneData->getFrameData(m_iFromIndex); - to = m_pMovementBoneData->getFrameData(m_iToIndex); + to = from = frames[m_iFromIndex]; } +// else +// { +// from = frames[m_iFromIndex]; +// to = frames[m_iToIndex]; +// } m_eFrameTweenEasing = from->tweenEasing; setBetween(from, to); } - currentPrecent = 1 - (m_iTotalDuration - playedTime) / (float)m_BetweenDuration; + currentPrecent = (playedTime - m_iTotalDuration) / (float)m_BetweenDuration; /* diff --git a/extensions/CCArmature/animation/CCTween.h b/extensions/CCArmature/animation/CCTween.h index b968fb6183..bb27f2f111 100644 --- a/extensions/CCArmature/animation/CCTween.h +++ b/extensions/CCArmature/animation/CCTween.h @@ -124,7 +124,6 @@ protected: CCFrameData *m_pTo; //! To frame data, used for calculate between value CCFrameData *m_pBetween; //! Between frame data, used for calculate current CCFrameData(m_pNode) value - CCFrameData *m_pCurrentKeyFrame; //! A weak reference to the current CCFrameData. The data is in the data pool CCBone *m_pBone; //! A weak reference to the CCBone diff --git a/extensions/CCArmature/datas/CCDatas.cpp b/extensions/CCArmature/datas/CCDatas.cpp index e6ce61d295..b525332248 100644 --- a/extensions/CCArmature/datas/CCDatas.cpp +++ b/extensions/CCArmature/datas/CCDatas.cpp @@ -269,7 +269,8 @@ CCBoneData *CCArmatureData::getBoneData(const char *boneName) } CCFrameData::CCFrameData(void) - : duration(1) + : frameID(0) + , duration(1) , tweenEasing(Linear) , displayIndex(0) @@ -288,6 +289,7 @@ void CCFrameData::copy(CCFrameData *frameData) { CCBaseData::copy(frameData); + frameID = frameData->frameID; duration = frameData->duration; displayIndex = frameData->displayIndex; tweenEasing = frameData->tweenEasing; @@ -313,7 +315,6 @@ bool CCMovementBoneData::init() void CCMovementBoneData::addFrameData(CCFrameData *frameData) { frameList.addObject(frameData); - duration += frameData->duration; } CCFrameData *CCMovementBoneData::getFrameData(int index) diff --git a/extensions/CCArmature/datas/CCDatas.h b/extensions/CCArmature/datas/CCDatas.h index 81dcb29181..9ecbc225be 100644 --- a/extensions/CCArmature/datas/CCDatas.h +++ b/extensions/CCArmature/datas/CCDatas.h @@ -285,6 +285,7 @@ public: virtual void copy(CCFrameData *frameData); public: + int frameID; int duration; //! The frame will last m_iDuration frames CCTweenType tweenEasing; //! Every frame's tween easing effect diff --git a/extensions/CCArmature/utils/CCDataReaderHelper.cpp b/extensions/CCArmature/utils/CCDataReaderHelper.cpp index 5ec773408a..4fcc3b0019 100644 --- a/extensions/CCArmature/utils/CCDataReaderHelper.cpp +++ b/extensions/CCArmature/utils/CCDataReaderHelper.cpp @@ -562,11 +562,20 @@ CCMovementBoneData *CCDataReaderHelper::decodeMovementBone(tinyxml2::XMLElement CCFrameData *frameData = decodeFrame( frameXML, parentFrameXML, boneData); movBoneData->addFrameData(frameData); + frameData->frameID = totalDuration; totalDuration += frameData->duration; + movBoneData->duration = totalDuration; frameXML = frameXML->NextSiblingElement(FRAME); } + + // + CCFrameData *frameData = CCFrameData::create(); + frameData->copy((CCFrameData*)movBoneData->frameList.lastObject()); + frameData->frameID = movBoneData->duration; + movBoneData->addFrameData(frameData); + return movBoneData; } @@ -1049,12 +1058,22 @@ CCMovementBoneData *CCDataReaderHelper::decodeMovementBone(cs::CSJsonDictionary { cs::CSJsonDictionary *dic = json.getSubItemFromArray(FRAME_DATA, i); CCFrameData *frameData = decodeFrame(*dic); - movementBoneData->addFrameData(frameData); - //movementBoneData->duration += frameData->duration; + + movementBoneData->addFrameData(frameData); + + frameData->frameID = movementBoneData->duration; + movementBoneData->duration += frameData->duration; delete dic; } + // + CCFrameData *frameData = CCFrameData::create(); + frameData->copy((CCFrameData*)movementBoneData->frameList.lastObject()); + movementBoneData->addFrameData(frameData); + + frameData->frameID = movementBoneData->duration; + return movementBoneData; }