diff --git a/src/core/ActionHelper.h b/src/core/ActionHelper.h index f09a27f9..8b187215 100644 --- a/src/core/ActionHelper.h +++ b/src/core/ActionHelper.h @@ -52,12 +52,16 @@ namespace easy2d TweenActionHelper& SetEaseFunc(EaseFunc ease) { this->ease = ease; return (*this); } - TweenActionHelper(ActionTweenPtr const& base) : base(base), dur(), loop(0), ease(EaseFunc::Linear) {} + TweenActionHelper(ActionTweenPtr const& base) : base(base), dur(), loop(0), ease(nullptr) + { + dur = base->GetDuration(); + } operator ActionPtr() const { - base->SetEaseFunction(ease); + base->SetEaseFunc(ease); base->SetDuration(dur); + if (loop) return ActionPtr(new (std::nothrow) Loop(base)); return base; diff --git a/src/core/ActionTween.cpp b/src/core/ActionTween.cpp index 5039847f..78425d7c 100644 --- a/src/core/ActionTween.cpp +++ b/src/core/ActionTween.cpp @@ -26,6 +26,42 @@ namespace easy2d { + //------------------------------------------------------- + // Ease Functions + //------------------------------------------------------- + + EaseFunc Ease::Linear = math::Linear; + EaseFunc Ease::EaseIn = MakeEaseIn(2.f); + EaseFunc Ease::EaseOut = MakeEaseOut(2.f); + EaseFunc Ease::EaseInOut = MakeEaseInOut(2.f); + EaseFunc Ease::ExpoIn = math::EaseExponentialIn; + EaseFunc Ease::ExpoOut = math::EaseExponentialOut; + EaseFunc Ease::ExpoInOut = math::EaseExponentialInOut; + EaseFunc Ease::BounceIn = math::EaseBounceIn; + EaseFunc Ease::BounceOut = math::EaseBounceOut; + EaseFunc Ease::BounceInOut = math::EaseBounceInOut; + EaseFunc Ease::ElasticIn = MakeEaseElasticIn(0.3f); + EaseFunc Ease::ElasticOut = MakeEaseElasticOut(0.3f); + EaseFunc Ease::ElasticInOut = MakeEaseElasticInOut(0.3f); + EaseFunc Ease::SineIn = math::EaseSineIn; + EaseFunc Ease::SineOut = math::EaseSineOut; + EaseFunc Ease::SineInOut = math::EaseSineInOut; + EaseFunc Ease::BackIn = math::EaseBackIn; + EaseFunc Ease::BackOut = math::EaseBackOut; + EaseFunc Ease::BackInOut = math::EaseBackInOut; + EaseFunc Ease::QuadIn = math::EaseQuadIn; + EaseFunc Ease::QuadOut = math::EaseQuadOut; + EaseFunc Ease::QuadInOut = math::EaseQuadInOut; + EaseFunc Ease::CubicIn = math::EaseCubicIn; + EaseFunc Ease::CubicOut = math::EaseCubicOut; + EaseFunc Ease::CubicInOut = math::EaseCubicInOut; + EaseFunc Ease::QuartIn = math::EaseQuartIn; + EaseFunc Ease::QuartOut = math::EaseQuartOut; + EaseFunc Ease::QuartInOut = math::EaseQuartInOut; + EaseFunc Ease::QuintIn = math::EaseQuintIn; + EaseFunc Ease::QuintOut = math::EaseQuintOut; + EaseFunc Ease::QuintInOut = math::EaseQuintInOut; + //------------------------------------------------------- // ActionTween //------------------------------------------------------- @@ -33,127 +69,25 @@ namespace easy2d ActionTween::ActionTween() : elapsed_() , duration_() - , ease_func_(math::Linear) - , ease_type_(EaseFunc::Linear) + , ease_func_(nullptr) { } ActionTween::ActionTween(Duration duration, EaseFunc func) : elapsed_() - , ease_func_(math::Linear) - , ease_type_(EaseFunc::Linear) { SetDuration(duration); - SetEaseFunction(func); + SetEaseFunc(func); } - void ActionTween::SetEaseFunction(EaseFunc func) - { - ease_type_ = func; - switch (func) - { - case EaseFunc::Linear: - ease_func_ = math::Linear; - break; - case EaseFunc::EaseIn: - ease_func_ = MakeEaseIn(2.f); - break; - case EaseFunc::EaseOut: - ease_func_ = MakeEaseOut(2.f); - break; - case EaseFunc::EaseInOut: - ease_func_ = MakeEaseInOut(2.f); - break; - case EaseFunc::ExpoIn: - ease_func_ = math::EaseExponentialIn; - break; - case EaseFunc::ExpoOut: - ease_func_ = math::EaseExponentialOut; - break; - case EaseFunc::ExpoInOut: - ease_func_ = math::EaseExponentialInOut; - break; - case EaseFunc::BounceIn: - ease_func_ = math::EaseBounceIn; - break; - case EaseFunc::BounceOut: - ease_func_ = math::EaseBounceOut; - break; - case EaseFunc::BounceInOut: - ease_func_ = math::EaseBounceInOut; - break; - case EaseFunc::ElasticIn: - ease_func_ = MakeEaseElasticIn(0.3f); - break; - case EaseFunc::ElasticOut: - ease_func_ = MakeEaseElasticOut(0.3f); - break; - case EaseFunc::ElasticInOut: - ease_func_ = MakeEaseElasticInOut(0.3f); - break; - case EaseFunc::SineIn: - ease_func_ = math::EaseSineIn; - break; - case EaseFunc::SineOut: - ease_func_ = math::EaseSineOut; - break; - case EaseFunc::SineInOut: - ease_func_ = math::EaseSineInOut; - break; - case EaseFunc::BackIn: - ease_func_ = math::EaseBackIn; - break; - case EaseFunc::BackOut: - ease_func_ = math::EaseBackOut; - break; - case EaseFunc::BackInOut: - ease_func_ = math::EaseBackInOut; - break; - case EaseFunc::QuadIn: - ease_func_ = math::EaseQuadIn; - break; - case EaseFunc::QuadOut: - ease_func_ = math::EaseQuadOut; - break; - case EaseFunc::QuadInOut: - ease_func_ = math::EaseQuadInOut; - break; - case EaseFunc::CubicIn: - ease_func_ = math::EaseCubicIn; - break; - case EaseFunc::CubicOut: - ease_func_ = math::EaseCubicOut; - break; - case EaseFunc::CubicInOut: - ease_func_ = math::EaseCubicInOut; - break; - case EaseFunc::QuartIn: - ease_func_ = math::EaseQuartIn; - break; - case EaseFunc::QuartOut: - ease_func_ = math::EaseQuartOut; - break; - case EaseFunc::QuartInOut: - ease_func_ = math::EaseQuartInOut; - break; - case EaseFunc::QuintIn: - ease_func_ = math::EaseQuintIn; - break; - case EaseFunc::QuintOut: - ease_func_ = math::EaseQuintOut; - break; - case EaseFunc::QuintInOut: - ease_func_ = math::EaseQuintInOut; - break; - default: - break; - } - } - - void ActionTween::SetEaseFunction(EaseFunction func) + void ActionTween::SetEaseFunc(EaseFunc const& func) { ease_func_ = func; - ease_type_ = EaseFunc(-1); + } + + EaseFunc const & ActionTween::GetEaseFunc() const + { + return ease_func_; } void ActionTween::Reset() @@ -177,22 +111,28 @@ namespace easy2d Action::Update(target, dt); float step; + if (duration_.IsZero()) { step = 1.f; + this->Stop(); } else { elapsed_ += dt; - step = std::min(elapsed_ / duration_, 1.f); + step = elapsed_ / duration_; + + if (1.f <= step) + { + step = 1.f; + this->Stop(); + } } - if ((1.f - step) <= FLT_EPSILON) - { - this->Stop(); - } + if (ease_func_) + step = ease_func_(step); - UpdateStep(target, ease_func_(step)); + UpdateStep(target, step); } void ActionTween::SetDuration(Duration duration) @@ -234,12 +174,12 @@ namespace easy2d ActionPtr MoveBy::Clone() const { - return new (std::nothrow) MoveBy(duration_, delta_pos_, ease_type_); + return new (std::nothrow) MoveBy(duration_, delta_pos_, ease_func_); } ActionPtr MoveBy::Reverse() const { - return new (std::nothrow) MoveBy(duration_, -delta_pos_, ease_type_); + return new (std::nothrow) MoveBy(duration_, -delta_pos_, ease_func_); } MoveTo::MoveTo(Duration duration, Point const& pos, EaseFunc func) @@ -250,7 +190,7 @@ namespace easy2d ActionPtr MoveTo::Clone() const { - return new (std::nothrow) MoveTo(duration_, end_pos_, ease_type_); + return new (std::nothrow) MoveTo(duration_, end_pos_, ease_func_); } void MoveTo::Init(Node* target) @@ -274,12 +214,12 @@ namespace easy2d ActionPtr JumpBy::Clone() const { - return new (std::nothrow) JumpBy(duration_, delta_pos_, height_, jumps_, ease_type_); + return new (std::nothrow) JumpBy(duration_, delta_pos_, height_, jumps_, ease_func_); } ActionPtr JumpBy::Reverse() const { - return new (std::nothrow) JumpBy(duration_, -delta_pos_, height_, jumps_, ease_type_); + return new (std::nothrow) JumpBy(duration_, -delta_pos_, height_, jumps_, ease_func_); } void JumpBy::Init(Node* target) @@ -316,7 +256,7 @@ namespace easy2d ActionPtr JumpTo::Clone() const { - return new (std::nothrow) JumpTo(duration_, end_pos_, height_, jumps_, ease_type_); + return new (std::nothrow) JumpTo(duration_, end_pos_, height_, jumps_, ease_func_); } void JumpTo::Init(Node* target) @@ -362,12 +302,12 @@ namespace easy2d ActionPtr ScaleBy::Clone() const { - return new (std::nothrow) ScaleBy(duration_, delta_x_, delta_y_, ease_type_); + return new (std::nothrow) ScaleBy(duration_, delta_x_, delta_y_, ease_func_); } ActionPtr ScaleBy::Reverse() const { - return new (std::nothrow) ScaleBy(duration_, -delta_x_, -delta_y_, ease_type_); + return new (std::nothrow) ScaleBy(duration_, -delta_x_, -delta_y_, ease_func_); } ScaleTo::ScaleTo(Duration duration, float scale, EaseFunc func) @@ -386,7 +326,7 @@ namespace easy2d ActionPtr ScaleTo::Clone() const { - return new (std::nothrow) ScaleTo(duration_, end_scale_x_, end_scale_y_, ease_type_); + return new (std::nothrow) ScaleTo(duration_, end_scale_x_, end_scale_y_, ease_func_); } void ScaleTo::Init(Node* target) @@ -424,12 +364,12 @@ namespace easy2d ActionPtr OpacityBy::Clone() const { - return new (std::nothrow) OpacityBy(duration_, delta_val_, ease_type_); + return new (std::nothrow) OpacityBy(duration_, delta_val_, ease_func_); } ActionPtr OpacityBy::Reverse() const { - return new (std::nothrow) OpacityBy(duration_, -delta_val_, ease_type_); + return new (std::nothrow) OpacityBy(duration_, -delta_val_, ease_func_); } OpacityTo::OpacityTo(Duration duration, float opacity, EaseFunc func) @@ -440,7 +380,7 @@ namespace easy2d ActionPtr OpacityTo::Clone() const { - return new (std::nothrow) OpacityTo(duration_, end_val_, ease_type_); + return new (std::nothrow) OpacityTo(duration_, end_val_, ease_func_); } void OpacityTo::Init(Node* target) @@ -482,17 +422,21 @@ namespace easy2d void RotateBy::UpdateStep(Node* target, float step) { - target->SetRotation(start_val_ + delta_val_ * step); + float rotation = start_val_ + delta_val_ * step; + if (rotation > 360.f) + rotation -= 360.f; + + target->SetRotation(rotation); } ActionPtr RotateBy::Clone() const { - return new (std::nothrow) RotateBy(duration_, delta_val_, ease_type_); + return new (std::nothrow) RotateBy(duration_, delta_val_, ease_func_); } ActionPtr RotateBy::Reverse() const { - return new (std::nothrow) RotateBy(duration_, -delta_val_, ease_type_); + return new (std::nothrow) RotateBy(duration_, -delta_val_, ease_func_); } RotateTo::RotateTo(Duration duration, float rotation, EaseFunc func) @@ -503,7 +447,7 @@ namespace easy2d ActionPtr RotateTo::Clone() const { - return new (std::nothrow) RotateTo(duration_, end_val_, ease_type_); + return new (std::nothrow) RotateTo(duration_, end_val_, ease_func_); } void RotateTo::Init(Node* target) @@ -528,12 +472,12 @@ namespace easy2d ActionPtr PathAction::Clone() const { - return new PathAction(duration_, geo_, rotating_, start_, end_, ease_type_); + return new PathAction(duration_, geo_, rotating_, start_, end_, ease_func_); } ActionPtr PathAction::Reverse() const { - return new PathAction(duration_, geo_, rotating_, end_, start_, ease_type_); + return new PathAction(duration_, geo_, rotating_, end_, start_, ease_func_); } void PathAction::Init(Node * target) diff --git a/src/core/ActionTween.h b/src/core/ActionTween.h index b09f3e8c..26c64a07 100644 --- a/src/core/ActionTween.h +++ b/src/core/ActionTween.h @@ -25,52 +25,52 @@ namespace easy2d { + // 缓动函数 + using EaseFunc = std::function; + // 缓动函数枚举 // See https://easings.net for more information - enum class EaseFunc + struct Ease { - Linear, // 线性 - EaseIn, // 由慢变快 - EaseOut, // 由快变慢 - EaseInOut, // 由慢变快, 再由快变慢 - ExpoIn, // 由慢变极快 - ExpoOut, // 由极快变慢 - ExpoInOut, // 由慢至极快, 再由极快边慢 - ElasticIn, // 自起点赋予弹性 - ElasticOut, // 自终点赋予弹性 - ElasticInOut, // 再起点和终点赋予弹性 - BounceIn, // 自起点赋予反弹力 - BounceOut, // 自终点赋予反弹力 - BounceInOut, // 在起点和终点赋予反弹力 - BackIn, - BackOut, - BackInOut, - QuadIn, - QuadOut, - QuadInOut, - CubicIn, - CubicOut, - CubicInOut, - QuartIn, - QuartOut, - QuartInOut, - QuintIn, - QuintOut, - QuintInOut, - SineIn, - SineOut, - SineInOut, + static EaseFunc Linear; // 线性 + static EaseFunc EaseIn; // 由慢变快 + static EaseFunc EaseOut; // 由快变慢 + static EaseFunc EaseInOut; // 由慢变快, 再由快变慢 + static EaseFunc ExpoIn; // 由慢变极快 + static EaseFunc ExpoOut; // 由极快变慢 + static EaseFunc ExpoInOut; // 由慢至极快, 再由极快边慢 + static EaseFunc ElasticIn; // 自起点赋予弹性 + static EaseFunc ElasticOut; // 自终点赋予弹性 + static EaseFunc ElasticInOut; // 再起点和终点赋予弹性 + static EaseFunc BounceIn; // 自起点赋予反弹力 + static EaseFunc BounceOut; // 自终点赋予反弹力 + static EaseFunc BounceInOut; // 在起点和终点赋予反弹力 + static EaseFunc BackIn; + static EaseFunc BackOut; + static EaseFunc BackInOut; + static EaseFunc QuadIn; + static EaseFunc QuadOut; + static EaseFunc QuadInOut; + static EaseFunc CubicIn; + static EaseFunc CubicOut; + static EaseFunc CubicInOut; + static EaseFunc QuartIn; + static EaseFunc QuartOut; + static EaseFunc QuartInOut; + static EaseFunc QuintIn; + static EaseFunc QuintOut; + static EaseFunc QuintInOut; + static EaseFunc SineIn; + static EaseFunc SineOut; + static EaseFunc SineInOut; }; - // 缓动函数 - using EaseFunction = std::function; - - inline EaseFunction MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); } - inline EaseFunction MakeEaseOut(float rate) { return std::bind(math::EaseOut, std::placeholders::_1, rate); } - inline EaseFunction MakeEaseInOut(float rate) { return std::bind(math::EaseInOut, std::placeholders::_1, rate); } - inline EaseFunction MakeEaseElasticIn(float period) { return std::bind(math::EaseElasticIn, std::placeholders::_1, period); } - inline EaseFunction MakeEaseElasticOut(float period) { return std::bind(math::EaseElasticOut, std::placeholders::_1, period); } - inline EaseFunction MakeEaseElasticInOut(float period) { return std::bind(math::EaseElasticInOut, std::placeholders::_1, period); } + inline EaseFunc MakeEaseIn(float rate) { return std::bind(math::EaseIn, std::placeholders::_1, rate); } + inline EaseFunc MakeEaseOut(float rate) { return std::bind(math::EaseOut, std::placeholders::_1, rate); } + inline EaseFunc MakeEaseInOut(float rate) { return std::bind(math::EaseInOut, std::placeholders::_1, rate); } + inline EaseFunc MakeEaseElasticIn(float period) { return std::bind(math::EaseElasticIn, std::placeholders::_1, period); } + inline EaseFunc MakeEaseElasticOut(float period) { return std::bind(math::EaseElasticOut, std::placeholders::_1, period); } + inline EaseFunc MakeEaseElasticInOut(float period) { return std::bind(math::EaseElasticInOut, std::placeholders::_1, period); } // 补间动画 @@ -80,20 +80,17 @@ namespace easy2d public: ActionTween(); - explicit ActionTween( + ActionTween( Duration duration, EaseFunc func ); - // 设置缓动函数 - void SetEaseFunction( - EaseFunc func + // 自定义缓动函数 + void SetEaseFunc( + EaseFunc const& func ); - // 自定义缓动函数 - void SetEaseFunction( - EaseFunction func - ); + EaseFunc const& GetEaseFunc() const; void Reset() override; @@ -109,10 +106,9 @@ namespace easy2d virtual void UpdateStep(Node* target, float step) = 0; protected: - Duration duration_; - Duration elapsed_; - EaseFunc ease_type_; - EaseFunction ease_func_; + Duration duration_; + Duration elapsed_; + EaseFunc ease_func_; }; @@ -121,10 +117,10 @@ namespace easy2d : public ActionTween { public: - explicit MoveBy( - Duration duration, /* 持续时长 */ - Point const& vector, /* 移动距离 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + MoveBy( + Duration duration, /* 持续时长 */ + Point const& vector, /* 移动距离 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -150,10 +146,10 @@ namespace easy2d : public MoveBy { public: - explicit MoveTo( - Duration duration, /* 持续时长 */ - Point const& pos, /* 目的坐标 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + MoveTo( + Duration duration, /* 持续时长 */ + Point const& pos, /* 目的坐标 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -179,12 +175,12 @@ namespace easy2d : public ActionTween { public: - explicit JumpBy( - Duration duration, /* 持续时长 */ - Point const& vec, /* 跳跃距离 */ - float height, /* 跳跃高度 */ - int jumps = 1, /* 跳跃次数 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + JumpBy( + Duration duration, /* 持续时长 */ + Point const& vec, /* 跳跃距离 */ + float height, /* 跳跃高度 */ + int jumps = 1, /* 跳跃次数 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -212,12 +208,12 @@ namespace easy2d : public JumpBy { public: - explicit JumpTo( - Duration duration, /* 持续时长 */ - Point const& pos, /* 目的坐标 */ - float height, /* 跳跃高度 */ - int jumps = 1, /* 跳跃次数 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + JumpTo( + Duration duration, /* 持续时长 */ + Point const& pos, /* 目的坐标 */ + float height, /* 跳跃高度 */ + int jumps = 1, /* 跳跃次数 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -243,17 +239,17 @@ namespace easy2d : public ActionTween { public: - explicit ScaleBy( - Duration duration, /* 持续时长 */ - float scale, /* 相对变化值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + ScaleBy( + Duration duration, /* 持续时长 */ + float scale, /* 相对变化值 */ + EaseFunc func = nullptr /* 速度变化 */ ); - explicit ScaleBy( - Duration duration, /* 持续时长 */ - float scale_x, /* 横向缩放相对变化值 */ - float scale_y, /* 纵向缩放相对变化值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + ScaleBy( + Duration duration, /* 持续时长 */ + float scale_x, /* 横向缩放相对变化值 */ + float scale_y, /* 纵向缩放相对变化值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -280,17 +276,17 @@ namespace easy2d : public ScaleBy { public: - explicit ScaleTo( - Duration duration, /* 持续时长 */ - float scale, /* 目标值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + ScaleTo( + Duration duration, /* 持续时长 */ + float scale, /* 目标值 */ + EaseFunc func = nullptr /* 速度变化 */ ); - explicit ScaleTo( - Duration duration, /* 持续时长 */ - float scale_x, /* 横向缩放目标值 */ - float scale_y, /* 纵向缩放目标值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + ScaleTo( + Duration duration, /* 持续时长 */ + float scale_x, /* 横向缩放目标值 */ + float scale_y, /* 纵向缩放目标值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -317,10 +313,10 @@ namespace easy2d : public ActionTween { public: - explicit OpacityBy( - Duration duration, /* 持续时长 */ - float opacity, /* 相对变化值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + OpacityBy( + Duration duration, /* 持续时长 */ + float opacity, /* 相对变化值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -345,10 +341,10 @@ namespace easy2d : public OpacityBy { public: - explicit OpacityTo( - Duration duration, /* 持续时长 */ - float opacity, /* 目标值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + OpacityTo( + Duration duration, /* 持续时长 */ + float opacity, /* 目标值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -376,8 +372,8 @@ namespace easy2d public: // 创建淡入动作 explicit FadeIn( - Duration duration, /* 持续时长 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + Duration duration, /* 持续时长 */ + EaseFunc func = nullptr /* 速度变化 */ ); }; @@ -389,8 +385,8 @@ namespace easy2d public: // 创建淡出动作 explicit FadeOut( - Duration duration, /* 持续时长 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + Duration duration, /* 持续时长 */ + EaseFunc func = Ease::Linear /* 速度变化 */ ); }; @@ -400,10 +396,10 @@ namespace easy2d : public ActionTween { public: - explicit RotateBy( - Duration duration, /* 持续时长 */ - float rotation, /* 相对变化值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + RotateBy( + Duration duration, /* 持续时长 */ + float rotation, /* 相对变化值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -428,10 +424,10 @@ namespace easy2d : public RotateBy { public: - explicit RotateTo( - Duration duration, /* 持续时长 */ - float rotation, /* 目标值 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + RotateTo( + Duration duration, /* 持续时长 */ + float rotation, /* 目标值 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 @@ -457,13 +453,13 @@ namespace easy2d : public ActionTween { public: - explicit PathAction( - Duration duration, /* 持续时长 */ - GeometryPtr const& geo, /* 几何图形 */ - bool rotating = false, /* 沿路径切线方向旋转 */ - float start = 0.f, /* 起点 */ - float end = 1.f, /* 终点 */ - EaseFunc func = EaseFunc::Linear /* 速度变化 */ + PathAction( + Duration duration, /* 持续时长 */ + GeometryPtr const& geo, /* 几何图形 */ + bool rotating = false, /* 沿路径切线方向旋转 */ + float start = 0.f, /* 起点 */ + float end = 1.f, /* 终点 */ + EaseFunc func = nullptr /* 速度变化 */ ); // 获取该动作的拷贝对象 diff --git a/src/core/Animation.cpp b/src/core/Animation.cpp index a3b5298a..678959e3 100644 --- a/src/core/Animation.cpp +++ b/src/core/Animation.cpp @@ -86,7 +86,7 @@ namespace easy2d { if (frames_) { - return new (std::nothrow) Animation(duration_, frames_, ease_type_); + return new (std::nothrow) Animation(duration_, frames_, ease_func_); } return nullptr; } @@ -98,7 +98,7 @@ namespace easy2d FramesPtr frames = frames_->Reverse(); if (frames) { - return new (std::nothrow) Animation(duration_, frames, ease_type_); + return new (std::nothrow) Animation(duration_, frames, ease_func_); } } return nullptr; diff --git a/src/core/Animation.h b/src/core/Animation.h index c974aa0b..8345218d 100644 --- a/src/core/Animation.h +++ b/src/core/Animation.h @@ -33,7 +33,7 @@ namespace easy2d Animation( Duration duration, /* 动画时长 */ FramesPtr const& frames, /* 帧集合 */ - EaseFunc func = EaseFunc::Linear + EaseFunc func = nullptr /* 速度变化 */ ); virtual ~Animation(); diff --git a/src/core/Application.cpp b/src/core/Application.cpp index 40009204..2e46feae 100644 --- a/src/core/Application.cpp +++ b/src/core/Application.cpp @@ -28,7 +28,6 @@ #include "Transition.h" #include #include -#include #pragma comment (lib ,"imm32.lib") diff --git a/src/core/DebugNode.cpp b/src/core/DebugNode.cpp index dc49f46d..b3cc221f 100644 --- a/src/core/DebugNode.cpp +++ b/src/core/DebugNode.cpp @@ -69,8 +69,6 @@ namespace easy2d { auto rt = RenderSystem::Instance(); - rt->SetTransform(Matrix{}); - rt->GetSolidBrush()->SetColor(Color(0.0f, 0.0f, 0.0f, 0.5f)); rt->GetRenderTarget()->FillRoundedRectangle( diff --git a/src/core/helper.hpp b/src/core/helper.hpp index 7cfa6444..a8078b6e 100644 --- a/src/core/helper.hpp +++ b/src/core/helper.hpp @@ -50,6 +50,9 @@ namespace easy2d using String = std::wstring; using StringStream = std::wstringstream; + template + using Pair = std::pair; + template using Array = std::vector; diff --git a/src/core/time.h b/src/core/time.h index 1429c648..1e9ec81f 100644 --- a/src/core/time.h +++ b/src/core/time.h @@ -62,6 +62,8 @@ namespace easy2d // 转为字符串 std::wstring ToString() const; + inline operator bool() const { return !IsZero(); } + bool operator== (const Duration &) const; bool operator!= (const Duration &) const; bool operator> (const Duration &) const; diff --git a/src/core/window.cpp b/src/core/window.cpp index 0540055a..a86b01ab 100644 --- a/src/core/window.cpp +++ b/src/core/window.cpp @@ -45,6 +45,7 @@ namespace easy2d , width_(0) , height_(0) , device_name_(nullptr) + , is_fullscreen_(false) { } @@ -66,7 +67,7 @@ namespace easy2d { E2D_LOG(L"Creating window"); - HINSTANCE hinst = GetModuleHandleW(nullptr); + HINSTANCE hinst = GetModuleHandleW(nullptr); WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); wcex.lpszClassName = E2D_WND_CLASS_NAME; diff --git a/src/math/Rect.hpp b/src/math/Rect.hpp index 00ba1459..f8a845c0 100644 --- a/src/math/Rect.hpp +++ b/src/math/Rect.hpp @@ -96,6 +96,8 @@ namespace easy2d inline float GetBottom() const { return origin.y + size.y; } + inline bool IsEmpty() const { return origin.IsOrigin() && size.IsOrigin(); } + // 判断点是否在矩形内 inline bool ContainsPoint(const Vector2& point) const { diff --git a/src/math/vector.hpp b/src/math/vector.hpp index dba36693..ca44125e 100644 --- a/src/math/vector.hpp +++ b/src/math/vector.hpp @@ -42,6 +42,11 @@ namespace easy2d return math::Sqrt(x * x + y * y); } + inline bool IsOrigin() const + { + return (x == 0) && (y == 0); + } + inline const Vector2 operator + (const Vector2 & other) const { return Vector2(x + other.x, y + other.y); diff --git a/src/utils/ResLoader.cpp b/src/utils/ResLoader.cpp index 7f2befd9..d8d15c40 100644 --- a/src/utils/ResLoader.cpp +++ b/src/utils/ResLoader.cpp @@ -43,25 +43,57 @@ namespace easy2d void ResLoader::AddImage(String const& id, Resource const& image) { - auto path = Search(image.GetFileName(), search_paths_); - res_.insert(std::make_pair(id, ImagePtr(new Image(path.c_str())))); + String path = Search(image.GetFileName(), search_paths_); + ImagePtr ptr = new Image(path.c_str()); + res_.insert(std::make_pair(id, ptr)); + } + + void ResLoader::AddImage(String const & id, ImagePtr const & image) + { + res_.insert(std::make_pair(id, image)); } void ResLoader::AddFrames(String const& id, Array const& images) { - auto frames = FramesPtr(new Frames); + FramesPtr frames = new Frames; for (const auto& image : images) { - auto path = Search(image.GetFileName(), search_paths_); - frames->Add(ImagePtr(new Image(path.c_str()))); + String path = Search(image.GetFileName(), search_paths_); + ImagePtr ptr = new Image(path.c_str()); + frames->Add(ptr); + } + res_.insert(std::make_pair(id, frames)); + } + + void ResLoader::AddFrames(String const& id, Array const& images) + { + FramesPtr frames = new Frames; + for (const auto& image : images) + frames->Add(image); + res_.insert(std::make_pair(id, frames)); + } + + void ResLoader::AddFrames(String const& id, Array> const& images) + { + FramesPtr frames = new Frames; + for (const auto& pair : images) + { + String path = Search(pair.first.GetFileName(), search_paths_); + ImagePtr image = new Image(path.c_str()); + if (!pair.second.IsEmpty()) + { + image->Crop(pair.second); + } + frames->Add(image); } res_.insert(std::make_pair(id, frames)); } void ResLoader::AddMusic(String const & id, Resource const & music) { - auto path = Search(music.GetFileName(), search_paths_); - res_.insert(std::make_pair(id, MusicPtr(new Music(path.c_str())))); + String path = Search(music.GetFileName(), search_paths_); + MusicPtr ptr = new Music(path.c_str()); + res_.insert(std::make_pair(id, ptr)); } void ResLoader::AddObj(String const& id, ObjectPtr const& obj) diff --git a/src/utils/ResLoader.h b/src/utils/ResLoader.h index d6094e65..f15685e5 100644 --- a/src/utils/ResLoader.h +++ b/src/utils/ResLoader.h @@ -29,8 +29,14 @@ namespace easy2d public: void AddImage(String const& id, Resource const& image); + void AddImage(String const& id, ImagePtr const& image); + void AddFrames(String const& id, Array const& images); + void AddFrames(String const& id, Array> const& images); + + void AddFrames(String const& id, Array const& images); + void AddMusic(String const& id, Resource const& music); void AddObj(String const& id, ObjectPtr const& obj);