移除ActionManager

This commit is contained in:
Nomango 2018-09-05 00:08:03 +08:00
parent 8984817dd8
commit 11e9d1ce9b
15 changed files with 150 additions and 392 deletions

View File

@ -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_;
}

View File

@ -39,7 +39,6 @@ e2d::GC::~GC()
{
// 删除所有对象
Timer::GetInstance()->ClearAllTasks();
ActionManager::GetInstance()->ClearAll();
cleanup_ = true;
for (const auto& ref : pool_)

View File

@ -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;
}

View File

@ -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<Action*> 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::Action*> e2d::ActionManager::Get(const String& name)
{
std::vector<Action*> actions;
for (const auto& action : actions_)
{
if (action->GetName() == name)
{
actions.push_back(action);
}
}
return std::move(actions);
}
const std::vector<e2d::Action*>& e2d::ActionManager::GetAll()
{
return actions_;
}
void e2d::ActionManager::UpdateTime()
{
for (const auto& action : running_actions_)
{
action->ResetTime();
}
}

View File

@ -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<Updatable*>(this);
if (updatableNode)
{
@ -308,6 +316,39 @@ void e2d::Node::UpdateOpacity()
}
}
void e2d::Node::UpdateActions()
{
if (actions_.empty())
return;
std::vector<Action*> 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)

View File

@ -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_;

View File

@ -314,8 +314,6 @@ protected:
};
class Timer;
class ActionManager;
class Scene;
class Transition;

View File

@ -7,97 +7,6 @@ namespace e2d
class Node;
class Action;
// 动作管理器
class ActionManager
{
friend class Action;
public:
// 获取动作管理器实例
static ActionManager * GetInstance();
// 获取所有名称相同的动作
std::vector<Action *> Get(
const String& name
);
// 获取所有动作
const std::vector<Action*>& 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<Action*> actions_;
std::vector<Action*> running_actions_;
};
// 碰撞体管理器

View File

@ -79,6 +79,7 @@ public:
public:
typedef std::vector<Node*> Nodes;
typedef std::vector<Action*> 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_;

View File

@ -79,7 +79,6 @@
<ClCompile Include="..\..\core\Event\Collision.cpp" />
<ClCompile Include="..\..\core\Event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Canvas.cpp" />

View File

@ -154,9 +154,6 @@
<ClCompile Include="..\..\core\Custom\VoiceCallback.cpp">
<Filter>Custom</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Button.cpp">
<Filter>Node</Filter>
</ClCompile>

View File

@ -223,7 +223,6 @@
<ClCompile Include="..\..\core\Event\Collision.cpp" />
<ClCompile Include="..\..\core\Event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Canvas.cpp" />

View File

@ -154,9 +154,6 @@
<ClCompile Include="..\..\core\Custom\VoiceCallback.cpp">
<Filter>Custom</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Button.cpp">
<Filter>Node</Filter>
</ClCompile>

View File

@ -243,7 +243,6 @@
<ClCompile Include="..\..\core\Event\Collision.cpp" />
<ClCompile Include="..\..\core\Event\KeyEvent.cpp" />
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
<ClCompile Include="..\..\core\Node\Canvas.cpp" />

View File

@ -39,9 +39,6 @@
<ClCompile Include="..\..\core\Common\String.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Node\Button.cpp">
<Filter>Node</Filter>
</ClCompile>