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