修改Transition处理方式

This commit is contained in:
Nomango 2018-08-14 00:26:20 +08:00
parent adcd8ff1f2
commit 1c1f349234
9 changed files with 150 additions and 124 deletions

View File

@ -42,14 +42,6 @@ void e2d::SceneManager::push(Scene * scene, bool saveCurrentScene)
if (_nextScene) _nextScene->release(); if (_nextScene) _nextScene->release();
_nextScene = scene; _nextScene = scene;
_nextScene->retain(); _nextScene->retain();
// 初始化场景切换动画
if (_transition && !_transition->init(_currScene, _nextScene))
{
WARN("Transition initialize failed!");
_transition->release();
_transition = nullptr;
}
if (saveCurrentScene && _currScene) if (saveCurrentScene && _currScene)
{ {
@ -57,6 +49,30 @@ void e2d::SceneManager::push(Scene * scene, bool saveCurrentScene)
} }
} }
void e2d::SceneManager::push(Transition * transition, bool saveCurrentScene)
{
if (!transition)
return;
SceneManager::push(transition->_inScene, saveCurrentScene);
if (_transition)
{
_transition->_stop();
_transition->release();
}
_transition = transition;
_transition->retain();
// 初始化场景切换动画
if (!_transition->_init(_currScene))
{
WARN("Transition initialize failed!");
_transition->release();
_transition = nullptr;
}
}
e2d::Scene* e2d::SceneManager::pop() e2d::Scene* e2d::SceneManager::pop()
{ {
// 栈为空时,调用返回场景函数失败 // 栈为空时,调用返回场景函数失败
@ -67,31 +83,41 @@ e2d::Scene* e2d::SceneManager::pop()
} }
_nextScene = _scenes.top(); _nextScene = _scenes.top();
_nextScene->release();
_scenes.pop(); _scenes.pop();
// 初始化场景切换动画
if (_transition && !_transition->init(_currScene, _nextScene))
{
WARN("Transition initialize failed!");
_transition->release();
_transition = nullptr;
}
return _nextScene; return _nextScene;
} }
void e2d::SceneManager::setTransition(Transition * transition) e2d::Scene * e2d::SceneManager::pop(Transition * transition)
{ {
if (transition) if (!transition)
return nullptr;
auto scene = SceneManager::pop();
if (scene)
{ {
if (_transition) if (_transition)
{ {
_transition->stop(); _transition->_stop();
_transition->release(); _transition->release();
} }
_transition = transition; _transition = transition;
_transition->retain(); _transition->retain();
_transition->_inScene = scene;
_transition->_inScene->retain();
// 初始化场景切换动画
if (!_transition->_init(_currScene))
{
WARN("Transition initialize failed!");
_transition->release();
_transition = nullptr;
}
} }
return scene;
} }
void e2d::SceneManager::clear() void e2d::SceneManager::clear()
@ -122,7 +148,7 @@ void e2d::SceneManager::update()
{ {
if (_transition) if (_transition)
{ {
_transition->update(); _transition->_update();
if (_transition->isDone()) if (_transition->isDone())
{ {
@ -157,7 +183,7 @@ void e2d::SceneManager::render()
{ {
if (_transition) if (_transition)
{ {
_transition->render(); _transition->_render();
} }
else if (_currScene) else if (_currScene)
{ {

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::BoxTransition::BoxTransition(float duration) e2d::BoxTransition::BoxTransition(Scene* scene, float duration)
: Transition(duration) : Transition(scene, duration)
{ {
} }
bool e2d::BoxTransition::init(Scene * prev, Scene * next) bool e2d::BoxTransition::_init(Scene * prev)
{ {
if (Transition::init(prev, next)) if (Transition::_init(prev))
{ {
_inLayerParam.opacity = 0; _inLayerParam.opacity = 0;
return true; return true;
@ -16,9 +16,9 @@ bool e2d::BoxTransition::init(Scene * prev, Scene * next)
return false; return false;
} }
void e2d::BoxTransition::update() void e2d::BoxTransition::_update()
{ {
Transition::update(); Transition::_update();
if (_delta <= 0.5) if (_delta <= 0.5)
{ {
@ -41,11 +41,7 @@ void e2d::BoxTransition::update()
); );
if (_delta >= 1) if (_delta >= 1)
{ {
this->stop(); this->_stop();
} }
} }
} }
void e2d::BoxTransition::reset()
{
}

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::EmergeTransition::EmergeTransition(float duration) e2d::EmergeTransition::EmergeTransition(Scene* scene, float duration)
: Transition(duration) : Transition(scene, duration)
{ {
} }
bool e2d::EmergeTransition::init(Scene * prev, Scene * next) bool e2d::EmergeTransition::_init(Scene * prev)
{ {
if (Transition::init(prev, next)) if (Transition::_init(prev))
{ {
_outLayerParam.opacity = 1; _outLayerParam.opacity = 1;
_inLayerParam.opacity = 0; _inLayerParam.opacity = 0;
@ -17,19 +17,15 @@ bool e2d::EmergeTransition::init(Scene * prev, Scene * next)
return false; return false;
} }
void e2d::EmergeTransition::update() void e2d::EmergeTransition::_update()
{ {
Transition::update(); Transition::_update();
_outLayerParam.opacity = 1 - _delta; _outLayerParam.opacity = 1 - _delta;
_inLayerParam.opacity = _delta; _inLayerParam.opacity = _delta;
if (_delta >= 1) if (_delta >= 1)
{ {
this->stop(); this->_stop();
} }
} }
void e2d::EmergeTransition::reset()
{
}

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::FadeTransition::FadeTransition(float duration) e2d::FadeTransition::FadeTransition(Scene* scene, float duration)
: Transition(duration) : Transition(scene, duration)
{ {
} }
bool e2d::FadeTransition::init(Scene * prev, Scene * next) bool e2d::FadeTransition::_init(Scene * prev)
{ {
if (Transition::init(prev, next)) if (Transition::_init(prev))
{ {
_outLayerParam.opacity = 1; _outLayerParam.opacity = 1;
_inLayerParam.opacity = 0; _inLayerParam.opacity = 0;
@ -17,9 +17,9 @@ bool e2d::FadeTransition::init(Scene * prev, Scene * next)
return false; return false;
} }
void e2d::FadeTransition::update() void e2d::FadeTransition::_update()
{ {
Transition::update(); Transition::_update();
if (_delta < 0.5) if (_delta < 0.5)
{ {
@ -32,11 +32,7 @@ void e2d::FadeTransition::update()
_inLayerParam.opacity = (_delta - 0.5f) * 2; _inLayerParam.opacity = (_delta - 0.5f) * 2;
if (_delta >= 1) if (_delta >= 1)
{ {
this->stop(); this->_stop();
} }
} }
} }
void e2d::FadeTransition::reset()
{
}

View File

@ -1,15 +1,15 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::MoveTransition::MoveTransition(float duration, Direction direction) e2d::MoveTransition::MoveTransition(Scene* scene, float duration, Direction direction)
: Transition(duration) : Transition(scene, duration)
, _direction(direction) , _direction(direction)
{ {
} }
bool e2d::MoveTransition::init(Scene * prev, Scene * next) bool e2d::MoveTransition::_init(Scene * prev)
{ {
if (Transition::init(prev, next)) if (Transition::_init(prev))
{ {
float width = _windowSize.width; float width = _windowSize.width;
float height = _windowSize.height; float height = _windowSize.height;
@ -41,9 +41,9 @@ bool e2d::MoveTransition::init(Scene * prev, Scene * next)
return false; return false;
} }
void e2d::MoveTransition::update() void e2d::MoveTransition::_update()
{ {
Transition::update(); Transition::_update();
if (_outScene) if (_outScene)
{ {
@ -56,11 +56,11 @@ void e2d::MoveTransition::update()
if (_delta >= 1) if (_delta >= 1)
{ {
this->stop(); this->_stop();
} }
} }
void e2d::MoveTransition::reset() void e2d::MoveTransition::_reset()
{ {
if (_outScene) _outScene->setPos(0, 0); if (_outScene) _outScene->setPos(0, 0);
_inScene->setPos(0, 0); _inScene->setPos(0, 0);

View File

@ -2,18 +2,20 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dnode.h" #include "..\e2dnode.h"
e2d::Transition::Transition(float duration) e2d::Transition::Transition(Scene* scene, float duration)
: _end(false) : _end(false)
, _started() , _started()
, _delta(0) , _delta(0)
, _outScene(nullptr) , _outScene(nullptr)
, _inScene(nullptr) , _inScene(scene)
, _outLayer(nullptr) , _outLayer(nullptr)
, _inLayer(nullptr) , _inLayer(nullptr)
, _outLayerParam() , _outLayerParam()
, _inLayerParam() , _inLayerParam()
{ {
_duration = std::max(duration, 0.f); _duration = std::max(duration, 0.f);
if (_inScene)
_inScene->retain();
} }
e2d::Transition::~Transition() e2d::Transition::~Transition()
@ -29,13 +31,23 @@ bool e2d::Transition::isDone()
return _end; return _end;
} }
bool e2d::Transition::init(Scene * prev, Scene * next) bool e2d::Transition::_init(Scene * prev)
{ {
auto renderer = Renderer::getInstance(); _started = Time::now();
// ´´½¨Í¼²ã _outScene = prev;
HRESULT hr = renderer->getRenderTarget()->CreateLayer(&_inLayer);
if (SUCCEEDED(hr)) if (_outScene)
_outScene->retain();
// ´´½¨Í¼²ã
HRESULT hr = S_OK;
auto renderer = Renderer::getInstance();
if (_inScene)
{
hr = renderer->getRenderTarget()->CreateLayer(&_inLayer);
}
if (SUCCEEDED(hr) && _outScene)
{ {
hr = renderer->getRenderTarget()->CreateLayer(&_outLayer); hr = renderer->getRenderTarget()->CreateLayer(&_outLayer);
} }
@ -45,12 +57,6 @@ bool e2d::Transition::init(Scene * prev, Scene * next)
return false; return false;
} }
_started = Time::now();
_outScene = prev;
_inScene = next;
if (_outScene) _outScene->retain();
if (_inScene) _inScene->retain();
_windowSize = Window::getInstance()->getSize(); _windowSize = Window::getInstance()->getSize();
_outLayerParam = _inLayerParam = D2D1::LayerParameters( _outLayerParam = _inLayerParam = D2D1::LayerParameters(
D2D1::InfiniteRect(), D2D1::InfiniteRect(),
@ -65,7 +71,7 @@ bool e2d::Transition::init(Scene * prev, Scene * next)
return true; return true;
} }
void e2d::Transition::update() void e2d::Transition::_update()
{ {
if (_duration == 0) if (_duration == 0)
{ {
@ -78,7 +84,7 @@ void e2d::Transition::update()
} }
} }
void e2d::Transition::render() void e2d::Transition::_render()
{ {
auto pRT = Renderer::getInstance()->getRenderTarget(); auto pRT = Renderer::getInstance()->getRenderTarget();
@ -121,8 +127,8 @@ void e2d::Transition::render()
} }
} }
void e2d::Transition::stop() void e2d::Transition::_stop()
{ {
_end = true; _end = true;
reset(); _reset();
} }

View File

@ -24,16 +24,22 @@ public:
// 场景入栈 // 场景入栈
void push( void push(
Scene * scene, /* 下一个场景的指针 */ Scene * scene, /* 下一个场景的指针 */
bool saveCurrentScene = true /* 是否保存当前场景 */ bool saveCurrentScene = true /* 是否保存当前场景 */
);
// 场景入栈
void push(
Transition * transition, /* 场景动画 */
bool saveCurrentScene = true /* 是否保存当前场景 */
); );
// 场景出栈 // 场景出栈
Scene* pop(); Scene* pop();
// 设置场景切换动作 // 场景出栈
void setTransition( Scene* pop(
Transition * transition /* 场景切换动作 */ Transition * transition /* 场景动画 */
); );
// 清空保存的所有场景 // 清空保存的所有场景

View File

@ -5,7 +5,7 @@ namespace e2d
{ {
class Layer; class Scene;
class Action; class Action;
class Transition; class Transition;
class CollisionManager; class CollisionManager;
@ -14,7 +14,6 @@ class Node :
public Ref public Ref
{ {
friend class Scene; friend class Scene;
friend class Layer;
friend class Collider; friend class Collider;
friend class Transition; friend class Transition;
friend class CollisionManager; friend class CollisionManager;

View File

@ -6,36 +6,42 @@ namespace e2d
class Scene; class Scene;
class SceneManager;
// 场景过渡 // 场景过渡
class Transition : class Transition :
public Ref public Ref
{ {
friend class SceneManager;
public: public:
explicit Transition(float duration); explicit Transition(
Scene* scene,
float duration
);
virtual ~Transition(); virtual ~Transition();
// 场景过渡动画是否结束 // 场景过渡动画是否结束
bool isDone(); bool isDone();
protected:
// 初始化场景过渡动画 // 初始化场景过渡动画
virtual bool init( virtual bool _init(
Scene * prev, Scene * prev
Scene * next
); );
// 更新场景过渡动画 // 更新场景过渡动画
virtual void update(); virtual void _update();
// 渲染场景过渡动画 // 渲染场景过渡动画
virtual void render(); virtual void _render();
// 停止场景过渡动画 // 停止场景过渡动画
virtual void stop(); virtual void _stop();
// 重置场景过渡动画 // 重置场景过渡动画
virtual void reset() = 0; virtual void _reset() { };
protected: protected:
bool _end; bool _end;
@ -58,18 +64,17 @@ class FadeTransition :
{ {
public: public:
explicit FadeTransition( explicit FadeTransition(
float duration /* 动画持续时长 */ Scene* scene, /* 切换的场景 */
float duration /* 动画持续时长 */
); );
protected:
// 更新动画 // 更新动画
virtual void update() override; virtual void _update() override;
virtual bool init( virtual bool _init(
Scene * prev, Scene * prev
Scene * next
) override; ) override;
virtual void reset() override;
}; };
@ -79,18 +84,16 @@ class EmergeTransition :
{ {
public: public:
explicit EmergeTransition( explicit EmergeTransition(
float duration /* 浮现动画持续时长 */ Scene* scene, /* 切换的场景 */
float duration /* 浮现动画持续时长 */
); );
// 更新动画 protected:
virtual void update() override; virtual void _update() override;
virtual bool init( virtual bool _init(
Scene * prev, Scene * prev
Scene * next
) override; ) override;
virtual void reset() override;
}; };
@ -100,18 +103,16 @@ class BoxTransition :
{ {
public: public:
explicit BoxTransition( explicit BoxTransition(
float duration /* 动画持续时长 */ Scene* scene, /* 切换的场景 */
float duration /* 动画持续时长 */
); );
// 更新动画 protected:
virtual void update() override; virtual void _update() override;
virtual bool init( virtual bool _init(
Scene * prev, Scene * prev
Scene * next
) override; ) override;
virtual void reset() override;
}; };
@ -121,19 +122,19 @@ class MoveTransition :
{ {
public: public:
explicit MoveTransition( explicit MoveTransition(
float moveDuration, /* 场景移动动画持续时长 */ Scene* scene, /* 切换的场景 */
float moveDuration, /* 场景移动动画持续时长 */
Direction direction = Direction::Left /* 场景移动方向 */ Direction direction = Direction::Left /* 场景移动方向 */
); );
// 更新动画 protected:
virtual void update() override; virtual void _update() override;
virtual bool init( virtual bool _init(
Scene * prev, Scene * prev
Scene * next
) override; ) override;
virtual void reset() override; virtual void _reset() override;
protected: protected:
Direction _direction; Direction _direction;