add ActionGroup

This commit is contained in:
Nomango 2019-02-11 18:57:28 +08:00 committed by Nomango
parent 2c1cef7e4d
commit 20ebd63215
7 changed files with 189 additions and 204 deletions

View File

@ -24,9 +24,9 @@ public:
// 执行动画 // 执行动画
text->AddAction( text->AddAction(
Tween::Spawn({ // Action5: 同时执行 Action1 和 Action4, 并无限循环 Tween::Multiple({ // Action5: 同时执行 Action1 和 Action4, 并无限循环
Tween::RotateBy(60).SetDuration(1000), // Action1: 1秒旋转 60 度 Tween::RotateBy(60).SetDuration(1000), // Action1: 1秒旋转 60 度
Tween::Sequence({ // Action4: 顺序执行 Action2 和 Action3 Tween::Group({ // Action4: 顺序执行 Action2 和 Action3
Tween::FadeOut(500), // Action2: 500毫秒淡出动画 Tween::FadeOut(500), // Action2: 500毫秒淡出动画
Tween::FadeIn(500) // Action3: 500毫秒淡入动画 Tween::FadeIn(500) // Action3: 500毫秒淡入动画
}) })

View File

@ -34,8 +34,7 @@ namespace easy2d
, protected IntrusiveListItem<ActionPtr> , protected IntrusiveListItem<ActionPtr>
{ {
friend class ActionManager; friend class ActionManager;
friend class ActionSequence; friend class ActionGroup;
friend class ActionSpawn;
friend class IntrusiveList<ActionPtr>; friend class IntrusiveList<ActionPtr>;
public: public:

View File

@ -23,189 +23,162 @@
namespace easy2d namespace easy2d
{ {
//-------------------------------------------------------
// ActionGroup
//-------------------------------------------------------
ActionGroup::ActionGroup()
: sequence_(true)
{
}
ActionGroup::ActionGroup(Array<ActionPtr> const& actions, bool sequence)
: sequence_(sequence)
{
this->Add(actions);
}
ActionGroup::~ActionGroup()
{
}
void ActionGroup::Init(NodePtr const& target)
{
if (actions_.IsEmpty())
{
Done();
return;
}
current_ = actions_.First();
current_->Restart(target); // init first action
if (!sequence_)
{
// init all actions
for (; current_; current_ = current_->NextItem())
{
current_->Restart(target);
}
}
}
void ActionGroup::Update(NodePtr const& target, Duration dt)
{
if (sequence_)
{
if (current_)
{
current_->UpdateStep(target, dt);
if (current_->IsDone())
{
current_ = current_->NextItem();
if (current_)
current_->Restart(target); // init next action
else
Complete(target);
}
}
}
else
{
bool done = true;
for (current_ = actions_.First(); current_; current_ = current_->NextItem())
{
if (!current_->IsDone())
{
done = false;
current_->UpdateStep(target, dt);
}
}
if (done)
{
Complete(target);
}
}
}
void ActionGroup::Add(ActionPtr const& action)
{
if (action)
{
actions_.PushBack(action);
}
}
void ActionGroup::Add(Array<ActionPtr> const& actions)
{
for (const auto& action : actions)
Add(action);
}
ActionPtr ActionGroup::Clone() const
{
auto group = new (std::nothrow) ActionGroup();
if (group)
{
for (auto action = actions_.First(); action; action = action->NextItem())
{
if (action)
{
group->Add(action->Clone());
}
}
}
return group;
}
ActionPtr ActionGroup::Reverse() const
{
auto group = new (std::nothrow) ActionGroup();
if (group && !actions_.IsEmpty())
{
for (auto action = actions_.Last(); action; action = action->PrevItem())
{
group->Add(action->Reverse());
}
}
return group;
}
//------------------------------------------------------- //-------------------------------------------------------
// ActionSequence // ActionSequence
//------------------------------------------------------- //-------------------------------------------------------
ActionSequence::ActionSequence() ActionSequence::ActionSequence()
: ActionGroup()
{ {
} }
ActionSequence::ActionSequence(Array<ActionPtr> const& actions) ActionSequence::ActionSequence(Array<ActionPtr> const & actions)
: ActionGroup(actions, true)
{ {
this->Add(actions);
} }
ActionSequence::~ActionSequence() ActionSequence::~ActionSequence()
{ {
} }
void ActionSequence::Init(NodePtr const& target)
{
if (actions_.IsEmpty())
Done();
else
{
current_ = actions_.First();
current_->Restart(target); // init
}
}
void ActionSequence::Update(NodePtr const& target, Duration dt)
{
if (current_)
{
current_->UpdateStep(target, dt);
if (current_->IsDone())
{
current_ = current_->NextItem();
if (current_)
current_->Restart(target);
}
}
if (!current_)
{
Complete(target);
}
}
void ActionSequence::Add(ActionPtr const& action)
{
if (action)
{
actions_.PushBack(action);
}
}
void ActionSequence::Add(Array<ActionPtr> const& actions)
{
for (const auto& action : actions)
Add(action);
}
ActionPtr ActionSequence::Clone() const
{
auto sequence = new (std::nothrow) ActionSequence();
if (sequence)
{
for (auto action = actions_.First(); action; action = action->NextItem())
{
if (action)
{
sequence->Add(action->Clone());
}
}
}
return sequence;
}
ActionPtr ActionSequence::Reverse() const
{
auto sequence = new (std::nothrow) ActionSequence();
if (sequence && !actions_.IsEmpty())
{
for (auto action = actions_.Last(); action; action = action->PrevItem())
{
sequence->Add(action->Reverse());
}
}
return sequence;
}
//------------------------------------------------------- //-------------------------------------------------------
// ActionSpawn // ActionSpawn
//------------------------------------------------------- //-------------------------------------------------------
ActionSpawn::ActionSpawn() ActionSpawn::ActionSpawn()
: size_(0) : ActionGroup()
{ {
sequence_ = false;
} }
ActionSpawn::ActionSpawn(Array<ActionPtr> const& actions) ActionSpawn::ActionSpawn(Array<ActionPtr> const & actions)
: size_(0) : ActionGroup(actions, false)
{ {
this->Add(actions);
} }
ActionSpawn::~ActionSpawn() ActionSpawn::~ActionSpawn()
{ {
} }
void ActionSpawn::Init(NodePtr const& target)
{
if (actions_.IsEmpty())
Done();
else
{
for (auto action = actions_.First(); action; action = action->NextItem())
{
action->Restart(target); // init
}
}
}
void ActionSpawn::Update(NodePtr const& target, Duration dt)
{
int done_num = 0;
for (auto action = actions_.First(); action; action = action->NextItem())
{
if (action->IsDone())
{
++done_num;
}
else
{
action->UpdateStep(target, dt);
}
}
if (done_num == size_)
{
Complete(target);
}
}
void ActionSpawn::Add(ActionPtr const& action)
{
if (action)
{
actions_.PushBack(action);
++size_;
}
}
void ActionSpawn::Add(Array<ActionPtr> const& actions)
{
for (const auto& action : actions)
Add(action);
}
ActionPtr ActionSpawn::Clone() const
{
auto spawn = new (std::nothrow) ActionSpawn();
if (spawn)
{
for (auto action = actions_.First(); action; action = action->NextItem())
{
spawn->Add(action->Clone());
}
}
return spawn;
}
ActionPtr ActionSpawn::Reverse() const
{
auto spawn = new (std::nothrow) ActionSpawn();
if (spawn && !actions_.IsEmpty())
{
for (auto action = actions_.Last(); action; action = action->PrevItem())
{
spawn->Add(action->Reverse());
}
}
return spawn;
}
} }

View File

@ -23,29 +23,35 @@
namespace easy2d namespace easy2d
{ {
// 顺序动作 // 动作
class E2D_API ActionSequence class E2D_API ActionGroup
: public Action : public Action
{ {
public: public:
ActionSequence(); using ActionList = IntrusiveList<ActionPtr>;
explicit ActionSequence( ActionGroup();
Array<ActionPtr> const& actions /* 动作列表 */
explicit ActionGroup(
Array<ActionPtr> const& actions,
bool sequence = true // 按顺序执行或同时执行
); );
virtual ~ActionSequence(); virtual ~ActionGroup();
// 在结尾添加动作 // 添加动作
void Add( void Add(
ActionPtr const& action ActionPtr const& action
); );
// 在结尾添加多个动作 // 添加多个动作
void Add( void Add(
Array<ActionPtr> const& actions /* 动作列表 */ Array<ActionPtr> const& actions
); );
// 获取所有动作
inline ActionList const& GetActions() { return actions_; }
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
ActionPtr Clone() const override; ActionPtr Clone() const override;
@ -60,49 +66,40 @@ namespace easy2d
void Update(NodePtr const& target, Duration dt) override; void Update(NodePtr const& target, Duration dt) override;
protected: protected:
ActionPtr current_; bool sequence_;
IntrusiveList<ActionPtr> actions_; ActionPtr current_;
ActionList actions_;
};
// 顺序动作
class E2D_DEPRECATED("ActionSequence is deprecated, use ActionGroup instead") E2D_API
ActionSequence
: public ActionGroup
{
public:
ActionSequence();
explicit ActionSequence(
Array<ActionPtr> const& actions
);
virtual ~ActionSequence();
}; };
// 同步动作 // 同步动作
class E2D_API ActionSpawn class E2D_DEPRECATED("ActionSpawn is deprecated, use ActionGroup instead") E2D_API
: public Action ActionSpawn
: public ActionGroup
{ {
public: public:
ActionSpawn(); ActionSpawn();
explicit ActionSpawn( explicit ActionSpawn(
Array<ActionPtr> const& actions /* 动作列表 */ Array<ActionPtr> const& actions
); );
virtual ~ActionSpawn(); virtual ~ActionSpawn();
// 在结尾添加动作
void Add(
ActionPtr const& action
);
// 在结尾添加多个动作
void Add(
Array<ActionPtr> const& actions /* 动作列表 */
);
// 获取该动作的拷贝对象
ActionPtr Clone() const override;
// 获取该动作的倒转
virtual ActionPtr Reverse() const;
protected:
// 初始化动作
void Init(NodePtr const& target) override;
// 更新动作
void Update(NodePtr const& target, Duration dt) override;
protected:
int size_;
IntrusiveList<ActionPtr> actions_;
}; };
} }

View File

@ -198,15 +198,29 @@ namespace easy2d
} }
static inline ActionHelper static inline ActionHelper
Sequence(Array<ActionPtr> const& actions) Group(Array<ActionPtr> const& actions, bool sequence = true)
{ {
return ActionHelper(new easy2d::ActionSequence(actions)); return ActionHelper(new easy2d::ActionGroup(actions, sequence));
} }
static inline ActionHelper
Multiple(Array<ActionPtr> const& actions)
{
return ActionHelper(new easy2d::ActionGroup(actions, false));
}
E2D_DEPRECATED("Tween::Sequence is deprecated, use Tween::Group instead")
static inline ActionHelper
Sequence(Array<ActionPtr> const& actions)
{
return ActionHelper(new easy2d::ActionGroup(actions, true));
}
E2D_DEPRECATED("Tween::Spawn is deprecated, use Tween::Multiple instead")
static inline ActionHelper static inline ActionHelper
Spawn(Array<ActionPtr> const& actions) Spawn(Array<ActionPtr> const& actions)
{ {
return ActionHelper(new easy2d::ActionSpawn(actions)); return ActionHelper(new easy2d::ActionGroup(actions, false));
} }
}; };
} }

View File

@ -113,7 +113,7 @@ namespace easy2d
E2D_DECLARE_SMART_PTR(ActionRotateTo); E2D_DECLARE_SMART_PTR(ActionRotateTo);
E2D_DECLARE_SMART_PTR(ActionPath); E2D_DECLARE_SMART_PTR(ActionPath);
E2D_DECLARE_SMART_PTR(Animation); E2D_DECLARE_SMART_PTR(Animation);
E2D_DECLARE_SMART_PTR(ActionSequence); E2D_DECLARE_SMART_PTR(ActionGroup);
E2D_DECLARE_SMART_PTR(ActionSpawn); E2D_DECLARE_SMART_PTR(ActionSpawn);
E2D_DECLARE_SMART_PTR(Transition); E2D_DECLARE_SMART_PTR(Transition);

View File

@ -114,3 +114,5 @@
#endif #endif
#define E2D_NOT_USED(VAR) ((void)VAR) #define E2D_NOT_USED(VAR) ((void)VAR)
#define E2D_DEPRECATED(...) __declspec(deprecated(__VA_ARGS__))