From 93c701751dec4395dcdc5d160d246849c53d13fb Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 11 Sep 2018 15:12:25 +0800 Subject: [PATCH] add: RotationTransition --- core/e2dmacros.h | 2 +- core/e2dtransition.h | 33 ++++++++-- core/tools/Music.cpp | 2 +- core/transitions/BoxTransition.cpp | 25 +++----- core/transitions/EmergeTransition.cpp | 9 +-- core/transitions/FadeTransition.cpp | 10 +-- core/transitions/MoveTransition.cpp | 30 ++++----- core/transitions/RotationTransition.cpp | 83 +++++++++++++++++++++++++ core/transitions/Transition.cpp | 13 ++-- core/utils/Rect.cpp | 4 +- project/vs2012/Easy2D.vcxproj | 1 + project/vs2012/Easy2D.vcxproj.filters | 3 + project/vs2013/Easy2D.vcxproj | 1 + project/vs2013/Easy2D.vcxproj.filters | 3 + project/vs2017/Easy2D.vcxproj | 1 + project/vs2017/Easy2D.vcxproj.filters | 3 + 16 files changed, 162 insertions(+), 61 deletions(-) create mode 100644 core/transitions/RotationTransition.cpp diff --git a/core/e2dmacros.h b/core/e2dmacros.h index d64ee1cc..37d7591f 100644 --- a/core/e2dmacros.h +++ b/core/e2dmacros.h @@ -1,7 +1,7 @@ #pragma once #ifndef WINVER -# define WINVER 0x0700 // Allow use of features specific to Windows 7 or later +# define WINVER 0x0700 // Allow use of features specific to Windows 7 or later #endif #ifndef _WIN32_WINNT diff --git a/core/e2dtransition.h b/core/e2dtransition.h index 98171602..e14c856a 100644 --- a/core/e2dtransition.h +++ b/core/e2dtransition.h @@ -84,7 +84,7 @@ namespace e2d { public: explicit EmergeTransition( - float duration /* 浮现动画持续时长 */ + float duration /* 动画持续时长 */ ); protected: @@ -116,14 +116,14 @@ namespace e2d }; - // 移入过渡 + // 位移过渡 class MoveTransition : public Transition { public: explicit MoveTransition( - float moveDuration, /* 场景移动动画持续时长 */ - Direction direction = Direction::Left /* 场景移动方向 */ + float moveDuration, /* 动画持续时长 */ + Direction direction = Direction::Left /* 移动方向 */ ); protected: @@ -142,4 +142,29 @@ namespace e2d Point start_pos_; }; + + // 旋转过渡 + class RotationTransition + : public Transition + { + public: + explicit MoveTransition( + float moveDuration, /* 动画持续时长 */ + float rotation = 360 /* 旋转度数 */ + ); + + protected: + virtual void Update() override; + + virtual void Init( + Scene * prev, + Scene * next + ) override; + + virtual void Reset() override; + + protected: + float rotation_; + } + } \ No newline at end of file diff --git a/core/tools/Music.cpp b/core/tools/Music.cpp index c808793c..48d4fa68 100644 --- a/core/tools/Music.cpp +++ b/core/tools/Music.cpp @@ -3,7 +3,7 @@ #ifndef SAFE_DELETE -#define SAFE_DELETE(p) { if (p) { delete (p); (p)=nullptr; } } +#define SAFE_DELETE(p) { if (p) { delete (p); (p)=nullptr; } } #endif #ifndef SAFE_DELETE_ARRAY diff --git a/core/transitions/BoxTransition.cpp b/core/transitions/BoxTransition.cpp index 65a1cb02..29878aab 100644 --- a/core/transitions/BoxTransition.cpp +++ b/core/transitions/BoxTransition.cpp @@ -1,6 +1,4 @@ #include "..\e2dtransition.h" -#include "..\e2dobject.h" -#include "..\e2dmodule.h" e2d::BoxTransition::BoxTransition(float duration) : Transition(duration) @@ -18,14 +16,13 @@ void e2d::BoxTransition::Update() { Transition::Update(); - auto size = Window::GetInstance()->GetSize(); - if (delta_ <= 0.5) + if (process_ < .5f) { out_layer_param_.contentBounds = D2D1::RectF( - size.width * delta_, - size.height * delta_, - size.width * (1 - delta_), - size.height * (1 - delta_) + window_size_.width * process_, + window_size_.height * process_, + window_size_.width * (1 - process_), + window_size_.height * (1 - process_) ); } else @@ -33,14 +30,10 @@ void e2d::BoxTransition::Update() out_layer_param_.opacity = 0; in_layer_param_.opacity = 1; in_layer_param_.contentBounds = D2D1::RectF( - size.width * (1 - delta_), - size.height * (1 - delta_), - size.width * delta_, - size.height * delta_ + window_size_.width * (1 - process_), + window_size_.height * (1 - process_), + window_size_.width * process_, + window_size_.height * process_ ); - if (delta_ >= 1) - { - this->Stop(); - } } } diff --git a/core/transitions/EmergeTransition.cpp b/core/transitions/EmergeTransition.cpp index 15b0d996..75531862 100644 --- a/core/transitions/EmergeTransition.cpp +++ b/core/transitions/EmergeTransition.cpp @@ -18,11 +18,6 @@ void e2d::EmergeTransition::Update() { Transition::Update(); - out_layer_param_.opacity = 1 - delta_; - in_layer_param_.opacity = delta_; - - if (delta_ >= 1) - { - this->Stop(); - } + out_layer_param_.opacity = 1 - process_; + in_layer_param_.opacity = process_; } diff --git a/core/transitions/FadeTransition.cpp b/core/transitions/FadeTransition.cpp index 942fd091..eba42866 100644 --- a/core/transitions/FadeTransition.cpp +++ b/core/transitions/FadeTransition.cpp @@ -18,18 +18,14 @@ void e2d::FadeTransition::Update() { Transition::Update(); - if (delta_ < 0.5) + if (process_ < 0.5) { - out_layer_param_.opacity = 1 - delta_ * 2; + out_layer_param_.opacity = 1 - process_ * 2; in_layer_param_.opacity = 0; } else { out_layer_param_.opacity = 0; - in_layer_param_.opacity = (delta_ - 0.5f) * 2; - if (delta_ >= 1) - { - this->Stop(); - } + in_layer_param_.opacity = (process_ - 0.5f) * 2; } } diff --git a/core/transitions/MoveTransition.cpp b/core/transitions/MoveTransition.cpp index a8d7818c..c50422b1 100644 --- a/core/transitions/MoveTransition.cpp +++ b/core/transitions/MoveTransition.cpp @@ -1,6 +1,4 @@ #include "..\e2dtransition.h" -#include "..\e2dobject.h" -#include "..\e2dmodule.h" e2d::MoveTransition::MoveTransition(float duration, Direction direction) : Transition(duration) @@ -12,25 +10,24 @@ void e2d::MoveTransition::Init(Scene * prev, Scene * next) { Transition::Init(prev, next); - if (direction_ == Direction::Up) + switch (direction_) { + case Direction::Up: pos_delta_ = Point(0, -window_size_.height); start_pos_ = Point(0, window_size_.height); - } - else if (direction_ == Direction::Down) - { + break; + case Direction::Down: pos_delta_ = Point(0, window_size_.height); start_pos_ = Point(0, -window_size_.height); - } - else if (direction_ == Direction::Left) - { + break; + case Direction::Left: pos_delta_ = Point(-window_size_.width, 0); start_pos_ = Point(window_size_.width, 0); - } - else if (direction_ == Direction::Right) - { + break; + case Direction::Right: pos_delta_ = Point(window_size_.width, 0); start_pos_ = Point(-window_size_.width, 0); + break; } if (out_scene_) @@ -55,7 +52,7 @@ void e2d::MoveTransition::Update() if (out_scene_) { - auto translation = pos_delta_ * delta_; + auto translation = pos_delta_ * process_; out_scene_->SetTransform( D2D1::Matrix3x2F::Translation( translation.x, @@ -66,7 +63,7 @@ void e2d::MoveTransition::Update() if (in_scene_) { - auto translation = start_pos_ + pos_delta_ * delta_; + auto translation = start_pos_ + pos_delta_ * process_; in_scene_->SetTransform( D2D1::Matrix3x2F::Translation( translation.x, @@ -74,11 +71,6 @@ void e2d::MoveTransition::Update() ) ); } - - if (delta_ >= 1) - { - this->Stop(); - } } void e2d::MoveTransition::Reset() diff --git a/core/transitions/RotationTransition.cpp b/core/transitions/RotationTransition.cpp new file mode 100644 index 00000000..57c1c9f5 --- /dev/null +++ b/core/transitions/RotationTransition.cpp @@ -0,0 +1,83 @@ +#include "..\e2dtransition.h" + +e2d::RotationTransition::RotationTransition(float duration, float rotation) + : Transition(duration) + , rotation_(rotation) +{ +} + +void e2d::RotationTransition::Init(Scene * prev, Scene * next) +{ + Transition::Init(prev, next); + + if (out_scene_) + { + out_scene_->SetTransform(D2D1::Matrix3x2F::Identity()); + } + + if (in_scene_) + { + in_scene_->SetTransform(D2D1::Matrix3x2F::Identity()); + } + + in_layer_param_.opacity = 0; +} + +void e2d::RotationTransition::Update() +{ + Transition::Update(); + + auto center_pos = D2D1::Point2F( + window_size_.width / 2, + window_size_.height / 2 + ); + + if (process_ < .5f) + { + if (out_scene_) + { + out_scene_->SetTransform( + D2D1::Matrix3x2F::Scale( + (.5f - process_) * 2, + (.5f - process_) * 2, + center_pos + ) * D2D1::Matrix3x2F::Rotation( + rotation * (.5f - process_) * 2, + center_pos, + ) + ); + } + } + else + { + if (in_scene_) + { + out_layer_param_.opacity = 0; + in_layer_param_.opacity = 1; + + in_scene_->SetTransform( + D2D1::Matrix3x2F::Scale( + (process_ - .5f) * 2, + (process_ - .5f) * 2, + center_pos + ) * D2D1::Matrix3x2F::Rotation( + rotation * (process_ - .5f) * 2, + center_pos, + ) + ); + } + } +} + +void e2d::RotationTransition::Reset() +{ + if (out_scene_) + { + out_scene_->SetTransform(D2D1::Matrix3x2F::Identity()); + } + + if (in_scene_) + { + in_scene_->SetTransform(D2D1::Matrix3x2F::Identity()); + } +} diff --git a/core/transitions/Transition.cpp b/core/transitions/Transition.cpp index 7374903f..68d1531b 100644 --- a/core/transitions/Transition.cpp +++ b/core/transitions/Transition.cpp @@ -5,7 +5,7 @@ e2d::Transition::Transition(float duration) : done_(false) , started_() - , delta_(0) + , process_(0) , window_size_() , out_scene_(nullptr) , in_scene_(nullptr) @@ -78,12 +78,17 @@ void e2d::Transition::Update() { if (duration_ == 0) { - delta_ = 1; + process_ = 1; } else { - delta_ = (Time::Now() - started_).Seconds() / duration_; - delta_ = std::min(delta_, 1.f); + process_ = (Time::Now() - started_).Seconds() / duration_; + process_ = std::min(process_, 1.f); + } + + if (process_ >= 1) + { + this->Stop(); } } diff --git a/core/utils/Rect.cpp b/core/utils/Rect.cpp index 8c666100..9080602f 100644 --- a/core/utils/Rect.cpp +++ b/core/utils/Rect.cpp @@ -49,7 +49,7 @@ bool e2d::Rect::ContainsPoint(const Point& point) const bool e2d::Rect::Intersects(const Rect& rect) const { return !((origin.x + size.width) < rect.origin.x || - (rect.origin.x + rect.size.width) < origin.x || + (rect.origin.x + rect.size.width) < origin.x || (origin.y + size.height) < rect.origin.y || - (rect.origin.y + rect.size.height) < origin.y); + (rect.origin.y + rect.size.height) < origin.y); } diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index e045201a..309c9e1e 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -72,6 +72,7 @@ + diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 35b3d936..b187697b 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -198,6 +198,9 @@ transitions + + transitions + transitions diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 25795ca8..ddece2e5 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -216,6 +216,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 35b3d936..b187697b 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -198,6 +198,9 @@ transitions + + transitions + transitions diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 8a92fa4d..71f8b250 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -249,6 +249,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 35b3d936..b187697b 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -198,6 +198,9 @@ transitions + + transitions + transitions