demo_view_animation.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/container.h>
  4. #include "demo_view.h"
  5. /*
  6. * 直接在DC上绘图以实现动画效果
  7. *
  8. * 动画是依赖于定时器驱动的,会上下翻滚显示文字
  9. * "飞线乱飞"
  10. */
  11. static rt_int8_t dx = 1, dy = 1;
  12. static rtgui_rect_t text_rect;
  13. static rtgui_timer_t *timer;
  14. void timeout(struct rtgui_timer *timer, void *parameter)
  15. {
  16. struct rtgui_dc *dc;
  17. struct rtgui_rect rect;
  18. struct rtgui_widget *widget;
  19. /* 控件(container)通过parameter参数传递给定时器 */
  20. widget = RTGUI_WIDGET(parameter);
  21. /* 获得控件所属的DC */
  22. dc = rtgui_dc_begin_drawing(widget);
  23. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  24. return ;
  25. /* 获得demo container允许绘图的区域,主要用于判断边界 */
  26. demo_view_get_rect(RTGUI_CONTAINER(widget), &rect);
  27. rect.y2 -= 5;
  28. /* 判断是否是第一次绘图 */
  29. if ((text_rect.x1 == 0) && (text_rect.y1 == 0))
  30. {
  31. rtgui_rect_moveto(&text_rect, rect.x1, rect.y1);
  32. }
  33. else
  34. {
  35. /* 擦除老的文字 */
  36. rtgui_dc_fill_rect(dc, &text_rect);
  37. }
  38. /* 设置dx和dy */
  39. if (text_rect.x2 >= rect.x2) dx = -1;
  40. if (text_rect.x1 < rect.x1) dx = 1;
  41. if (text_rect.y2 >= rect.y2) dy = -1;
  42. if (text_rect.y1 < rect.y1) dy = 1;
  43. /* 移动文本框的位置 */
  44. text_rect.x1 += dx;
  45. text_rect.x2 += dx;
  46. text_rect.y1 += dy;
  47. text_rect.y2 += dy;
  48. /* 绘图 */
  49. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  50. /* 绘图完成 */
  51. rtgui_dc_end_drawing(dc);
  52. }
  53. static rt_bool_t animation_on_show(struct rtgui_object *object, struct rtgui_event *event)
  54. {
  55. rt_kprintf("animation on show\n");
  56. if (timer != RT_NULL)
  57. rtgui_timer_start(timer);
  58. return RT_TRUE;
  59. }
  60. static rt_bool_t animation_on_hide(struct rtgui_object *object, struct rtgui_event *event)
  61. {
  62. rt_kprintf("animation on hide\n");
  63. if (timer != RT_NULL)
  64. rtgui_timer_stop(timer);
  65. return RT_TRUE;
  66. }
  67. rt_bool_t animation_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  68. {
  69. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  70. if (event->type == RTGUI_EVENT_PAINT)
  71. {
  72. struct rtgui_dc *dc;
  73. rtgui_rect_t rect;
  74. /* 因为用的是demo container,上面本身有一部分控件,所以在绘图时先要让demo container先绘图 */
  75. rtgui_container_event_handler(object, event);
  76. /* 获得控件所属的DC */
  77. dc = rtgui_dc_begin_drawing(widget);
  78. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  79. return RT_FALSE;
  80. /* 获得demo container允许绘图的区域 */
  81. demo_view_get_rect(RTGUI_CONTAINER(widget), &rect);
  82. /* 擦除所有 */
  83. rtgui_dc_fill_rect(dc, &rect);
  84. /* 绘图 */
  85. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  86. /* 绘图完成 */
  87. rtgui_dc_end_drawing(dc);
  88. }
  89. else if (event->type == RTGUI_EVENT_SHOW)
  90. {
  91. rtgui_container_event_handler(object, event);
  92. animation_on_show(object, event);
  93. }
  94. else if (event->type == RTGUI_EVENT_HIDE)
  95. {
  96. rtgui_container_event_handler(object, event);
  97. animation_on_hide(object, event);
  98. }
  99. else
  100. {
  101. /* 调用默认的事件处理函数 */
  102. return rtgui_container_event_handler(object, event);
  103. }
  104. return RT_FALSE;
  105. }
  106. rtgui_container_t *demo_view_animation()
  107. {
  108. rtgui_container_t *container;
  109. container = demo_view("DC 动画");
  110. if (container != RT_NULL)
  111. rtgui_object_set_event_handler(RTGUI_OBJECT(container), animation_event_handler);
  112. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(container), "飞线乱飞", &text_rect);
  113. rtgui_rect_moveto(&text_rect, 0, 45);
  114. timer = rtgui_timer_create(2, RT_TIMER_FLAG_PERIODIC, timeout, (void *)container);
  115. return container;
  116. }