Mercurial > MadButterfly
annotate src/mbaf/animated_menu.c @ 517:82c1ea2ba929 Android_Skia
Implement SurfaceHolder.Callback for waiting surface creature.
SurfaceView should wait for surface being created. After that, user
of SurfaceView can be used to draw graphics.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 01 Dec 2009 22:55:27 +0800 |
parents | 26c302b47de1 |
children | f43224bf3524 |
rev | line source |
---|---|
318 | 1 #include <stdio.h> |
2 #include <mb.h> | |
3 #include <string.h> | |
325 | 4 //#include "menu.h" |
456
26c302b47de1
Change name of header files.
Thinker K.F. Li <thinker@branda.to>
parents:
455
diff
changeset
|
5 #include "mb_af.h" |
26c302b47de1
Change name of header files.
Thinker K.F. Li <thinker@branda.to>
parents:
455
diff
changeset
|
6 #include "mb_ani_menu.h" |
344 | 7 static void mb_animated_menu_update(mb_animated_menu_t *m); |
318 | 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 // fill new item | |
34 for(i=0;i<8;i++) { | |
35 group = (coord_t *) m->objects[m->items[i]]; | |
36 if (i < m->max) | |
37 set_text(group, m->titles[m->top+i]); | |
38 else | |
39 set_text(group, ""); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
40 rdman_coord_changed(MBAF_RDMAN(m->app),group); |
318 | 41 } |
42 | |
43 | |
44 textgroup = (coord_t *) m->objects[m->items[i]]; | |
45 coord_hide(textgroup); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
46 rdman_coord_changed(MBAF_RDMAN(m->app),textgroup); |
318 | 47 |
48 lightbar = (coord_t *) m->lightbar; | |
49 group = (coord_t *) m->objects[m->cur]; | |
50 coord_y(lightbar) = coord_y(group); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
51 rdman_coord_changed(MBAF_RDMAN(m->app),lightbar); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
52 rdman_redraw_changed(MBAF_RDMAN(m->app)); |
318 | 53 } |
54 | |
55 static void mb_animated_menu_complete(event_t *ev,void *arg) | |
56 { | |
57 mb_animated_menu_t *m = (mb_animated_menu_t *) arg; | |
58 | |
59 m->ready++; | |
60 } | |
61 | |
62 static void mb_animated_menu_fillMenuContentUp(mb_animated_menu_t *m) | |
63 { | |
64 int i; | |
65 coord_t *textgroup; | |
66 shape_t *text; | |
67 coord_t *group; | |
68 coord_t *lightbar; | |
69 int tmp; | |
70 mb_timeval_t start, playing, now; | |
71 mb_progm_t *progm; | |
72 mb_word_t *word; | |
73 | |
74 | |
75 // fill new item | |
76 group = (coord_t *) m->objects[m->items[8]]; | |
77 set_text(group, m->titles[m->top]); | |
78 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
79 m->progm = progm = mb_progm_new(2, MBAF_RDMAN(m->app)); |
318 | 80 MB_TIMEVAL_SET(&start, 0, 0); |
81 MB_TIMEVAL_SET(&playing, 0, m->speed); | |
82 word = mb_progm_next_word(progm, &start, &playing); | |
83 get_now(&now); | |
84 | |
85 for(i=0;i<7;i++) { | |
86 //shift to the next item | |
87 textgroup = (coord_t *) m->objects[m->items[i]]; | |
88 mb_shift_new(0,m->menus_y[i+1]-coord_y(textgroup), textgroup,word); | |
89 } | |
90 // fade out the item[7] | |
91 textgroup = (coord_t *) m->objects[m->items[7]]; | |
92 mb_shift_new(0,100, textgroup,word); | |
93 | |
94 // fade in the item[8] | |
95 textgroup = (coord_t *) m->objects[m->items[8]]; | |
96 group = (coord_t *) m->objects[m->items[0]]; | |
97 coord_y(textgroup) = m->menus_y[0]-100; | |
98 coord_show(textgroup); | |
99 mb_shift_new(0,100, textgroup,word); | |
100 | |
101 lightbar = (coord_t *) m->lightbar; | |
102 mb_shift_new(0,m->menus_y[m->cur]-coord_y(lightbar),lightbar,word); | |
103 | |
104 MB_TIMEVAL_SET(&start, 0, m->speed); | |
105 MB_TIMEVAL_SET(&playing, 0, 0); | |
106 word = mb_progm_next_word(progm, &start, &playing); | |
107 textgroup = (coord_t *) m->objects[m->items[7]]; | |
108 mb_visibility_new(VIS_HIDDEN, textgroup,word); | |
109 | |
110 mb_progm_free_completed(progm); | |
111 m->ready--; | |
112 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
113 mb_progm_start(progm, X_MB_tman(MBAF_RDMAN(m->app)->rt), &now); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
114 rdman_redraw_changed(MBAF_RDMAN(m->app)); |
318 | 115 tmp = m->items[8]; |
116 for(i=8;i>0;i--) { | |
117 m->items[i] = m->items[i-1]; | |
118 } | |
119 m->items[0] = tmp; | |
120 } | |
121 | |
122 | |
123 static void mb_animated_menu_fillMenuContentDown(mb_animated_menu_t *m) | |
124 { | |
125 int i; | |
126 coord_t *textgroup; | |
127 shape_t *text; | |
128 coord_t *group; | |
129 coord_t *lightbar; | |
130 char name[255]; | |
131 int tmp; | |
132 mb_timeval_t start, playing, now; | |
133 mb_progm_t *progm; | |
134 mb_word_t *word; | |
135 | |
136 | |
137 // fill new item | |
138 set_text((coord_t *)m->objects[m->items[8]], m->titles[m->top+7]); | |
139 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
140 m->progm = progm = mb_progm_new(2, MBAF_RDMAN(m->app)); |
318 | 141 MB_TIMEVAL_SET(&start, 0, 0); |
142 MB_TIMEVAL_SET(&playing, 0, m->speed); | |
143 word = mb_progm_next_word(progm, &start, &playing); | |
144 get_now(&now); | |
145 | |
146 for(i=1;i<8;i++) { | |
147 //shift to the next item | |
148 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); | |
149 } | |
150 // fade out the item[0] | |
151 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[0]],word); | |
152 | |
153 // fade in the item[8] | |
154 coord_y((coord_t *)m->objects[m->items[8]]) = m->menus_y[7]+100; | |
155 coord_show(((coord_t *)(m->objects[m->items[8]]))); | |
156 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[8]],word); | |
157 | |
158 mb_shift_new(0,m->menus_y[m->cur]-coord_y((coord_t *)m->lightbar),((coord_t *)(m->lightbar)),word); | |
159 | |
160 MB_TIMEVAL_SET(&start, 0, m->speed); | |
161 MB_TIMEVAL_SET(&playing, 0, 0); | |
162 word = mb_progm_next_word(progm, &start, &playing); | |
163 mb_visibility_new(VIS_VISIBLE, (coord_t *) m->objects[m->items[0]],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); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
168 mb_progm_start(progm, X_MB_tman(MBAF_RDMAN(m->app)->rt), &now); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
169 rdman_redraw_changed(MBAF_RDMAN(m->app)); |
318 | 170 tmp = m->items[0]; |
171 for(i=0;i<8;i++) { | |
172 m->items[i] = m->items[i+1]; | |
173 } | |
174 m->items[8] = tmp; | |
175 } | |
176 | |
177 void mb_animated_menu_moveLightBar(mb_animated_menu_t *m) | |
178 { | |
179 mb_timeval_t start, playing, now; | |
180 mb_progm_t *progm; | |
181 mb_word_t *word; | |
182 coord_t *group; | |
183 coord_t *lightbar; | |
184 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
185 m->progm = progm = mb_progm_new(1, MBAF_RDMAN(m->app)); |
318 | 186 MB_TIMEVAL_SET(&start, 0, 0); |
187 MB_TIMEVAL_SET(&playing, 0, m->speed); | |
188 word = mb_progm_next_word(progm, &start, &playing); | |
189 get_now(&now); | |
190 | |
191 lightbar = (coord_t *) m->lightbar; | |
192 mb_shift_new(0,m->menus_y[m->cur]-coord_y(lightbar),lightbar,word); | |
193 mb_progm_free_completed(progm); | |
194 m->ready--; | |
195 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m); | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
196 mb_progm_start(progm, X_MB_tman(MBAF_RDMAN(m->app)->rt), &now); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
197 rdman_redraw_changed(MBAF_RDMAN(m->app)); |
318 | 198 } |
199 | |
200 static void mb_animated_menu_up(mb_animated_menu_t *m) | |
201 { | |
202 if (m->cur > 5) { | |
203 m->cur--; | |
204 mb_animated_menu_moveLightBar(m); | |
344 | 205 mb_animated_menu_update(m); |
318 | 206 } else { |
207 if (m->top > 0) { | |
208 m->top--; | |
209 mb_animated_menu_fillMenuContentUp(m); | |
344 | 210 mb_animated_menu_update(m); |
318 | 211 } else { |
212 if (m->cur == 0) | |
213 return; | |
214 m->cur--; | |
215 mb_animated_menu_moveLightBar(m); | |
344 | 216 mb_animated_menu_update(m); |
318 | 217 } |
218 } | |
219 } | |
220 static void mb_animated_menu_down(mb_animated_menu_t *m) | |
221 { | |
222 | |
223 if (m->cur < 5) { | |
224 if (m->top+m->cur <= m->max) { | |
225 m->cur++; | |
226 mb_animated_menu_moveLightBar(m); | |
344 | 227 mb_animated_menu_update(m); |
318 | 228 } |
229 } else { | |
230 if ((m->top+8) < m->max-1) { | |
231 m->top++; | |
232 mb_animated_menu_fillMenuContentDown(m); | |
344 | 233 mb_animated_menu_update(m); |
318 | 234 } else { |
235 if (m->cur+m->top < m->max-1) { | |
236 m->cur++; | |
237 mb_animated_menu_moveLightBar(m); | |
344 | 238 mb_animated_menu_update(m); |
318 | 239 } else |
240 return; | |
241 } | |
242 } | |
243 } | |
244 | |
344 | 245 void mb_animated_menu_set_update_callback(mb_animated_menu_t *m, void (*f)(mb_animated_menu_t *m, int sel)) |
246 { | |
247 m->update_callback = f; | |
248 } | |
318 | 249 void mb_animated_menu_set_callback(mb_animated_menu_t *m, void (*f)(mb_animated_menu_t *m, int sel)) |
250 { | |
251 m->callback = f; | |
252 } | |
344 | 253 static void mb_animated_menu_update(mb_animated_menu_t *m) |
254 { | |
255 if (m->update_callback) | |
256 m->update_callback(m,m->top+m->cur); | |
257 | |
258 } | |
318 | 259 static void mb_animated_menu_select(mb_animated_menu_t *m) |
260 { | |
261 if (m->callback) | |
262 m->callback(m,m->top+m->cur); | |
263 } | |
264 | |
332
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
265 // Send the pending key in the animation progm complete callback |
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
266 static void mb_animated_menu_keyHandler(event_t *ev, void *arg); |
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
267 static void mb_animated_menu_send_pending_key(event_t *ev,void *arg) |
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
268 { |
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
269 mb_animated_menu_t *m = (mb_animated_menu_t *) arg; |
455
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
270 X_kb_event_t *xkey; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
271 |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
272 xkey = &m->pending_keys[m->pending_pos]; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
273 m->pending_pos = (m->pending_pos + 1) & 0xf; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
274 mb_animated_menu_keyHandler((event_t *) xkey, m); |
332
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
275 } |
318 | 276 static void mb_animated_menu_keyHandler(event_t *ev, void *arg) |
277 { | |
278 mb_animated_menu_t *m = (mb_animated_menu_t *) arg; | |
279 X_kb_event_t *xkey = (X_kb_event_t *)ev; | |
280 if(xkey->event.type != EVT_KB_PRESS) { | |
281 return; | |
282 } | |
332
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
283 if (m->ready<=0) { |
455
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
284 m->pending_keys[m->pending_last++] = *xkey; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
285 m->pending_last &= 0xf; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
286 if(m->pending_last == m->pending_pos) |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
287 m->pending_last = (m->pending_last - 1) & 0xf; |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
288 else |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
289 subject_add_observer(mb_progm_get_complete(m->progm), |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
290 mb_animated_menu_send_pending_key, |
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
291 m); |
332
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
292 return; |
f90c60967a9c
Add mb_progm_finish to terminate the current animation and put all objects in the final position.
wycc
parents:
329
diff
changeset
|
293 } |
318 | 294 switch(xkey->sym) { |
295 case 0xff51: /* left */ | |
296 break; | |
297 | |
298 case 0xff52: /* up */ | |
299 mb_animated_menu_up(m); | |
300 break; | |
301 | |
302 case 0xff53: /* right */ | |
303 break; | |
304 | |
305 case 0xff54: /* down */ | |
306 mb_animated_menu_down(m); | |
307 break; | |
308 | |
309 case 0xff0d: /* enter */ | |
310 mb_animated_menu_select(m); | |
311 break; | |
312 default: | |
313 return; | |
314 } | |
315 } | |
316 | |
317 /** \brief Create an instace of animated menu. | |
318 * | |
319 * The objectnames is used to extract symbols from the SVG file. | |
320 * ${objectnames}0 - ${objectnames}8 is the text object. | |
321 * ${objectnames}_lightbar is the lightbar. | |
322 * | |
323 */ | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
324 mb_animated_menu_t *mb_animated_menu_new(mbaf_t *app,mb_sprite_t *sp,char *objnames,char *menus[]) |
318 | 325 { |
326 mb_animated_menu_t *m; | |
327 int i,len; | |
328 char name[255]; | |
329 mb_obj_t *l; | |
330 int ii; | |
331 | |
332 if (menus == NULL) | |
333 i=0; | |
334 else | |
335 for(i=0;menus[i];i++); | |
336 ii=9; | |
337 | |
338 m = (mb_animated_menu_t *) malloc(sizeof(mb_animated_menu_t)); | |
339 m->items = (int *) malloc(sizeof(int)*ii*2+sizeof(mb_obj_t *)*ii); | |
340 m->app = app; | |
341 m->sprite = sp; | |
342 m->top = 0; | |
343 m->cur = 0; | |
344 m->ready = 1; | |
345 m->max = i; | |
346 m->menus_y = (int *) (m->items+ii); | |
347 m->objects = (mb_obj_t **) (m->menus_y+ii); | |
348 m->callback = NULL; | |
344 | 349 m->update_callback = NULL; |
318 | 350 m->speed = 300000; |
351 for(i=0;i<9;i++) { | |
352 m->items[i] = i; | |
353 snprintf(name,sizeof(name),"%s%d", objnames, i+1); | |
354 l = MB_SPRITE_GET_OBJ(sp,name); | |
355 if (l == NULL) { | |
356 fprintf(stderr,"Can not find symbol %s\n",name); | |
357 } | |
358 m->objects[i] = (mb_obj_t *) l; | |
359 m->menus_y[i] = coord_y((coord_t*)l); | |
360 } | |
361 m->titles = menus; | |
362 snprintf(name,sizeof(name), "%s_lightbar", objnames); | |
363 m->lightbar = (mb_obj_t *) MB_SPRITE_GET_OBJ(sp,name); | |
364 if (m->lightbar==NULL) | |
365 fprintf(stderr,"Can not find object %s\n",name); | |
455
58b6337fb3b2
Fix bugs of pending keys of animated menu.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
366 m->pending_pos = m->pending_last = 0; |
318 | 367 mb_animated_menu_fillMenuContent(m); |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
368
diff
changeset
|
368 subject_add_observer(MBAF_KB_SUBJECT(app), mb_animated_menu_keyHandler,m); |
318 | 369 return m; |
370 } | |
371 | |
372 void mb_animated_menu_set_titles(mb_animated_menu_t *m, char *menus[]) | |
373 { | |
374 int i; | |
375 for(i=0;menus[i];i++); | |
376 | |
377 m->max = i; | |
378 m->top = 0; | |
379 m->cur = 0; | |
380 m->titles = menus; | |
381 mb_animated_menu_fillMenuContent(m); | |
329
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
328
diff
changeset
|
382 mb_animated_menu_moveLightBar(m); |
318 | 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 } |