Magic_Game/src/kiwano/2d/action/ActionTween.cpp

438 lines
12 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/action/ActionTween.h>
#include <kiwano/2d/Actor.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
//-------------------------------------------------------
// Ease Functions
//-------------------------------------------------------
2019-12-23 18:05:08 +08:00
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); }
KGE_API EaseFunc Ease::Linear = math::Linear;
KGE_API EaseFunc Ease::EaseIn = MakeEaseIn(2.f);
KGE_API EaseFunc Ease::EaseOut = MakeEaseOut(2.f);
KGE_API EaseFunc Ease::EaseInOut = MakeEaseInOut(2.f);
KGE_API EaseFunc Ease::ExpoIn = math::EaseExponentialIn;
KGE_API EaseFunc Ease::ExpoOut = math::EaseExponentialOut;
KGE_API EaseFunc Ease::ExpoInOut = math::EaseExponentialInOut;
KGE_API EaseFunc Ease::BounceIn = math::EaseBounceIn;
KGE_API EaseFunc Ease::BounceOut = math::EaseBounceOut;
KGE_API EaseFunc Ease::BounceInOut = math::EaseBounceInOut;
KGE_API EaseFunc Ease::ElasticIn = MakeEaseElasticIn(0.3f);
KGE_API EaseFunc Ease::ElasticOut = MakeEaseElasticOut(0.3f);
KGE_API EaseFunc Ease::ElasticInOut = MakeEaseElasticInOut(0.3f);
KGE_API EaseFunc Ease::SineIn = math::EaseSineIn;
KGE_API EaseFunc Ease::SineOut = math::EaseSineOut;
KGE_API EaseFunc Ease::SineInOut = math::EaseSineInOut;
KGE_API EaseFunc Ease::BackIn = math::EaseBackIn;
KGE_API EaseFunc Ease::BackOut = math::EaseBackOut;
KGE_API EaseFunc Ease::BackInOut = math::EaseBackInOut;
KGE_API EaseFunc Ease::QuadIn = math::EaseQuadIn;
KGE_API EaseFunc Ease::QuadOut = math::EaseQuadOut;
KGE_API EaseFunc Ease::QuadInOut = math::EaseQuadInOut;
KGE_API EaseFunc Ease::CubicIn = math::EaseCubicIn;
KGE_API EaseFunc Ease::CubicOut = math::EaseCubicOut;
KGE_API EaseFunc Ease::CubicInOut = math::EaseCubicInOut;
KGE_API EaseFunc Ease::QuartIn = math::EaseQuartIn;
KGE_API EaseFunc Ease::QuartOut = math::EaseQuartOut;
KGE_API EaseFunc Ease::QuartInOut = math::EaseQuartInOut;
KGE_API EaseFunc Ease::QuintIn = math::EaseQuintIn;
KGE_API EaseFunc Ease::QuintOut = math::EaseQuintOut;
KGE_API EaseFunc Ease::QuintInOut = math::EaseQuintInOut;
//-------------------------------------------------------
// ActionTween
//-------------------------------------------------------
ActionTween::ActionTween()
: dur_()
, ease_func_(nullptr)
{
}
ActionTween::ActionTween(Duration duration, EaseFunc func)
{
SetDuration(duration);
SetEaseFunc(func);
}
void ActionTween::SetEaseFunc(EaseFunc const& func)
{
ease_func_ = func;
}
EaseFunc const & ActionTween::GetEaseFunc() const
{
return ease_func_;
}
Duration ActionTween::GetDuration() const
{
return dur_;
}
2019-12-23 18:05:08 +08:00
void ActionTween::Update(Actor* target, Duration dt)
{
2019-09-29 22:23:13 +08:00
float percent;
if (dur_.IsZero())
{
percent = 1.f;
Complete(target);
}
else
{
2019-11-07 18:16:28 +08:00
Duration elapsed = GetElapsed() - GetDelay();
2019-09-29 22:23:13 +08:00
float loops_done = elapsed / dur_;
2019-11-07 18:16:28 +08:00
while (GetLoopsDone() < static_cast<int>(loops_done))
{
Complete(target); // loops_done_++
}
2019-11-07 18:16:28 +08:00
percent = (GetStatus() == Status::Done) ? 1.f : (loops_done - static_cast<float>(GetLoopsDone()));
}
if (ease_func_)
percent = ease_func_(percent);
UpdateTween(target, percent);
}
void ActionTween::SetDuration(Duration duration)
{
dur_ = duration;
}
//-------------------------------------------------------
// Move Action
//-------------------------------------------------------
2019-12-23 18:05:08 +08:00
ActionMoveBy::ActionMoveBy(Duration duration, Vec2 const& vector, EaseFunc func)
: ActionTween(duration, func)
{
delta_pos_ = vector;
}
2019-12-23 18:05:08 +08:00
void ActionMoveBy::Init(Actor* target)
{
if (target)
{
prev_pos_ = start_pos_ = target->GetPosition();
}
}
2019-12-23 18:05:08 +08:00
void ActionMoveBy::UpdateTween(Actor* target, float percent)
{
Point diff = target->GetPosition() - prev_pos_;
start_pos_ = start_pos_ + diff;
Point new_pos = start_pos_ + (delta_pos_ * percent);
target->SetPosition(new_pos);
prev_pos_ = new_pos;
}
ActionPtr ActionMoveBy::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionMoveBy(GetDuration(), delta_pos_, GetEaseFunc());
}
ActionPtr ActionMoveBy::Reverse() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionMoveBy(GetDuration(), -delta_pos_, GetEaseFunc());
}
ActionMoveTo::ActionMoveTo(Duration duration, Point const& pos, EaseFunc func)
: ActionMoveBy(duration, Point(), func)
{
end_pos_ = pos;
}
ActionPtr ActionMoveTo::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionMoveTo(GetDuration(), end_pos_, GetEaseFunc());
}
2019-12-23 18:05:08 +08:00
void ActionMoveTo::Init(Actor* target)
{
ActionMoveBy::Init(target);
delta_pos_ = end_pos_ - start_pos_;
}
//-------------------------------------------------------
// Jump Action
//-------------------------------------------------------
2019-12-23 18:05:08 +08:00
ActionJumpBy::ActionJumpBy(Duration duration, Vec2 const& vec, float height, int jumps, EaseFunc func)
: ActionTween(duration, func)
, delta_pos_(vec)
, height_(height)
, jumps_(jumps)
{
}
ActionPtr ActionJumpBy::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionJumpBy(GetDuration(), delta_pos_, height_, jumps_, GetEaseFunc());
}
ActionPtr ActionJumpBy::Reverse() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionJumpBy(GetDuration(), -delta_pos_, height_, jumps_, GetEaseFunc());
}
2019-12-23 18:05:08 +08:00
void ActionJumpBy::Init(Actor* target)
{
if (target)
{
prev_pos_ = start_pos_ = target->GetPosition();
}
}
2019-12-23 18:05:08 +08:00
void ActionJumpBy::UpdateTween(Actor* target, float percent)
{
2019-09-29 22:23:13 +08:00
float frac = fmod(percent * jumps_, 1.f);
float x = delta_pos_.x * percent;
float y = height_ * 4 * frac * (1 - frac);
y += delta_pos_.y * percent;
Point diff = target->GetPosition() - prev_pos_;
start_pos_ = diff + start_pos_;
Point new_pos = start_pos_ + Point(x, y);
target->SetPosition(new_pos);
prev_pos_ = new_pos;
}
2019-09-29 22:23:13 +08:00
ActionJumpTo::ActionJumpTo(Duration duration, Point const& pos, float height, int jumps, EaseFunc func)
: ActionJumpBy(duration, Point(), height, jumps, func)
, end_pos_(pos)
{
}
ActionPtr ActionJumpTo::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionJumpTo(GetDuration(), end_pos_, height_, jumps_, GetEaseFunc());
}
2019-12-23 18:05:08 +08:00
void ActionJumpTo::Init(Actor* target)
{
ActionJumpBy::Init(target);
delta_pos_ = end_pos_ - start_pos_;
}
//-------------------------------------------------------
// Scale Action
//-------------------------------------------------------
2019-09-29 22:23:13 +08:00
ActionScaleBy::ActionScaleBy(Duration duration, float scale_x, float scale_y, EaseFunc func)
: ActionTween(duration, func)
2019-06-12 13:24:04 +08:00
, delta_x_(scale_x)
, delta_y_(scale_y)
, start_scale_x_(0.f)
, start_scale_y_(0.f)
{
}
2019-12-23 18:05:08 +08:00
void ActionScaleBy::Init(Actor* target)
{
if (target)
{
start_scale_x_ = target->GetScaleX();
start_scale_y_ = target->GetScaleY();
}
}
2019-12-23 18:05:08 +08:00
void ActionScaleBy::UpdateTween(Actor* target, float percent)
{
2019-08-20 19:32:36 +08:00
target->SetScale(Vec2{ start_scale_x_ + delta_x_ * percent, start_scale_y_ + delta_y_ * percent });
}
ActionPtr ActionScaleBy::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionScaleBy(GetDuration(), delta_x_, delta_y_, GetEaseFunc());
}
ActionPtr ActionScaleBy::Reverse() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionScaleBy(GetDuration(), -delta_x_, -delta_y_, GetEaseFunc());
}
2019-09-29 22:23:13 +08:00
ActionScaleTo::ActionScaleTo(Duration duration, float scale_x, float scale_y, EaseFunc func)
: ActionScaleBy(duration, 0, 0, func)
{
end_scale_x_ = scale_x;
end_scale_y_ = scale_y;
}
ActionPtr ActionScaleTo::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionScaleTo(GetDuration(), end_scale_x_, end_scale_y_, GetEaseFunc());
}
2019-12-23 18:05:08 +08:00
void ActionScaleTo::Init(Actor* target)
{
ActionScaleBy::Init(target);
delta_x_ = end_scale_x_ - start_scale_x_;
delta_y_ = end_scale_y_ - start_scale_y_;
}
//-------------------------------------------------------
// Opacity Action
//-------------------------------------------------------
2019-09-29 22:23:13 +08:00
ActionFadeTo::ActionFadeTo(Duration duration, float opacity, EaseFunc func)
: ActionTween(duration, func)
2019-07-29 12:56:17 +08:00
, delta_val_(0.f)
, start_val_(0.f)
2019-07-29 12:56:17 +08:00
, end_val_(opacity)
{
}
2019-12-23 18:05:08 +08:00
void ActionFadeTo::Init(Actor* target)
{
if (target)
{
start_val_ = target->GetOpacity();
2019-07-29 12:56:17 +08:00
delta_val_ = end_val_ - start_val_;
}
}
2019-12-23 18:05:08 +08:00
void ActionFadeTo::UpdateTween(Actor* target, float percent)
{
target->SetOpacity(start_val_ + delta_val_ * percent);
}
2019-07-29 12:56:17 +08:00
ActionPtr ActionFadeTo::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionFadeTo(GetDuration(), end_val_, GetEaseFunc());
}
ActionFadeIn::ActionFadeIn(Duration duration, EaseFunc func)
2019-07-29 12:56:17 +08:00
: ActionFadeTo(duration, 1, func)
{
}
ActionFadeOut::ActionFadeOut(Duration duration, EaseFunc func)
2019-07-29 12:56:17 +08:00
: ActionFadeTo(duration, 0, func)
{
}
//-------------------------------------------------------
// Rotate Action
//-------------------------------------------------------
2019-09-29 22:23:13 +08:00
ActionRotateBy::ActionRotateBy(Duration duration, float rotation, EaseFunc func)
: ActionTween(duration, func)
2019-06-12 13:24:04 +08:00
, start_val_()
, delta_val_(rotation)
{
}
2019-12-23 18:05:08 +08:00
void ActionRotateBy::Init(Actor* target)
{
if (target)
{
start_val_ = target->GetRotation();
}
}
2019-12-23 18:05:08 +08:00
void ActionRotateBy::UpdateTween(Actor* target, float percent)
{
2019-09-29 22:23:13 +08:00
float rotation = start_val_ + delta_val_ * percent;
if (rotation > 360.f)
rotation -= 360.f;
target->SetRotation(rotation);
}
ActionPtr ActionRotateBy::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionRotateBy(GetDuration(), delta_val_, GetEaseFunc());
}
ActionPtr ActionRotateBy::Reverse() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionRotateBy(GetDuration(), -delta_val_, GetEaseFunc());
}
2019-09-29 22:23:13 +08:00
ActionRotateTo::ActionRotateTo(Duration duration, float rotation, EaseFunc func)
: ActionRotateBy(duration, 0, func)
{
end_val_ = rotation;
}
ActionPtr ActionRotateTo::Clone() const
{
2019-12-23 18:05:08 +08:00
return new (std::nothrow) ActionRotateTo(GetDuration(), end_val_, GetEaseFunc());
}
2019-12-23 18:05:08 +08:00
void ActionRotateTo::Init(Actor* target)
{
ActionRotateBy::Init(target);
delta_val_ = end_val_ - start_val_;
}
2019-06-12 13:24:04 +08:00
//-------------------------------------------------------
// ActionCustom
//-------------------------------------------------------
ActionCustom::ActionCustom(Duration duration, TweenFunc tween_func, EaseFunc func)
: ActionTween(duration, func)
, tween_func_(tween_func)
{
}
ActionPtr ActionCustom::Clone() const
{
2019-12-23 18:05:08 +08:00
return new ActionCustom(GetDuration(), tween_func_);
2019-06-12 13:24:04 +08:00
}
2019-12-23 18:05:08 +08:00
void ActionCustom::Init(Actor* target)
2019-06-12 13:24:04 +08:00
{
if (!tween_func_)
this->Done();
}
2019-12-23 18:05:08 +08:00
void ActionCustom::UpdateTween(Actor* target, float percent)
2019-06-12 13:24:04 +08:00
{
if (tween_func_)
tween_func_(target, percent);
}
}