comparison examples/menu/main.c @ 302:8b45e7b374b8

Rewrite the menu as a widget style
author wycc
date Sun, 15 Feb 2009 08:33:47 +0800
parents 5bf503270419
children c8f31eef947b
comparison
equal deleted inserted replaced
301:c8173a65e384 302:8b45e7b374b8
13 #include <stdio.h> 13 #include <stdio.h>
14 #include <mb.h> 14 #include <mb.h>
15 #include <string.h> 15 #include <string.h>
16 #include "menu.h" 16 #include "menu.h"
17 #include "mbapp.h" 17 #include "mbapp.h"
18
19
20 typedef struct {
21 char **titles;
22 int *menus_y;
23 int *items;
24 int top;
25 int cur;
26 int max;
27 int ready;
28 MBApp *app;
29 mb_sprite_t *sprite;
30 mb_obj_t **objects;
31 mb_obj_t *lightbar;
32 } mb_animated_menu_t;
18 33
19 char *menus[] = { 34 char *menus[] = {
20 "Item 1", 35 "Item 1",
21 "Item 2", 36 "Item 2",
22 "Item 3", 37 "Item 3",
40 int menus_y[10]; 55 int menus_y[10];
41 int items[10]; 56 int items[10];
42 #define SPEED 600000 57 #define SPEED 600000
43 58
44 typedef struct { 59 typedef struct {
45 int top; 60 mb_animated_menu_t *m;
46 int cur;
47 int max;
48 }MyAppData; 61 }MyAppData;
49 62
50 MBApp *myApp; 63 MBApp *myApp;
51 64
52 static void fillMenuContent() 65
66 static void set_text(coord_t *g, char *text)
67 {
68 geo_t *geo;
69 shape_t *shape;
70
71 FOR_COORD_MEMBERS(g, geo) {
72 shape = geo_get_shape(geo);
73 if(shape->obj.obj_type == MBO_TEXT) {
74 sh_text_set_text(shape, text);
75 }
76 }
77 }
78
79 static void mb_animated_menu_fillMenuContent(mb_animated_menu_t *m)
53 { 80 {
54 int i; 81 int i;
55 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 82 coord_t *textgroup;
56 mb_sprite_t *sprite=myApp->rootsprite; 83 shape_t *text;
84 coord_t *group;
85 coord_t *lightbar;
86 int tmp;
87 mb_timeval_t start, playing, now;
88 mb_progm_t *progm;
89 mb_word_t *word;
90
91
92 // fill new item
93 for(i=0;i<8;i++) {
94 text = (shape_t *) m->objects[m->items[i]];
95 set_text(text, m->titles[m->top+i]);
96 //rdman_shape_changed(MBAPP_RDMAN(m->app),text);
97 }
98
99
100 textgroup = (coord_t *) m->objects[m->items[i]];
101 coord_hide(textgroup);
102 rdman_coord_changed(MBAPP_RDMAN(m->app),textgroup);
103
104 lightbar = (coord_t *) m->lightbar;
105 group = (coord_t *) m->objects[m->cur+1];
106 coord_x(lightbar) = coord_x(group);
107 coord_y(lightbar) = coord_y(group);
108 rdman_redraw_changed(MBAPP_RDMAN(m->app));
109 }
110
111 static void mb_animated_menu_complete(void *arg)
112 {
113 mb_animated_menu_t *m = (mb_animated_menu_t *) arg;
114
115 m->ready++;
116 }
117
118 static void mb_animated_menu_fillMenuContentUp(mb_animated_menu_t *m)
119 {
120 int i;
121 coord_t *textgroup;
122 shape_t *text;
123 coord_t *group;
124 coord_t *lightbar;
125 int tmp;
126 mb_timeval_t start, playing, now;
127 mb_progm_t *progm;
128 mb_word_t *word;
129
130
131 // fill new item
132 text = (shape_t *) m->objects[m->items[8]];
133 set_text(text, m->titles[m->top]);
134
135 progm = mb_progm_new(2, MBAPP_RDMAN(m->app));
136 MB_TIMEVAL_SET(&start, 0, 0);
137 MB_TIMEVAL_SET(&playing, 0, SPEED);
138 word = mb_progm_next_word(progm, &start, &playing);
139 get_now(&now);
140
141 for(i=0;i<7;i++) {
142 //shift to the next item
143 textgroup = (coord_t *) m->objects[m->items[i]];
144 mb_shift_new(0,m->menus_y[i+1]-coord_y(textgroup), textgroup,word);
145 }
146 // fade out the item[7]
147 textgroup = (coord_t *) m->objects[m->items[7]];
148 mb_shift_new(0,100, textgroup,word);
149
150 // fade in the item[8]
151 textgroup = (coord_t *) m->objects[m->items[8]];
152 group = (coord_t *) m->objects[m->items[0]];
153 coord_y(textgroup) = m->menus_y[0]-100;
154 coord_show(textgroup);
155 mb_shift_new(0,100, textgroup,word);
156
157 lightbar = (coord_t *) m->lightbar;
158 mb_shift_new(0,m->menus_y[m->cur]-coord_y(lightbar),lightbar,word);
159
160 MB_TIMEVAL_SET(&start, 0, SPEED);
161 MB_TIMEVAL_SET(&playing, 0, 0);
162 word = mb_progm_next_word(progm, &start, &playing);
163 textgroup = (coord_t *) m->objects[m->items[7]];
164 mb_visibility_new(VIS_HIDDEN, textgroup,word);
165
166 mb_progm_free_completed(progm);
167 m->ready--;
168 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m);
169 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(m->app)->rt), &now);
170 rdman_redraw_changed(MBAPP_RDMAN(m->app));
171 tmp = m->items[8];
172 for(i=8;i>0;i--) {
173 m->items[i] = m->items[i-1];
174 }
175 m->items[0] = tmp;
176 }
177
178
179 static void mb_animated_menu_fillMenuContentDown(mb_animated_menu_t *m)
180 {
181 int i;
57 coord_t *textgroup; 182 coord_t *textgroup;
58 shape_t *text; 183 shape_t *text;
59 coord_t *group; 184 coord_t *group;
60 coord_t *lightbar; 185 coord_t *lightbar;
61 char name[255]; 186 char name[255];
64 mb_progm_t *progm; 189 mb_progm_t *progm;
65 mb_word_t *word; 190 mb_word_t *word;
66 191
67 192
68 // fill new item 193 // fill new item
69 for(i=0;i<8;i++) { 194 set_text(m->objects[m->items[8]], m->titles[m->top+7]);
70 snprintf(name, sizeof(name),"item%dtext", items[i]); 195
71 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name); 196 progm = mb_progm_new(2, MBAPP_RDMAN(m->app));
72 sh_text_set_text(text, menus[data->top+i]);
73 rdman_shape_changed(MBAPP_RDMAN(myApp),text);
74 }
75
76
77 snprintf(name, sizeof(name),"item%d", items[i]);
78 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
79 coord_hide(textgroup);
80 rdman_coord_changed(MBAPP_RDMAN(myApp),textgroup);
81
82 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
83 snprintf(name,sizeof(name),"item%d", data->cur+1);
84 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
85 coord_x(lightbar) = coord_x(group);
86 coord_y(lightbar) = coord_y(group);
87 rdman_redraw_changed(MBAPP_RDMAN(myApp));
88 }
89
90 static void fillMenuContentUp()
91 {
92 int i;
93 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
94 mb_sprite_t *sprite=myApp->rootsprite;
95 coord_t *textgroup;
96 shape_t *text;
97 coord_t *group;
98 coord_t *lightbar;
99 char name[255];
100 int tmp;
101 mb_timeval_t start, playing, now;
102 mb_progm_t *progm;
103 mb_word_t *word;
104
105
106 // fill new item
107 snprintf(name, sizeof(name),"item%dtext", items[8]);
108 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
109 sh_text_set_text(text, menus[data->top]);
110
111 progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
112 MB_TIMEVAL_SET(&start, 0, 0); 197 MB_TIMEVAL_SET(&start, 0, 0);
113 MB_TIMEVAL_SET(&playing, 0, SPEED); 198 MB_TIMEVAL_SET(&playing, 0, SPEED);
114 word = mb_progm_next_word(progm, &start, &playing); 199 word = mb_progm_next_word(progm, &start, &playing);
115 get_now(&now); 200 get_now(&now);
116 201
117 for(i=0;i<7;i++) { 202 for(i=1;i<8;i++) {
118 //shift to the next item 203 //shift to the next item
119 snprintf(name, sizeof(name),"item%d", items[i]); 204 mb_shift_new(0,m->menus_y[i-1]-coord_y((coord_t *)m->objects[m->items[i]]), (coord_t *) m->objects[m->items[i]],word);
120 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name); 205 }
121 mb_shift_new(0,menus_y[i+1]-coord_y(textgroup), textgroup,word); 206 // fade out the item[0]
122 } 207 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[0]],word);
123 // fade out the item[7]
124 snprintf(name, sizeof(name),"item%d", items[7]);
125 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
126 mb_shift_new(0,100, textgroup,word);
127 208
128 // fade in the item[8] 209 // fade in the item[8]
129 snprintf(name, sizeof(name),"item%d", items[8]); 210 coord_y((coord_t *)m->objects[m->items[8]]) = m->menus_y[7]+100;
130 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name); 211 coord_show(((coord_t *)(m->objects[m->items[8]])));
131 snprintf(name,sizeof(name),"item%d", items[0]); 212 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[8]],word);
132 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name); 213
133 coord_y(textgroup) = menus_y[0]-100; 214 mb_shift_new(0,m->menus_y[m->cur]-coord_y((coord_t *)m->lightbar),((coord_t *)(m->lightbar)),word);
134 coord_show(textgroup); 215
135 mb_shift_new(0,100, textgroup,word);
136
137 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
138 mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
139
140 MB_TIMEVAL_SET(&start, 0, SPEED); 216 MB_TIMEVAL_SET(&start, 0, SPEED);
141 MB_TIMEVAL_SET(&playing, 0, 0); 217 MB_TIMEVAL_SET(&playing, 0, 0);
142 word = mb_progm_next_word(progm, &start, &playing); 218 word = mb_progm_next_word(progm, &start, &playing);
143 snprintf(name, sizeof(name),"item%d", items[7]); 219 mb_visibility_new(VIS_VISIBLE, (coord_t *) m->objects[m->items[0]],word);
144 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
145 mb_visibility_new(VIS_HIDDEN, textgroup,word);
146 220
147 mb_progm_free_completed(progm); 221 mb_progm_free_completed(progm);
148 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now); 222 m->ready--;
149 rdman_redraw_changed(MBAPP_RDMAN(myApp)); 223 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m);
150 tmp = items[8]; 224 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(m->app)->rt), &now);
151 for(i=8;i>0;i--) { 225 rdman_redraw_changed(MBAPP_RDMAN(m->app));
152 items[i] = items[i-1]; 226 tmp = m->items[0];
153 }
154 items[0] = tmp;
155 }
156 static void fillMenuContentDown()
157 {
158 int i;
159 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
160 mb_sprite_t *sprite=myApp->rootsprite;
161 coord_t *textgroup;
162 shape_t *text;
163 coord_t *group;
164 coord_t *lightbar;
165 char name[255];
166 int tmp;
167 mb_timeval_t start, playing, now;
168 mb_progm_t *progm;
169 mb_word_t *word;
170
171
172 // fill new item
173 snprintf(name, sizeof(name),"item%dtext", items[8]);
174 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
175 sh_text_set_text(text, menus[data->top+7]);
176
177 progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
178 MB_TIMEVAL_SET(&start, 0, 0);
179 MB_TIMEVAL_SET(&playing, 0, SPEED);
180 word = mb_progm_next_word(progm, &start, &playing);
181 get_now(&now);
182
183 for(i=1;i<8;i++) {
184 //shift to the next item
185 snprintf(name, sizeof(name),"item%d", items[i]);
186 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
187 mb_shift_new(0,menus_y[i-1]-coord_y(textgroup), textgroup,word);
188 }
189 // fade out the item[0]
190 snprintf(name, sizeof(name),"item%d", items[0]);
191 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
192 mb_shift_new(0,-100, textgroup,word);
193
194 // fade in the item[8]
195 snprintf(name, sizeof(name),"item%d", items[8]);
196 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
197 coord_y(textgroup) = menus_y[7]+100;
198 coord_show(textgroup);
199 mb_shift_new(0,-100, textgroup,word);
200
201 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
202 mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
203
204 MB_TIMEVAL_SET(&start, 0, SPEED);
205 MB_TIMEVAL_SET(&playing, 0, 0);
206 word = mb_progm_next_word(progm, &start, &playing);
207 snprintf(name, sizeof(name),"item%d", items[0]);
208 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
209 mb_visibility_new(VIS_VISIBLE, textgroup,word);
210
211 mb_progm_free_completed(progm);
212 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
213 rdman_redraw_changed(MBAPP_RDMAN(myApp));
214 tmp = items[0];
215 for(i=0;i<8;i++) { 227 for(i=0;i<8;i++) {
216 items[i] = items[i+1]; 228 m->items[i] = m->items[i+1];
217 } 229 }
218 items[8] = tmp; 230 m->items[8] = tmp;
219 } 231 }
220 232
221 MoveLightBar() 233 void mb_animated_menu_moveLightBar(mb_animated_menu_t *m)
222 { 234 {
223 mb_sprite_t *sprite=myApp->rootsprite;
224 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
225 mb_timeval_t start, playing, now; 235 mb_timeval_t start, playing, now;
226 mb_progm_t *progm; 236 mb_progm_t *progm;
227 mb_word_t *word; 237 mb_word_t *word;
228 coord_t *group; 238 coord_t *group;
229 coord_t *lightbar; 239 coord_t *lightbar;
230 char name[255]; 240
231 241 progm = mb_progm_new(1, MBAPP_RDMAN(m->app));
232 progm = mb_progm_new(1, MBAPP_RDMAN(myApp));
233 MB_TIMEVAL_SET(&start, 0, 0); 242 MB_TIMEVAL_SET(&start, 0, 0);
234 MB_TIMEVAL_SET(&playing, 0, 200000); 243 MB_TIMEVAL_SET(&playing, 0, 200000);
235 word = mb_progm_next_word(progm, &start, &playing); 244 word = mb_progm_next_word(progm, &start, &playing);
236 get_now(&now); 245 get_now(&now);
237 246
238 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar"); 247 lightbar = (coord_t *) m->lightbar;
239 snprintf(name,sizeof(name),"item%d", items[data->cur]); 248 group = (coord_t *) m->objects[m->cur];
240 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
241 mb_shift_new(coord_x(group)-coord_x(lightbar),coord_y(group)-coord_y(lightbar),lightbar,word); 249 mb_shift_new(coord_x(group)-coord_x(lightbar),coord_y(group)-coord_y(lightbar),lightbar,word);
242 mb_progm_free_completed(progm); 250 mb_progm_free_completed(progm);
243 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now); 251 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(m->app)->rt), &now);
244 rdman_redraw_changed(MBAPP_RDMAN(myApp)); 252 rdman_redraw_changed(MBAPP_RDMAN(m->app));
245 } 253 }
246 254
247 void menu_up() 255 void mb_animated_menu_up(mb_animated_menu_t *m)
248 { 256 {
249 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 257 if (m->cur > 5) {
250 258 m->cur--;
251 if (data->cur > 5) { 259 mb_animated_menu_moveLightBar(m);
252 data->cur--;
253 MoveLightBar();
254 } else { 260 } else {
255 if (data->top > 0) { 261 if (m->top > 0) {
256 data->top--; 262 m->top--;
257 fillMenuContentUp(); 263 mb_animated_menu_fillMenuContentUp(m);
258 } else { 264 } else {
259 if (data->cur == 0) 265 if (m->cur == 0)
260 return; 266 return;
261 data->cur--; 267 m->cur--;
262 MoveLightBar(); 268 mb_animated_menu_moveLightBar(m);
263 } 269 }
264 } 270 }
265 } 271 }
266 void menu_down() 272 void mb_animated_menu_down(mb_animated_menu_t *m)
267 { 273 {
268 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 274
269 275 if (m->cur < 4) {
270 if (data->cur < 4) { 276 if (m->top+m->cur <= m->max) {
271 if (data->top+data->cur <= data->max) { 277 m->cur++;
272 data->cur++; 278 mb_animated_menu_moveLightBar(m);
273 MoveLightBar();
274 } 279 }
275 } else { 280 } else {
276 if ((data->top+8) < data->max) { 281 if ((m->top+8) < m->max) {
277 data->top++; 282 m->top++;
278 fillMenuContentDown(); 283 mb_animated_menu_fillMenuContentDown(m);
279 } else { 284 } else {
280 if (data->cur+data->top < data->max-1) { 285 if (m->cur+m->top < m->max-1) {
281 data->cur++; 286 m->cur++;
282 MoveLightBar(); 287 mb_animated_menu_moveLightBar(m);
283 } else 288 } else
284 return; 289 return;
285 } 290 }
286 } 291 }
287 } 292 }
288 void menu_select() 293 void mb_animated_menu_select(mb_animated_menu_t *m)
289 { 294 {
290 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 295 }
291 296
292 printf("menu '%s' is selected\n", menus[data->top+data->cur]); 297 void mb_animated_menu_keyHandler(event_t *ev, void *arg)
293 } 298 {
294 299 mb_animated_menu_t *m = (mb_animated_menu_t *) arg;
295 void keyHandler(event_t *ev, void *arg)
296 {
297 X_kb_event_t *xkey = (X_kb_event_t *)ev; 300 X_kb_event_t *xkey = (X_kb_event_t *)ev;
298 if(xkey->event.type != EVT_KB_PRESS) { 301 if(xkey->event.type != EVT_KB_PRESS) {
299 return; 302 return;
300 } 303 }
301 switch(xkey->sym) { 304 switch(xkey->sym) {
302 case 0xff51: /* left */ 305 case 0xff51: /* left */
303 break; 306 break;
304 307
305 case 0xff52: /* up */ 308 case 0xff52: /* up */
306 menu_up(); 309 mb_animated_menu_up(m);
307 break; 310 break;
308 311
309 case 0xff53: /* right */ 312 case 0xff53: /* right */
310 break; 313 break;
311 314
312 case 0xff54: /* down */ 315 case 0xff54: /* down */
313 menu_down(); 316 mb_animated_menu_down(m);
314 break; 317 break;
315 318
316 case 0xff0d: /* enter */ 319 case 0xff0d: /* enter */
317 menu_select(); 320 mb_animated_menu_select(m);
318 break; 321 break;
319 default: 322 default:
320 return; 323 return;
321 } 324 }
325 }
326
327 /** \brief Create an instace of animated menu.
328 *
329 * The objectnames is used to extract symbols from the SVG file.
330 * ${objectnames}0 - ${objectnames}8 is the text object.
331 * ${objectnames}_lightbar is the lightbar.
332 *
333 */
334 mb_animated_menu_t *mb_animated_menu_new(MBApp *app,mb_sprite_t *sp,char *objnames,char *menus[])
335 {
336 mb_animated_menu_t *m;
337 int i,len;
338 char name[255];
339 mb_obj_t *l;
340
341 for(i=0;menus[i];i++);
342 len = i;
343
344 m = (mb_animated_menu_t *) malloc(sizeof(mb_animated_menu_t)+sizeof(int)*len*2+sizeof(mb_obj_t *)*len);
345 m->app = app;
346 m->sprite = sp;
347 m->top = 0;
348 m->cur = 0;
349 m->ready = 1;
350 m->max = len;
351 m->items = (int *) (m+1);
352 m->menus_y = (int *) (m->items+len);
353 m->objects = (mb_obj_t **) (m->menus_y+len);
354 for(i=0;i<9;i++) {
355 m->items[i] = i;
356 snprintf(name,sizeof(name),"%s%d", objnames, i+1);
357 l = MB_SPRITE_GET_OBJ(sp,name);
358 if (l == NULL) {
359 fprintf(stderr,"Can not find symbol %s\n",name);
360 }
361 m->objects[i] = (mb_obj_t *) l;
362 m->menus_y[i] = coord_y((coord_t*)l);
363 }
364 m->titles = menus;
365 snprintf(name,sizeof(name), "%s_lightbar", objnames);
366 m->lightbar = (mb_obj_t *) MB_SPRITE_GET_OBJ(sp,name);
367 if (m->lightbar)
368 fprintf(stderr,"Can not find object %s\n",name);
369 mb_animated_menu_fillMenuContent(m);
370 subject_add_observer(MBAPP_keySubject(myApp), mb_animated_menu_keyHandler,m);
371 return m;
322 } 372 }
323 373
324 MyApp_InitContent() 374 MyApp_InitContent()
325 { 375 {
326 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 376 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
328 char name[255]; 378 char name[255];
329 coord_t *l; 379 coord_t *l;
330 int i; 380 int i;
331 mb_sprite_t *sprite=myApp->rootsprite; 381 mb_sprite_t *sprite=myApp->rootsprite;
332 382
333 data->top = 0; 383 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",menus);
334 data->cur = 0;
335 data->max = sizeof(menus)/sizeof(int)-1;
336 for(i=0;i<9;i++) {
337 items[i] = i+1;
338 snprintf(name,255,"item%d", i+1);
339 l = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
340 menus_y[i] = coord_y(l);
341 }
342
343 fillMenuContent();
344 subject_add_observer(key, keyHandler,NULL);
345 } 384 }
346 385
347 int main(int argc, char * const argv[]) { 386 int main(int argc, char * const argv[]) {
348 subject_t *subject; 387 subject_t *subject;
349 mb_obj_t *button; 388 mb_obj_t *button;