demo_view_buffer_animation.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. static struct rtgui_dc *dc_buffer;
  15. static void timeout(struct rtgui_timer* timer, void* parameter)
  16. {
  17. struct rtgui_dc* dc;
  18. rtgui_rect_t rect;
  19. rtgui_widget_t *widget;
  20. /* 控件(view)通过parameter参数传递给定时器 */
  21. widget = (rtgui_widget_t*)parameter;
  22. /* 获得控件所属的DC */
  23. dc = rtgui_dc_begin_drawing(widget);
  24. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  25. return ;
  26. /* 获得demo view允许绘图的区域,主要用于判断边界 */
  27. demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
  28. rect.y2 -= 5;
  29. /* 判断是否是第一次绘图 */
  30. if ((text_rect.x1 == 0) && (text_rect.y1 == 0))
  31. {
  32. rtgui_rect_moveto(&text_rect, rect.x1, rect.y1);
  33. }
  34. /* 设置dx和dy */
  35. if (text_rect.x2 >= rect.x2) dx = -1;
  36. if (text_rect.x1 < rect.x1) dx = 1;
  37. if (text_rect.y2 >= rect.y2) dy = -1;
  38. if (text_rect.y1 < rect.y1) dy = 1;
  39. /* 移动文本框的位置 */
  40. text_rect.x1 += dx; text_rect.x2 += dx;
  41. text_rect.y1 += dy; text_rect.y2 += dy;
  42. /* 绘图 */
  43. rect = text_rect;
  44. rect.x2 += 2; rect.y2 += 2;
  45. rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
  46. /* 绘图完成 */
  47. rtgui_dc_end_drawing(dc);
  48. }
  49. static rt_bool_t animation_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  50. {
  51. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  52. if (event->type == RTGUI_EVENT_PAINT)
  53. {
  54. struct rtgui_dc* dc;
  55. rtgui_rect_t rect;
  56. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  57. rtgui_container_event_handler(object, event);
  58. /* 获得控件所属的DC */
  59. dc = rtgui_dc_begin_drawing(widget);
  60. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  61. return RT_FALSE;
  62. /* 获得demo view允许绘图的区域 */
  63. demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
  64. /* 绘图 */
  65. rect = text_rect;
  66. rtgui_rect_inflate(&rect, +1);
  67. rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
  68. /* 绘图完成 */
  69. rtgui_dc_end_drawing(dc);
  70. }
  71. else
  72. {
  73. /* 调用默认的事件处理函数 */
  74. return rtgui_container_event_handler(object, event);
  75. }
  76. return RT_FALSE;
  77. }
  78. static rt_bool_t animation_on_show(struct rtgui_object *object, struct rtgui_event *event)
  79. {
  80. rt_kprintf("buffer animation on show\n");
  81. rtgui_timer_start(timer);
  82. return RT_TRUE;
  83. }
  84. static rt_bool_t animation_on_hide(struct rtgui_object *object, struct rtgui_event *event)
  85. {
  86. rt_kprintf("buffer animation on hide\n");
  87. rtgui_timer_stop(timer);
  88. return RT_TRUE;
  89. }
  90. struct rtgui_container *demo_view_buffer_animation(void)
  91. {
  92. struct rtgui_container *container;
  93. container= demo_view("DC 缓冲区动画");
  94. if (container!= RT_NULL)
  95. rtgui_object_set_event_handler(RTGUI_OBJECT(container), animation_event_handler);
  96. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(container)), "缓冲动画", &text_rect);
  97. if (dc_buffer == RT_NULL)
  98. {
  99. rtgui_rect_t rect;
  100. rect.x1 = 0; rect.x2 = rtgui_rect_width(text_rect) + 2;
  101. rect.y1 = 0; rect.y2 = rtgui_rect_height(text_rect) + 2;
  102. /* 创建 DC Buffer,长 50,宽 50 */
  103. dc_buffer = rtgui_dc_buffer_create(rtgui_rect_width(rect), rtgui_rect_height(rect));
  104. RTGUI_DC_FC(dc_buffer) = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(container));
  105. rtgui_dc_fill_rect(dc_buffer, &rect);
  106. RTGUI_DC_FC(dc_buffer) = black;
  107. rect.x1 = 1; rect.y1 = 1;
  108. rtgui_dc_draw_text(dc_buffer, "缓冲动画", &rect);
  109. }
  110. /* 启动定时器以触发动画 */
  111. timer = rtgui_timer_create(1, RT_TIMER_FLAG_PERIODIC, timeout, (void*)container);
  112. rtgui_widget_set_onshow(RTGUI_WIDGET(container), animation_on_show);
  113. rtgui_widget_set_onhide(RTGUI_WIDGET(container), animation_on_hide);
  114. return container;
  115. }