Kiwano Engine v1.3.x
AnimationWrapper.h
1// Copyright (c) 2020-2021 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/animation/Animation.h>
23#include <kiwano/2d/animation/TweenAnimation.h>
24#include <kiwano/2d/animation/PathAnimation.h>
25#include <kiwano/2d/animation/DelayAnimation.h>
26#include <kiwano/2d/animation/FrameAnimation.h>
27#include <kiwano/2d/animation/CustomAnimation.h>
28#include <kiwano/2d/animation/AnimationGroup.h>
29
35namespace kiwano
36{
37
40class KGE_API AnimationWrapper
41{
42public:
43 AnimationWrapper() = default;
44
46 : ptr(ptr)
47 {
48 }
49
52 inline AnimationWrapper& Loops(int loops)
53 {
54 if (ptr)
55 ptr->SetLoops(loops);
56 return (*this);
57 }
58
62 {
63 if (ptr)
64 ptr->SetDelay(delay);
65 return (*this);
66 }
67
71 {
72 if (ptr)
73 ptr->SetHandler(handler);
74 return (*this);
75 }
76
80 {
81 if (ptr)
82 ptr->RemoveTargetWhenDone();
83 return (*this);
84 }
85
89 {
90 if (ptr)
91 ptr->SetName(name);
92 return (*this);
93 }
94
97 inline AnimationWrapper& Ease(const EaseFunc& ease)
98 {
99 auto tween = dynamic_cast<TweenAnimation*>(ptr.Get());
100 if (tween)
101 {
102 tween->SetEaseFunc(ease);
103 }
104 return (*this);
105 }
106
109 inline AnimationWrapper Clone() const
110 {
111 if (ptr)
112 return AnimationWrapper(ptr->Clone());
113 return AnimationWrapper();
114 }
115
119 {
120 if (ptr)
121 return AnimationWrapper(ptr->Reverse());
122 return AnimationWrapper();
123 }
124
127 inline Animation* Get() const
128 {
129 return const_cast<Animation*>(ptr.Get());
130 }
131
135 {
136 this->ptr = ptr;
137 }
138
139 inline Animation* operator->() const
140 {
141 return Get();
142 }
143
144 inline operator Animation*() const
145 {
146 return Get();
147 }
148
149 inline operator RefPtr<Animation>() const
150 {
151 return ptr;
152 }
153
154 inline operator bool() const
155 {
156 return ptr != nullptr;
157 }
158
159protected:
160 RefPtr<Animation> ptr;
161};
162
163namespace animation
164{
165
170inline AnimationWrapper MoveBy(kiwano::Duration duration, const Vec2& displacement)
171{
172 return AnimationWrapper(new MoveByAnimation(duration, displacement));
173}
174
179inline AnimationWrapper MoveTo(kiwano::Duration duration, const Point& distination)
180{
181 return AnimationWrapper(new MoveToAnimation(duration, distination));
182}
183
190inline AnimationWrapper JumpBy(kiwano::Duration duration, const Vec2& displacement, float height, int count = 1)
191{
192 return AnimationWrapper(new JumpByAnimation(duration, displacement, height, count));
193}
194
201inline AnimationWrapper JumpTo(kiwano::Duration duration, const Point& distination, float height, int count = 1)
202{
203 return AnimationWrapper(new JumpToAnimation(duration, distination, height, count));
204}
205
210inline AnimationWrapper ScaleBy(kiwano::Duration duration, Vec2 scale)
211{
212 return AnimationWrapper(new ScaleByAnimation(duration, scale));
213}
214
219inline AnimationWrapper ScaleTo(kiwano::Duration duration, Vec2 scale)
220{
221 return AnimationWrapper(new ScaleToAnimation(duration, scale));
222}
223
228inline AnimationWrapper FadeTo(kiwano::Duration duration, float opacity)
229{
230 return AnimationWrapper(new FadeToAnimation(duration, opacity));
231}
232
236inline AnimationWrapper FadeIn(kiwano::Duration duration)
237{
238 return AnimationWrapper(new FadeToAnimation(duration, 1.0f));
239}
240
244inline AnimationWrapper FadeOut(kiwano::Duration duration)
245{
246 return AnimationWrapper(new FadeToAnimation(duration, 0.0f));
247}
248
253inline AnimationWrapper RotateBy(kiwano::Duration duration, float rotation)
254{
255 return AnimationWrapper(new RotateByAnimation(duration, rotation));
256}
257
262inline AnimationWrapper RotateTo(kiwano::Duration duration, float rotation)
263{
264 return AnimationWrapper(new RotateToAnimation(duration, rotation));
265}
266
271inline AnimationWrapper Custom(kiwano::Duration duration, Function<void(Actor*, float)> tween_func)
272{
273 return AnimationWrapper(CustomAnimation::Create(duration, tween_func));
274}
275
283inline AnimationWrapper Path(kiwano::Duration duration, RefPtr<Shape> path, bool rotating = false, float start = 0.f,
284 float end = 1.f)
285{
286 return AnimationWrapper(new PathAnimation(duration, path, rotating, start, end));
287}
288
292inline AnimationWrapper Delay(kiwano::Duration delay)
293{
294 return AnimationWrapper(new DelayAnimation(delay));
295}
296
301inline AnimationWrapper Frames(kiwano::Duration duration, RefPtr<FrameSequence> frame_seq)
302{
303 return AnimationWrapper(new FrameAnimation(duration, frame_seq));
304}
305
310inline AnimationWrapper Group(const Vector<RefPtr<Animation>>& animations, bool parallel = false)
311{
312 return AnimationWrapper(new AnimationGroup(animations, parallel));
313}
314
315} // namespace animation
316} // namespace kiwano
317
动画包装器
Definition: AnimationWrapper.h:41
AnimationWrapper & Handler(RefPtr< AnimationEventHandler > handler)
设置动画结束回调函数
Definition: AnimationWrapper.h:70
AnimationWrapper & RemoveTargetWhenDone()
动画结束时移除目标角色
Definition: AnimationWrapper.h:79
AnimationWrapper & Delay(Duration delay)
设置动画延迟
Definition: AnimationWrapper.h:61
AnimationWrapper Reverse() const
获取反向动画
Definition: AnimationWrapper.h:118
void SetEntity(RefPtr< Animation > ptr)
设置动画
Definition: AnimationWrapper.h:134
AnimationWrapper & Loops(int loops)
设置循环次数
Definition: AnimationWrapper.h:52
AnimationWrapper Clone() const
克隆动画
Definition: AnimationWrapper.h:109
AnimationWrapper & Ease(const EaseFunc &ease)
设置缓动函数
Definition: AnimationWrapper.h:97
Animation * Get() const
获取指针
Definition: AnimationWrapper.h:127
AnimationWrapper & Name(StringView name)
设置名称
Definition: AnimationWrapper.h:88
动画
Definition: Animation.h:103
static RefPtr< CustomAnimation > Create(Duration duration, Function< void(Actor *, float)> tween_func)
创建自定义动画
Definition: CustomAnimation.cpp:31
引用计数智能指针
Definition: RefBasePtr.hpp:35
补间动画
Definition: TweenAnimation.h:37
void SetEaseFunc(const EaseFunc &func)
设置动画速度缓动函数
Definition: TweenAnimation.h:472
时间段
Definition: Duration.h:48