1
0

demo_gui_animation.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/widget.h>
  4. #include <rtgui/widgets/view.h>
  5. #include "demo_view.h"
  6. /*
  7. * 直接在DC上绘图以实现动画效果
  8. *
  9. * 动画是依赖于定时器驱动的,会上下翻滚显示文字
  10. * "飞线乱飞"
  11. */
  12. static rt_int8_t dx = 1, dy = 1;
  13. static rtgui_rect_t text_rect;
  14. static rtgui_timer_t *timer;
  15. 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. rtgui_widget_get_rect(widget, &rect);
  28. rtgui_rect_inflate(&rect, -5);
  29. rect.y1 += 35;
  30. /* 判断是否是第一次绘图 */
  31. if ((text_rect.x1 == 0) && (text_rect.y1 == 0))
  32. {
  33. rtgui_rect_moveto(&text_rect, rect.x1, rect.y1);
  34. }
  35. else
  36. {
  37. /* 擦除老的文字 */
  38. rtgui_dc_fill_rect(dc, &text_rect);
  39. }
  40. /* 设置dx和dy */
  41. if (text_rect.x2 >= rect.x2) dx = -1;
  42. if (text_rect.x1 < rect.x1) dx = 1;
  43. if (text_rect.y2 >= rect.y2) dy = -1;
  44. if (text_rect.y1 < rect.y1) dy = 1;
  45. /* 移动文本框的位置 */
  46. text_rect.x1 += dx; text_rect.x2 += dx;
  47. text_rect.y1 += dy; text_rect.y2 += dy;
  48. /* 绘图 */
  49. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  50. /* 绘图完成 */
  51. rtgui_dc_end_drawing(dc);
  52. }
  53. rt_bool_t animation_event_handler(PVOID wdt, rtgui_event_t *event)
  54. {
  55. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  56. if (event->type == RTGUI_EVENT_PAINT)
  57. {
  58. rtgui_dc_t* dc;
  59. rtgui_rect_t rect;
  60. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  61. rtgui_view_event_handler(widget, event);
  62. /* 获得控件所属的DC */
  63. dc = rtgui_dc_begin_drawing(widget);
  64. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  65. return RT_FALSE;
  66. /* 获得demo view允许绘图的区域 */
  67. rtgui_widget_get_rect(widget, &rect);
  68. rtgui_rect_inflate(&rect, -5);
  69. rect.y1 += 35;
  70. /* 擦除所有 */
  71. rtgui_dc_fill_rect(dc, &rect);
  72. /* 绘图 */
  73. rtgui_dc_draw_text(dc, "飞线乱飞", &text_rect);
  74. /* 绘图完成 */
  75. rtgui_dc_end_drawing(dc);
  76. }
  77. else
  78. {
  79. /* 调用默认的事件处理函数 */
  80. return rtgui_view_event_handler(widget, event);
  81. }
  82. return RT_FALSE;
  83. }
  84. rtgui_view_t *demo_gui_animation(rtgui_view_t* parent_view)
  85. {
  86. rtgui_view_t *view;
  87. view = demo_view_create(parent_view, "DC 动画");
  88. if (view != RT_NULL)
  89. rtgui_widget_set_event_handler(view, animation_event_handler);
  90. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(view), "飞线乱飞", &text_rect);
  91. rtgui_rect_moveto(&text_rect, 5, 40);
  92. /* 启动定时器以触发动画 */
  93. timer = rtgui_timer_create(2, RT_TIMER_FLAG_PERIODIC, timeout, view);
  94. rtgui_timer_start(timer);
  95. return view;
  96. }