Parcourir la source

update tetris

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1651 bbd45198-f89e-11dd-88c7-29a3b14d5316
qiuyiuestc@gmail.com il y a 13 ans
Parent
commit
11953570f7
1 fichiers modifiés avec 87 ajouts et 32 suppressions
  1. 87 32
      examples/module/rtgui_app/tetris/tetris_ui.c

+ 87 - 32
examples/module/rtgui_app/tetris/tetris_ui.c

@@ -15,6 +15,7 @@
 #include <rtgui/rtgui.h>
 #include <rtgui/rtgui_system.h>
 #include <rtgui/widgets/view.h>
+#include <rtgui/widgets/list_view.h>
 #include <rtgui/widgets/label.h>
 #include <rtgui/widgets/button.h>
 #include <rtgui/widgets/window.h>
@@ -24,8 +25,10 @@
 
 struct app_info
 {
-	struct rtgui_view* home_view;
-	struct rtgui_workbench* workbench;
+	rtgui_workbench_t* workbench;
+	rtgui_view_t* game_view;
+	rtgui_list_view_t* function_view;
+	
 	rt_tetris_t * tetris;
 	rt_tetris_view_t* tetris_view;
 	rtgui_timer_t* _timer;
@@ -35,16 +38,15 @@ static app_info g_app_info;
 
 static void _game_over(void)
 {
-	rtgui_timer_stop(g_app_info._timer);
-	rt_tetris_pause(g_app_info.tetris);	
+	rtgui_timer_destory(g_app_info._timer);
 	rt_tetris_destory(g_app_info.tetris);
 	rt_tetris_view_destroy(g_app_info.tetris_view);	
-	rtgui_view_destroy(g_app_info.home_view);
-	rtgui_workbench_close(g_app_info.workbench);
 	rt_kprintf("GAME OVER\n");
+	
+	rtgui_view_show(g_app_info.function_view, RT_FALSE);
 }
 
-static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
+static rt_bool_t game_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
 {
 	if (event->type == RTGUI_EVENT_PAINT)
 	{
@@ -125,9 +127,9 @@ static rt_bool_t workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *
 				&& !RTGUI_WORKBENCH_IS_MODAL_MODE(g_app_info.workbench))
 		{
 			/* active home view */
-			if (g_app_info.workbench->current_view != g_app_info.home_view)
+			if (g_app_info.workbench->current_view != g_app_info.game_view)
 			{
-				rtgui_view_show(g_app_info.home_view, RT_FALSE);
+				rtgui_view_show(g_app_info.game_view, RT_FALSE);
 				return RT_TRUE;
 			}
 		}
@@ -136,44 +138,97 @@ static rt_bool_t workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *
 	return rtgui_workbench_event_handler(widget, event);
 }
 
-void tetris_ui_entry(void* parameter)
+static void listitem_action_return(void)
+{
+	rtgui_view_destroy(g_app_info.game_view);
+	rtgui_list_view_destroy(g_app_info.function_view);
+	rtgui_workbench_close(g_app_info.workbench);	
+}	
+
+static void listitem_action_start(void)
+{
+	/* create tetris modal instance */
+	g_app_info.tetris = rt_tetris_create(16, 17);
+
+	/* create tetris view instance */
+	g_app_info.tetris_view = rt_tetris_view_create(RTGUI_WIDGET(g_app_info.game_view));	
+
+	/* register tetris view to tetris modal */
+	rt_tetris_add_view(g_app_info.tetris, g_app_info.tetris_view);
+
+	/* create timer */
+	g_app_info._timer = rtgui_timer_create(40, RT_TIMER_FLAG_PERIODIC, _timer_timeout, RT_NULL);
+
+	/* this view can be focused */
+	RTGUI_WIDGET(g_app_info.game_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
+	
+	/* set widget focus */
+	rtgui_widget_focus(RTGUI_WIDGET(g_app_info.game_view));
+	RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(g_app_info.game_view)) = RTGUI_RGB(0xff, 0xff, 0xff);	
+	
+	rtgui_view_show(g_app_info.game_view, RT_FALSE);
+	rtgui_timer_start(g_app_info._timer);
+}
+
+static void listitem_action_continue(void)
+{
+
+}	
+
+static void listitem_action_adjust(void)
+{
+
+}	
+
+static void listitem_action_description(void)
+{
+
+}
+
+static const struct rtgui_list_item function_list[] =
+{
+	{"新游戏", RT_NULL, listitem_action_start, RT_NULL},
+	{"继续", RT_NULL, listitem_action_continue, RT_NULL},
+	{"等级", RT_NULL, listitem_action_adjust, RT_NULL},
+	{"游戏说明", RT_NULL, listitem_action_description, RT_NULL},
+	{"退出游戏", RT_NULL, listitem_action_return, RT_NULL},
+};
+
+void rt_application_init(void* parameter)
 {
 	rt_mq_t mq;
+	rtgui_rect_t rect;
 
 	mq = rt_mq_create("tetris_ui", 256, 4, RT_IPC_FLAG_FIFO);
 	rtgui_thread_register(rt_thread_self(), mq);
 
-	g_app_info.workbench = rtgui_workbench_create("main", "workbench");
+	g_app_info.workbench = rtgui_workbench_create("main", "tetris");
 	if (g_app_info.workbench == RT_NULL) 
 	{
 		rt_kprintf("can't find panel 'main'\n");
+		rt_mq_delete(mq);
+	
 		return;
-	}	
+	}
 	rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.workbench), workbench_event_handler);
 
-	/* add home view */
-	g_app_info.home_view = rtgui_view_create("Home");
-	rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.home_view), home_view_event_handler);
-
-	rtgui_workbench_add_view(g_app_info.workbench, g_app_info.home_view);
-	/* this view can be focused */
-	RTGUI_WIDGET(g_app_info.home_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
-	/* set widget focus */
-	rtgui_widget_focus(RTGUI_WIDGET(g_app_info.home_view));
-	RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(g_app_info.home_view)) = RTGUI_RGB(0xff, 0xff, 0xff);	
-	rtgui_view_show(g_app_info.home_view, RT_FALSE);
+	/* add function view */
+	rtgui_widget_get_rect(RTGUI_WIDGET(g_app_info.workbench), &rect);
 
-	/* create tetris modal instance */
-	g_app_info.tetris = rt_tetris_create(16, 17);
+	g_app_info.function_view = rtgui_list_view_create(function_list,
+	                                       sizeof(function_list) / sizeof(struct rtgui_list_item),
+	                                       &rect,
+	                                       RTGUI_LIST_VIEW_LIST);
+	
+	rtgui_workbench_add_view(g_app_info.workbench, RTGUI_VIEW(g_app_info.function_view));
 
-	/* create tetris view instance */
-	g_app_info.tetris_view = rt_tetris_view_create(RTGUI_WIDGET(g_app_info.home_view));	
+	/* add home view */
+	g_app_info.game_view = rtgui_view_create("game");
+	
+	rtgui_widget_set_event_handler(RTGUI_WIDGET(g_app_info.game_view), game_view_event_handler);
+	rtgui_workbench_add_view(g_app_info.workbench, g_app_info.game_view);
 
-	/* register tetris view to tetris modal */
-	rt_tetris_add_view(g_app_info.tetris, g_app_info.tetris_view);
-		
-	g_app_info._timer = rtgui_timer_create(40, RT_TIMER_FLAG_PERIODIC, _timer_timeout, RT_NULL);
-	rtgui_timer_start(g_app_info._timer);
+	rtgui_view_show(RTGUI_VIEW(g_app_info.function_view), RT_FALSE);
 
 	rtgui_workbench_event_loop(g_app_info.workbench);
 	rtgui_workbench_destroy(g_app_info.workbench);