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

550 lines
14 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// 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:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// 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-12-25 18:07:57 +08:00
#include <functional>
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/2d/action/ActionTween.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
//-------------------------------------------------------
// Ease Functions
//-------------------------------------------------------
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)
2020-02-12 22:34:40 +08:00
: dur_(duration)
, ease_func_(func)
2020-01-21 10:09:55 +08:00
{
}
void ActionTween::Update(Actor* target, Duration dt)
{
float percent;
if (dur_.IsZero())
{
percent = 1.f;
Complete(target);
}
else
{
Duration elapsed = GetElapsed() - GetDelay();
float loops_done = elapsed / dur_;
while (GetLoopsDone() < static_cast<int>(loops_done))
{
Complete(target); // loops_done_++
}
percent = (GetStatus() == Status::Done) ? 1.f : (loops_done - static_cast<float>(GetLoopsDone()));
}
if (ease_func_)
percent = ease_func_(percent);
UpdateTween(target, percent);
}
2020-04-03 17:48:00 +08:00
ActionPtr ActionTween::DoClone(ActionTweenPtr to) const
2020-02-12 23:37:05 +08:00
{
if (to)
{
to->SetDuration(this->GetDuration());
to->SetEaseFunc(this->GetEaseFunc());
}
2020-04-03 17:48:00 +08:00
return Action::DoClone(to);
2020-02-12 23:37:05 +08:00
}
2020-01-21 10:09:55 +08:00
//-------------------------------------------------------
// Move Action
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
ActionMoveByPtr ActionMoveBy::Create(Duration duration, const Vec2& displacement)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionMoveByPtr ptr = memory::New<ActionMoveBy>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetDisplacement(displacement);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
2020-02-12 22:34:40 +08:00
ActionMoveBy::ActionMoveBy() {}
2020-01-21 10:09:55 +08:00
void ActionMoveBy::Init(Actor* target)
{
if (target)
{
prev_pos_ = start_pos_ = target->GetPosition();
}
}
void ActionMoveBy::UpdateTween(Actor* target, float percent)
{
Point diff = target->GetPosition() - prev_pos_;
start_pos_ = start_pos_ + diff;
2020-02-12 22:34:40 +08:00
Point new_pos = start_pos_ + (displacement_ * percent);
2020-01-21 10:09:55 +08:00
target->SetPosition(new_pos);
prev_pos_ = new_pos;
}
ActionPtr ActionMoveBy::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionMoveBy::Create(GetDuration(), displacement_));
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionMoveBy::Reverse() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionMoveBy::Create(GetDuration(), -displacement_));
2020-01-21 10:09:55 +08:00
}
2020-02-19 12:09:50 +08:00
ActionMoveToPtr ActionMoveTo::Create(Duration duration, const Point& distination)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionMoveToPtr ptr = memory::New<ActionMoveTo>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetDistination(distination);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionMoveTo::ActionMoveTo() {}
2020-01-21 10:09:55 +08:00
ActionPtr ActionMoveTo::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionMoveTo::Create(GetDuration(), distination_));
2020-01-21 10:09:55 +08:00
}
void ActionMoveTo::Init(Actor* target)
{
ActionMoveBy::Init(target);
2020-02-12 22:34:40 +08:00
displacement_ = distination_ - start_pos_;
2020-01-21 10:09:55 +08:00
}
//-------------------------------------------------------
// Jump Action
//-------------------------------------------------------
2020-02-19 12:09:50 +08:00
ActionJumpByPtr ActionJumpBy::Create(Duration duration, const Vec2& displacement, float height, int count,
2020-02-12 22:34:40 +08:00
EaseFunc ease)
{
2020-02-20 22:27:09 +08:00
ActionJumpByPtr ptr = memory::New<ActionJumpBy>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetEaseFunc(ease);
ptr->SetJumpHeight(height);
ptr->SetJumpCount(count);
ptr->SetDisplacement(displacement);
}
return ptr;
}
ActionJumpBy::ActionJumpBy()
: height_(0.0f)
, jump_count_(0)
2020-01-21 10:09:55 +08:00
{
}
ActionPtr ActionJumpBy::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionJumpBy::Create(GetDuration(), displacement_, height_, jump_count_));
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionJumpBy::Reverse() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionJumpBy::Create(GetDuration(), -displacement_, height_, jump_count_));
2020-01-21 10:09:55 +08:00
}
void ActionJumpBy::Init(Actor* target)
{
if (target)
{
prev_pos_ = start_pos_ = target->GetPosition();
}
}
void ActionJumpBy::UpdateTween(Actor* target, float percent)
{
2020-02-12 22:34:40 +08:00
float frac = fmod(percent * jump_count_, 1.f);
float x = displacement_.x * percent;
2020-01-21 10:09:55 +08:00
float y = height_ * 4 * frac * (1 - frac);
2020-02-12 22:34:40 +08:00
y += displacement_.y * percent;
2020-01-21 10:09:55 +08:00
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;
}
2020-02-19 12:09:50 +08:00
ActionJumpToPtr ActionJumpTo::Create(Duration duration, const Point& distination, float height, int count,
2020-02-12 22:34:40 +08:00
EaseFunc ease)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionJumpToPtr ptr = memory::New<ActionJumpTo>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetEaseFunc(ease);
ptr->SetJumpHeight(height);
ptr->SetJumpCount(count);
ptr->SetDistination(distination);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionJumpTo::ActionJumpTo() {}
2020-01-21 10:09:55 +08:00
ActionPtr ActionJumpTo::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionJumpTo::Create(GetDuration(), distination_, height_, jump_count_));
2020-01-21 10:09:55 +08:00
}
void ActionJumpTo::Init(Actor* target)
{
ActionJumpBy::Init(target);
2020-02-12 22:34:40 +08:00
displacement_ = distination_ - start_pos_;
2020-01-21 10:09:55 +08:00
}
//-------------------------------------------------------
// Scale Action
//-------------------------------------------------------
2020-02-12 22:34:40 +08:00
ActionScaleByPtr ActionScaleBy::Create(Duration duration, float scale_x, float scale_y)
{
2020-02-20 22:27:09 +08:00
ActionScaleByPtr ptr = memory::New<ActionScaleBy>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetScaleX(scale_x);
ptr->SetScaleY(scale_y);
}
return ptr;
}
ActionScaleBy::ActionScaleBy()
: delta_x_(0.0f)
, delta_y_(0.0f)
2020-01-21 10:09:55 +08:00
, start_scale_x_(0.f)
, start_scale_y_(0.f)
{
}
void ActionScaleBy::Init(Actor* target)
{
if (target)
{
start_scale_x_ = target->GetScaleX();
start_scale_y_ = target->GetScaleY();
}
}
void ActionScaleBy::UpdateTween(Actor* target, float percent)
{
target->SetScale(Vec2{ start_scale_x_ + delta_x_ * percent, start_scale_y_ + delta_y_ * percent });
}
ActionPtr ActionScaleBy::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionScaleBy::Create(GetDuration(), delta_x_, delta_y_));
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionScaleBy::Reverse() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionScaleBy::Create(GetDuration(), -delta_x_, -delta_y_));
2020-02-12 22:34:40 +08:00
}
ActionScaleToPtr ActionScaleTo::Create(Duration duration, float scale_x, float scale_y)
{
2020-02-20 22:27:09 +08:00
ActionScaleToPtr ptr = memory::New<ActionScaleTo>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTargetScaleX(scale_x);
ptr->SetTargetScaleY(scale_y);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionScaleTo::ActionScaleTo()
: end_scale_x_(0.0f)
, end_scale_y_(0.0f)
2020-01-21 10:09:55 +08:00
{
}
ActionPtr ActionScaleTo::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionScaleTo::Create(GetDuration(), end_scale_x_, end_scale_y_));
2020-01-21 10:09:55 +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
//-------------------------------------------------------
2020-02-12 22:34:40 +08:00
ActionFadeToPtr ActionFadeTo::Create(Duration duration, float opacity)
{
2020-02-20 22:27:09 +08:00
ActionFadeToPtr ptr = memory::New<ActionFadeTo>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTargetOpacity(opacity);
}
return ptr;
}
ActionFadeTo::ActionFadeTo()
: delta_val_(0.0f)
2020-01-21 10:09:55 +08:00
, start_val_(0.f)
2020-02-12 22:34:40 +08:00
, end_val_(0.0f)
2020-01-21 10:09:55 +08:00
{
}
void ActionFadeTo::Init(Actor* target)
{
if (target)
{
start_val_ = target->GetOpacity();
delta_val_ = end_val_ - start_val_;
}
}
void ActionFadeTo::UpdateTween(Actor* target, float percent)
{
target->SetOpacity(start_val_ + delta_val_ * percent);
}
ActionPtr ActionFadeTo::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionFadeTo::Create(GetDuration(), end_val_));
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionFadeInPtr ActionFadeIn::Create(Duration duration)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionFadeInPtr ptr = memory::New<ActionFadeIn>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTargetOpacity(1.0f);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionFadeOutPtr ActionFadeOut::Create(Duration duration)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionFadeOutPtr ptr = memory::New<ActionFadeOut>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTargetOpacity(0.0f);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
//-------------------------------------------------------
// Rotate Action
//-------------------------------------------------------
2020-02-12 22:34:40 +08:00
ActionRotateByPtr ActionRotateBy::Create(Duration duration, float rotation)
{
2020-02-20 22:27:09 +08:00
ActionRotateByPtr ptr = memory::New<ActionRotateBy>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetRotation(rotation);
}
return ptr;
}
ActionRotateBy::ActionRotateBy()
: start_val_(0.0f)
, delta_val_(0.0f)
2020-01-21 10:09:55 +08:00
{
}
void ActionRotateBy::Init(Actor* target)
{
if (target)
{
start_val_ = target->GetRotation();
}
}
void ActionRotateBy::UpdateTween(Actor* target, float percent)
{
float rotation = start_val_ + delta_val_ * percent;
if (rotation > 360.f)
rotation -= 360.f;
target->SetRotation(rotation);
}
ActionPtr ActionRotateBy::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionRotateBy::Create(GetDuration(), delta_val_));
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionRotateBy::Reverse() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionRotateBy::Create(GetDuration(), -delta_val_));
2020-02-12 22:34:40 +08:00
}
ActionRotateToPtr ActionRotateTo::Create(Duration duration, float rotation)
{
2020-02-20 22:27:09 +08:00
ActionRotateToPtr ptr = memory::New<ActionRotateTo>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTargetRotation(rotation);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionRotateTo::ActionRotateTo()
: end_val_(0.0f)
2020-01-21 10:09:55 +08:00
{
}
ActionPtr ActionRotateTo::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionRotateTo::Create(GetDuration(), end_val_));
2020-01-21 10:09:55 +08:00
}
void ActionRotateTo::Init(Actor* target)
{
ActionRotateBy::Init(target);
delta_val_ = end_val_ - start_val_;
}
//-------------------------------------------------------
// ActionCustom
//-------------------------------------------------------
2020-02-12 22:34:40 +08:00
ActionCustomPtr ActionCustom::Create(Duration duration, TweenFunc tween_func)
2020-01-21 10:09:55 +08:00
{
2020-02-20 22:27:09 +08:00
ActionCustomPtr ptr = memory::New<ActionCustom>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
ptr->SetDuration(duration);
ptr->SetTweenFunc(tween_func);
}
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-02-12 22:34:40 +08:00
ActionCustom::ActionCustom() {}
2020-01-21 10:09:55 +08:00
ActionPtr ActionCustom::Clone() const
{
2020-04-03 17:48:00 +08:00
return DoClone(ActionCustom::Create(GetDuration(), tween_func_));
2020-01-21 10:09:55 +08:00
}
void ActionCustom::Init(Actor* target)
{
if (!tween_func_)
this->Done();
}
void ActionCustom::UpdateTween(Actor* target, float percent)
{
if (tween_func_)
tween_func_(target, percent);
}
} // namespace kiwano