Magic_Game/src/kiwano/2d/animation/AnimationWrapper.h

312 lines
8.1 KiB
C
Raw Normal View History

2020-10-10 21:12:37 +08:00
// Copyright (c) 2020-2021 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.
#pragma once
#include <kiwano/2d/animation/Animation.h>
#include <kiwano/2d/animation/TweenAnimation.h>
#include <kiwano/2d/animation/PathAnimation.h>
#include <kiwano/2d/animation/DelayAnimation.h>
#include <kiwano/2d/animation/FrameAnimation.h>
#include <kiwano/2d/animation/CustomAnimation.h>
#include <kiwano/2d/animation/AnimationGroup.h>
namespace kiwano
{
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>
class KGE_API AnimationWrapper
{
public:
AnimationWrapper() = default;
inline AnimationWrapper(AnimationPtr ptr)
: ptr(ptr)
{
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper& Loops(int loops)
{
if (ptr)
ptr->SetLoops(loops);
return (*this);
}
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>ӳ<EFBFBD>
inline AnimationWrapper& Delay(Duration delay)
{
if (ptr)
ptr->SetDelay(delay);
return (*this);
}
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper& Handler(AnimationEventHandlerPtr handler)
{
if (ptr)
ptr->SetHandler(handler);
return (*this);
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƴ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ɫ
inline AnimationWrapper& RemoveTargetWhenDone()
{
if (ptr)
ptr->RemoveTargetWhenDone();
return (*this);
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper& Name(const String& name)
{
if (ptr)
ptr->SetName(name);
return (*this);
}
/// \~chinese
/// @brief <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper& Ease(const EaseFunc& ease)
{
auto tween = dynamic_cast<TweenAnimation*>(ptr.Get());
if (tween)
{
tween->SetEaseFunc(ease);
}
return (*this);
}
/// \~chinese
/// @brief <20><>¡<EFBFBD><C2A1><EFBFBD><EFBFBD>
inline AnimationWrapper Clone() const
{
if (ptr)
return AnimationWrapper(ptr->Clone());
return AnimationWrapper();
}
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>򶯻<EFBFBD>
inline AnimationWrapper Reverse() const
{
if (ptr)
return AnimationWrapper(ptr->Reverse());
return AnimationWrapper();
}
/// \~chinese
/// @brief <20><>ȡָ<C8A1><D6B8>
inline Animation* Get() const
{
return const_cast<Animation*>(ptr.Get());
}
/// \~chinese
/// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>
inline void SetEntity(AnimationPtr ptr)
{
this->ptr = ptr;
}
inline Animation* operator->() const
{
return Get();
}
inline operator Animation*() const
{
return Get();
}
inline operator AnimationPtr() const
{
return ptr;
}
inline operator bool() const
{
return ptr != nullptr;
}
protected:
AnimationPtr ptr;
};
namespace animation
{
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param displacement λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper MoveBy(kiwano::Duration duration, const Vec2& displacement)
{
return AnimationWrapper(new MoveByAnimation(duration, displacement));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>λ<EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param distination Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper MoveTo(kiwano::Duration duration, const Point& distination)
{
return AnimationWrapper(new MoveToAnimation(duration, distination));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param displacement <20><>Ծλ<D4BE><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param height <20><>Ծ<EFBFBD>߶<EFBFBD>
/// @param count <20><>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>
inline AnimationWrapper JumpBy(kiwano::Duration duration, const Vec2& displacement, float height, int count = 1)
{
return AnimationWrapper(new JumpByAnimation(duration, displacement, height, count));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param distination Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param height <20><>Ծ<EFBFBD>߶<EFBFBD>
/// @param count <20><>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>
inline AnimationWrapper JumpTo(kiwano::Duration duration, const Point& distination, float height, int count = 1)
{
return AnimationWrapper(new JumpToAnimation(duration, distination, height, count));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param scale <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա仯ֵ
inline AnimationWrapper ScaleBy(kiwano::Duration duration, Vec2 scale)
{
return AnimationWrapper(new ScaleByAnimation(duration, scale));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><C5B6><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param scale <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>ֵ
inline AnimationWrapper ScaleTo(kiwano::Duration duration, Vec2 scale)
{
return AnimationWrapper(new ScaleToAnimation(duration, scale));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>͸<EFBFBD><CDB8><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><E4B6AF>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param opacity Ŀ<><C4BF>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
inline AnimationWrapper FadeTo(kiwano::Duration duration, float opacity)
{
return AnimationWrapper(new FadeToAnimation(duration, opacity));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBB6AF>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
inline AnimationWrapper FadeIn(kiwano::Duration duration)
{
return AnimationWrapper(new FadeToAnimation(duration, 1.0f));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
inline AnimationWrapper FadeOut(kiwano::Duration duration)
{
return AnimationWrapper(new FadeToAnimation(duration, 0.0f));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param rotation <20>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>Ա仯ֵ
inline AnimationWrapper RotateBy(kiwano::Duration duration, float rotation)
{
return AnimationWrapper(new RotateByAnimation(duration, rotation));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param rotation Ŀ<><C4BF><EFBFBD>Ƕ<EFBFBD>
inline AnimationWrapper RotateTo(kiwano::Duration duration, float rotation)
{
return AnimationWrapper(new RotateToAnimation(duration, rotation));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><E5B6AF>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param tween_func <20><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
inline AnimationWrapper Custom(kiwano::Duration duration, TweenFunc tween_func)
{
return AnimationWrapper(new CustomAnimation(duration, tween_func));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param path ·<><C2B7><EFBFBD><EFBFBD>״
/// @param rotating <20>Ƿ<EFBFBD><C7B7><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD><DFB7><EFBFBD><EFBFBD><EFBFBD>ת
/// @param start ·<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷֱȣ<D6B1>
/// @param end ·<><C2B7><EFBFBD>յ㣨<D5B5>ٷֱȣ<D6B1>
inline AnimationWrapper Path(kiwano::Duration duration, ShapePtr path, bool rotating = false, float start = 0.f,
float end = 1.f)
{
return AnimationWrapper(new PathAnimation(duration, path, rotating, start, end));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
/// @param delay <20><>ʱʱ<CAB1><CAB1>
inline AnimationWrapper Delay(kiwano::Duration delay)
{
return AnimationWrapper(new DelayAnimation(delay));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>
/// @param duration <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
/// @param frame_seq <20><><EFBFBD><EFBFBD>֡
inline AnimationWrapper Frames(kiwano::Duration duration, FrameSequencePtr frame_seq)
{
return AnimationWrapper(new FrameAnimation(duration, frame_seq));
}
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param actions <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param parallel ͬ<><CDAC>ִ<EFBFBD><D6B4>
inline AnimationWrapper Group(const Vector<AnimationPtr>& animations, bool parallel = false)
{
return AnimationWrapper(new AnimationGroup(animations, parallel));
}
} // namespace animation
} // namespace kiwano