// TestTimelineAnimationList void TestTimelineAnimationList::onEnter() { ActionTimelineBaseTest::onEnter(); Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb"); ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb"); cocostudio::timeline::AnimationInfo standinfo("stand", 0, 40); cocostudio::timeline::AnimationInfo walkinfo("walk", 41, 81); action->addAnimationInfo(standinfo); action->addAnimationInfo(walkinfo); node->runAction(action); action->play("walk", true); node->setScale(0.2f); node->setPosition(150,100); addChild(node); }
ActionTimeline* ActionTimelineCache::loadAnimationActionWithFlatBuffersFile(const std::string &fileName) { // if already exists an action with filename, then return this action ActionTimeline* action = _animationActions.at(fileName); if (action) return action; std::string path = fileName; std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName.c_str()); CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath)); Data buf = FileUtils::getInstance()->getDataFromFile(fullPath); auto csparsebinary = GetCSParseBinary(buf.getBytes()); auto nodeAction = csparsebinary->action(); action = ActionTimeline::create(); int duration = nodeAction->duration(); action->setDuration(duration); float speed = nodeAction->speed(); action->setTimeSpeed(speed); auto animationlist = csparsebinary->animationList(); int animationcount = animationlist->size(); for (int i = 0; i < animationcount; i++) { auto animationdata = animationlist->Get(i); AnimationInfo info; info.name = animationdata->name()->c_str(); info.startIndex = animationdata->startIndex(); info.endIndex = animationdata->endIndex(); action->addAnimationInfo(info); } auto timelines = nodeAction->timeLines(); int timelineLength = timelines->size(); for (int i = 0; i < timelineLength; i++) { auto timelineFlatBuf = timelines->Get(i); Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf); if (timeline) action->addTimeline(timeline); } _animationActions.insert(fileName, action); return action; }
ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersForSimulator(const std::string& fileName) { FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance(); fbs->_isSimulator = true; auto builder = fbs->createFlatBuffersWithXMLFileForSimulator(fileName); ActionTimeline* action = ActionTimeline::create(); auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer()); auto nodeAction = csparsebinary->action(); action = ActionTimeline::create(); int duration = nodeAction->duration(); action->setDuration(duration); float speed = nodeAction->speed(); action->setTimeSpeed(speed); auto animationlist = csparsebinary->animationList(); int animationcount = animationlist->size(); for (int i = 0; i < animationcount; i++) { auto animationdata = animationlist->Get(i); AnimationInfo info; info.name = animationdata->name()->c_str(); info.startIndex = animationdata->startIndex(); info.endIndex = animationdata->endIndex(); action->addAnimationInfo(info); } auto timeLines = nodeAction->timeLines(); int timelineLength = timeLines->size(); std::multimap<std::string, cocostudio::timeline::Timeline*> properTimelineMap;// order the timelines depends property name for (int i = 0; i < timelineLength; i++) { auto timelineFlatBuf = timeLines->Get(i); Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf); if (timeline) { properTimelineMap.insert(std::make_pair(timelineFlatBuf->property()->c_str(), timeline)); } } for (const auto& properTimelinePair : properTimelineMap) { action->addTimeline(properTimelinePair.second); } fbs->deleteFlatBufferBuilder(); return action; }
ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersForSimulator(const std::string& fileName) { FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance(); fbs->_isSimulator = true; auto builder = fbs->createFlatBuffersWithXMLFileForSimulator(fileName); ActionTimeline* action = ActionTimeline::create(); auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer()); auto nodeAction = csparsebinary->action(); action = ActionTimeline::create(); int duration = nodeAction->duration(); action->setDuration(duration); float speed = nodeAction->speed(); action->setTimeSpeed(speed); auto animationlist = csparsebinary->animationList(); int animationcount = animationlist->size(); for (int i = 0; i < animationcount; i++) { auto animationdata = animationlist->Get(i); AnimationInfo info; info.name = animationdata->name()->c_str(); info.startIndex = animationdata->startIndex(); info.endIndex = animationdata->endIndex(); action->addAnimationInfo(info); } auto timeLines = nodeAction->timeLines(); int timelineLength = timeLines->size(); for (int i = 0; i < timelineLength; i++) { auto timelineFlatBuf = timeLines->Get(i); Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf); if (timeline) action->addTimeline(timeline); } fbs->deleteFlatBufferBuilder(); return action; }
ActionTimeline* ActionTimeline::clone() const { ActionTimeline* newAction = ActionTimeline::create(); newAction->setDuration(_duration); newAction->setTimeSpeed(_timeSpeed); for (auto timelines : _timelineMap) { for(auto timeline : timelines.second) { Timeline* newTimeline = timeline->clone(); newAction->addTimeline(newTimeline); } } for( auto info : _animationInfos) { newAction->addAnimationInfo(info.second); } return newAction; }
//TestAnimationClipEndCallBack void TestAnimationClipEndCallBack::onEnter() { ActionTimelineBaseTest::onEnter(); Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer_skeleton.csb"); ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer_skeleton.csb"); node->runAction(action); node->setScale(0.2f); node->setPosition(150, 150); // test for frame end call back action->addFrameEndCallFunc(5, "CallBackAfterFifthFrame", [this] { auto text = ui::Text::create(); text->setString("CallBackAfterFifthFrame"); text->setPosition(Vec2(100, 40)); text->setLocalZOrder(1000); this->runAction(Sequence::create( CallFunc::create([this, text]{this->addChild(text); }), DelayTime::create(3), CallFunc::create([text]{text->removeFromParent(); }), nullptr)); }); action->addFrameEndCallFunc(5, "AnotherCallBackAfterFifthFrame", [this] { auto text = ui::Text::create(); text->setString("AnotherCallBackAfterFifthFrame"); text->setPosition(Vec2(100, 70)); this->runAction(Sequence::create( CallFunc::create([this, text]{this->addChild(text); }), DelayTime::create(3), CallFunc::create([text]{text->removeFromParent(); }), nullptr)); }); action->addFrameEndCallFunc(7, "CallBackAfterSenvnthFrame", [this] { auto text = ui::Text::create(); text->setString("CallBackAfterSenvnthFrame"); text->setPosition(Vec2(100, 100)); this->runAction(Sequence::create( CallFunc::create([this, text]{this->addChild(text); }), DelayTime::create(3), CallFunc::create([text]{text->removeFromParent(); }), nullptr)); }); // test for animation clip end call back action->setAnimationEndCallFunc("stand", [this] { auto text = ui::Text::create(); text->setString("CallBackAfterStandAnimationClip"); text->setPosition(Vec2(100, 130)); this->runAction(Sequence::create( CallFunc::create([this, text]{this->addChild(text); }), DelayTime::create(3), CallFunc::create([text]{text->removeFromParent(); }), nullptr)); }); AnimationClip animClip("testClip", 3, 13); animClip.clipEndCallBack = ([this,node] { auto text = ui::Text::create(); text->setString("testClip"); text->setPosition(Vec2(100, 140)); this->runAction(Sequence::create( CallFunc::create([this, text]{this->addChild(text); }), DelayTime::create(3), CallFunc::create([text]{text->removeFromParent(); }), nullptr)); }); action->addAnimationInfo(animClip); action->setTimeSpeed(0.2f); addChild(node); action->gotoFrameAndPlay(0); }