Kiwano Engine v1.3.x
Animation.h
1// Copyright (c) 2016-2018 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/core/Common.h>
23#include <kiwano/core/Cloneable.h>
24#include <kiwano/base/ObjectBase.h>
25#include <kiwano/core/Time.h>
26#include <kiwano/core/IntrusiveList.h>
27#include <kiwano/math/Math.h>
28
29namespace kiwano
30{
31class Actor;
32class Animator;
33class Animation;
34
48{
49 Started,
50 LoopDone,
51 Done,
52};
53
56class KGE_API AnimationEventHandler : public RefObject
57{
58public:
64 virtual void Handle(Animation* anim, Actor* target, AnimationEvent evt) = 0;
65
69 static RefPtr<AnimationEventHandler> Create(const Function<void(Animation*, Actor*, AnimationEvent)>& handler);
70
75 static RefPtr<AnimationEventHandler> Create(AnimationEvent evt, const Function<void(Animation*, Actor*)>& handler);
76
80 static RefPtr<AnimationEventHandler> HandleStarted(const Function<void(Animation*, Actor*)>& handler);
81
85 static RefPtr<AnimationEventHandler> HandleLoopDone(const Function<void(Animation*, Actor*)>& handler);
86
90 static RefPtr<AnimationEventHandler> HandleDone(const Function<void(Animation*, Actor*)>& handler);
91};
92
96
99class KGE_API Animation
100 : public ObjectBase
101 , public Cloneable<Animation>
102 , protected IntrusiveListValue<RefPtr<Animation>>
103{
104 friend class Animator;
105 friend class AnimationGroup;
107
108public:
109 Animation();
110
111 virtual ~Animation();
112
115 void Resume();
116
119 void Pause();
120
123 void Stop();
124
127 void SetDelay(Duration delay);
128
132 void SetLoops(int loops);
133
136 void RemoveTargetWhenDone();
137
140 virtual Animation* Reverse() const = 0;
141
144 bool IsRunning() const;
145
148 int GetLoops() const;
149
152 Duration GetDelay() const;
153
156 void SetHandler(RefPtr<AnimationEventHandler> handler);
157
160 RefPtr<AnimationEventHandler> GetHandler() const;
161
162protected:
165 virtual void Init(Actor* target);
166
169 virtual void Update(Actor* target, Duration dt);
170
173 void UpdateStep(Actor* target, Duration dt);
174
177 void Complete(Actor* target);
178
181 void Reset();
182
185 enum class Status
186 {
187 NotStarted,
188 Delayed,
189 Started,
190 Done,
191 Removeable
192 };
193
196 Status GetStatus() const;
197
200 Duration GetElapsed() const;
201
204 int GetLoopsDone() const;
205
208 void Done();
209
212 bool IsDone() const;
213
216 bool IsRemoveable() const;
217
220 void EmitEvent(Actor* target, AnimationEvent evt);
221
224 void DoClone(Animation* to) const;
225
226private:
227 Status status_;
228 bool running_;
229 bool detach_target_;
230 int loops_;
231 int loops_done_;
232 Duration delay_;
233 Duration elapsed_;
234
236};
237
240inline void Animation::Resume()
241{
242 running_ = true;
243}
244
245inline void Animation::Pause()
246{
247 running_ = false;
248}
249
250inline void Animation::Stop()
251{
252 Done();
253}
254
256{
257 delay_ = delay;
258}
259
260inline void Animation::SetLoops(int loops)
261{
262 loops_ = loops;
263}
264
266{
267 detach_target_ = true;
268}
269
270inline void Animation::Done()
271{
272 status_ = Status::Done;
273}
274
276{
277 return status_;
278}
279
280inline bool Animation::IsRunning() const
281{
282 return running_;
283}
284
285inline bool Animation::IsDone() const
286{
287 return status_ == Status::Done || status_ == Status::Removeable;
288}
289
290inline bool Animation::IsRemoveable() const
291{
292 return status_ == Status::Removeable;
293}
294
296{
297 if (handler_)
298 {
299 handler_->Handle(this, target, evt);
300 }
301}
302
303inline int Animation::GetLoops() const
304{
305 return loops_;
306}
307
309{
310 return delay_;
311}
312
314{
315 handler_ = handler;
316}
317
319{
320 return handler_;
321}
322
324{
325 return elapsed_;
326}
327
328inline int Animation::GetLoopsDone() const
329{
330 return loops_done_;
331}
332
333} // namespace kiwano
角色
Definition: Actor.h:63
动画事件处理器
Definition: Animation.h:57
virtual void Handle(Animation *anim, Actor *target, AnimationEvent evt)=0
处理动画事件
动画组合
Definition: AnimationGroup.h:34
动画
Definition: Animation.h:103
void Done()
结束动画
Definition: Animation.h:270
RefPtr< AnimationEventHandler > GetHandler() const
获取动画事件处理
Definition: Animation.h:318
Status
动画状态
Definition: Animation.h:186
void EmitEvent(Actor *target, AnimationEvent evt)
发出动画事件
Definition: Animation.h:295
bool IsRemoveable() const
是否可移除
Definition: Animation.h:290
virtual Animation * Reverse() const =0
获取动画的倒转
void RemoveTargetWhenDone()
动画结束时移除目标角色
Definition: Animation.h:265
Duration GetDelay() const
获取动画的延时
Definition: Animation.h:308
void Resume()
继续动画
Definition: Animation.h:240
void SetDelay(Duration delay)
设置动画延时
Definition: Animation.h:255
void Pause()
暂停动画
Definition: Animation.h:245
void SetHandler(RefPtr< AnimationEventHandler > handler)
设置动画事件处理
Definition: Animation.h:313
bool IsDone() const
是否已结束
Definition: Animation.h:285
bool IsRunning() const
获取动画的运行状态
Definition: Animation.h:280
Status GetStatus() const
获取动画状态
Definition: Animation.h:275
void SetLoops(int loops)
设置循环次数
Definition: Animation.h:260
int GetLoopsDone() const
获取完成的循环次数
Definition: Animation.h:328
int GetLoops() const
获取动画的循环次数
Definition: Animation.h:303
Duration GetElapsed() const
获取消逝时间
Definition: Animation.h:323
void Stop()
停止动画
Definition: Animation.h:250
动画调度器
Definition: Animator.h:37
Definition: Cloneable.h:28
Definition: Function.h:228
侵入式链表元素
Definition: IntrusiveList.h:434
侵入式链表
Definition: IntrusiveList.h:34
基础对象
Definition: ObjectBase.h:138
引用计数智能指针
Definition: RefBasePtr.hpp:35
引用计数器
Definition: RefObject.h:33
AnimationEvent
动画事件
Definition: Animation.h:48
IntrusiveList< RefPtr< Animation > > AnimationList
动画列表
Definition: Animation.h:95
@ LoopDone
动画一次循环结束
@ Started
动画开始
时间段
Definition: Duration.h:48