Mercurial > MadButterfly
comparison examples/menu/main.c @ 296:2e97e8082d83
* Fix the symbol definition code which does not assume the id is the same as the mbname.
* Add appleTV style list demo. We need to add animation latter. It is staic for now.
author | wycc |
---|---|
date | Sun, 01 Feb 2009 16:28:28 +0800 |
parents | |
children | e885dc875f30 |
comparison
equal
deleted
inserted
replaced
295:2469f8d23658 | 296:2e97e8082d83 |
---|---|
1 /*! \file | |
2 * | |
3 * This is the demo program for the animated menu. We will use to test the MBAF API. | |
4 */ | |
5 #include <stdio.h> | |
6 #include <mb.h> | |
7 #include <string.h> | |
8 #include "menu.h" | |
9 #include "mbapp.h" | |
10 | |
11 char *menus[] = { | |
12 "Item 1", | |
13 "Item 2", | |
14 "Item 3", | |
15 "Item 4", | |
16 "Item 5", | |
17 "Item 6", | |
18 "Item 7", | |
19 "Item 8", | |
20 "Item 9", | |
21 "Item 10", | |
22 "Item 11", | |
23 "Item 12", | |
24 "Item 13", | |
25 "Item 14", | |
26 "Item 15", | |
27 "Item 16", | |
28 "Item 17", | |
29 "Item 18", | |
30 }; | |
31 | |
32 typedef struct { | |
33 int top; | |
34 int cur; | |
35 int max; | |
36 }MyAppData; | |
37 | |
38 MBApp *myApp; | |
39 | |
40 static void fillMenuContent() | |
41 { | |
42 int i; | |
43 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
44 mb_sprite_t *sprite=myApp->rootsprite; | |
45 shape_t *text; | |
46 coord_t *group; | |
47 coord_t *lightbar; | |
48 char name[255]; | |
49 | |
50 for(i=0;i<8;i++) { | |
51 if (i+data->top > data->max) break; | |
52 snprintf(name,sizeof(name),"item%dtext", i+1); | |
53 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name); | |
54 if (text == NULL) { | |
55 printf("Can not find object %s\n",name); | |
56 continue; | |
57 } | |
58 sh_text_set_text(text,menus[i+data->top]); | |
59 rdman_shape_changed(MBAPP_RDMAN(myApp), text); | |
60 } | |
61 for(;i<8;i++) { | |
62 snprintf(name,sizeof(name),"item%dtext", i+1); | |
63 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name); | |
64 if (text == NULL) { | |
65 printf("Can not find object %s\n",name); | |
66 continue; | |
67 } | |
68 sh_text_set_text(text,""); | |
69 rdman_shape_changed(MBAPP_RDMAN(myApp), text); | |
70 } | |
71 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar"); | |
72 snprintf(name,sizeof(name),"item%d", data->cur+1); | |
73 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name); | |
74 coord_x(lightbar) = coord_x(group); | |
75 coord_y(lightbar) = coord_y(group); | |
76 rdman_coord_changed(MBAPP_RDMAN(myApp), lightbar); | |
77 rdman_redraw_changed(MBAPP_RDMAN(myApp)); | |
78 } | |
79 | |
80 void menu_up() | |
81 { | |
82 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
83 | |
84 if (data->cur > 5) | |
85 data->cur--; | |
86 else { | |
87 if (data->top > 0) { | |
88 data->top--; | |
89 } else { | |
90 if (data->cur == 0) | |
91 return; | |
92 data->cur--; | |
93 } | |
94 } | |
95 fillMenuContent(); | |
96 } | |
97 void menu_down() | |
98 { | |
99 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
100 | |
101 if (data->cur < 5) { | |
102 if (data->top+data->cur <= data->max) | |
103 data->cur++; | |
104 } else { | |
105 if ((data->top+8) < data->max) { | |
106 data->top++; | |
107 } else { | |
108 if (data->cur+data->top < data->max-1) | |
109 data->cur++; | |
110 else | |
111 return; | |
112 } | |
113 printf("top=%d\n",data->top); | |
114 } | |
115 fillMenuContent(); | |
116 } | |
117 void menu_select() | |
118 { | |
119 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
120 | |
121 printf("menu '%s' is selected\n", menus[data->top+data->cur]); | |
122 } | |
123 | |
124 void keyHandler(event_t *ev, void *arg) | |
125 { | |
126 X_kb_event_t *xkey = (X_kb_event_t *)ev; | |
127 if(xkey->event.type != EVT_KB_PRESS) { | |
128 return; | |
129 } | |
130 switch(xkey->sym) { | |
131 case 0xff51: /* left */ | |
132 break; | |
133 | |
134 case 0xff52: /* up */ | |
135 menu_up(); | |
136 break; | |
137 | |
138 case 0xff53: /* right */ | |
139 break; | |
140 | |
141 case 0xff54: /* down */ | |
142 menu_down(); | |
143 break; | |
144 | |
145 case 0xff0d: /* enter */ | |
146 menu_select(); | |
147 break; | |
148 default: | |
149 return; | |
150 } | |
151 } | |
152 | |
153 MyApp_InitContent() | |
154 { | |
155 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
156 subject_t *key = MBAPP_keySubject(myApp); | |
157 | |
158 data->top = 0; | |
159 data->cur = 0; | |
160 data->max = sizeof(menus)/sizeof(int)-1; | |
161 | |
162 fillMenuContent(); | |
163 subject_add_observer(key, keyHandler,NULL); | |
164 } | |
165 | |
166 int main(int argc, char * const argv[]) { | |
167 subject_t *subject; | |
168 mb_obj_t *button; | |
169 MyAppData data; | |
170 mb_timeval_t tmo,interval; | |
171 | |
172 if (argc > 1) | |
173 myApp = MBApp_Init(argv[1]); | |
174 else | |
175 myApp = MBApp_Init("list"); | |
176 MBApp_setData(myApp,&data); | |
177 MyApp_InitContent(); | |
178 | |
179 MBApp_loop(myApp); | |
180 | |
181 return 0; | |
182 } | |
183 | |
184 /* vim: set ts=4 */ |