axmol/tests/cpp-tests/Classes/TransitionsTest/TransitionsTest.cpp

390 lines
10 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "TransitionsTest.h"
#include "../testResource.h"
2015-04-09 12:23:47 +08:00
USING_NS_CC;
#define TRANSITION_DURATION (1.2f)
2021-12-28 16:06:23 +08:00
class FadeWhiteTransition : public TransitionFade
2010-08-20 14:35:37 +08:00
{
public:
2021-12-28 16:06:23 +08:00
static TransitionScene* create(float t, Scene* s) { return TransitionFade::create(t, s, Color3B::WHITE); }
2010-08-20 14:35:37 +08:00
};
2021-12-28 16:06:23 +08:00
class FlipXLeftOver : public TransitionFlipX
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
return TransitionFlipX::create(t, s, TransitionScene::Orientation::LEFT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class FlipXRightOver : public TransitionFlipX
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
return TransitionFlipX::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class FlipYUpOver : public TransitionFlipY
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionFlipY::create(t, s, TransitionScene::Orientation::UP_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class FlipYDownOver : public TransitionFlipY
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionFlipY::create(t, s, TransitionScene::Orientation::DOWN_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class FlipAngularLeftOver : public TransitionFlipAngular
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionFlipAngular::create(t, s, TransitionScene::Orientation::LEFT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class FlipAngularRightOver : public TransitionFlipAngular
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
return TransitionFlipAngular::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipXLeftOver : public TransitionZoomFlipX
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::LEFT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipXRightOver : public TransitionZoomFlipX
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
return TransitionZoomFlipX::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipYUpOver : public TransitionZoomFlipY
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::UP_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipYDownOver : public TransitionZoomFlipY
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionZoomFlipY::create(t, s, TransitionScene::Orientation::DOWN_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipAngularLeftOver : public TransitionZoomFlipAngular
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
2021-12-28 16:06:23 +08:00
return TransitionZoomFlipAngular::create(t, s, TransitionScene::Orientation::LEFT_OVER);
2010-08-20 14:35:37 +08:00
}
};
2021-12-28 16:06:23 +08:00
class ZoomFlipAngularRightOver : public TransitionZoomFlipAngular
2010-08-20 14:35:37 +08:00
{
public:
static TransitionScene* create(float t, Scene* s)
2010-08-20 14:35:37 +08:00
{
return TransitionZoomFlipAngular::create(t, s, TransitionScene::Orientation::RIGHT_OVER);
2010-08-20 14:35:37 +08:00
}
};
class PageTransitionForward : public TransitionPageTurn
2010-08-20 14:35:37 +08:00
{
public:
2021-12-28 16:06:23 +08:00
static TransitionScene* create(float t, Scene* s) { return TransitionPageTurn::create(t, s, false); }
2010-08-20 14:35:37 +08:00
};
class PageTransitionBackward : public TransitionPageTurn
2010-08-20 14:35:37 +08:00
{
public:
2021-12-28 16:06:23 +08:00
static TransitionScene* create(float t, Scene* s) { return TransitionPageTurn::create(t, s, true); }
};
#define STRINGIFY(x) #x
2021-12-28 16:06:23 +08:00
#define TRANS(__className__) \
{ \
[](float t, Scene* s) { return __className__::create(t, s); }, STRINGIFY(__className__), \
}
2021-12-28 16:06:23 +08:00
struct _transitions
{
std::function<TransitionScene*(float t, Scene* s)> function;
2021-12-28 16:06:23 +08:00
const char* name;
} transitions[] = {
TRANS(TransitionJumpZoom),
TRANS(TransitionProgressRadialCCW),
TRANS(TransitionProgressRadialCW),
TRANS(TransitionProgressHorizontal),
TRANS(TransitionProgressVertical),
TRANS(TransitionProgressInOut),
TRANS(TransitionProgressOutIn),
TRANS(TransitionCrossFade),
TRANS(PageTransitionForward),
TRANS(PageTransitionBackward),
TRANS(TransitionFadeTR),
TRANS(TransitionFadeBL),
TRANS(TransitionFadeUp),
TRANS(TransitionFadeDown),
TRANS(TransitionTurnOffTiles),
TRANS(TransitionSplitRows),
TRANS(TransitionSplitCols),
TRANS(TransitionFade),
TRANS(FadeWhiteTransition),
TRANS(FlipXLeftOver),
TRANS(FlipXRightOver),
TRANS(FlipYUpOver),
TRANS(FlipYDownOver),
TRANS(FlipAngularLeftOver),
TRANS(FlipAngularRightOver),
TRANS(ZoomFlipXLeftOver),
TRANS(ZoomFlipXRightOver),
TRANS(ZoomFlipYUpOver),
TRANS(ZoomFlipYDownOver),
TRANS(ZoomFlipAngularLeftOver),
TRANS(ZoomFlipAngularRightOver),
TRANS(TransitionShrinkGrow),
TRANS(TransitionRotoZoom),
TRANS(TransitionMoveInL),
TRANS(TransitionMoveInR),
TRANS(TransitionMoveInT),
TRANS(TransitionMoveInB),
TRANS(TransitionSlideInL),
TRANS(TransitionSlideInR),
TRANS(TransitionSlideInT),
TRANS(TransitionSlideInB),
};
TransitionsTests::TransitionsTests()
2010-08-20 14:35:37 +08:00
{
int sceneIndex = 0;
for (auto& test : transitions)
{
2021-12-28 16:06:23 +08:00
addTestCase(test.name, [sceneIndex]() {
auto scene = TransitionsTest::create();
// fix bug #486, without setDepthTest(false), FlipX,Y will flickers
2019-06-30 11:36:52 +08:00
Director::getInstance()->getRenderer()->setDepthTest(false);
if (sceneIndex % 2)
{
scene->addChild(TestLayer2::create(transitions[sceneIndex].name));
}
else
{
scene->addChild(TestLayer1::create(transitions[sceneIndex].name));
}
return transitions[sceneIndex].function(TRANSITION_DURATION, scene);
});
sceneIndex++;
}
}
2021-12-28 15:58:56 +08:00
TestLayer1* TestLayer1::create(std::string_view transitionName)
{
2021-12-08 00:11:53 +08:00
auto layer = new TestLayer1(transitionName);
if (layer->init())
{
layer->autorelease();
}
else
{
delete layer;
layer = nullptr;
}
return layer;
}
2021-12-28 15:58:56 +08:00
TestLayer1::TestLayer1(std::string_view transitionName)
{
2021-12-28 16:06:23 +08:00
float x, y;
2010-08-20 14:35:37 +08:00
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
x = size.width;
y = size.height;
2010-08-20 14:35:37 +08:00
auto bg1 = Sprite::create(s_back1);
2021-12-28 16:06:23 +08:00
bg1->setPosition(Vec2(size.width / 2, size.height / 2));
2010-08-20 14:35:37 +08:00
addChild(bg1, -1);
auto title = Label::createWithTTF(transitionName, "fonts/Thonburi.ttf", 32);
2010-08-20 14:35:37 +08:00
addChild(title);
2021-12-28 16:06:23 +08:00
title->setColor(Color3B(255, 32, 32));
title->setPosition(Vec2(x / 2, y - 100));
2010-08-20 14:35:37 +08:00
auto label = Label::createWithTTF("SCENE 1", "fonts/Marker Felt.ttf", 38);
2021-12-28 16:06:23 +08:00
label->setColor(Color3B(16, 16, 255));
label->setPosition(Vec2(x / 2, y / 2));
addChild(label);
2010-08-20 14:35:37 +08:00
2021-12-28 16:06:23 +08:00
schedule(CC_SCHEDULE_SELECTOR(TestLayer1::step), 1.0f);
}
2021-12-28 16:06:23 +08:00
TestLayer1::~TestLayer1() {}
2021-12-28 16:06:23 +08:00
void TestLayer1::step(float dt) {}
void TestLayer1::onEnter()
{
Layer::onEnter();
log("Scene 1 onEnter");
}
void TestLayer1::onEnterTransitionDidFinish()
{
Layer::onEnterTransitionDidFinish();
log("Scene 1: onEnterTransitionDidFinish");
}
void TestLayer1::onExitTransitionDidStart()
{
Layer::onExitTransitionDidStart();
log("Scene 1: onExitTransitionDidStart");
}
void TestLayer1::onExit()
{
Layer::onExit();
2019-06-30 11:36:52 +08:00
Director::getInstance()->getRenderer()->setDepthTest(false);
log("Scene 1 onExit");
}
2021-12-28 15:58:56 +08:00
TestLayer2* TestLayer2::create(std::string_view transitionName)
{
2021-12-08 00:11:53 +08:00
auto layer = new TestLayer2(transitionName);
if (layer->init())
{
layer->autorelease();
}
else
{
delete layer;
layer = nullptr;
}
return layer;
}
2021-12-28 15:58:56 +08:00
TestLayer2::TestLayer2(std::string_view transitionName)
{
2021-12-28 16:06:23 +08:00
float x, y;
2010-08-20 14:35:37 +08:00
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
x = size.width;
y = size.height;
2010-08-20 14:35:37 +08:00
auto bg1 = Sprite::create(s_back2);
2021-12-28 16:06:23 +08:00
bg1->setPosition(Vec2(size.width / 2, size.height / 2));
2010-08-20 14:35:37 +08:00
addChild(bg1, -1);
auto title = Label::createWithTTF(transitionName, "fonts/Thonburi.ttf", 32);
2010-08-20 14:35:37 +08:00
addChild(title);
2021-12-28 16:06:23 +08:00
title->setColor(Color3B(255, 32, 32));
title->setPosition(Vec2(x / 2, y - 100));
2010-08-20 14:35:37 +08:00
auto label = Label::createWithTTF("SCENE 2", "fonts/Marker Felt.ttf", 38);
2021-12-28 16:06:23 +08:00
label->setColor(Color3B(16, 16, 255));
label->setPosition(Vec2(x / 2, y / 2));
addChild(label);
2010-08-20 14:35:37 +08:00
schedule(CC_SCHEDULE_SELECTOR(TestLayer2::step), 1.0f);
}
2021-12-28 16:06:23 +08:00
TestLayer2::~TestLayer2() {}
2021-12-28 16:06:23 +08:00
void TestLayer2::step(float dt) {}
void TestLayer2::onEnter()
{
Layer::onEnter();
log("Scene 2 onEnter");
}
void TestLayer2::onEnterTransitionDidFinish()
{
Layer::onEnterTransitionDidFinish();
log("Scene 2: onEnterTransitionDidFinish");
}
void TestLayer2::onExitTransitionDidStart()
{
Layer::onExitTransitionDidStart();
log("Scene 2: onExitTransitionDidStart");
}
void TestLayer2::onExit()
{
Layer::onExit();
2019-06-30 11:36:52 +08:00
Director::getInstance()->getRenderer()->setDepthTest(false);
log("Scene 2 onExit");
}