axmol/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioActionTimelineTest/ActionTimelineTestScene.cpp

327 lines
8.6 KiB
C++
Raw Normal View History

#include "ActionTimelineTestScene.h"
2014-06-11 11:10:07 +08:00
#include "../../testResource.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
2014-12-02 16:00:30 +08:00
#include "VisibleRect.h"
2014-06-11 11:10:07 +08:00
using namespace cocos2d;
using namespace cocostudio;
Layer *NextAnimationTest();
Layer *BackAnimationTest();
Layer *RestartAnimationTest();
static int s_nActionIdx = -1;
Layer *CreateAnimationLayer(int index)
{
Layer *pLayer = nullptr;
switch(index)
{
case TEST_ANIMATIONELEMENT:
pLayer = new (std::nothrow) TestActionTimeline();
2014-06-11 11:10:07 +08:00
break;
2014-06-17 15:57:30 +08:00
case TEST_CHANGE_PLAY_SECTION:
pLayer = new (std::nothrow) TestChangePlaySection();
2014-06-17 15:57:30 +08:00
break;
2014-12-02 16:00:30 +08:00
/*
2014-06-18 11:22:38 +08:00
case TEST_TIMELINE_FRAME_EVENT:
pLayer = new (std::nothrow) TestTimelineFrameEvent();
2014-06-18 11:22:38 +08:00
break;
2014-12-02 16:00:30 +08:00
*/
2014-06-17 11:13:46 +08:00
case TEST_TIMELINE_PERFORMACE:
pLayer = new (std::nothrow) TestTimelinePerformance();
2014-06-17 11:13:46 +08:00
break;
2014-06-11 11:10:07 +08:00
default:
break;
}
return pLayer;
}
Layer *NextAnimationTest()
{
++s_nActionIdx;
s_nActionIdx = s_nActionIdx % TEST_ANIMATION_LAYER_COUNT;
Layer *pLayer = CreateAnimationLayer(s_nActionIdx);
pLayer->autorelease();
return pLayer;
}
Layer *BackAnimationTest()
{
--s_nActionIdx;
if( s_nActionIdx < 0 )
s_nActionIdx += TEST_ANIMATION_LAYER_COUNT;
Layer *pLayer = CreateAnimationLayer(s_nActionIdx);
pLayer->autorelease();
return pLayer;
}
Layer *RestartAnimationTest()
{
Layer *pLayer = CreateAnimationLayer(s_nActionIdx);
pLayer->autorelease();
return pLayer;
}
ActionTimelineTestScene::ActionTimelineTestScene(bool bPortrait)
2014-06-11 11:10:07 +08:00
{
TestScene::init();
Sprite *bg = Sprite::create("armature/bg.jpg");
bg->setPosition(VisibleRect::center());
float scaleX = VisibleRect::getVisibleRect().size.width / bg->getContentSize().width;
float scaleY = VisibleRect::getVisibleRect().size.height / bg->getContentSize().height;
bg->setScaleX(scaleX);
bg->setScaleY(scaleY);
addChild(bg);
}
void ActionTimelineTestScene::runThisTest()
2014-06-11 11:10:07 +08:00
{
s_nActionIdx = -1;
addChild(NextAnimationTest());
Director::getInstance()->replaceScene(this);
}
void ActionTimelineTestScene::MainMenuCallback(Ref *pSender)
2014-06-11 11:10:07 +08:00
{
//TestScene::MainMenuCallback(pSender);
removeAllChildren();
}
void ActionTimelineTestLayer::onEnter()
2014-06-11 11:10:07 +08:00
{
Layer::onEnter();
// add title and subtitle
std::string str = title();
2014-06-26 16:15:02 +08:00
const char *title = str.c_str();
auto label = Label::createWithSystemFont(title, "Arial", 18);
2014-06-11 11:10:07 +08:00
label->setColor(Color3B(0, 0, 0));
addChild(label, 1, 10000);
label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - 30) );
std::string strSubtitle = subtitle();
if( ! strSubtitle.empty() )
{
2014-06-26 16:15:02 +08:00
auto l = Label::createWithSystemFont(strSubtitle.c_str(), "Arial", 18);
2014-06-11 11:10:07 +08:00
l->setColor(Color3B(0, 0, 0));
addChild(l, 1, 10001);
l->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60);
2014-06-11 11:10:07 +08:00
}
// add menu
backItem = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(ActionTimelineTestLayer::backCallback, this) );
restartItem = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(ActionTimelineTestLayer::restartCallback, this) );
nextItem = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(ActionTimelineTestLayer::nextCallback, this) );
2014-06-11 11:10:07 +08:00
Menu *menu = Menu::create(backItem, restartItem, nextItem, nullptr);
menu->setPosition(Point::ZERO);
backItem->setPosition(VisibleRect::center().x - restartItem->getContentSize().width * 2, VisibleRect::bottom().y + restartItem->getContentSize().height / 2);
restartItem->setPosition(VisibleRect::center().x, VisibleRect::bottom().y + restartItem->getContentSize().height / 2);
nextItem->setPosition(VisibleRect::center().x + restartItem->getContentSize().width * 2, VisibleRect::bottom().y + restartItem->getContentSize().height / 2);
2014-06-11 11:10:07 +08:00
addChild(menu, 100);
2014-06-26 16:15:02 +08:00
setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
2014-06-11 11:10:07 +08:00
}
void ActionTimelineTestLayer::onExit()
2014-06-11 11:10:07 +08:00
{
removeAllChildren();
backItem = restartItem = nextItem = nullptr;
ActionTimelineCache::getInstance()->purge();
2014-10-09 18:28:09 +08:00
CSLoader::getInstance()->purge();
2014-06-11 11:10:07 +08:00
Layer::onExit();
}
std::string ActionTimelineTestLayer::title() const
2014-06-11 11:10:07 +08:00
{
return "Armature Test Bed";
}
std::string ActionTimelineTestLayer::subtitle() const
2014-06-11 11:10:07 +08:00
{
return "";
}
void ActionTimelineTestLayer::restartCallback(Ref *pSender)
2014-06-11 11:10:07 +08:00
{
Scene *s = new (std::nothrow) ActionTimelineTestScene();
2014-06-11 11:10:07 +08:00
s->addChild( RestartAnimationTest() );
Director::getInstance()->replaceScene(s);
s->release();
}
void ActionTimelineTestLayer::nextCallback(Ref *pSender)
2014-06-11 11:10:07 +08:00
{
Scene *s = new (std::nothrow) ActionTimelineTestScene();
2014-06-12 16:21:41 +08:00
s->addChild( NextAnimationTest() );
Director::getInstance()->replaceScene(s);
s->release();
2014-06-11 11:10:07 +08:00
}
void ActionTimelineTestLayer::backCallback(Ref *pSender)
2014-06-11 11:10:07 +08:00
{
Scene *s = new (std::nothrow) ActionTimelineTestScene();
2014-06-12 16:21:41 +08:00
s->addChild( BackAnimationTest() );
Director::getInstance()->replaceScene(s);
s->release();
2014-06-11 11:10:07 +08:00
}
2014-06-17 15:57:30 +08:00
2014-06-17 11:13:46 +08:00
// TestActionTimeline
void TestActionTimeline::onEnter()
2014-06-11 11:10:07 +08:00
{
ActionTimelineTestLayer::onEnter();
2014-06-11 11:10:07 +08:00
Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb");
ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb");
node->runAction(action);
action->gotoFrameAndPlay(0);
// ActionTimelineNode* node = CSLoader::createActionTimelineNode("ActionTimeline/DemoPlayer.csb", 0, 40, true);
2014-06-11 11:10:07 +08:00
2014-06-17 11:13:46 +08:00
node->setScale(0.2f);
2014-12-02 16:00:30 +08:00
node->setPosition(VisibleRect::center());
2014-06-12 00:38:23 +08:00
2014-06-12 16:21:41 +08:00
addChild(node);
2014-06-12 00:38:23 +08:00
}
std::string TestActionTimeline::title() const
2014-06-12 00:38:23 +08:00
{
2014-06-16 09:45:44 +08:00
return "Test ActionTimeline";
2014-06-11 11:10:07 +08:00
}
2014-06-17 11:13:46 +08:00
2014-06-17 15:57:30 +08:00
// TestActionTimeline
void TestChangePlaySection::onEnter()
{
ActionTimelineTestLayer::onEnter();
Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb");
action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb");
node->runAction(action);
action->gotoFrameAndPlay(41);
/*
2014-12-04 16:49:43 +08:00
ActionTimelineNode* node = CSLoader::createActionTimelineNode("ActionTimeline/DemoPlayer.csb", 41, 81, true);
action = node->getActionTimeline();
*/
2014-06-17 15:57:30 +08:00
node->setScale(0.2f);
2014-12-02 16:00:30 +08:00
node->setPosition(VisibleRect::center());
2014-06-17 15:57:30 +08:00
// add touch event listener
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(TestChangePlaySection::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
addChild(node);
}
std::string TestChangePlaySection::title() const
{
return "Test Change Play Section";
}
void TestChangePlaySection::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
if(action->getStartFrame() == 0)
2014-12-04 16:49:43 +08:00
action->gotoFrameAndPlay(41, 81, true);
2014-06-17 15:57:30 +08:00
else
2014-12-04 16:49:43 +08:00
action->gotoFrameAndPlay(0, 40, true);
2014-06-17 15:57:30 +08:00
}
2014-06-18 11:22:38 +08:00
// TestFrameEvent
void TestTimelineFrameEvent::onEnter()
{
ActionTimelineTestLayer::onEnter();
Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb");
ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb");
node->runAction(action);
action->gotoFrameAndPlay(0);
/*
2014-12-04 16:49:43 +08:00
ActionTimelineNode* node = CSLoader::createActionTimelineNode("ActionTimeline/DemoPlayer.csb", 0, 40, true);
2014-11-17 21:05:20 +08:00
ActionTimeline* action = node->getActionTimeline();
*/
2014-06-18 11:22:38 +08:00
node->setScale(0.2f);
node->setPosition(150,100);
addChild(node);
action->setFrameEventCallFunc(CC_CALLBACK_1(TestTimelineFrameEvent::onFrameEvent, this));
}
std::string TestTimelineFrameEvent::title() const
{
return "Test Frame Event";
}
void TestTimelineFrameEvent::onFrameEvent(Frame* frame)
{
EventFrame* evnt = dynamic_cast<EventFrame*>(frame);
if(!evnt)
return;
std::string str = evnt->getEvent();
if (str == "changeColor")
{
evnt->getNode()->setColor(Color3B(0,0,0));
}
else if(str == "endChangeColor")
{
evnt->getNode()->setColor(Color3B(255,255,255));
}
}
2014-06-17 15:57:30 +08:00
2014-06-17 11:13:46 +08:00
// TestTimelinePerformance
void TestTimelinePerformance::onEnter()
{
ActionTimelineTestLayer::onEnter();
for (int i = 0; i< 100; i++)
{
Node* node = CSLoader::createNode("ActionTimeline/DemoPlayer.csb");
ActionTimeline* action = CSLoader::createTimeline("ActionTimeline/DemoPlayer.csb");
node->runAction(action);
action->gotoFrameAndPlay(41);
// ActionTimelineNode* node = CSLoader::createActionTimelineNode("ActionTimeline/DemoPlayer.csb", 41, 81, true);
2014-06-17 11:13:46 +08:00
2014-06-17 11:45:57 +08:00
node->setScale(0.1f);
node->setPosition(i*2,100);
2014-06-17 11:13:46 +08:00
addChild(node);
}
}
std::string TestTimelinePerformance::title() const
{
return "Test ActionTimeline performance";
}