comparison src/mbaf/animated_menu.c @ 368:080aca2b7c47

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