Mercurial > MadButterfly
comparison examples/menu/animated_menu.c @ 318:4b58e58c78da
Add missing file
author | wycc |
---|---|
date | Thu, 05 Mar 2009 07:53:58 +0800 |
parents | |
children | 44cc65e7e234 |
comparison
equal
deleted
inserted
replaced
312:e4abe8e149de | 318:4b58e58c78da |
---|---|
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 set_text(coord_t *g, char *text) | |
8 { | |
9 geo_t *geo; | |
10 shape_t *shape; | |
11 | |
12 FOR_COORD_MEMBERS(g, geo) { | |
13 shape = geo_get_shape(geo); | |
14 if(shape->obj.obj_type == MBO_TEXT) { | |
15 sh_text_set_text(shape, text); | |
16 } | |
17 } | |
18 } | |
19 | |
20 static void mb_animated_menu_fillMenuContent(mb_animated_menu_t *m) | |
21 { | |
22 int i; | |
23 coord_t *textgroup; | |
24 shape_t *text; | |
25 coord_t *group; | |
26 coord_t *lightbar; | |
27 int tmp; | |
28 mb_timeval_t start, playing, now; | |
29 mb_progm_t *progm; | |
30 mb_word_t *word; | |
31 | |
32 printf("max item is %d\n", m->max); | |
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, ""); | |
40 rdman_coord_changed(MBAPP_RDMAN(m->app),group); | |
41 } | |
42 | |
43 | |
44 textgroup = (coord_t *) m->objects[m->items[i]]; | |
45 coord_hide(textgroup); | |
46 rdman_coord_changed(MBAPP_RDMAN(m->app),textgroup); | |
47 | |
48 lightbar = (coord_t *) m->lightbar; | |
49 group = (coord_t *) m->objects[m->cur]; | |
50 coord_x(lightbar) = coord_x(group); | |
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 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 } | |
123 | |
124 | |
125 static void mb_animated_menu_fillMenuContentDown(mb_animated_menu_t *m) | |
126 { | |
127 int i; | |
128 coord_t *textgroup; | |
129 shape_t *text; | |
130 coord_t *group; | |
131 coord_t *lightbar; | |
132 char name[255]; | |
133 int tmp; | |
134 mb_timeval_t start, playing, now; | |
135 mb_progm_t *progm; | |
136 mb_word_t *word; | |
137 | |
138 | |
139 // fill new item | |
140 set_text((coord_t *)m->objects[m->items[8]], m->titles[m->top+7]); | |
141 | |
142 progm = mb_progm_new(2, MBAPP_RDMAN(m->app)); | |
143 MB_TIMEVAL_SET(&start, 0, 0); | |
144 MB_TIMEVAL_SET(&playing, 0, m->speed); | |
145 word = mb_progm_next_word(progm, &start, &playing); | |
146 get_now(&now); | |
147 | |
148 for(i=1;i<8;i++) { | |
149 //shift to the next item | |
150 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); | |
151 } | |
152 // fade out the item[0] | |
153 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[0]],word); | |
154 | |
155 // fade in the item[8] | |
156 coord_y((coord_t *)m->objects[m->items[8]]) = m->menus_y[7]+100; | |
157 coord_show(((coord_t *)(m->objects[m->items[8]]))); | |
158 mb_shift_new(0,-100, (coord_t *)m->objects[m->items[8]],word); | |
159 | |
160 mb_shift_new(0,m->menus_y[m->cur]-coord_y((coord_t *)m->lightbar),((coord_t *)(m->lightbar)),word); | |
161 | |
162 MB_TIMEVAL_SET(&start, 0, m->speed); | |
163 MB_TIMEVAL_SET(&playing, 0, 0); | |
164 word = mb_progm_next_word(progm, &start, &playing); | |
165 mb_visibility_new(VIS_VISIBLE, (coord_t *) m->objects[m->items[0]],word); | |
166 | |
167 mb_progm_free_completed(progm); | |
168 m->ready--; | |
169 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m); | |
170 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(m->app)->rt), &now); | |
171 rdman_redraw_changed(MBAPP_RDMAN(m->app)); | |
172 tmp = m->items[0]; | |
173 for(i=0;i<8;i++) { | |
174 m->items[i] = m->items[i+1]; | |
175 } | |
176 m->items[8] = tmp; | |
177 } | |
178 | |
179 void mb_animated_menu_moveLightBar(mb_animated_menu_t *m) | |
180 { | |
181 mb_timeval_t start, playing, now; | |
182 mb_progm_t *progm; | |
183 mb_word_t *word; | |
184 coord_t *group; | |
185 coord_t *lightbar; | |
186 | |
187 progm = mb_progm_new(1, MBAPP_RDMAN(m->app)); | |
188 MB_TIMEVAL_SET(&start, 0, 0); | |
189 MB_TIMEVAL_SET(&playing, 0, m->speed); | |
190 word = mb_progm_next_word(progm, &start, &playing); | |
191 get_now(&now); | |
192 | |
193 lightbar = (coord_t *) m->lightbar; | |
194 mb_shift_new(0,m->menus_y[m->cur]-coord_y(lightbar),lightbar,word); | |
195 mb_progm_free_completed(progm); | |
196 m->ready--; | |
197 subject_add_observer(mb_progm_get_complete(progm), mb_animated_menu_complete,m); | |
198 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(m->app)->rt), &now); | |
199 rdman_redraw_changed(MBAPP_RDMAN(m->app)); | |
200 } | |
201 | |
202 static void mb_animated_menu_up(mb_animated_menu_t *m) | |
203 { | |
204 if (m->cur > 5) { | |
205 m->cur--; | |
206 mb_animated_menu_moveLightBar(m); | |
207 } else { | |
208 if (m->top > 0) { | |
209 m->top--; | |
210 mb_animated_menu_fillMenuContentUp(m); | |
211 } else { | |
212 if (m->cur == 0) | |
213 return; | |
214 m->cur--; | |
215 mb_animated_menu_moveLightBar(m); | |
216 } | |
217 } | |
218 } | |
219 static void mb_animated_menu_down(mb_animated_menu_t *m) | |
220 { | |
221 | |
222 if (m->cur < 5) { | |
223 if (m->top+m->cur <= m->max) { | |
224 m->cur++; | |
225 mb_animated_menu_moveLightBar(m); | |
226 } | |
227 } else { | |
228 if ((m->top+8) < m->max-1) { | |
229 m->top++; | |
230 mb_animated_menu_fillMenuContentDown(m); | |
231 } else { | |
232 if (m->cur+m->top < m->max-1) { | |
233 m->cur++; | |
234 mb_animated_menu_moveLightBar(m); | |
235 } else | |
236 return; | |
237 } | |
238 } | |
239 } | |
240 | |
241 void mb_animated_menu_set_callback(mb_animated_menu_t *m, void (*f)(mb_animated_menu_t *m, int sel)) | |
242 { | |
243 m->callback = f; | |
244 } | |
245 static void mb_animated_menu_select(mb_animated_menu_t *m) | |
246 { | |
247 if (m->callback) | |
248 m->callback(m,m->top+m->cur); | |
249 } | |
250 | |
251 static void mb_animated_menu_keyHandler(event_t *ev, void *arg) | |
252 { | |
253 mb_animated_menu_t *m = (mb_animated_menu_t *) arg; | |
254 X_kb_event_t *xkey = (X_kb_event_t *)ev; | |
255 if(xkey->event.type != EVT_KB_PRESS) { | |
256 return; | |
257 } | |
258 printf("read=%d\n",m->ready); | |
259 if (m->ready<=0) return; | |
260 switch(xkey->sym) { | |
261 case 0xff51: /* left */ | |
262 break; | |
263 | |
264 case 0xff52: /* up */ | |
265 mb_animated_menu_up(m); | |
266 break; | |
267 | |
268 case 0xff53: /* right */ | |
269 break; | |
270 | |
271 case 0xff54: /* down */ | |
272 mb_animated_menu_down(m); | |
273 break; | |
274 | |
275 case 0xff0d: /* enter */ | |
276 mb_animated_menu_select(m); | |
277 break; | |
278 default: | |
279 return; | |
280 } | |
281 } | |
282 | |
283 /** \brief Create an instace of animated menu. | |
284 * | |
285 * The objectnames is used to extract symbols from the SVG file. | |
286 * ${objectnames}0 - ${objectnames}8 is the text object. | |
287 * ${objectnames}_lightbar is the lightbar. | |
288 * | |
289 */ | |
290 mb_animated_menu_t *mb_animated_menu_new(MBApp *app,mb_sprite_t *sp,char *objnames,char *menus[]) | |
291 { | |
292 mb_animated_menu_t *m; | |
293 int i,len; | |
294 char name[255]; | |
295 mb_obj_t *l; | |
296 int ii; | |
297 | |
298 if (menus == NULL) | |
299 i=0; | |
300 else | |
301 for(i=0;menus[i];i++); | |
302 ii=9; | |
303 | |
304 m = (mb_animated_menu_t *) malloc(sizeof(mb_animated_menu_t)); | |
305 m->items = (int *) malloc(sizeof(int)*ii*2+sizeof(mb_obj_t *)*ii); | |
306 m->app = app; | |
307 m->sprite = sp; | |
308 m->top = 0; | |
309 m->cur = 0; | |
310 m->ready = 1; | |
311 m->max = i; | |
312 m->menus_y = (int *) (m->items+ii); | |
313 m->objects = (mb_obj_t **) (m->menus_y+ii); | |
314 m->callback = NULL; | |
315 m->speed = 300000; | |
316 for(i=0;i<9;i++) { | |
317 m->items[i] = i; | |
318 snprintf(name,sizeof(name),"%s%d", objnames, i+1); | |
319 l = MB_SPRITE_GET_OBJ(sp,name); | |
320 if (l == NULL) { | |
321 fprintf(stderr,"Can not find symbol %s\n",name); | |
322 } | |
323 m->objects[i] = (mb_obj_t *) l; | |
324 m->menus_y[i] = coord_y((coord_t*)l); | |
325 } | |
326 m->titles = menus; | |
327 snprintf(name,sizeof(name), "%s_lightbar", objnames); | |
328 m->lightbar = (mb_obj_t *) MB_SPRITE_GET_OBJ(sp,name); | |
329 if (m->lightbar==NULL) | |
330 fprintf(stderr,"Can not find object %s\n",name); | |
331 mb_animated_menu_fillMenuContent(m); | |
332 subject_add_observer(MBAPP_keySubject(app), mb_animated_menu_keyHandler,m); | |
333 return m; | |
334 } | |
335 | |
336 void mb_animated_menu_set_titles(mb_animated_menu_t *m, char *menus[]) | |
337 { | |
338 int i; | |
339 for(i=0;menus[i];i++); | |
340 | |
341 m->max = i; | |
342 m->top = 0; | |
343 m->cur = 0; | |
344 m->titles = menus; | |
345 mb_animated_menu_fillMenuContent(m); | |
346 } | |
347 | |
348 | |
349 void mb_animated_menu_set_speed(mb_animated_menu_t *m,int speed) | |
350 { | |
351 m->speed = speed*1000; | |
352 } | |
353 | |
354 int mb_animated_menu_get_speed(mb_animated_menu_t *m) | |
355 { | |
356 return m->speed/1000; | |
357 } |