demo_view_benchmark.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <rtgui/dc.h>
  2. #include <rtgui/dc_hw.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/view.h>
  5. #include "demo_view.h"
  6. #if RT_VERSION == 4
  7. #define RAND(x1, x2) ((rand() % (x2 - x1)) + x1)
  8. static rtgui_view_t* view = RT_NULL;
  9. static int running = 0;
  10. void _onidle(rtgui_widget_t* widget, rtgui_event_t *event)
  11. {
  12. rtgui_color_t color;
  13. rtgui_rect_t rect, draw_rect;
  14. struct rtgui_dc *dc;
  15. /* 获得控件所属的DC */
  16. // dc = rtgui_dc_hw_create(RTGUI_WIDGET(view));
  17. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view));
  18. if (dc == RT_NULL) return ;
  19. demo_view_get_logic_rect(RTGUI_VIEW(view), &rect);
  20. draw_rect.x1 = RAND(rect.x1, rect.x2);
  21. draw_rect.y1 = RAND(rect.y1, rect.y2);
  22. draw_rect.x2 = RAND(draw_rect.x1, rect.x2);
  23. draw_rect.y2 = RAND(draw_rect.y1, rect.y2);
  24. color = RTGUI_RGB(rand() % 255, rand() % 255, rand() % 255);
  25. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = color;
  26. rtgui_dc_fill_rect(dc, &draw_rect);
  27. /* 绘图完成 */
  28. rtgui_dc_end_drawing(dc);
  29. }
  30. void _draw_default(rtgui_widget_t* widget, rtgui_event_t* event)
  31. {
  32. struct rtgui_dc* dc;
  33. rtgui_rect_t rect;
  34. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  35. rtgui_view_event_handler(widget, event);
  36. /* 获得控件所属的DC */
  37. dc = rtgui_dc_begin_drawing(widget);
  38. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  39. return ;
  40. /* 获得demo view允许绘图的区域 */
  41. demo_view_get_logic_rect(RTGUI_VIEW(widget), &rect);
  42. /* 擦除所有 */
  43. RTGUI_WIDGET_BACKGROUND(widget) = default_background;
  44. rtgui_dc_fill_rect(dc, &rect);
  45. /* 显示提示 */
  46. rtgui_dc_draw_text(dc, "按任意键开始/停止测试...", &rect);
  47. /* 绘图完成 */
  48. rtgui_dc_end_drawing(dc);
  49. }
  50. rt_bool_t benchmark_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  51. {
  52. if (event->type == RTGUI_EVENT_PAINT)
  53. {
  54. _draw_default(widget, event);
  55. }
  56. else if (event->type == RTGUI_EVENT_KBD)
  57. {
  58. struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd*)event;
  59. if (RTGUI_KBD_IS_UP(kbd))
  60. {
  61. if (running)
  62. {
  63. /* stop */
  64. rtgui_thread_set_onidle(RT_NULL);
  65. _draw_default(widget, event);
  66. }
  67. else
  68. {
  69. /* run */
  70. rtgui_thread_set_onidle(_onidle);
  71. }
  72. running = !running;
  73. }
  74. return RT_TRUE;
  75. }
  76. else
  77. {
  78. /* 调用默认的事件处理函数 */
  79. return rtgui_view_event_handler(widget, event);
  80. }
  81. return RT_FALSE;
  82. }
  83. rtgui_view_t *demo_view_benchmark(rtgui_workbench_t* workbench)
  84. {
  85. srand(100);
  86. view = demo_view(workbench, "绘图测试");
  87. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), benchmark_event_handler);
  88. return view;
  89. }
  90. #endif