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(
Tween::Spawn({ // Action5: 同时执行 Action1 和 Action4, 并无限循环
Tween::Multiple({ // Action5: 同时执行 Action1 和 Action4, 并无限循环
Tween::RotateBy(60).SetDuration(1000), // Action1: 1秒旋转 60 度
Tween::Sequence({ // Action4: 顺序执行 Action2 和 Action3
Tween::Group({ // Action4: 顺序执行 Action2 和 Action3
Tween::FadeOut(500), // Action2: 500毫秒淡出动画
Tween::FadeIn(500) // Action3: 500毫秒淡入动画
})

View File

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

View File

@ -24,34 +24,48 @@
namespace easy2d
{
//-------------------------------------------------------
// ActionSequence
// ActionGroup
//-------------------------------------------------------
ActionSequence::ActionSequence()
ActionGroup::ActionGroup()
: sequence_(true)
{
}
ActionSequence::ActionSequence(Array<ActionPtr> const& actions)
ActionGroup::ActionGroup(Array<ActionPtr> const& actions, bool sequence)
: sequence_(sequence)
{
this->Add(actions);
}
ActionSequence::~ActionSequence()
ActionGroup::~ActionGroup()
{
}
void ActionSequence::Init(NodePtr const& target)
void ActionGroup::Init(NodePtr const& target)
{
if (actions_.IsEmpty())
Done();
else
{
Done();
return;
}
current_ = actions_.First();
current_->Restart(target); // init
current_->Restart(target); // init first action
if (!sequence_)
{
// init all actions
for (; current_; current_ = current_->NextItem())
{
current_->Restart(target);
}
}
}
void ActionSequence::Update(NodePtr const& target, Duration dt)
void ActionGroup::Update(NodePtr const& target, Duration dt)
{
if (sequence_)
{
if (current_)
{
@ -60,18 +74,34 @@ namespace easy2d
if (current_->IsDone())
{
current_ = current_->NextItem();
if (current_)
current_->Restart(target);
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 (!current_)
if (done)
{
Complete(target);
}
}
}
void ActionSequence::Add(ActionPtr const& action)
void ActionGroup::Add(ActionPtr const& action)
{
if (action)
{
@ -79,133 +109,76 @@ namespace easy2d
}
}
void ActionSequence::Add(Array<ActionPtr> const& actions)
void ActionGroup::Add(Array<ActionPtr> const& actions)
{
for (const auto& action : actions)
Add(action);
}
ActionPtr ActionSequence::Clone() const
ActionPtr ActionGroup::Clone() const
{
auto sequence = new (std::nothrow) ActionSequence();
if (sequence)
auto group = new (std::nothrow) ActionGroup();
if (group)
{
for (auto action = actions_.First(); action; action = action->NextItem())
{
if (action)
{
sequence->Add(action->Clone());
group->Add(action->Clone());
}
}
}
return sequence;
return group;
}
ActionPtr ActionSequence::Reverse() const
ActionPtr ActionGroup::Reverse() const
{
auto sequence = new (std::nothrow) ActionSequence();
if (sequence && !actions_.IsEmpty())
auto group = new (std::nothrow) ActionGroup();
if (group && !actions_.IsEmpty())
{
for (auto action = actions_.Last(); action; action = action->PrevItem())
{
sequence->Add(action->Reverse());
group->Add(action->Reverse());
}
}
return sequence;
return group;
}
//-------------------------------------------------------
// ActionSequence
//-------------------------------------------------------
ActionSequence::ActionSequence()
: ActionGroup()
{
}
ActionSequence::ActionSequence(Array<ActionPtr> const & actions)
: ActionGroup(actions, true)
{
}
ActionSequence::~ActionSequence()
{
}
//-------------------------------------------------------
// ActionSpawn
//-------------------------------------------------------
ActionSpawn::ActionSpawn()
: size_(0)
: ActionGroup()
{
sequence_ = false;
}
ActionSpawn::ActionSpawn(Array<ActionPtr> const& actions)
: size_(0)
ActionSpawn::ActionSpawn(Array<ActionPtr> const & actions)
: ActionGroup(actions, false)
{
this->Add(actions);
}
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
{
// 顺序动作
class E2D_API ActionSequence
// 动作
class E2D_API ActionGroup
: public Action
{
public:
ActionSequence();
using ActionList = IntrusiveList<ActionPtr>;
explicit ActionSequence(
Array<ActionPtr> const& actions /* 动作列表 */
ActionGroup();
explicit ActionGroup(
Array<ActionPtr> const& actions,
bool sequence = true // 按顺序执行或同时执行
);
virtual ~ActionSequence();
virtual ~ActionGroup();
// 在结尾添加动作
// 添加动作
void Add(
ActionPtr const& action
);
// 在结尾添加多个动作
// 添加多个动作
void Add(
Array<ActionPtr> const& actions /* 动作列表 */
Array<ActionPtr> const& actions
);
// 获取所有动作
inline ActionList const& GetActions() { return actions_; }
// 获取该动作的拷贝对象
ActionPtr Clone() const override;
@ -60,49 +66,40 @@ namespace easy2d
void Update(NodePtr const& target, Duration dt) override;
protected:
bool sequence_;
ActionPtr current_;
IntrusiveList<ActionPtr> actions_;
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
: public Action
class E2D_DEPRECATED("ActionSpawn is deprecated, use ActionGroup instead") E2D_API
ActionSpawn
: public ActionGroup
{
public:
ActionSpawn();
explicit ActionSpawn(
Array<ActionPtr> const& actions /* 动作列表 */
Array<ActionPtr> const& actions
);
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
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
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(ActionPath);
E2D_DECLARE_SMART_PTR(Animation);
E2D_DECLARE_SMART_PTR(ActionSequence);
E2D_DECLARE_SMART_PTR(ActionGroup);
E2D_DECLARE_SMART_PTR(ActionSpawn);
E2D_DECLARE_SMART_PTR(Transition);

View File

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