From 70052b2a3f9f2fe576277512b6b820c739623520 Mon Sep 17 00:00:00 2001 From: youyou <501251991@qq.com> Date: Mon, 22 Dec 2014 18:57:48 +0800 Subject: [PATCH] support animation list --- .../ActionTimeline/CCActionTimeline.cpp | 33 +++++ .../ActionTimeline/CCActionTimeline.h | 17 ++- .../ActionTimeline/CCActionTimelineCache.cpp | 16 +++ .../cocostudio/CSParseBinary_generated.h | 115 +++++++++++++----- .../ActionTimelineTestScene.cpp | 5 +- 5 files changed, 153 insertions(+), 33 deletions(-) diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp index 052f25b4f7..55da3d090b 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.cpp @@ -91,6 +91,17 @@ bool ActionTimeline::init() return true; } +void ActionTimeline::play(std::string name, bool loop) +{ + if(_indexes.find(name) == _indexes.end()) + { + CCLOG("Cann't find action indexes for %s.", name.c_str()); + } + + ActionIndexes& indexes = _indexes[name]; + gotoFrameAndPlay(indexes.startIndex, indexes.endIndex, loop); +} + void ActionTimeline::gotoFrameAndPlay(int startIndex) { gotoFrameAndPlay(startIndex, true); @@ -266,6 +277,28 @@ void ActionTimeline::removeTimeline(Timeline* timeline) } } +void ActionTimeline::addIndexes(const ActionIndexes& indexes) +{ + if(_indexes.find(indexes.name) != _indexes.end()) + { + CCLOG("ActionIndexes (%s) already exsists.", indexes.name.c_str()); + return; + } + + _indexes[indexes.name] = indexes; +} + +void ActionTimeline::removeIndexes(std::string name) +{ + if(_indexes.find(name) == _indexes.end()) + { + CCLOG("ActionIndexes %s don't exsists.", name.c_str()); + return; + } + + _indexes.erase(name); +} + void ActionTimeline::setFrameEventCallFunc(std::function listener) { _frameEventListener = listener; diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.h b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.h index 76a3299d78..567188dec2 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.h +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimeline.h @@ -31,6 +31,13 @@ THE SOFTWARE. NS_TIMELINE_BEGIN +struct ActionIndexes +{ + std::string name; + int startIndex; + int endIndex; +}; + class CC_STUDIO_DLL ActionTimelineData : public cocos2d::Ref { public: @@ -57,6 +64,8 @@ public: virtual ~ActionTimeline(); virtual bool init(); + + virtual void play(std::string name, bool loop); /** Goto the specified frame index, and start playing from this index. * @param startIndex The animation will play from this index. @@ -88,6 +97,7 @@ public: * @param startIndex The animation will pause at this index. */ virtual void gotoFrameAndPause(int startIndex); + /** Pause the animation. */ virtual void pause(); @@ -122,7 +132,11 @@ public: /** add Timeline to ActionTimeline */ virtual void addTimeline(Timeline* timeline); virtual void removeTimeline(Timeline* timeline); - + + /** add ActionIndexes */ + virtual void addIndexes(const ActionIndexes& indexes); + virtual void removeIndexes(std::string name); + virtual const cocos2d::Vector& getTimelines() const { return _timelineList; } /** Set ActionTimeline's frame event callback function */ @@ -168,6 +182,7 @@ protected: std::function _frameEventListener; std::function _lastFrameListener; + std::map _indexes; }; NS_TIMELINE_END diff --git a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimelineCache.cpp b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimelineCache.cpp index bb3c9e9d40..9bb008ec69 100644 --- a/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimelineCache.cpp +++ b/cocos/editor-support/cocostudio/ActionTimeline/CCActionTimelineCache.cpp @@ -439,6 +439,22 @@ ActionTimeline* ActionTimelineCache::loadAnimationActionWithFlatBuffersFile(cons float speed = nodeAction->speed(); action->setTimeSpeed(speed); + auto animationList = nodeAction->animationList(); + if(animationList) + { + auto animationLength = animationList->size(); + for (int i = 0; i < animationLength; i++) + { + auto animationFlatBuf = animationList->Get(i); + ActionIndexes indexes; + indexes.startIndex = animationFlatBuf->startIndex(); + indexes.endIndex = animationFlatBuf->endIndex(); + indexes.name = animationFlatBuf->name()->c_str(); + + action->addIndexes(indexes); + } + } + auto timelines = nodeAction->timeLines(); int timelineLength = timelines->size(); for (int i = 0; i < timelineLength; i++) diff --git a/cocos/editor-support/cocostudio/CSParseBinary_generated.h b/cocos/editor-support/cocostudio/CSParseBinary_generated.h index d4360f64a0..703d981c45 100644 --- a/cocos/editor-support/cocostudio/CSParseBinary_generated.h +++ b/cocos/editor-support/cocostudio/CSParseBinary_generated.h @@ -1686,44 +1686,97 @@ inline flatbuffers::Offset CreateComAudioOptions(flatbuffers::F return builder_.Finish(); } + +struct AnimationInfo : private flatbuffers::Table { + const flatbuffers::String *name() const { return GetPointer(4); } + int32_t startIndex() const { return GetField(6, 0); } + int32_t endIndex() const { return GetField(8, 0); } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, 4 /* name */) && + verifier.Verify(name()) && + VerifyField(verifier, 6 /* startIndex */) && + VerifyField(verifier, 8 /* endIndex */) && + verifier.EndTable(); + } +}; + +struct AnimationInfoBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_name(flatbuffers::Offset name) { fbb_.AddOffset(4, name); } + void add_startIndex(int32_t startIndex) { fbb_.AddElement(6, startIndex, 0); } + void add_endIndex(int32_t endIndex) { fbb_.AddElement(8, endIndex, 0); } + AnimationInfoBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } + AnimationInfoBuilder &operator=(const AnimationInfoBuilder &); + flatbuffers::Offset Finish() { + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 3)); + return o; + } +}; + +inline flatbuffers::Offset CreateAnimationInfo(flatbuffers::FlatBufferBuilder &_fbb, + flatbuffers::Offset name = 0, + int32_t startIndex = 0, + int32_t endIndex = 0) { + AnimationInfoBuilder builder_(_fbb); + builder_.add_endIndex(endIndex); + builder_.add_startIndex(startIndex); + builder_.add_name(name); + return builder_.Finish(); +} + struct NodeAction : private flatbuffers::Table { - int32_t duration() const { return GetField(4, 0); } - float speed() const { return GetField(6, 0); } - const flatbuffers::Vector> *timeLines() const { return GetPointer> *>(8); } - bool Verify(flatbuffers::Verifier &verifier) const { - return VerifyTableStart(verifier) && - VerifyField(verifier, 4 /* duration */) && - VerifyField(verifier, 6 /* speed */) && - VerifyField(verifier, 8 /* timeLines */) && - verifier.Verify(timeLines()) && - verifier.VerifyVectorOfTables(timeLines()) && - verifier.EndTable(); - } + int32_t duration() const { return GetField(4, 0); } + float speed() const { return GetField(6, 0); } + const flatbuffers::Vector> *timeLines() const { return GetPointer> *>(8); } + const flatbuffers::Vector> *animationList() const { return GetPointer> *>(10); } + const flatbuffers::String *currentAnimationName() const { return GetPointer(12); } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, 4 /* duration */) && + VerifyField(verifier, 6 /* speed */) && + VerifyField(verifier, 8 /* timeLines */) && + verifier.Verify(timeLines()) && + verifier.VerifyVectorOfTables(timeLines()) && + VerifyField(verifier, 10 /* animationList */) && + verifier.Verify(animationList()) && + verifier.VerifyVectorOfTables(animationList()) && + VerifyField(verifier, 12 /* currentAnimationName */) && + verifier.Verify(currentAnimationName()) && + verifier.EndTable(); + } }; struct NodeActionBuilder { - flatbuffers::FlatBufferBuilder &fbb_; - flatbuffers::uoffset_t start_; - void add_duration(int32_t duration) { fbb_.AddElement(4, duration, 0); } - void add_speed(float speed) { fbb_.AddElement(6, speed, 0); } - void add_timeLines(flatbuffers::Offset>> timeLines) { fbb_.AddOffset(8, timeLines); } - NodeActionBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } - NodeActionBuilder &operator=(const NodeActionBuilder &); - flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 3)); - return o; - } + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_duration(int32_t duration) { fbb_.AddElement(4, duration, 0); } + void add_speed(float speed) { fbb_.AddElement(6, speed, 0); } + void add_timeLines(flatbuffers::Offset>> timeLines) { fbb_.AddOffset(8, timeLines); } + void add_animationList(flatbuffers::Offset>> animationList) { fbb_.AddOffset(10, animationList); } + void add_currentAnimationName(flatbuffers::Offset currentAnimationName) { fbb_.AddOffset(12, currentAnimationName); } + NodeActionBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } + NodeActionBuilder &operator=(const NodeActionBuilder &); + flatbuffers::Offset Finish() { + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 5)); + return o; + } }; inline flatbuffers::Offset CreateNodeAction(flatbuffers::FlatBufferBuilder &_fbb, - int32_t duration = 0, - float speed = 0, - flatbuffers::Offset>> timeLines = 0) { - NodeActionBuilder builder_(_fbb); - builder_.add_timeLines(timeLines); - builder_.add_speed(speed); - builder_.add_duration(duration); - return builder_.Finish(); + int32_t duration = 0, + float speed = 0, + flatbuffers::Offset>> timeLines = 0, + flatbuffers::Offset>> animationList = 0, + flatbuffers::Offset currentAnimationName = 0) { + NodeActionBuilder builder_(_fbb); + builder_.add_currentAnimationName(currentAnimationName); + builder_.add_animationList(animationList); + builder_.add_timeLines(timeLines); + builder_.add_speed(speed); + builder_.add_duration(duration); + return builder_.Finish(); } struct TimeLine : private flatbuffers::Table { diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioActionTimelineTest/ActionTimelineTestScene.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioActionTimelineTest/ActionTimelineTestScene.cpp index d07e074521..bd4a39ac04 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioActionTimelineTest/ActionTimelineTestScene.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioActionTimelineTest/ActionTimelineTestScene.cpp @@ -199,9 +199,12 @@ void TestActionTimeline::onEnter() Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb"); ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb"); + + ActionIndexes indexes = {"walk", 0, 40}; + action->addIndexes(indexes); node->runAction(action); - action->gotoFrameAndPlay(0, 40, true); + action->play("walk", true); node->setScale(0.2f); node->setPosition(VisibleRect::center());