Tween supports Animation now

This commit is contained in:
Nomango 2019-01-26 17:18:31 +08:00 committed by Nomango
parent 5b0cd1969f
commit d223cd60ae
16 changed files with 135 additions and 207 deletions

View File

@ -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<ActionPtr> actions_;

View File

@ -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)
{

View File

@ -25,7 +25,7 @@ namespace easy2d
{
void ActionManager::UpdateActions(Node* target, Duration dt)
{
if (actions_.IsEmpty())
if (actions_.IsEmpty() || !target)
return;
ActionPtr next;

View File

@ -222,8 +222,6 @@ namespace easy2d
}
void MoveBy::UpdateStep(Node* target, float step)
{
if (target)
{
Point diff = target->GetPosition() - prev_pos_;
start_pos_ = start_pos_ + diff;
@ -233,7 +231,6 @@ namespace easy2d
prev_pos_ = new_pos;
}
}
ActionPtr MoveBy::Clone() const
{
@ -296,8 +293,6 @@ namespace easy2d
}
void JumpBy::UpdateStep(Node* target, float step)
{
if (target)
{
float frac = fmod(step * jumps_, 1.f);
float x = delta_pos_.x * step;
@ -312,7 +307,6 @@ namespace easy2d
prev_pos_ = new_pos;
}
}
JumpTo::JumpTo(Duration duration, Point const& pos, float height, int jumps, EaseFunc func)
: JumpBy(duration, Point(), height, jumps, func)
@ -362,12 +356,9 @@ 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);
}
}
ActionPtr ScaleBy::Clone() const
{
@ -427,12 +418,9 @@ namespace easy2d
}
void OpacityBy::UpdateStep(Node* target, float step)
{
if (target)
{
target->SetOpacity(start_val_ + delta_val_ * step);
}
}
ActionPtr OpacityBy::Clone() const
{
@ -493,12 +481,9 @@ namespace easy2d
}
void RotateBy::UpdateStep(Node* target, float step)
{
if (target)
{
target->SetRotation(start_val_ + delta_val_ * step);
}
}
ActionPtr RotateBy::Clone() const
{
@ -562,8 +547,6 @@ 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);
@ -580,6 +563,5 @@ namespace easy2d
}
}
}
}
}

View File

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

View File

@ -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<Sprite*>(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& frames = frames_->GetFrames();
auto sprite_target = dynamic_cast<Sprite*>(target);
if (sprite_target)
if (sprite_target && frames_)
{
sprite_target->Load(frames[frame_index_]);
sprite_target->Load(frames_->GetFrames()[0]);
}
}
delta_ -= frames_->GetInterval();
++frame_index_;
if (frame_index_ == frames.size())
void Animation::UpdateStep(Node * target, float step)
{
this->Stop();
break;
}
}
}
E2D_ASSERT(dynamic_cast<Sprite*>(target) && "Animation only supports Sprites");
void Animation::Reset()
{
Action::Reset();
frame_index_ = 0;
const auto& frames = frames_->GetFrames();
size_t size = frames.size();
size_t index = std::min(static_cast<size_t>(math::Floor(size * step)), size - 1);
static_cast<Sprite*>(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;

View File

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

View File

@ -218,7 +218,7 @@ namespace easy2d
// 导出为图片
ImagePtr ExportToImage() const;
virtual void OnRender() override;
void OnRender() override;
protected:
D2DBitmapPtr const& GetBitmap() const;

View File

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

View File

@ -25,18 +25,10 @@
namespace easy2d
{
Frames::Frames()
: interval_(200)
{
}
Frames::Frames(Array<ImagePtr> const& frames)
: interval_(200)
{
this->Add(frames);
}
Frames::Frames(Duration interval, Array<ImagePtr> 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<ImagePtr> 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)

View File

@ -35,11 +35,6 @@ namespace easy2d
Array<ImagePtr> const& frames /* 关键帧数组 */
);
explicit Frames(
Duration interval, /* 帧间隔 */
Array<ImagePtr> const& frames /* 关键帧数组 */
);
virtual ~Frames();
// 添加关键帧
@ -52,17 +47,9 @@ namespace easy2d
Array<ImagePtr> const& frames
);
// 获取帧间隔
Duration GetInterval() const;
// 获取关键帧
Array<ImagePtr> const& GetFrames() const;
// 设置每一帧的时间间隔
void SetInterval(
Duration interval
);
// 获取帧动画的拷贝对象
FramesPtr Clone() const;
@ -70,7 +57,6 @@ namespace easy2d
FramesPtr Reverse() const;
protected:
Duration interval_;
Array<ImagePtr> frames_;
};
}

View File

@ -77,7 +77,7 @@ namespace easy2d
// ťńČĄĎßĚőĎཝŃůĘ˝
StrokeStyle SetOutlineJoinStyle() const { return outline_join_; }
virtual void OnRender() override;
void OnRender() override;
protected:
Color fill_color_;

View File

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

View File

@ -65,7 +65,7 @@ namespace easy2d
ImagePtr const& GetImage() const;
// äÖȾ¾«Áé
virtual void OnRender() override;
void OnRender() override;
protected:
ImagePtr image_;

View File

@ -199,7 +199,7 @@ namespace easy2d
TextStyle const& style
);
virtual void OnRender() override;
void OnRender() override;
protected:
void UpdateLayout();

View File

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