diff --git a/project/Samples/Demo1.h b/project/Samples/Demo1.h index c13668bb..b25d36e2 100644 --- a/project/Samples/Demo1.h +++ b/project/Samples/Demo1.h @@ -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毫秒淡入动画 }) diff --git a/src/core/Action.h b/src/core/Action.h index 4424b7f2..98e999f2 100644 --- a/src/core/Action.h +++ b/src/core/Action.h @@ -34,8 +34,7 @@ namespace easy2d , protected IntrusiveListItem { friend class ActionManager; - friend class ActionSequence; - friend class ActionSpawn; + friend class ActionGroup; friend class IntrusiveList; public: diff --git a/src/core/ActionGroup.cpp b/src/core/ActionGroup.cpp index 794baba3..0204ae88 100644 --- a/src/core/ActionGroup.cpp +++ b/src/core/ActionGroup.cpp @@ -23,189 +23,162 @@ namespace easy2d { + //------------------------------------------------------- + // ActionGroup + //------------------------------------------------------- + + ActionGroup::ActionGroup() + : sequence_(true) + { + } + + ActionGroup::ActionGroup(Array 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 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() + : ActionGroup() { } - ActionSequence::ActionSequence(Array const& actions) + ActionSequence::ActionSequence(Array const & actions) + : ActionGroup(actions, true) { - this->Add(actions); } 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 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() - : size_(0) + : ActionGroup() { + sequence_ = false; } - ActionSpawn::ActionSpawn(Array const& actions) - : size_(0) + ActionSpawn::ActionSpawn(Array 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 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; - } } \ No newline at end of file diff --git a/src/core/ActionGroup.h b/src/core/ActionGroup.h index d241cf50..1f32ea43 100644 --- a/src/core/ActionGroup.h +++ b/src/core/ActionGroup.h @@ -23,29 +23,35 @@ namespace easy2d { - // 顺序动作 - class E2D_API ActionSequence + // 动作组 + class E2D_API ActionGroup : public Action { public: - ActionSequence(); + using ActionList = IntrusiveList; - explicit ActionSequence( - Array const& actions /* 动作列表 */ + ActionGroup(); + + explicit ActionGroup( + Array const& actions, + bool sequence = true // 按顺序执行或同时执行 ); - virtual ~ActionSequence(); + virtual ~ActionGroup(); - // 在结尾添加动作 + // 添加动作 void Add( ActionPtr const& action ); - // 在结尾添加多个动作 + // 添加多个动作 void Add( - Array const& actions /* 动作列表 */ + Array 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: - ActionPtr current_; - IntrusiveList actions_; + bool sequence_; + ActionPtr current_; + ActionList actions_; + }; + + + // 顺序动作 + class E2D_DEPRECATED("ActionSequence is deprecated, use ActionGroup instead") E2D_API + ActionSequence + : public ActionGroup + { + public: + ActionSequence(); + + explicit ActionSequence( + Array 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 const& actions /* 动作列表 */ + Array const& actions ); virtual ~ActionSpawn(); - - // 在结尾添加动作 - void Add( - ActionPtr const& action - ); - - // 在结尾添加多个动作 - void Add( - Array 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 actions_; }; } diff --git a/src/core/ActionHelper.h b/src/core/ActionHelper.h index b174e5d5..91285da9 100644 --- a/src/core/ActionHelper.h +++ b/src/core/ActionHelper.h @@ -198,15 +198,29 @@ namespace easy2d } static inline ActionHelper - Sequence(Array const& actions) + Group(Array const& actions, bool sequence = true) { - return ActionHelper(new easy2d::ActionSequence(actions)); + return ActionHelper(new easy2d::ActionGroup(actions, sequence)); } + static inline ActionHelper + Multiple(Array const& actions) + { + return ActionHelper(new easy2d::ActionGroup(actions, false)); + } + + E2D_DEPRECATED("Tween::Sequence is deprecated, use Tween::Group instead") + static inline ActionHelper + Sequence(Array const& actions) + { + return ActionHelper(new easy2d::ActionGroup(actions, true)); + } + + E2D_DEPRECATED("Tween::Spawn is deprecated, use Tween::Multiple instead") static inline ActionHelper Spawn(Array const& actions) { - return ActionHelper(new easy2d::ActionSpawn(actions)); + return ActionHelper(new easy2d::ActionGroup(actions, false)); } }; } diff --git a/src/core/helper.hpp b/src/core/helper.hpp index 73503211..5343148d 100644 --- a/src/core/helper.hpp +++ b/src/core/helper.hpp @@ -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); diff --git a/src/core/macros.h b/src/core/macros.h index 511f8ce3..2ecd965e 100644 --- a/src/core/macros.h +++ b/src/core/macros.h @@ -114,3 +114,5 @@ #endif #define E2D_NOT_USED(VAR) ((void)VAR) + +#define E2D_DEPRECATED(...) __declspec(deprecated(__VA_ARGS__))