diff --git a/src/core/ActionGroup.h b/src/core/ActionGroup.h index 3d097859..e2925d79 100644 --- a/src/core/ActionGroup.h +++ b/src/core/ActionGroup.h @@ -36,22 +36,22 @@ namespace easy2d virtual ~Loop(); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; // 重置动作 - virtual void Reset() override; + void Reset() override; - virtual bool IsRunning() override; + bool IsRunning() override; protected: // 初始化动作 - virtual void Init(Node* target) override; + void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target, Duration dt) override; + void Update(Node* target, Duration dt) override; protected: ActionPtr action_; @@ -84,20 +84,20 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; // 重置动作 - virtual void Reset() override; + void Reset() override; protected: // 初始化动作 - virtual void Init(Node* target) override; + void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target, Duration dt) override; + void Update(Node* target, Duration dt) override; protected: UINT action_index_; @@ -129,20 +129,20 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const; // 重置动作 - virtual void Reset() override; + void Reset() override; protected: // 初始化动作 - virtual void Init(Node* target) override; + void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target, Duration dt) override; + void Update(Node* target, Duration dt) override; protected: Array actions_; diff --git a/src/core/ActionHelper.h b/src/core/ActionHelper.h index 9aa26df3..f09a27f9 100644 --- a/src/core/ActionHelper.h +++ b/src/core/ActionHelper.h @@ -21,6 +21,7 @@ #pragma once #include "ActionGroup.h" #include "ActionTween.h" +#include "Animation.h" #include "Delay.h" namespace easy2d @@ -38,6 +39,7 @@ namespace easy2d return base; } + private: ActionPtr base; int loop; }; @@ -61,6 +63,7 @@ namespace easy2d return base; } + private: ActionTweenPtr base; Duration dur; int loop; @@ -170,6 +173,12 @@ namespace easy2d return TweenActionHelper(new easy2d::PathAction(0, geo, rotating, start, end)); } + static inline TweenActionHelper + Animation(FramesPtr const& frames) + { + return TweenActionHelper(new easy2d::Animation(0, frames)); + } + static inline ActionHelper Delay(Duration dur) { diff --git a/src/core/ActionManager.cpp b/src/core/ActionManager.cpp index 97dba9be..5b03a7b7 100644 --- a/src/core/ActionManager.cpp +++ b/src/core/ActionManager.cpp @@ -25,7 +25,7 @@ namespace easy2d { void ActionManager::UpdateActions(Node* target, Duration dt) { - if (actions_.IsEmpty()) + if (actions_.IsEmpty() || !target) return; ActionPtr next; diff --git a/src/core/ActionTween.cpp b/src/core/ActionTween.cpp index 8889e32b..5039847f 100644 --- a/src/core/ActionTween.cpp +++ b/src/core/ActionTween.cpp @@ -223,16 +223,13 @@ namespace easy2d void MoveBy::UpdateStep(Node* target, float step) { - if (target) - { - Point diff = target->GetPosition() - prev_pos_; - start_pos_ = start_pos_ + diff; + Point diff = target->GetPosition() - prev_pos_; + start_pos_ = start_pos_ + diff; - Point new_pos = start_pos_ + (delta_pos_ * step); - target->SetPosition(new_pos); + Point new_pos = start_pos_ + (delta_pos_ * step); + target->SetPosition(new_pos); - prev_pos_ = new_pos; - } + prev_pos_ = new_pos; } ActionPtr MoveBy::Clone() const @@ -297,21 +294,18 @@ namespace easy2d void JumpBy::UpdateStep(Node* target, float step) { - if (target) - { - float frac = fmod(step * jumps_, 1.f); - float x = delta_pos_.x * step; - float y = height_ * 4 * frac * (1 - frac); - y += delta_pos_.y * step; + float frac = fmod(step * jumps_, 1.f); + float x = delta_pos_.x * step; + float y = height_ * 4 * frac * (1 - frac); + y += delta_pos_.y * step; - Point diff = target->GetPosition() - prev_pos_; - start_pos_ = diff + start_pos_; + Point diff = target->GetPosition() - prev_pos_; + start_pos_ = diff + start_pos_; - Point new_pos = start_pos_ + Point(x, y); - target->SetPosition(new_pos); + Point new_pos = start_pos_ + Point(x, y); + target->SetPosition(new_pos); - prev_pos_ = new_pos; - } + prev_pos_ = new_pos; } JumpTo::JumpTo(Duration duration, Point const& pos, float height, int jumps, EaseFunc func) @@ -363,10 +357,7 @@ namespace easy2d void ScaleBy::UpdateStep(Node* target, float step) { - if (target) - { - target->SetScale(start_scale_x_ + delta_x_ * step, start_scale_y_ + delta_y_ * step); - } + target->SetScale(start_scale_x_ + delta_x_ * step, start_scale_y_ + delta_y_ * step); } ActionPtr ScaleBy::Clone() const @@ -428,10 +419,7 @@ namespace easy2d void OpacityBy::UpdateStep(Node* target, float step) { - if (target) - { - target->SetOpacity(start_val_ + delta_val_ * step); - } + target->SetOpacity(start_val_ + delta_val_ * step); } ActionPtr OpacityBy::Clone() const @@ -494,10 +482,7 @@ namespace easy2d void RotateBy::UpdateStep(Node* target, float step) { - if (target) - { - target->SetRotation(start_val_ + delta_val_ * step); - } + target->SetRotation(start_val_ + delta_val_ * step); } ActionPtr RotateBy::Clone() const @@ -563,21 +548,18 @@ namespace easy2d void PathAction::UpdateStep(Node* target, float step) { - if (target) + float length = geo_->GetLength() * std::min(std::max((end_ - start_) * step + start_, 0.f), 1.f); + + Point point, tangent; + if (geo_->ComputePointAt(length, &point, &tangent)) { - float length = geo_->GetLength() * std::min(std::max((end_ - start_) * step + start_, 0.f), 1.f); + target->SetPosition(start_pos_ + point); - Point point, tangent; - if (geo_->ComputePointAt(length, &point, &tangent)) + if (rotating_) { - target->SetPosition(start_pos_ + point); - - if (rotating_) - { - float ac = math::Acos(tangent.x); - float rotation = (tangent.y < 0.f) ? 360.f - ac : ac; - target->SetRotation(rotation); - } + float ac = math::Acos(tangent.x); + float rotation = (tangent.y < 0.f) ? 360.f - ac : ac; + target->SetRotation(rotation); } } } diff --git a/src/core/ActionTween.h b/src/core/ActionTween.h index e910c553..b09f3e8c 100644 --- a/src/core/ActionTween.h +++ b/src/core/ActionTween.h @@ -95,16 +95,16 @@ namespace easy2d EaseFunction func ); - virtual void Reset() override; + void Reset() override; Duration GetDuration() const; void SetDuration(Duration duration); protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void Update(Node* target, Duration dt) override; + void Update(Node* target, Duration dt) override; virtual void UpdateStep(Node* target, float step) = 0; @@ -128,15 +128,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: Point start_pos_; @@ -157,7 +157,7 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const override @@ -167,7 +167,7 @@ namespace easy2d } protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; protected: Point end_pos_; @@ -188,15 +188,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: Point start_pos_; @@ -221,7 +221,7 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const override @@ -231,7 +231,7 @@ namespace easy2d } protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; protected: Point end_pos_; @@ -257,15 +257,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: float start_scale_x_; @@ -294,7 +294,7 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const override @@ -304,7 +304,7 @@ namespace easy2d } protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; protected: float end_scale_x_; @@ -324,15 +324,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: float start_val_; @@ -352,7 +352,7 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const override @@ -362,7 +362,7 @@ namespace easy2d } protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; protected: float end_val_; @@ -407,15 +407,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: float start_val_; @@ -435,7 +435,7 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 virtual ActionPtr Reverse() const override @@ -445,7 +445,7 @@ namespace easy2d } protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; protected: float end_val_; @@ -467,15 +467,15 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void UpdateStep(Node* target, float step) override; + void UpdateStep(Node* target, float step) override; protected: bool rotating_; diff --git a/src/core/Animation.cpp b/src/core/Animation.cpp index d514c84f..a3b5298a 100644 --- a/src/core/Animation.cpp +++ b/src/core/Animation.cpp @@ -26,13 +26,12 @@ namespace easy2d { Animation::Animation() - : frame_index_(0) - , frames_(nullptr) + : frames_(nullptr) { } - Animation::Animation(FramesPtr const& animation) - : frame_index_(0) + Animation::Animation(Duration duration, FramesPtr const& animation, EaseFunc func) + : ActionTween(duration, func) , frames_(nullptr) { this->SetAnimation(animation); @@ -52,65 +51,42 @@ namespace easy2d if (animation && animation != frames_) { frames_ = animation; - frame_index_ = 0; } } void Animation::Init(Node* target) { - Action::Init(target); + ActionTween::Init(target); - auto sprite_target = dynamic_cast(target); - if (sprite_target && frames_) - { - sprite_target->Load(frames_->GetFrames()[frame_index_]); - ++frame_index_; - } - } - - void Animation::Update(Node* target, Duration dt) - { - Action::Update(target, dt); - - if (!frames_) + if (!frames_ || frames_->GetFrames().empty()) { this->Stop(); return; } - delta_ += dt; - while (delta_ >= frames_->GetInterval()) + auto sprite_target = dynamic_cast(target); + if (sprite_target && frames_) { - auto& frames = frames_->GetFrames(); - auto sprite_target = dynamic_cast(target); - - if (sprite_target) - { - sprite_target->Load(frames[frame_index_]); - } - - delta_ -= frames_->GetInterval(); - ++frame_index_; - - if (frame_index_ == frames.size()) - { - this->Stop(); - break; - } + sprite_target->Load(frames_->GetFrames()[0]); } } - void Animation::Reset() + void Animation::UpdateStep(Node * target, float step) { - Action::Reset(); - frame_index_ = 0; + E2D_ASSERT(dynamic_cast(target) && "Animation only supports Sprites"); + + const auto& frames = frames_->GetFrames(); + size_t size = frames.size(); + size_t index = std::min(static_cast(math::Floor(size * step)), size - 1); + + static_cast(target)->Load(frames[index]); } ActionPtr Animation::Clone() const { if (frames_) { - return new (std::nothrow) Animation(frames_); + return new (std::nothrow) Animation(duration_, frames_, ease_type_); } return nullptr; } @@ -119,10 +95,10 @@ namespace easy2d { if (frames_) { - auto animation = frames_->Reverse(); - if (animation) + FramesPtr frames = frames_->Reverse(); + if (frames) { - return new (std::nothrow) Animation(animation); + return new (std::nothrow) Animation(duration_, frames, ease_type_); } } return nullptr; diff --git a/src/core/Animation.h b/src/core/Animation.h index 0d5b8a66..c974aa0b 100644 --- a/src/core/Animation.h +++ b/src/core/Animation.h @@ -19,19 +19,21 @@ // THE SOFTWARE. #pragma once -#include "Action.hpp" +#include "ActionTween.h" namespace easy2d { // 帧动画 class Animation - : public Action + : public ActionTween { public: Animation(); - explicit Animation( - FramesPtr const& animation + Animation( + Duration duration, /* 动画时长 */ + FramesPtr const& frames, /* 帧集合 */ + EaseFunc func = EaseFunc::Linear ); virtual ~Animation(); @@ -45,24 +47,17 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; - - // 重置动作 - virtual void Reset() override; + ActionPtr Reverse() const override; protected: - // 初始化动作 - virtual void Init(Node* target) override; + void Init(Node* target) override; - // 更新动作 - virtual void Update(Node* target, Duration dt) override; + void UpdateStep(Node* target, float step) override; protected: - size_t frame_index_; - Duration delta_; FramesPtr frames_; }; } diff --git a/src/core/Canvas.h b/src/core/Canvas.h index d8c7037d..2a73799e 100644 --- a/src/core/Canvas.h +++ b/src/core/Canvas.h @@ -218,7 +218,7 @@ namespace easy2d // 导出为图片 ImagePtr ExportToImage() const; - virtual void OnRender() override; + void OnRender() override; protected: D2DBitmapPtr const& GetBitmap() const; diff --git a/src/core/Delay.h b/src/core/Delay.h index dbe3bf80..fe8f453a 100644 --- a/src/core/Delay.h +++ b/src/core/Delay.h @@ -33,18 +33,18 @@ namespace easy2d ); // 获取该动作的拷贝对象 - virtual ActionPtr Clone() const override; + ActionPtr Clone() const override; // 获取该动作的倒转 - virtual ActionPtr Reverse() const override; + ActionPtr Reverse() const override; // 重置动作 - virtual void Reset() override; + void Reset() override; protected: - virtual void Init(Node* target) override; + void Init(Node* target) override; - virtual void Update(Node* target, Duration dt) override; + void Update(Node* target, Duration dt) override; protected: Duration delay_; diff --git a/src/core/Frames.cpp b/src/core/Frames.cpp index 6b0e96cc..c700c445 100644 --- a/src/core/Frames.cpp +++ b/src/core/Frames.cpp @@ -25,18 +25,10 @@ namespace easy2d { Frames::Frames() - : interval_(200) { } Frames::Frames(Array const& frames) - : interval_(200) - { - this->Add(frames); - } - - Frames::Frames(Duration interval, Array const& frames) - : interval_(interval) { this->Add(frames); } @@ -45,11 +37,6 @@ namespace easy2d { } - void Frames::SetInterval(Duration interval) - { - interval_ = interval; - } - void Frames::Add(ImagePtr const& frame) { E2D_ASSERT(frame && "Frames::Add failed, NULL pointer exception"); @@ -68,11 +55,6 @@ namespace easy2d } } - Duration Frames::GetInterval() const - { - return interval_; - } - Array const& Frames::GetFrames() const { return frames_; @@ -83,7 +65,6 @@ namespace easy2d auto animation = new (std::nothrow) Frames; if (animation) { - animation->SetInterval(interval_); for (const auto& frame : frames_) { animation->Add(frame); @@ -97,7 +78,6 @@ namespace easy2d auto animation = new (std::nothrow) Frames; if (!frames_.empty()) { - animation->SetInterval(interval_); for (auto iter = frames_.crbegin(), crend = frames_.crend(); iter != crend; ++iter) { if (*iter) diff --git a/src/core/Frames.h b/src/core/Frames.h index 8cc41c1a..05a835db 100644 --- a/src/core/Frames.h +++ b/src/core/Frames.h @@ -35,11 +35,6 @@ namespace easy2d Array const& frames /* 关键帧数组 */ ); - explicit Frames( - Duration interval, /* 帧间隔 */ - Array const& frames /* 关键帧数组 */ - ); - virtual ~Frames(); // 添加关键帧 @@ -52,17 +47,9 @@ namespace easy2d Array const& frames ); - // 获取帧间隔 - Duration GetInterval() const; - // 获取关键帧 Array const& GetFrames() const; - // 设置每一帧的时间间隔 - void SetInterval( - Duration interval - ); - // 获取帧动画的拷贝对象 FramesPtr Clone() const; @@ -70,7 +57,6 @@ namespace easy2d FramesPtr Reverse() const; protected: - Duration interval_; Array frames_; }; } diff --git a/src/core/GeometryNode.h b/src/core/GeometryNode.h index 557c6c6a..7ab3c6ff 100644 --- a/src/core/GeometryNode.h +++ b/src/core/GeometryNode.h @@ -77,7 +77,7 @@ namespace easy2d // 获取线条相交样式 StrokeStyle SetOutlineJoinStyle() const { return outline_join_; } - virtual void OnRender() override; + void OnRender() override; protected: Color fill_color_; diff --git a/src/core/Node.h b/src/core/Node.h index 688db051..2f0668b9 100644 --- a/src/core/Node.h +++ b/src/core/Node.h @@ -56,7 +56,7 @@ namespace easy2d virtual void OnRender() {} // 事件分发 - virtual void Dispatch(Event& evt) override; + void Dispatch(Event& evt) override; // 获取显示状态 bool IsVisible() const { return visible_; } diff --git a/src/core/Sprite.h b/src/core/Sprite.h index e8f60dd0..bc3beb06 100644 --- a/src/core/Sprite.h +++ b/src/core/Sprite.h @@ -65,7 +65,7 @@ namespace easy2d ImagePtr const& GetImage() const; // 渲染精灵 - virtual void OnRender() override; + void OnRender() override; protected: ImagePtr image_; diff --git a/src/core/Text.h b/src/core/Text.h index bc92bac5..4e532111 100644 --- a/src/core/Text.h +++ b/src/core/Text.h @@ -199,7 +199,7 @@ namespace easy2d TextStyle const& style ); - virtual void OnRender() override; + void OnRender() override; protected: void UpdateLayout(); diff --git a/src/core/Transition.h b/src/core/Transition.h index 206e43e5..a4dfca8b 100644 --- a/src/core/Transition.h +++ b/src/core/Transition.h @@ -81,7 +81,7 @@ namespace easy2d protected: // 更新动画 - virtual void Update(Duration dt) override; + void Update(Duration dt) override; virtual void Init( ScenePtr const& prev, @@ -100,7 +100,7 @@ namespace easy2d ); protected: - virtual void Update(Duration dt) override; + void Update(Duration dt) override; virtual void Init( ScenePtr const& prev, @@ -119,7 +119,7 @@ namespace easy2d ); protected: - virtual void Update(Duration dt) override; + void Update(Duration dt) override; virtual void Init( ScenePtr const& prev, @@ -139,14 +139,14 @@ namespace easy2d ); protected: - virtual void Update(Duration dt) override; + void Update(Duration dt) override; virtual void Init( ScenePtr const& prev, ScenePtr const& next ) override; - virtual void Reset() override; + void Reset() override; protected: Direction direction_; @@ -166,14 +166,14 @@ namespace easy2d ); protected: - virtual void Update(Duration dt) override; + void Update(Duration dt) override; virtual void Init( ScenePtr const& prev, ScenePtr const& next ) override; - virtual void Reset() override; + void Reset() override; protected: float rotation_;