pm_animation.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * File : pm_animation.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include <pm_widget.h>
  11. #include <pm_window.h>
  12. #include <pm_timer.h>
  13. #include <vector>
  14. namespace Persimmon
  15. {
  16. enum
  17. {
  18. ANIM_TICK_RANGE = 1024,
  19. };
  20. class AnimAbstractAnimator
  21. {
  22. public:
  23. AnimAbstractAnimator(struct rtgui_dc *b) : buffer(b), wgt(NULL) {}
  24. AnimAbstractAnimator(Widget &w) : buffer(NULL), wgt(&w) {}
  25. virtual ~AnimAbstractAnimator()
  26. {
  27. if (wgt && buffer)
  28. rtgui_dc_destory(buffer);
  29. }
  30. virtual void act(struct rtgui_dc* dest_dc,
  31. int progress) = 0;
  32. protected:
  33. struct rtgui_dc *buffer;
  34. Widget *wgt;
  35. };
  36. class AnimFadeAnimator: public AnimAbstractAnimator
  37. {
  38. typedef AnimAbstractAnimator super;
  39. public:
  40. AnimFadeAnimator(struct rtgui_dc *buffer,
  41. bool is_fade_out,
  42. const Point &start_pt);
  43. AnimFadeAnimator(Widget &wgt,
  44. bool is_fade_out,
  45. const Point &start_pt);
  46. virtual ~AnimFadeAnimator();
  47. virtual void act(struct rtgui_dc *dest_dc, int progress);
  48. protected:
  49. Point start_pt;
  50. bool is_fade_out;
  51. };
  52. class AnimMoveAnimator: public AnimAbstractAnimator
  53. {
  54. typedef AnimAbstractAnimator super;
  55. public:
  56. AnimMoveAnimator(struct rtgui_dc *buffer,
  57. const Point &start_pt,
  58. const Point &stop_pt);
  59. AnimMoveAnimator(Widget &wgt,
  60. const Point &start_pt,
  61. const Point &stop_pt);
  62. void enableFade(bool in, int min = 0, int max = 255)
  63. {
  64. is_fade_in = in;
  65. a_min = min;
  66. a_max = max;
  67. is_fade = true;
  68. }
  69. void disableFade(void)
  70. {
  71. is_fade = false;
  72. }
  73. virtual ~AnimMoveAnimator();
  74. virtual void act(struct rtgui_dc *dest_dc, int progress);
  75. protected:
  76. Point start_pt, stop_pt;
  77. bool is_fade, is_fade_in;
  78. int a_min, a_max;
  79. };
  80. class AnimAbstractInterpolator
  81. {
  82. public:
  83. virtual ~AnimAbstractInterpolator() {}
  84. virtual int interpolate(int cur, int max) = 0;
  85. };
  86. class AnimLinearInterpolator: public AnimAbstractInterpolator
  87. {
  88. public:
  89. virtual ~AnimLinearInterpolator() {}
  90. virtual int interpolate(int cur, int max)
  91. {
  92. return cur * ANIM_TICK_RANGE / max;
  93. }
  94. };
  95. class AnimInsquareInterpolator : public AnimAbstractInterpolator
  96. {
  97. public:
  98. virtual ~AnimInsquareInterpolator() {}
  99. virtual int interpolate(int cur, int max)
  100. {
  101. /* Care about integer overflow. tick can within 0~(4G/RTGUI_ANIM_TICK_RANGE). */
  102. return cur * ANIM_TICK_RANGE / max * cur / max;
  103. }
  104. };
  105. class AnimOutsquareInterpolator : public AnimAbstractInterpolator
  106. {
  107. public:
  108. virtual ~AnimOutsquareInterpolator() {}
  109. virtual int interpolate(int cur, int max)
  110. {
  111. /* Care about integer overflow. tick can within 0~(4G/RTGUI_ANIM_TICK_RANGE). */
  112. cur = max - cur;
  113. return ANIM_TICK_RANGE - (cur * ANIM_TICK_RANGE / max * cur / max);
  114. }
  115. };
  116. class Animation: private utils::noncopyable<Animation>
  117. {
  118. public:
  119. Animation(Widget &w);
  120. ~Animation();
  121. Animation *setBGbuffer(struct rtgui_dc *dc)
  122. {
  123. m_bg = dc;
  124. return this;
  125. }
  126. Animation *setInterval(int inter)
  127. {
  128. m_interval = inter;
  129. return this;
  130. }
  131. Animation *setDuration(int dur)
  132. {
  133. m_duration = dur;
  134. return this;
  135. }
  136. Animation *setInterpolator(AnimAbstractInterpolator &inter)
  137. {
  138. m_interplator = &inter;
  139. return this;
  140. }
  141. Animation *addAnimator(AnimAbstractAnimator *anim)
  142. {
  143. m_animators.push_back(anim);
  144. return this;
  145. }
  146. Signal<struct rtgui_dc *, int> actSignal;
  147. Signal<void> endSignal;
  148. void start(bool is_modal=true);
  149. void stop();
  150. void exit();
  151. private:
  152. Widget *m_owner;
  153. Timer *m_timer;
  154. struct rtgui_dc *m_bg, *saveDc;
  155. unsigned int m_interval, m_duration, m_tick_start;
  156. std::vector<AnimAbstractAnimator*> m_animators;
  157. AnimAbstractInterpolator *m_interplator;
  158. enum stat
  159. {
  160. ANIM_ST_NONE = 0,
  161. ANIM_ST_MODAL = (1 << 0),
  162. ANIM_ST_STOP = (0 << 1),
  163. ANIM_ST_RUN = (1 << 1),
  164. } m_stat;
  165. void _drawframe();
  166. };
  167. }