animation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * File : animation.cpp
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009-2016 RT-Thread Develop Team
  5. */
  6. #include "animation.h"
  7. #include "image_box.h"
  8. #include "logo_image.h"
  9. #include <math.h>
  10. using namespace Persimmon;
  11. AnimationWin::AnimationWin()
  12. : Window("button")
  13. {
  14. _logo = rtgui_image_create_from_mem("hdc", persimmon_demo_image_hdc, sizeof(persimmon_demo_image_hdc), true);
  15. RT_ASSERT(_logo != RT_NULL);
  16. Rect wRect(0, 0, _logo->w, _logo->h);
  17. _logo_dc = rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, wRect.getWidth(), wRect.getHeight());
  18. RT_ASSERT(_logo_dc != RT_NULL);
  19. rtgui_image_blit(_logo, _logo_dc, wRect.getRect());
  20. img1 = new ImageBox(NULL);
  21. img1->setBackground(_logo_dc);
  22. img1->setBackground(TRANSPARENT);
  23. img1->setRect(Rect(0, 0, _logo->w, _logo->h));
  24. setBackground(LIGHT_GREY);
  25. setInterestGesture(RTGUI_GESTURE_LONGPRESS | RTGUI_GESTURE_TAP);
  26. Rect userRect = getRect();
  27. width = userRect.getWidth();
  28. height = userRect.getHeight();
  29. touch_point_x = 0;
  30. touch_point_y = 0;
  31. point_src_x = 0;
  32. point_src_y = 0;
  33. cnt = 0;
  34. is_press = false;
  35. timer = new Timer(RT_TICK_PER_SECOND, Timer::ONE_SHOT);
  36. timer->timeout.connect(this, &AnimationWin::onButton);
  37. timer->start();
  38. }
  39. AnimationWin::~AnimationWin()
  40. {
  41. }
  42. void AnimationWin::onButton(void)
  43. {
  44. uint16_t two_point_length = 0;
  45. uint16_t xx = 0;
  46. uint16_t yy = 0;
  47. int16_t touch_point_x_old = 0;
  48. int16_t touch_point_y_old = 0;
  49. for (;;)
  50. {
  51. Animation *anim = new Animation(*this);
  52. srand((unsigned int)rt_tick_get());
  53. int16_t position_point_x = rand() % (width - logo_image_width);
  54. srand((unsigned int)rt_tick_get());
  55. int16_t position_point_y = rand() % (height - logo_image_height);
  56. if (cnt == 0)
  57. {
  58. point_des_x = width - logo_image_width;
  59. point_des_y = position_point_y;
  60. }
  61. else if (cnt == 1)
  62. {
  63. point_des_x = position_point_x;
  64. point_des_y = height - logo_image_height;
  65. }
  66. else if (cnt == 2)
  67. {
  68. point_des_x = 0;
  69. point_des_y = position_point_y;
  70. }
  71. else if (cnt == 3)
  72. {
  73. point_des_x = position_point_x;
  74. point_des_y = 0;
  75. }
  76. if (is_press == true)
  77. {
  78. point_des_x = touch_point_x - logo_image_width/2;
  79. point_des_y = touch_point_y - logo_image_height/2;
  80. }
  81. if ((point_des_x - point_src_x) < 0)
  82. {
  83. xx = (point_src_x - point_des_x)*(point_src_x - point_des_x);
  84. }
  85. else
  86. {
  87. xx = (point_des_x - point_src_x)*(point_des_x - point_src_x);
  88. }
  89. if ((point_des_y - point_src_y) < 0)
  90. {
  91. yy = (point_src_y - point_des_y)*(point_src_y - point_des_y);
  92. }
  93. else
  94. {
  95. yy = (point_des_y - point_src_y)*(point_des_y - point_src_y);
  96. }
  97. two_point_length = (uint16_t)sqrtf((float)(xx+yy));
  98. AnimMoveAnimator *animator1 = new AnimMoveAnimator(_logo_dc, Point(point_src_x, point_src_y), Point(point_des_x, point_des_y));
  99. struct rtgui_dc *bgDC = getBufferDrawing();
  100. anim->setBGbuffer(bgDC)
  101. ->setInterval(RT_TICK_PER_SECOND/20) //RT_TICK_PER_SECOND/25
  102. ->setDuration((two_point_length / 20 + 1) * (RT_TICK_PER_SECOND/20)) // two_point_length/20
  103. ->addAnimator(animator1);
  104. anim->start();
  105. delete anim;
  106. rtgui_dc_destory(bgDC);
  107. point_src_x = point_des_x;
  108. point_src_y = point_des_y;
  109. cnt++;
  110. if (cnt > 3) cnt = 0;
  111. }
  112. }
  113. bool AnimationWin::handleGestureEvent(struct rtgui_event_gesture *gev,
  114. const struct rtgui_gesture *gest)
  115. {
  116. if(gev->type == 0x8008) // longpress end
  117. {
  118. is_press = false;
  119. }
  120. else if(gev->type == 0x8001) // tap click end
  121. {
  122. is_press = false;
  123. }
  124. else if(gev->type == 0x0001)
  125. {
  126. is_press = true;
  127. }
  128. else if(gev->type == 0x0008)
  129. {
  130. is_press = true;
  131. }
  132. else
  133. {
  134. is_press = false;
  135. }
  136. if (gest->current.x == touch_point_x && gest->current.y == touch_point_y)
  137. return true;
  138. touch_point_x = gest->current.x;
  139. touch_point_y = gest->current.y;
  140. return true;
  141. }