Kiwano Engine  v1.2.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 
26 namespace kiwano
27 {
28 
29 KGE_DECLARE_SMART_PTR(TweenAnimation);
30 KGE_DECLARE_SMART_PTR(MoveByAnimation);
31 KGE_DECLARE_SMART_PTR(MoveToAnimation);
32 KGE_DECLARE_SMART_PTR(JumpByAnimation);
33 KGE_DECLARE_SMART_PTR(JumpToAnimation);
34 KGE_DECLARE_SMART_PTR(ScaleByAnimation);
35 KGE_DECLARE_SMART_PTR(ScaleToAnimation);
36 KGE_DECLARE_SMART_PTR(FadeToAnimation);
37 KGE_DECLARE_SMART_PTR(RotateByAnimation);
38 KGE_DECLARE_SMART_PTR(RotateToAnimation);
39 
45 class KGE_API TweenAnimation : public Animation
48 {
49 public:
52  Duration GetDuration() const;
53 
56  void SetDuration(Duration duration);
57 
60  const EaseFunc& GetEaseFunc() const;
61 
64  void SetEaseFunc(const EaseFunc& func);
65 
66 protected:
67  TweenAnimation();
68 
69  TweenAnimation(Duration duration);
70 
71  float Interpolate(float frac);
72 
73  void Update(Actor* target, Duration dt) override;
74 
75  virtual void UpdateTween(Actor* target, float frac) = 0;
76 
77  void DoClone(TweenAnimation* to) const;
78 
79 private:
80  Duration dur_;
81  EaseFunc ease_func_;
82 };
83 
84 
87 class KGE_API MoveByAnimation : public TweenAnimation
88 {
89 public:
94  MoveByAnimation(Duration duration, const Vec2& displacement);
95 
98  Vec2 GetDisplacement() const;
99 
102  void SetDisplacement(const Vec2& displacement);
103 
106  MoveByAnimation* Clone() const override;
107 
110  MoveByAnimation* Reverse() const override;
111 
112 protected:
113  void Init(Actor* target) override;
114 
115  void UpdateTween(Actor* target, float frac) override;
116 
117 protected:
118  Point start_pos_;
119  Point prev_pos_;
120  Vec2 displacement_;
121 };
122 
123 
126 class KGE_API MoveToAnimation : public MoveByAnimation
127 {
128 public:
133  MoveToAnimation(Duration duration, const Point& distination);
134 
137  Point GetDistination() const;
138 
141  void SetDistination(const Point& distination);
142 
145  MoveToAnimation* Clone() const override;
146 
149  MoveToAnimation* Reverse() const override
150  {
151  KGE_ERRORF("Reverse() not supported in MoveToAnimation");
152  return nullptr;
153  }
154 
155 protected:
156  void Init(Actor* target) override;
157 
158 private:
159  Point distination_;
160 };
161 
162 
165 class KGE_API JumpByAnimation : public TweenAnimation
166 {
167 public:
174  JumpByAnimation(Duration duration, const Vec2& displacement, float height, int count = 1);
175 
178  Vec2 GetDisplacement() const;
179 
182  float GetJumpHeight() const;
183 
186  int GetJumpCount() const;
187 
190  void SetDisplacement(const Vec2& displacement);
191 
194  void SetJumpHeight(float height);
195 
198  void SetJumpCount(int count);
199 
202  JumpByAnimation* Clone() const override;
203 
206  JumpByAnimation* Reverse() const override;
207 
208 protected:
209  void Init(Actor* target) override;
210 
211  void UpdateTween(Actor* target, float frac) override;
212 
213 protected:
214  float height_;
215  int jump_count_;
216  Point start_pos_;
217  Point displacement_;
218  Point prev_pos_;
219 };
220 
221 
224 class KGE_API JumpToAnimation : public JumpByAnimation
225 {
226 public:
233  JumpToAnimation(Duration duration, const Point& distination, float height, int count = 1);
234 
237  Point GetDistination() const;
238 
241  void SetDistination(const Point& distination);
242 
245  JumpToAnimation* Clone() const override;
246 
249  JumpToAnimation* Reverse() const override
250  {
251  KGE_ERRORF("Reverse() not supported in JumpToAnimation");
252  return nullptr;
253  }
254 
255 protected:
256  void Init(Actor* target) override;
257 
258 private:
259  Point distination_;
260 };
261 
262 
265 class KGE_API ScaleByAnimation : public TweenAnimation
266 {
267 public:
272  ScaleByAnimation(Duration duration, const Vec2& scale);
273 
276  float GetScaleX() const;
277 
280  float GetScaleY() const;
281 
284  void SetScaleX(float scale_x);
285 
288  void SetScaleY(float scale_y);
289 
292  ScaleByAnimation* Clone() const override;
293 
296  ScaleByAnimation* Reverse() const override;
297 
298 protected:
299  void Init(Actor* target) override;
300 
301  void UpdateTween(Actor* target, float frac) override;
302 
303 protected:
304  Vec2 start_val_;
305  Vec2 delta_;
306 };
307 
308 
311 class KGE_API ScaleToAnimation : public ScaleByAnimation
312 {
313 public:
318  ScaleToAnimation(Duration duration, const Vec2& scale);
319 
322  float GetTargetScaleX() const;
323 
326  float GetTargetScaleY() const;
327 
330  void SetTargetScaleX(float scale_x);
331 
334  void SetTargetScaleY(float scale_y);
335 
338  ScaleToAnimation* Clone() const override;
339 
342  ScaleToAnimation* Reverse() const override
343  {
344  KGE_ERRORF("Reverse() not supported in ScaleToAnimation");
345  return nullptr;
346  }
347 
348 protected:
349  void Init(Actor* target) override;
350 
351 private:
352  Vec2 end_val_;
353 };
354 
355 
358 class KGE_API FadeToAnimation : public TweenAnimation
359 {
360 public:
365  FadeToAnimation(Duration duration, float opacity);
366 
369  float GetTargetOpacity() const;
370 
373  void SetTargetOpacity(float opacity);
374 
377  FadeToAnimation* Clone() const override;
378 
381  FadeToAnimation* Reverse() const override
382  {
383  KGE_ERRORF("Reverse() not supported in FadeToAnimation");
384  return nullptr;
385  }
386 
387 protected:
388  void Init(Actor* target) override;
389 
390  void UpdateTween(Actor* target, float frac) override;
391 
392 private:
393  float start_val_;
394  float delta_val_;
395  float end_val_;
396 };
397 
398 
401 class KGE_API RotateByAnimation : public TweenAnimation
402 {
403 public:
408  RotateByAnimation(Duration duration, float rotation);
409 
412  float GetRotation() const;
413 
416  void SetRotation(float rotation);
417 
420  RotateByAnimation* Clone() const override;
421 
424  RotateByAnimation* Reverse() const override;
425 
426 protected:
427  void Init(Actor* target) override;
428 
429  void UpdateTween(Actor* target, float frac) override;
430 
431 protected:
432  float start_val_;
433  float delta_val_;
434 };
435 
436 
439 class KGE_API RotateToAnimation : public RotateByAnimation
440 {
441 public:
446  RotateToAnimation(Duration duration, float rotation);
447 
450  float GetTargetRotation() const;
451 
454  void SetTargetRotation(float rotation);
455 
458  RotateToAnimation* Clone() const override;
459 
462  RotateToAnimation* Reverse() const override
463  {
464  KGE_ERRORF("Reverse() not supported in RotateToAnimation");
465  return nullptr;
466  }
467 
468 protected:
469  void Init(Actor* target) override;
470 
471 private:
472  float end_val_;
473 };
474 
478 {
479  return ease_func_;
480 }
481 
483 {
484  return dur_;
485 }
486 
488 {
489  dur_ = duration;
490 }
491 
492 inline void TweenAnimation::SetEaseFunc(const EaseFunc& func)
493 {
494  ease_func_ = func;
495 }
496 
498 {
499  return displacement_;
500 }
501 
502 inline void MoveByAnimation::SetDisplacement(const Vec2& displacement)
503 {
504  displacement_ = displacement;
505 }
506 
508 {
509  return distination_;
510 }
511 
512 inline void MoveToAnimation::SetDistination(const Point& distination)
513 {
514  distination_ = distination;
515 }
516 
518 {
519  return displacement_;
520 }
521 
522 inline float JumpByAnimation::GetJumpHeight() const
523 {
524  return height_;
525 }
526 
528 {
529  return jump_count_;
530 }
531 
532 inline void JumpByAnimation::SetDisplacement(const Vec2& displacement)
533 {
534  displacement_ = displacement;
535 }
536 
537 inline void JumpByAnimation::SetJumpHeight(float height)
538 {
539  height_ = height;
540 }
541 
542 inline void JumpByAnimation::SetJumpCount(int count)
543 {
544  jump_count_ = count;
545 }
546 
548 {
549  return distination_;
550 }
551 
552 inline void JumpToAnimation::SetDistination(const Point& distination)
553 {
554  distination_ = distination;
555 }
556 
557 inline float ScaleByAnimation::GetScaleX() const
558 {
559  return delta_.x;
560 }
561 
562 inline float ScaleByAnimation::GetScaleY() const
563 {
564  return delta_.y;
565 }
566 
567 inline void ScaleByAnimation::SetScaleX(float scale_x)
568 {
569  delta_.x = scale_x;
570 }
571 
572 inline void ScaleByAnimation::SetScaleY(float scale_y)
573 {
574  delta_.y = scale_y;
575 }
576 
578 {
579  return end_val_.x;
580 }
581 
583 {
584  return end_val_.y;
585 }
586 
587 inline void ScaleToAnimation::SetTargetScaleX(float scale_x)
588 {
589  end_val_.x = scale_x;
590 }
591 
592 inline void ScaleToAnimation::SetTargetScaleY(float scale_y)
593 {
594  end_val_.y = scale_y;
595 }
596 
598 {
599  return end_val_;
600 }
601 
602 inline void FadeToAnimation::SetTargetOpacity(float opacity)
603 {
604  end_val_ = opacity;
605 }
606 
607 inline float RotateByAnimation::GetRotation() const
608 {
609  return delta_val_;
610 }
611 
612 inline void RotateByAnimation::SetRotation(float rotation)
613 {
614  delta_val_ = rotation;
615 }
616 
618 {
619  return end_val_;
620 }
621 
622 inline void RotateToAnimation::SetTargetRotation(float rotation)
623 {
624  end_val_ = rotation;
625 }
626 
627 } // namespace kiwano
void SetTargetRotation(float rotation)
设置目标角度
Definition: TweenAnimation.h:622
float GetTargetScaleX() const
获取横向缩放目标值
Definition: TweenAnimation.h:577
相对缩放动画
Definition: TweenAnimation.h:265
void SetJumpHeight(float height)
设置跳跃高度
Definition: TweenAnimation.h:537
const EaseFunc & GetEaseFunc() const
获取动画速度缓动函数
Definition: TweenAnimation.h:477
透明度渐变动画
Definition: TweenAnimation.h:358
相对跳跃动画
Definition: TweenAnimation.h:165
void SetDisplacement(const Vec2 &displacement)
设置跳跃位移
Definition: TweenAnimation.h:532
JumpToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:249
float GetJumpHeight() const
获取跳跃高度
Definition: TweenAnimation.h:522
void SetScaleY(float scale_y)
设置纵向缩放相对变化值
Definition: TweenAnimation.h:572
void SetTargetScaleY(float scale_y)
设置纵向缩放目标值
Definition: TweenAnimation.h:592
Point GetDistination() const
获取目的坐标
Definition: TweenAnimation.h:507
旋转动画
Definition: TweenAnimation.h:439
缩放动画
Definition: TweenAnimation.h:311
Definition: Vec2.hpp:29
MoveToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:149
Vec2 GetDisplacement() const
获取跳跃位移
Definition: TweenAnimation.h:517
相对旋转动画
Definition: TweenAnimation.h:401
跳跃动画
Definition: TweenAnimation.h:224
void SetDistination(const Point &distination)
设置目的坐标
Definition: TweenAnimation.h:552
float GetTargetScaleY() const
获取横向缩放目标值
Definition: TweenAnimation.h:582
角色
Definition: Actor.h:63
float GetScaleY() const
获取横向缩放相对变化值
Definition: TweenAnimation.h:562
Vec2 GetDisplacement() const
获取位移向量
Definition: TweenAnimation.h:497
void SetRotation(float rotation)
设置角度相对变化值
Definition: TweenAnimation.h:612
ScaleToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:342
int GetJumpCount() const
获取跳跃次数
Definition: TweenAnimation.h:527
float GetTargetRotation() const
获取目标角度
Definition: TweenAnimation.h:617
void SetDistination(const Point &distination)
设置目的坐标
Definition: TweenAnimation.h:512
Duration GetDuration() const
获取动画时长
Definition: TweenAnimation.h:482
Definition: Actor.cpp:26
时间段
Definition: Duration.h:47
相对位移动画
Definition: TweenAnimation.h:87
补间动画
Definition: TweenAnimation.h:47
void SetDisplacement(const Vec2 &displacement)
设置位移向量
Definition: TweenAnimation.h:502
void SetScaleX(float scale_x)
设置纵向缩放相对变化值
Definition: TweenAnimation.h:567
FadeToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:381
float GetRotation() const
获取角度相对变化值
Definition: TweenAnimation.h:607
void SetJumpCount(int count)
设置跳跃次数
Definition: TweenAnimation.h:542
void SetTargetOpacity(float opacity)
设置目标透明度
Definition: TweenAnimation.h:602
位移动画
Definition: TweenAnimation.h:126
Point GetDistination() const
获取目的坐标
Definition: TweenAnimation.h:547
void SetDuration(Duration duration)
设置动画时长
Definition: TweenAnimation.h:487
RotateToAnimation * Reverse() const override
获取该动画的倒转
Definition: TweenAnimation.h:462
void SetEaseFunc(const EaseFunc &func)
设置动画速度缓动函数
Definition: TweenAnimation.h:492
void SetTargetScaleX(float scale_x)
设置纵向缩放目标值
Definition: TweenAnimation.h:587
float GetScaleX() const
获取横向缩放相对变化值
Definition: TweenAnimation.h:557
float GetTargetOpacity() const
获取目标透明度
Definition: TweenAnimation.h:597