Kiwano Engine v1.3.x
TweenAnimation.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/2d/animation/Animation.h>
23#include <kiwano/2d/animation/EaseFunc.h>
24#include <kiwano/utils/Logger.h>
25
26namespace kiwano
27{
28
36class KGE_API TweenAnimation : public Animation
37{
38public:
41 Duration GetDuration() const;
42
45 void SetDuration(Duration duration);
46
49 const EaseFunc& GetEaseFunc() const;
50
53 void SetEaseFunc(const EaseFunc& func);
54
55protected:
57
58 TweenAnimation(Duration duration);
59
60 float Interpolate(float frac);
61
62 void Update(Actor* target, Duration dt) override;
63
64 virtual void UpdateTween(Actor* target, float frac) = 0;
65
66 void DoClone(TweenAnimation* to) const;
67
68private:
69 Duration dur_;
70 EaseFunc ease_func_;
71};
72
75class KGE_API MoveByAnimation : public TweenAnimation
76{
77public:
82 MoveByAnimation(Duration duration, const Vec2& displacement);
83
86 Vec2 GetDisplacement() const;
87
90 void SetDisplacement(const Vec2& displacement);
91
94 MoveByAnimation* Clone() const override;
95
98 MoveByAnimation* Reverse() const override;
99
100protected:
101 void Init(Actor* target) override;
102
103 void UpdateTween(Actor* target, float frac) override;
104
105protected:
106 Point start_pos_;
107 Point prev_pos_;
108 Vec2 displacement_;
109};
110
113class KGE_API MoveToAnimation : public MoveByAnimation
114{
115public:
120 MoveToAnimation(Duration duration, const Point& distination);
121
124 Point GetDistination() const;
125
128 void SetDistination(const Point& distination);
129
132 MoveToAnimation* Clone() const override;
133
136 MoveToAnimation* Reverse() const override
137 {
138 KGE_ERRORF("Reverse() not supported in MoveToAnimation");
139 return nullptr;
140 }
141
142protected:
143 void Init(Actor* target) override;
144
145private:
146 Point distination_;
147};
148
151class KGE_API JumpByAnimation : public TweenAnimation
152{
153public:
160 JumpByAnimation(Duration duration, const Vec2& displacement, float height, int count = 1);
161
164 Vec2 GetDisplacement() const;
165
168 float GetJumpHeight() const;
169
172 int GetJumpCount() const;
173
176 void SetDisplacement(const Vec2& displacement);
177
180 void SetJumpHeight(float height);
181
184 void SetJumpCount(int count);
185
188 JumpByAnimation* Clone() const override;
189
192 JumpByAnimation* Reverse() const override;
193
194protected:
195 void Init(Actor* target) override;
196
197 void UpdateTween(Actor* target, float frac) override;
198
199protected:
200 float height_;
201 int jump_count_;
202 Point start_pos_;
203 Point displacement_;
204 Point prev_pos_;
205};
206
209class KGE_API JumpToAnimation : public JumpByAnimation
210{
211public:
218 JumpToAnimation(Duration duration, const Point& distination, float height, int count = 1);
219
222 Point GetDistination() const;
223
226 void SetDistination(const Point& distination);
227
230 JumpToAnimation* Clone() const override;
231
234 JumpToAnimation* Reverse() const override
235 {
236 KGE_ERRORF("Reverse() not supported in JumpToAnimation");
237 return nullptr;
238 }
239
240protected:
241 void Init(Actor* target) override;
242
243private:
244 Point distination_;
245};
246
249class KGE_API ScaleByAnimation : public TweenAnimation
250{
251public:
256 ScaleByAnimation(Duration duration, const Vec2& scale);
257
260 float GetScaleX() const;
261
264 float GetScaleY() const;
265
268 void SetScaleX(float scale_x);
269
272 void SetScaleY(float scale_y);
273
276 ScaleByAnimation* Clone() const override;
277
280 ScaleByAnimation* Reverse() const override;
281
282protected:
283 void Init(Actor* target) override;
284
285 void UpdateTween(Actor* target, float frac) override;
286
287protected:
288 Vec2 start_val_;
289 Vec2 delta_;
290};
291
294class KGE_API ScaleToAnimation : public ScaleByAnimation
295{
296public:
301 ScaleToAnimation(Duration duration, const Vec2& scale);
302
305 float GetTargetScaleX() const;
306
309 float GetTargetScaleY() const;
310
313 void SetTargetScaleX(float scale_x);
314
317 void SetTargetScaleY(float scale_y);
318
321 ScaleToAnimation* Clone() const override;
322
325 ScaleToAnimation* Reverse() const override
326 {
327 KGE_ERRORF("Reverse() not supported in ScaleToAnimation");
328 return nullptr;
329 }
330
331protected:
332 void Init(Actor* target) override;
333
334private:
335 Vec2 end_val_;
336};
337
340class KGE_API FadeToAnimation : public TweenAnimation
341{
342public:
347 FadeToAnimation(Duration duration, float opacity);
348
351 float GetTargetOpacity() const;
352
355 void SetTargetOpacity(float opacity);
356
359 FadeToAnimation* Clone() const override;
360
363 FadeToAnimation* Reverse() const override
364 {
365 KGE_ERRORF("Reverse() not supported in FadeToAnimation");
366 return nullptr;
367 }
368
369protected:
370 void Init(Actor* target) override;
371
372 void UpdateTween(Actor* target, float frac) override;
373
374private:
375 float start_val_;
376 float delta_val_;
377 float end_val_;
378};
379
382class KGE_API RotateByAnimation : public TweenAnimation
383{
384public:
389 RotateByAnimation(Duration duration, float rotation);
390
393 float GetRotation() const;
394
397 void SetRotation(float rotation);
398
401 RotateByAnimation* Clone() const override;
402
405 RotateByAnimation* Reverse() const override;
406
407protected:
408 void Init(Actor* target) override;
409
410 void UpdateTween(Actor* target, float frac) override;
411
412protected:
413 float start_val_;
414 float delta_val_;
415};
416
420{
421public:
426 RotateToAnimation(Duration duration, float rotation);
427
430 float GetTargetRotation() const;
431
434 void SetTargetRotation(float rotation);
435
438 RotateToAnimation* Clone() const override;
439
442 RotateToAnimation* Reverse() const override
443 {
444 KGE_ERRORF("Reverse() not supported in RotateToAnimation");
445 return nullptr;
446 }
447
448protected:
449 void Init(Actor* target) override;
450
451private:
452 float end_val_;
453};
454
458{
459 return ease_func_;
460}
461
463{
464 return dur_;
465}
466
468{
469 dur_ = duration;
470}
471
472inline void TweenAnimation::SetEaseFunc(const EaseFunc& func)
473{
474 ease_func_ = func;
475}
476
478{
479 return displacement_;
480}
481
482inline void MoveByAnimation::SetDisplacement(const Vec2& displacement)
483{
484 displacement_ = displacement;
485}
486
488{
489 return distination_;
490}
491
492inline void MoveToAnimation::SetDistination(const Point& distination)
493{
494 distination_ = distination;
495}
496
498{
499 return displacement_;
500}
501
503{
504 return height_;
505}
506
508{
509 return jump_count_;
510}
511
512inline void JumpByAnimation::SetDisplacement(const Vec2& displacement)
513{
514 displacement_ = displacement;
515}
516
517inline void JumpByAnimation::SetJumpHeight(float height)
518{
519 height_ = height;
520}
521
522inline void JumpByAnimation::SetJumpCount(int count)
523{
524 jump_count_ = count;
525}
526
528{
529 return distination_;
530}
531
532inline void JumpToAnimation::SetDistination(const Point& distination)
533{
534 distination_ = distination;
535}
536
537inline float ScaleByAnimation::GetScaleX() const
538{
539 return delta_.x;
540}
541
542inline float ScaleByAnimation::GetScaleY() const
543{
544 return delta_.y;
545}
546
547inline void ScaleByAnimation::SetScaleX(float scale_x)
548{
549 delta_.x = scale_x;
550}
551
552inline void ScaleByAnimation::SetScaleY(float scale_y)
553{
554 delta_.y = scale_y;
555}
556
558{
559 return end_val_.x;
560}
561
563{
564 return end_val_.y;
565}
566
567inline void ScaleToAnimation::SetTargetScaleX(float scale_x)
568{
569 end_val_.x = scale_x;
570}
571
572inline void ScaleToAnimation::SetTargetScaleY(float scale_y)
573{
574 end_val_.y = scale_y;
575}
576
578{
579 return end_val_;
580}
581
582inline void FadeToAnimation::SetTargetOpacity(float opacity)
583{
584 end_val_ = opacity;
585}
586
588{
589 return delta_val_;
590}
591
592inline void RotateByAnimation::SetRotation(float rotation)
593{
594 delta_val_ = rotation;
595}
596
598{
599 return end_val_;
600}
601
602inline void RotateToAnimation::SetTargetRotation(float rotation)
603{
604 end_val_ = rotation;
605}
606
607} // namespace kiwano
角色
Definition: Actor.h:63
动画
Definition: Animation.h:103
透明度渐变动画
Definition: TweenAnimation.h:341
void SetTargetOpacity(float opacity)
设置目标透明度
Definition: TweenAnimation.h:582
FadeToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:363
float GetTargetOpacity() const
获取目标透明度
Definition: TweenAnimation.h:577
相对跳跃动画
Definition: TweenAnimation.h:152
void SetJumpCount(int count)
设置跳跃次数
Definition: TweenAnimation.h:522
void SetDisplacement(const Vec2 &displacement)
设置跳跃位移
Definition: TweenAnimation.h:512
int GetJumpCount() const
获取跳跃次数
Definition: TweenAnimation.h:507
float GetJumpHeight() const
获取跳跃高度
Definition: TweenAnimation.h:502
Vec2 GetDisplacement() const
获取跳跃位移
Definition: TweenAnimation.h:497
void SetJumpHeight(float height)
设置跳跃高度
Definition: TweenAnimation.h:517
跳跃动画
Definition: TweenAnimation.h:210
void SetDistination(const Point &distination)
设置目的坐标
Definition: TweenAnimation.h:532
Point GetDistination() const
获取目的坐标
Definition: TweenAnimation.h:527
JumpToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:234
相对位移动画
Definition: TweenAnimation.h:76
Vec2 GetDisplacement() const
获取位移向量
Definition: TweenAnimation.h:477
void SetDisplacement(const Vec2 &displacement)
设置位移向量
Definition: TweenAnimation.h:482
位移动画
Definition: TweenAnimation.h:114
MoveToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:136
void SetDistination(const Point &distination)
设置目的坐标
Definition: TweenAnimation.h:492
Point GetDistination() const
获取目的坐标
Definition: TweenAnimation.h:487
相对旋转动画
Definition: TweenAnimation.h:383
void SetRotation(float rotation)
设置角度相对变化值
Definition: TweenAnimation.h:592
float GetRotation() const
获取角度相对变化值
Definition: TweenAnimation.h:587
旋转动画
Definition: TweenAnimation.h:420
RotateToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:442
void SetTargetRotation(float rotation)
设置目标角度
Definition: TweenAnimation.h:602
float GetTargetRotation() const
获取目标角度
Definition: TweenAnimation.h:597
相对缩放动画
Definition: TweenAnimation.h:250
void SetScaleX(float scale_x)
设置纵向缩放相对变化值
Definition: TweenAnimation.h:547
float GetScaleX() const
获取横向缩放相对变化值
Definition: TweenAnimation.h:537
float GetScaleY() const
获取横向缩放相对变化值
Definition: TweenAnimation.h:542
void SetScaleY(float scale_y)
设置纵向缩放相对变化值
Definition: TweenAnimation.h:552
缩放动画
Definition: TweenAnimation.h:295
void SetTargetScaleY(float scale_y)
设置纵向缩放目标值
Definition: TweenAnimation.h:572
float GetTargetScaleY() const
获取横向缩放目标值
Definition: TweenAnimation.h:562
float GetTargetScaleX() const
获取横向缩放目标值
Definition: TweenAnimation.h:557
void SetTargetScaleX(float scale_x)
设置纵向缩放目标值
Definition: TweenAnimation.h:567
ScaleToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:325
补间动画
Definition: TweenAnimation.h:37
Duration GetDuration() const
获取动画时长
Definition: TweenAnimation.h:462
void SetEaseFunc(const EaseFunc &func)
设置动画速度缓动函数
Definition: TweenAnimation.h:472
const EaseFunc & GetEaseFunc() const
获取动画速度缓动函数
Definition: TweenAnimation.h:457
void SetDuration(Duration duration)
设置动画时长
Definition: TweenAnimation.h:467
时间段
Definition: Duration.h:48