318
|
1 #include <stdio.h>
|
|
2 #include <mb.h>
|
|
3 #include <string.h>
|
325
|
4 //#include "menu.h"
|
318
|
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;
|
321
|
122 printf("fill menu\n");
|
318
|
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 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 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 } else {
|
|
209 if (m->top > 0) {
|
|
210 m->top--;
|
|
211 mb_animated_menu_fillMenuContentUp(m);
|
|
212 } else {
|
|
213 if (m->cur == 0)
|
|
214 return;
|
|
215 m->cur--;
|
|
216 mb_animated_menu_moveLightBar(m);
|
|
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);
|
|
227 }
|
|
228 } else {
|
|
229 if ((m->top+8) < m->max-1) {
|
|
230 m->top++;
|
|
231 mb_animated_menu_fillMenuContentDown(m);
|
|
232 } else {
|
|
233 if (m->cur+m->top < m->max-1) {
|
|
234 m->cur++;
|
|
235 mb_animated_menu_moveLightBar(m);
|
|
236 } else
|
|
237 return;
|
|
238 }
|
|
239 }
|
|
240 }
|
|
241
|
|
242 void mb_animated_menu_set_callback(mb_animated_menu_t *m, void (*f)(mb_animated_menu_t *m, int sel))
|
|
243 {
|
|
244 m->callback = f;
|
|
245 }
|
|
246 static void mb_animated_menu_select(mb_animated_menu_t *m)
|
|
247 {
|
|
248 if (m->callback)
|
|
249 m->callback(m,m->top+m->cur);
|
|
250 }
|
|
251
|
|
252 static void mb_animated_menu_keyHandler(event_t *ev, void *arg)
|
|
253 {
|
|
254 mb_animated_menu_t *m = (mb_animated_menu_t *) arg;
|
|
255 X_kb_event_t *xkey = (X_kb_event_t *)ev;
|
|
256 if(xkey->event.type != EVT_KB_PRESS) {
|
|
257 return;
|
|
258 }
|
|
259 printf("read=%d\n",m->ready);
|
|
260 if (m->ready<=0) return;
|
|
261 switch(xkey->sym) {
|
|
262 case 0xff51: /* left */
|
|
263 break;
|
|
264
|
|
265 case 0xff52: /* up */
|
|
266 mb_animated_menu_up(m);
|
|
267 break;
|
|
268
|
|
269 case 0xff53: /* right */
|
|
270 break;
|
|
271
|
|
272 case 0xff54: /* down */
|
|
273 mb_animated_menu_down(m);
|
|
274 break;
|
|
275
|
|
276 case 0xff0d: /* enter */
|
|
277 mb_animated_menu_select(m);
|
|
278 break;
|
|
279 default:
|
|
280 return;
|
|
281 }
|
|
282 }
|
|
283
|
|
284 /** \brief Create an instace of animated menu.
|
|
285 *
|
|
286 * The objectnames is used to extract symbols from the SVG file.
|
|
287 * ${objectnames}0 - ${objectnames}8 is the text object.
|
|
288 * ${objectnames}_lightbar is the lightbar.
|
|
289 *
|
|
290 */
|
|
291 mb_animated_menu_t *mb_animated_menu_new(MBApp *app,mb_sprite_t *sp,char *objnames,char *menus[])
|
|
292 {
|
|
293 mb_animated_menu_t *m;
|
|
294 int i,len;
|
|
295 char name[255];
|
|
296 mb_obj_t *l;
|
|
297 int ii;
|
|
298
|
|
299 if (menus == NULL)
|
|
300 i=0;
|
|
301 else
|
|
302 for(i=0;menus[i];i++);
|
|
303 ii=9;
|
|
304
|
|
305 m = (mb_animated_menu_t *) malloc(sizeof(mb_animated_menu_t));
|
|
306 m->items = (int *) malloc(sizeof(int)*ii*2+sizeof(mb_obj_t *)*ii);
|
|
307 m->app = app;
|
|
308 m->sprite = sp;
|
|
309 m->top = 0;
|
|
310 m->cur = 0;
|
|
311 m->ready = 1;
|
|
312 m->max = i;
|
|
313 m->menus_y = (int *) (m->items+ii);
|
|
314 m->objects = (mb_obj_t **) (m->menus_y+ii);
|
|
315 m->callback = NULL;
|
|
316 m->speed = 300000;
|
|
317 for(i=0;i<9;i++) {
|
|
318 m->items[i] = i;
|
|
319 snprintf(name,sizeof(name),"%s%d", objnames, i+1);
|
|
320 l = MB_SPRITE_GET_OBJ(sp,name);
|
|
321 if (l == NULL) {
|
|
322 fprintf(stderr,"Can not find symbol %s\n",name);
|
|
323 }
|
|
324 m->objects[i] = (mb_obj_t *) l;
|
|
325 m->menus_y[i] = coord_y((coord_t*)l);
|
|
326 }
|
|
327 m->titles = menus;
|
|
328 snprintf(name,sizeof(name), "%s_lightbar", objnames);
|
|
329 m->lightbar = (mb_obj_t *) MB_SPRITE_GET_OBJ(sp,name);
|
|
330 if (m->lightbar==NULL)
|
|
331 fprintf(stderr,"Can not find object %s\n",name);
|
|
332 mb_animated_menu_fillMenuContent(m);
|
|
333 subject_add_observer(MBAPP_keySubject(app), mb_animated_menu_keyHandler,m);
|
|
334 return m;
|
|
335 }
|
|
336
|
|
337 void mb_animated_menu_set_titles(mb_animated_menu_t *m, char *menus[])
|
|
338 {
|
|
339 int i;
|
|
340 for(i=0;menus[i];i++);
|
|
341
|
|
342 m->max = i;
|
|
343 m->top = 0;
|
|
344 m->cur = 0;
|
|
345 m->titles = menus;
|
|
346 mb_animated_menu_fillMenuContent(m);
|
|
347 }
|
|
348
|
|
349
|
|
350 void mb_animated_menu_set_speed(mb_animated_menu_t *m,int speed)
|
|
351 {
|
|
352 m->speed = speed*1000;
|
|
353 }
|
|
354
|
|
355 int mb_animated_menu_get_speed(mb_animated_menu_t *m)
|
|
356 {
|
|
357 return m->speed/1000;
|
|
358 }
|