axmol/core/2d/CCTransitionProgress.cpp

334 lines
9.9 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2009 Lam Pham
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2012 Ricardo Quesada
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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 "2d/CCTransitionProgress.h"
#include "base/CCDirector.h"
#include "2d/CCRenderTexture.h"
#include "2d/CCProgressTimer.h"
#include "2d/CCActionInstant.h"
#include "2d/CCActionProgressTimer.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
enum
{
2019-11-23 20:27:39 +08:00
kSceneRadial = 0xc001,
};
2021-12-25 10:04:45 +08:00
TransitionProgress::TransitionProgress() : _to(0.0f), _from(0.0f), _sceneToBeModified(nullptr) {}
2019-11-23 20:27:39 +08:00
TransitionProgress* TransitionProgress::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgress* newScene = new TransitionProgress();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
// TransitionProgress
void TransitionProgress::onEnter()
{
TransitionScene::onEnter();
setupTransition();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// create a transparent color layer
// in which we are going to add our rendertextures
auto& size = _director->getWinSize();
2019-11-23 20:27:39 +08:00
// create the second render texture for outScene
2021-12-25 10:04:45 +08:00
RenderTexture* texture = RenderTexture::create((int)size.width, (int)size.height, backend::PixelFormat::RGBA8,
backend::PixelFormat::D24S8);
texture->getSprite()->setAnchorPoint(Vec2(0.5f, 0.5f));
texture->setPosition(size.width / 2, size.height / 2);
texture->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
// render outScene to its texturebuffer
texture->beginWithClear(0, 0, 0, 1);
_sceneToBeModified->visit();
texture->end();
// Since we've passed the outScene to the texture we don't need it.
if (_sceneToBeModified == _outScene)
{
hideOutShowIn();
}
// We need the texture in RenderTexture.
2021-12-25 10:04:45 +08:00
ProgressTimer* node = progressTimerNodeWithRenderTexture(texture);
2019-11-23 20:27:39 +08:00
// create the blend action
2021-12-25 10:04:45 +08:00
auto layerAction = Sequence::create(ProgressFromTo::create(_duration, _from, _to),
2022-07-16 10:43:05 +08:00
CallFunc::create(AX_CALLBACK_0(TransitionScene::finish, this)), nullptr);
2019-11-23 20:27:39 +08:00
// run the blend action
node->runAction(layerAction);
// add the layer (which contains our two rendertextures) to the scene
addChild(node, 2, kSceneRadial);
}
// clean up on exit
void TransitionProgress::onExit()
{
// remove our layer and release all containing objects
removeChildByTag(kSceneRadial, true);
TransitionScene::onExit();
}
void TransitionProgress::sceneOrder()
{
_isInSceneOnTop = false;
}
void TransitionProgress::setupTransition()
{
_sceneToBeModified = _outScene;
2021-12-25 10:04:45 +08:00
_from = 100;
_to = 0;
2019-11-23 20:27:39 +08:00
}
ProgressTimer* TransitionProgress::progressTimerNodeWithRenderTexture(RenderTexture* /*texture*/)
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "override me - abstract class");
2019-11-23 20:27:39 +08:00
return nullptr;
}
// TransitionProgressRadialCCW
ProgressTimer* TransitionProgressRadialCCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
{
auto& size = _director->getWinSize();
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
node->setType(ProgressTimer::Type::RADIAL);
// Return the radial type that we want to use
node->setReverseDirection(false);
node->setPercentage(100);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
TransitionProgressRadialCCW* TransitionProgressRadialCCW::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressRadialCCW* newScene = new TransitionProgressRadialCCW();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
// TransitionProgressRadialCW
TransitionProgressRadialCW* TransitionProgressRadialCW::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressRadialCW* newScene = new TransitionProgressRadialCW();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
ProgressTimer* TransitionProgressRadialCW::progressTimerNodeWithRenderTexture(RenderTexture* texture)
{
auto& size = _director->getWinSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
2021-12-25 10:04:45 +08:00
node->setType(ProgressTimer::Type::RADIAL);
2019-11-23 20:27:39 +08:00
// Return the radial type that we want to use
node->setReverseDirection(true);
node->setPercentage(100);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
// TransitionProgressHorizontal
TransitionProgressHorizontal* TransitionProgressHorizontal::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressHorizontal* newScene = new TransitionProgressHorizontal();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
ProgressTimer* TransitionProgressHorizontal::progressTimerNodeWithRenderTexture(RenderTexture* texture)
2021-12-25 10:04:45 +08:00
{
auto& size = _director->getWinSize();
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
2021-12-25 10:04:45 +08:00
node->setType(ProgressTimer::Type::BAR);
2019-11-23 20:27:39 +08:00
node->setMidpoint(Vec2(1, 0));
2021-12-25 10:04:45 +08:00
node->setBarChangeRate(Vec2(1, 0));
2019-11-23 20:27:39 +08:00
node->setPercentage(100);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
// TransitionProgressVertical
TransitionProgressVertical* TransitionProgressVertical::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressVertical* newScene = new TransitionProgressVertical();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
ProgressTimer* TransitionProgressVertical::progressTimerNodeWithRenderTexture(RenderTexture* texture)
2021-12-25 10:04:45 +08:00
{
auto& size = _director->getWinSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
node->setType(ProgressTimer::Type::BAR);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
node->setMidpoint(Vec2(0, 0));
2021-12-25 10:04:45 +08:00
node->setBarChangeRate(Vec2(0, 1));
2019-11-23 20:27:39 +08:00
node->setPercentage(100);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
// TransitionProgressInOut
TransitionProgressInOut* TransitionProgressInOut::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressInOut* newScene = new TransitionProgressInOut();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
void TransitionProgressInOut::sceneOrder()
{
_isInSceneOnTop = false;
}
void TransitionProgressInOut::setupTransition()
{
_sceneToBeModified = _inScene;
2021-12-25 10:04:45 +08:00
_from = 0;
_to = 100;
2019-11-23 20:27:39 +08:00
}
ProgressTimer* TransitionProgressInOut::progressTimerNodeWithRenderTexture(RenderTexture* texture)
2021-12-25 10:04:45 +08:00
{
auto& size = _director->getWinSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
2021-12-25 10:04:45 +08:00
node->setType(ProgressTimer::Type::BAR);
2019-11-23 20:27:39 +08:00
node->setMidpoint(Vec2(0.5f, 0.5f));
node->setBarChangeRate(Vec2(1, 1));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
node->setPercentage(0);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
// TransitionProgressOutIn
TransitionProgressOutIn* TransitionProgressOutIn::create(float t, Scene* scene)
{
2021-12-08 00:11:53 +08:00
TransitionProgressOutIn* newScene = new TransitionProgressOutIn();
2021-12-25 10:04:45 +08:00
if (newScene->initWithDuration(t, scene))
2019-11-23 20:27:39 +08:00
{
newScene->autorelease();
return newScene;
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(newScene);
2019-11-23 20:27:39 +08:00
return nullptr;
}
ProgressTimer* TransitionProgressOutIn::progressTimerNodeWithRenderTexture(RenderTexture* texture)
2021-12-25 10:04:45 +08:00
{
auto& size = _director->getWinSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ProgressTimer* node = ProgressTimer::create(texture->getSprite());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// but it is flipped upside down so we flip the sprite
// node->getSprite()->setFlippedY(true);
2021-12-25 10:04:45 +08:00
node->setType(ProgressTimer::Type::BAR);
2019-11-23 20:27:39 +08:00
node->setMidpoint(Vec2(0.5f, 0.5f));
node->setBarChangeRate(Vec2(1, 1));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
node->setPercentage(100);
2021-12-25 10:04:45 +08:00
node->setPosition(size.width / 2, size.height / 2);
node->setAnchorPoint(Vec2(0.5f, 0.5f));
2019-11-23 20:27:39 +08:00
return node;
}
NS_AX_END