tetris_ui.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * File : tetris_ui.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-08-14 Yi.Qiu first version
  13. */
  14. #include <rtgui/rtgui.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/widgets/view.h>
  17. #include <rtgui/widgets/list_view.h>
  18. #include <rtgui/widgets/label.h>
  19. #include <rtgui/widgets/button.h>
  20. #include <rtgui/widgets/window.h>
  21. #include <rtgui/widgets/workbench.h>
  22. #include <rtgui/dc.h>
  23. #include "tetris.h"
  24. struct app_info
  25. {
  26. rtgui_workbench_t* workbench;
  27. rtgui_view_t* game_view;
  28. rtgui_list_view_t* function_view;
  29. rt_tetris_t * tetris;
  30. rt_tetris_view_t* tetris_view;
  31. rtgui_timer_t* _timer;
  32. };
  33. typedef struct app_info app_info;
  34. static app_info g_app_info;
  35. static void _game_over(void)
  36. {
  37. rtgui_timer_destory(g_app_info._timer);
  38. rt_tetris_destory(g_app_info.tetris);
  39. rt_tetris_view_destroy(g_app_info.tetris_view);
  40. rt_kprintf("GAME OVER\n");
  41. rtgui_view_show(g_app_info.function_view, RT_FALSE);
  42. }
  43. static rt_bool_t game_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  44. {
  45. if (event->type == RTGUI_EVENT_PAINT)
  46. {
  47. struct rtgui_dc* dc;
  48. rtgui_rect_t rect;
  49. /* draw child */
  50. rtgui_view_event_handler(widget, event);
  51. dc = rtgui_dc_begin_drawing(widget);
  52. if (dc == RT_NULL) return -RT_ERROR;
  53. rect.x1 = 96;
  54. rect.y1 = 0;
  55. rect.x2 = 128;
  56. rect.y2 = 16;
  57. rtgui_dc_draw_text(dc, "next", &rect);
  58. rect.y1 += 30;
  59. rect.y2 = rect.y1 + 16;
  60. rtgui_dc_draw_text(dc, "level", &rect);
  61. rect.y1 += 22;
  62. rect.y2 = rect.y1 + 16;
  63. rtgui_dc_draw_text(dc, "lines", &rect);
  64. rect.y1 += 22;
  65. rect.y2 = rect.y1 + 16;
  66. rtgui_dc_draw_text(dc, "score", &rect);
  67. rtgui_dc_end_drawing(dc);
  68. /* start tetris game, removed later */
  69. rt_tetris_start(g_app_info.tetris);
  70. return RT_FALSE;
  71. }
  72. else if ((event->type == RTGUI_EVENT_KBD))
  73. {
  74. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  75. if (ekbd->type == RTGUI_KEYDOWN)
  76. {
  77. if (ekbd->key == RTGUIK_RIGHT)
  78. {
  79. rt_tetris_right(g_app_info.tetris);
  80. }
  81. else if (ekbd->key == RTGUIK_LEFT)
  82. {
  83. rt_tetris_left(g_app_info.tetris);
  84. }
  85. else if (ekbd->key == RTGUIK_UP)
  86. {
  87. rt_tetris_rotate(g_app_info.tetris, RT_EOK);
  88. }
  89. else if (ekbd->key == RTGUIK_DOWN)
  90. {
  91. if( rt_tetris_drop(g_app_info.tetris) == -RT_ETIMEOUT
  92. && rt_tetris_status(g_app_info.tetris) != RT_FALSE)
  93. {
  94. _game_over();
  95. }
  96. }
  97. }
  98. }
  99. return rtgui_view_event_handler(widget, event);
  100. }
  101. static void _timer_timeout(rtgui_timer_t* timer, void* parameter)
  102. {
  103. if( rt_tetris_down(g_app_info.tetris) == -RT_ETIMEOUT)
  104. {
  105. _game_over();
  106. }
  107. }
  108. static rt_bool_t workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
  109. {
  110. if (event->type == RTGUI_EVENT_KBD)
  111. {
  112. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  113. if (((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
  114. && !RTGUI_WORKBENCH_IS_MODAL_MODE(g_app_info.workbench))
  115. {
  116. /* active home view */
  117. if (g_app_info.workbench->current_view != g_app_info.game_view)
  118. {
  119. rtgui_view_show(g_app_info.game_view, RT_FALSE);
  120. return RT_TRUE;
  121. }
  122. }
  123. }
  124. return rtgui_workbench_event_handler(widget, event);
  125. }
  126. static void listitem_action_return(void)
  127. {
  128. rtgui_view_destroy(g_app_info.game_view);
  129. rtgui_list_view_destroy(g_app_info.function_view);
  130. rtgui_workbench_close(g_app_info.workbench);
  131. }
  132. static void listitem_action_start(void)
  133. {
  134. /* create tetris modal instance */
  135. g_app_info.tetris = rt_tetris_create(16, 17);
  136. /* create tetris view instance */
  137. g_app_info.tetris_view = rt_tetris_view_create(RTGUI_WIDGET(g_app_info.game_view));
  138. /* register tetris view to tetris modal */
  139. rt_tetris_add_view(g_app_info.tetris, g_app_info.tetris_view);
  140. /* create timer */
  141. g_app_info._timer = rtgui_timer_create(40, RT_TIMER_FLAG_PERIODIC, _timer_timeout, RT_NULL);
  142. /* this view can be focused */
  143. RTGUI_WIDGET(g_app_info.game_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  144. /* set widget focus */
  145. rtgui_widget_focus(RTGUI_WIDGET(g_app_info.game_view));
  146. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(g_app_info.game_view)) = RTGUI_RGB(0xff, 0xff, 0xff);
  147. rtgui_view_show(g_app_info.game_view, RT_FALSE);
  148. rtgui_timer_start(g_app_info._timer);
  149. }
  150. static void listitem_action_continue(void)
  151. {
  152. }
  153. static void listitem_action_adjust(void)
  154. {
  155. }
  156. static void listitem_action_description(void)
  157. {
  158. }
  159. static const struct rtgui_list_item function_list[] =
  160. {
  161. {"新游戏", RT_NULL, listitem_action_start, RT_NULL},
  162. {"继续", RT_NULL, listitem_action_continue, RT_NULL},
  163. {"等级", RT_NULL, listitem_action_adjust, RT_NULL},
  164. {"游戏说明", RT_NULL, listitem_action_description, RT_NULL},
  165. {"退出游戏", RT_NULL, listitem_action_return, RT_NULL},
  166. };
  167. void rt_application_init(void* parameter)
  168. {
  169. rt_mq_t mq;
  170. rtgui_rect_t rect;
  171. mq = rt_mq_create("tetris_ui", 256, 4, RT_IPC_FLAG_FIFO);
  172. rtgui_thread_register(rt_thread_self(), mq);
  173. g_app_info.workbench = rtgui_workbench_create("main", "tetris");
  174. if (g_app_info.workbench == RT_NULL)
  175. {
  176. rt_kprintf("can't find panel 'main'\n");
  177. rt_mq_delete(mq);
  178. return;
  179. }
  180. rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.workbench), workbench_event_handler);
  181. /* add function view */
  182. rtgui_widget_get_rect(RTGUI_WIDGET(g_app_info.workbench), &rect);
  183. g_app_info.function_view = rtgui_list_view_create(function_list,
  184. sizeof(function_list) / sizeof(struct rtgui_list_item),
  185. &rect,
  186. RTGUI_LIST_VIEW_LIST);
  187. rtgui_workbench_add_view(g_app_info.workbench, RTGUI_VIEW(g_app_info.function_view));
  188. /* add home view */
  189. g_app_info.game_view = rtgui_view_create("game");
  190. rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.game_view), game_view_event_handler);
  191. rtgui_workbench_add_view(g_app_info.workbench, g_app_info.game_view);
  192. rtgui_view_show(RTGUI_VIEW(g_app_info.function_view), RT_FALSE);
  193. rtgui_workbench_event_loop(g_app_info.workbench);
  194. rtgui_workbench_destroy(g_app_info.workbench);
  195. rtgui_thread_deregister(rt_thread_self());
  196. rt_mq_delete(mq);
  197. }