From 11e9d1ce9b70a1485d8047a9c18e60dc7f1a24f6 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 5 Sep 2018 00:08:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4ActionManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Action/Action.cpp | 4 +- core/Base/GC.cpp | 1 - core/Base/Game.cpp | 5 +- core/Manager/ActionManager.cpp | 245 -------------------------- core/Node/Node.cpp | 153 +++++++++++++--- core/e2daction.h | 18 +- core/e2dbase.h | 2 - core/e2dmanager.h | 91 ---------- core/e2dnode.h | 11 ++ 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 - 15 files changed, 150 insertions(+), 392 deletions(-) delete mode 100644 core/Manager/ActionManager.cpp diff --git a/core/Action/Action.cpp b/core/Action/Action.cpp index 81655d57..5f46f2a6 100644 --- a/core/Action/Action.cpp +++ b/core/Action/Action.cpp @@ -7,12 +7,10 @@ e2d::Action::Action() , initialized_(false) , target_(nullptr) { - ActionManager::GetInstance()->Add(this); } e2d::Action::~Action() { - ActionManager::GetInstance()->Remove(this); } bool e2d::Action::IsRunning() @@ -57,7 +55,7 @@ void e2d::Action::Reset() started_ = Time::Now(); } -bool e2d::Action::IsDone() +bool e2d::Action::IsDone() const { return done_; } diff --git a/core/Base/GC.cpp b/core/Base/GC.cpp index d34fbfbc..321bac23 100644 --- a/core/Base/GC.cpp +++ b/core/Base/GC.cpp @@ -39,7 +39,6 @@ e2d::GC::~GC() { // 删除所有对象 Timer::GetInstance()->ClearAllTasks(); - ActionManager::GetInstance()->ClearAll(); cleanup_ = true; for (const auto& ref : pool_) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index d86e0178..870b3483 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -47,7 +47,6 @@ void e2d::Game::Start() auto input = Input::GetInstance(); auto renderer = Renderer::GetInstance(); auto timer = Timer::GetInstance(); - auto action_manager = ActionManager::GetInstance(); const int minInterval = 5; Time last = Time::Now(); @@ -71,7 +70,6 @@ void e2d::Game::Start() if (!paused_) { timer->Update(); - action_manager->Update(); UpdateScene(); } @@ -103,7 +101,8 @@ void e2d::Game::Resume() if (paused_ && !quit_) { Timer::GetInstance()->UpdateTime(); - ActionManager::GetInstance()->UpdateTime(); + if (curr_scene_) + curr_scene_->UpdateActionsTime(); } paused_ = false; } diff --git a/core/Manager/ActionManager.cpp b/core/Manager/ActionManager.cpp deleted file mode 100644 index 9ba975e9..00000000 --- a/core/Manager/ActionManager.cpp +++ /dev/null @@ -1,245 +0,0 @@ -#include "..\e2dmanager.h" -#include "..\e2daction.h" -#include "..\e2dnode.h" - - -e2d::ActionManager * e2d::ActionManager::GetInstance() -{ - static ActionManager instance; - return &instance; -} - -e2d::ActionManager::ActionManager() - : actions_() - , running_actions_() -{ -} - -e2d::ActionManager::~ActionManager() -{ -} - -void e2d::ActionManager::Update() -{ - if (running_actions_.empty()) - return; - - std::vector currActions; - currActions.reserve(running_actions_.size()); - std::copy_if( - running_actions_.begin(), - running_actions_.end(), - std::back_inserter(currActions), - [](Action* action) { return action->IsRunning() && !action->IsDone(); } - ); - - // 遍历所有正在运行的动作 - for (const auto& action : currActions) - action->Update(); - - // 清除完成的动作 - for (auto iter = running_actions_.begin(); iter != running_actions_.end();) - { - if ((*iter)->IsDone()) - { - (*iter)->Release(); - (*iter)->target_ = nullptr; - iter = running_actions_.erase(iter); - } - else - { - ++iter; - } - } -} - -void e2d::ActionManager::Add(Action * action) -{ - if (action) - { - auto iter = std::find(actions_.begin(), actions_.end(), action); - if (iter == actions_.end()) - { - actions_.push_back(action); - } - } -} - -void e2d::ActionManager::Remove(Action * action) -{ - if (actions_.empty() || action == nullptr) - return; - - auto iter = std::find(actions_.begin(), actions_.end(), action); - if (iter != actions_.end()) - { - actions_.erase(iter); - } -} - -void e2d::ActionManager::ResumeAllBindedWith(Node * target) -{ - if (running_actions_.empty() || target == nullptr) - return; - - for (const auto& action : running_actions_) - { - if (action->GetTarget() == target) - { - action->Resume(); - } - } -} - -void e2d::ActionManager::PauseAllBindedWith(Node * target) -{ - if (running_actions_.empty() || target == nullptr) - return; - - for (const auto& action : running_actions_) - { - if (action->GetTarget() == target) - { - action->Pause(); - } - } -} - -void e2d::ActionManager::StopAllBindedWith(Node * target) -{ - if (running_actions_.empty() || target == nullptr) - return; - - for (const auto& action : running_actions_) - { - if (action->GetTarget() == target) - { - action->Stop(); - } - } -} - -void e2d::ActionManager::Start(Action * action, Node * target, bool paused) -{ - WARN_IF(action == nullptr, "Action NULL pointer exception!"); - WARN_IF(target == nullptr, "GetTarget node NULL pointer exception!"); - - if (action && target) - { - if (action->target_ == nullptr) - { - auto iter = std::find(running_actions_.begin(), running_actions_.end(), action); - if (iter == running_actions_.end()) - { - action->Retain(); - action->StartWithTarget(target); - action->running_ = !paused; - running_actions_.push_back(action); - } - } - else - { - throw Exception("该 Action 已有执行目标"); - } - } -} - -void e2d::ActionManager::Resume(const String& name) -{ - if (running_actions_.empty() || name.IsEmpty()) - return; - - for (const auto& action : running_actions_) - { - if (action->GetName() == name) - { - action->Resume(); - } - } -} - -void e2d::ActionManager::Pause(const String& name) -{ - if (running_actions_.empty() || name.IsEmpty()) - return; - - for (const auto& action : running_actions_) - { - if (action->GetName() == name) - { - action->Pause(); - } - } -} - -void e2d::ActionManager::Stop(const String& name) -{ - if (running_actions_.empty() || name.IsEmpty()) - return; - - for (const auto& action : running_actions_) - { - if (action->GetName() == name) - { - action->Stop(); - } - } -} - -void e2d::ActionManager::ClearAllBindedWith(Node * target) -{ - if (target) - { - auto iter = std::find_if( - running_actions_.begin(), - running_actions_.end(), - [target](Action* action) ->bool { return action->GetTarget() == target; } - ); - - if (iter != running_actions_.end()) - { - (*iter)->Release(); - running_actions_.erase(iter); - } - } -} - -void e2d::ActionManager::ClearAll() -{ - if (!running_actions_.empty()) - { - for (const auto& action : running_actions_) - { - action->Release(); - } - running_actions_.clear(); - } - - actions_.clear(); -} - -std::vector e2d::ActionManager::Get(const String& name) -{ - std::vector actions; - for (const auto& action : actions_) - { - if (action->GetName() == name) - { - actions.push_back(action); - } - } - return std::move(actions); -} - -const std::vector& e2d::ActionManager::GetAll() -{ - return actions_; -} - -void e2d::ActionManager::UpdateTime() -{ - for (const auto& action : running_actions_) - { - action->ResetTime(); - } -} diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index e7cadf7e..f29c6107 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -31,18 +31,7 @@ e2d::Node::Property e2d::Node::Property::operator-(Property const & prop) const e2d::Node::Node() - : order_(0) - , pos_() - , size_() - , scale_(1.f, 1.f) - , rotation_(0) - , skew_(0, 0) - , display_opacity_(1.f) - , real_opacity_(1.f) - , anchor_() - , initial_matrix_(D2D1::Matrix3x2F::Identity()) - , final_matrix_(D2D1::Matrix3x2F::Identity()) - , visible_(true) + : visible_(true) , parent_(nullptr) , parent_scene_(nullptr) , hash_name_(0) @@ -52,6 +41,19 @@ e2d::Node::Node() , fixed_position_(false) , collider_(this) , border_(nullptr) + , order_(0) + , pos_() + , size_() + , scale_(1.f, 1.f) + , rotation_(0) + , skew_(0, 0) + , display_opacity_(1.f) + , real_opacity_(1.f) + , anchor_() + , children_() + , actions_() + , initial_matrix_(D2D1::Matrix3x2F::Identity()) + , final_matrix_(D2D1::Matrix3x2F::Identity()) , border_color_(Color::Red, 0.6f) , extrapolate_(Property::Origin) { @@ -61,7 +63,11 @@ e2d::Node::~Node() { SafeRelease(border_); - ActionManager::GetInstance()->ClearAllBindedWith(this); + for (const auto& action : actions_) + { + GC::GetInstance()->SafeRelease(action); + } + for (const auto& child : children_) { GC::GetInstance()->SafeRelease(child); @@ -75,6 +81,8 @@ void e2d::Node::Visit() if (!Game::GetInstance()->IsPaused()) { + UpdateActions(); + auto updatableNode = dynamic_cast(this); if (updatableNode) { @@ -308,6 +316,39 @@ void e2d::Node::UpdateOpacity() } } +void e2d::Node::UpdateActions() +{ + if (actions_.empty()) + return; + + std::vector currActions; + currActions.reserve(actions_.size()); + std::copy_if( + actions_.begin(), + actions_.end(), + std::back_inserter(currActions), + [](Action* action) { return action->IsRunning() && !action->IsDone(); } + ); + + // 遍历所有正在运行的动作 + for (const auto& action : currActions) + action->Update(); + + // 清除完成的动作 + for (auto iter = actions_.begin(); iter != actions_.end();) + { + if ((*iter)->IsDone()) + { + (*iter)->Release(); + iter = actions_.erase(iter); + } + else + { + ++iter; + } + } +} + bool e2d::Node::IsVisible() const { return visible_; @@ -796,15 +837,35 @@ void e2d::Node::RemoveAllChildren() void e2d::Node::RunAction(Action * action) { - ActionManager::GetInstance()->Start(action, this, false); + WARN_IF(action == nullptr, "Action NULL pointer exception!"); + + if (action) + { + if (action->GetTarget() == nullptr) + { + auto iter = std::find(actions_.begin(), actions_.end(), action); + if (iter == actions_.end()) + { + action->Retain(); + action->StartWithTarget(this); + actions_.push_back(action); + } + } + else + { + throw Exception("该 Action 已有执行目标"); + } + } } void e2d::Node::ResumeAction(const String& name) { - auto& actions = ActionManager::GetInstance()->Get(name); - for (const auto& action : actions) + if (actions_.empty()) + return; + + for (const auto& action : actions_) { - if (action->GetTarget() == this) + if (action->GetName() == name) { action->Resume(); } @@ -813,10 +874,12 @@ void e2d::Node::ResumeAction(const String& name) void e2d::Node::PauseAction(const String& name) { - auto& actions = ActionManager::GetInstance()->Get(name); - for (const auto& action : actions) + if (actions_.empty()) + return; + + for (const auto& action : actions_) { - if (action->GetTarget() == this) + if (action->GetName() == name) { action->Pause(); } @@ -825,10 +888,12 @@ void e2d::Node::PauseAction(const String& name) void e2d::Node::StopAction(const String& name) { - auto& actions = ActionManager::GetInstance()->Get(name); - for (const auto& action : actions) + if (actions_.empty()) + return; + + for (const auto& action : actions_) { - if (action->GetTarget() == this) + if (action->GetName() == name) { action->Stop(); } @@ -877,17 +942,53 @@ bool e2d::Node::Intersects(Node * node) void e2d::Node::ResumeAllActions() { - ActionManager::GetInstance()->ResumeAllBindedWith(this); + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Resume(); + } } void e2d::Node::PauseAllActions() { - ActionManager::GetInstance()->PauseAllBindedWith(this); + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Pause(); + } } void e2d::Node::StopAllActions() { - ActionManager::GetInstance()->StopAllBindedWith(this); + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Stop(); + } +} + +const e2d::Node::Actions & e2d::Node::GetAllActions() const +{ + return actions_; +} + +void e2d::Node::UpdateActionsTime() +{ + for (const auto& action : actions_) + { + action->ResetTime(); + } + + for (const auto& child : children_) + { + child->UpdateActionsTime(); + } } void e2d::Node::SetVisible(bool value) diff --git a/core/e2daction.h b/core/e2daction.h index 186537cc..56dab2d6 100644 --- a/core/e2daction.h +++ b/core/e2daction.h @@ -59,8 +59,10 @@ public: // 获取该动作的执行目标 virtual Node * GetTarget(); -protected: - E2D_DISABLE_COPY(Action); + // 开始动作 + virtual void StartWithTarget( + Node* target + ); // 初始化动作 virtual void Init(); @@ -68,16 +70,14 @@ protected: // 更新动作 virtual void Update(); - // 获取动作结束状态 - virtual bool IsDone(); - // 重置动作时间 virtual void ResetTime(); - // 开始动作 - virtual void StartWithTarget( - Node* target - ); + // 获取动作结束状态 + virtual bool IsDone() const; + +protected: + E2D_DISABLE_COPY(Action); protected: String name_; diff --git a/core/e2dbase.h b/core/e2dbase.h index 7b045b8a..e6445f07 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -314,8 +314,6 @@ protected: }; -class Timer; -class ActionManager; class Scene; class Transition; diff --git a/core/e2dmanager.h b/core/e2dmanager.h index 5039c0cc..3b2036d4 100644 --- a/core/e2dmanager.h +++ b/core/e2dmanager.h @@ -7,97 +7,6 @@ namespace e2d class Node; -class Action; - -// 动作管理器 -class ActionManager -{ - friend class Action; - -public: - // 获取动作管理器实例 - static ActionManager * GetInstance(); - - // 获取所有名称相同的动作 - std::vector Get( - const String& name - ); - - // 获取所有动作 - const std::vector& GetAll(); - - // 执行动作 - void Start( - Action * action, - Node * target, - bool paused - ); - - // 继续名称相同的所有动作 - void Resume( - const String& name - ); - - // 暂停名称相同的所有动作 - void Pause( - const String& name - ); - - // 停止名称相同的所有动作 - void Stop( - const String& name - ); - - // 继续绑定在节点上的所有动作 - void ResumeAllBindedWith( - Node * target - ); - - // 暂停绑定在节点上的所有动作 - void PauseAllBindedWith( - Node * target - ); - - // 停止绑定在节点上的所有动作 - void StopAllBindedWith( - Node * target - ); - - // 强制清除绑定在节点上的所有动作 - void ClearAllBindedWith( - Node * target - ); - - // 强制清除所有动作 - void ClearAll(); - - // 更新动作管理器状态 - void Update(); - - // 刷新所有动作计时 - void UpdateTime(); - -private: - ActionManager(); - - ~ActionManager(); - - E2D_DISABLE_COPY(ActionManager); - - // 添加动作 - void Add( - Action * action - ); - - // 删除动作 - void Remove( - Action * action - ); - -private: - std::vector actions_; - std::vector running_actions_; -}; // 碰撞体管理器 diff --git a/core/e2dnode.h b/core/e2dnode.h index 2de9ddc7..8a54f9e8 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -79,6 +79,7 @@ public: public: typedef std::vector Nodes; + typedef std::vector Actions; Node(); @@ -409,6 +410,12 @@ public: // 停止所有动作 void StopAllActions(); + // 获取所有动作 + const Actions& GetAllActions() const; + + // 刷新动作进度 + void UpdateActionsTime(); + // 分发鼠标消息 virtual bool Dispatch( const MouseEvent& e, @@ -447,6 +454,9 @@ protected: // 更新节点透明度 void UpdateOpacity(); + // 更新动作 + void UpdateActions(); + protected: String name_; size_t hash_name_; @@ -469,6 +479,7 @@ protected: Node * parent_; Property extrapolate_; Color border_color_; + Actions actions_; Nodes children_; ID2D1Geometry* border_; D2D1::Matrix3x2F initial_matrix_; diff --git a/project/vs2012/Easy2D.vcxproj b/project/vs2012/Easy2D.vcxproj index 233f75e2..e14b93f7 100644 --- a/project/vs2012/Easy2D.vcxproj +++ b/project/vs2012/Easy2D.vcxproj @@ -79,7 +79,6 @@ - diff --git a/project/vs2012/Easy2D.vcxproj.filters b/project/vs2012/Easy2D.vcxproj.filters index 523f97b7..f4d080ee 100644 --- a/project/vs2012/Easy2D.vcxproj.filters +++ b/project/vs2012/Easy2D.vcxproj.filters @@ -154,9 +154,6 @@ Custom - - Manager - Node diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 679f7c27..37dbee4c 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -223,7 +223,6 @@ - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index f04ff05f..a4368022 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -154,9 +154,6 @@ Custom - - Manager - Node diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index a3408b19..d7e61677 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -243,7 +243,6 @@ - diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 34fb4c3a..93fa4bea 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -39,9 +39,6 @@ Common - - Manager - Node