comparison examples/menu/main.c @ 297:e885dc875f30

Implement animation menu
author wycc
date Wed, 04 Feb 2009 01:23:45 +0800
parents 2e97e8082d83
children 959c4ac544a1
comparison
equal deleted inserted replaced
296:2e97e8082d83 297:e885dc875f30
1 /*! \file 1 /*! \file
2 * 2 *
3 * This is the demo program for the animated menu. We will use to test the MBAF API. 3 * This is the demo program for the animated menu. We will use to test the MBAF API.
4 * We need to have group item1-item9 in the SVG file. Initially, we will show
5 * item1-item8 only. When a up/down key is pressed, we will draw the next item in item9 and
6 * add two words to move item1-item9 smoothly. The first word move items to the 3/4 position
7 * fastly. The second will move it from 3/4 to the final position slowly to make retard effect.
8 *
9 * If we press another key before the second words finish, we will delete the word and replace
10 * it with a word to move it fastly to the final position and then we will repeat the procedure
11 * to add another two words to move it to the next position.
4 */ 12 */
5 #include <stdio.h> 13 #include <stdio.h>
6 #include <mb.h> 14 #include <mb.h>
7 #include <string.h> 15 #include <string.h>
8 #include "menu.h" 16 #include "menu.h"
27 "Item 16", 35 "Item 16",
28 "Item 17", 36 "Item 17",
29 "Item 18", 37 "Item 18",
30 }; 38 };
31 39
40 int menus_y[10];
41 int items[10];
42
43
32 typedef struct { 44 typedef struct {
33 int top; 45 int top;
34 int cur; 46 int cur;
35 int max; 47 int max;
36 }MyAppData; 48 }MyAppData;
40 static void fillMenuContent() 52 static void fillMenuContent()
41 { 53 {
42 int i; 54 int i;
43 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 55 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
44 mb_sprite_t *sprite=myApp->rootsprite; 56 mb_sprite_t *sprite=myApp->rootsprite;
57 coord_t *textgroup;
45 shape_t *text; 58 shape_t *text;
46 coord_t *group; 59 coord_t *group;
47 coord_t *lightbar; 60 coord_t *lightbar;
48 char name[255]; 61 char name[255];
49 62 int tmp;
63 mb_timeval_t start, playing, now;
64 mb_progm_t *progm;
65 mb_word_t *word;
66
67
68 // fill new item
50 for(i=0;i<8;i++) { 69 for(i=0;i<8;i++) {
51 if (i+data->top > data->max) break; 70 snprintf(name, sizeof(name),"item%dtext", items[i]);
52 snprintf(name,sizeof(name),"item%dtext", i+1); 71 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
53 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name); 72 sh_text_set_text(text, menus[data->top+i]);
54 if (text == NULL) { 73 rdman_shape_changed(MBAPP_RDMAN(myApp),text);
55 printf("Can not find object %s\n",name); 74 }
56 continue; 75
57 } 76
58 sh_text_set_text(text,menus[i+data->top]); 77 snprintf(name, sizeof(name),"item%d", items[i]);
59 rdman_shape_changed(MBAPP_RDMAN(myApp), text); 78 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
60 } 79 coord_hide(textgroup);
61 for(;i<8;i++) { 80 rdman_coord_changed(MBAPP_RDMAN(myApp),textgroup);
62 snprintf(name,sizeof(name),"item%dtext", i+1); 81
63 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name);
64 if (text == NULL) {
65 printf("Can not find object %s\n",name);
66 continue;
67 }
68 sh_text_set_text(text,"");
69 rdman_shape_changed(MBAPP_RDMAN(myApp), text);
70 }
71 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar"); 82 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
72 snprintf(name,sizeof(name),"item%d", data->cur+1); 83 snprintf(name,sizeof(name),"item%d", data->cur+1);
73 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name); 84 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
74 coord_x(lightbar) = coord_x(group); 85 coord_x(lightbar) = coord_x(group);
75 coord_y(lightbar) = coord_y(group); 86 coord_y(lightbar) = coord_y(group);
76 rdman_coord_changed(MBAPP_RDMAN(myApp), lightbar);
77 rdman_redraw_changed(MBAPP_RDMAN(myApp)); 87 rdman_redraw_changed(MBAPP_RDMAN(myApp));
78 } 88 }
79 89
90 static void fillMenuContentUp()
91 {
92 int i;
93 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
94 mb_sprite_t *sprite=myApp->rootsprite;
95 coord_t *textgroup;
96 shape_t *text;
97 coord_t *group;
98 coord_t *lightbar;
99 char name[255];
100 int tmp;
101 mb_timeval_t start, playing, now;
102 mb_progm_t *progm;
103 mb_word_t *word;
104
105
106 // fill new item
107 snprintf(name, sizeof(name),"item%dtext", items[8]);
108 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
109 sh_text_set_text(text, menus[data->top]);
110
111 progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
112 MB_TIMEVAL_SET(&start, 0, 0);
113 MB_TIMEVAL_SET(&playing, 0, 300000);
114 word = mb_progm_next_word(progm, &start, &playing);
115 get_now(&now);
116
117 for(i=0;i<7;i++) {
118 //shift to the next item
119 snprintf(name, sizeof(name),"item%d", items[i]);
120 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
121 mb_shift_new(0,menus_y[i+1]-coord_y(textgroup), textgroup,word);
122 }
123 // fade out the item[7]
124 snprintf(name, sizeof(name),"item%d", items[7]);
125 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
126 mb_shift_new(0,100, textgroup,word);
127 mb_visibility_new(VIS_HIDDEN, textgroup,word);
128
129 // fade in the item[8]
130 snprintf(name, sizeof(name),"item%d", items[8]);
131 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
132 snprintf(name,sizeof(name),"item%d", items[0]);
133 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
134 coord_y(textgroup) = menus_y[0]-100;
135 coord_show(textgroup);
136 mb_shift_new(0,100, textgroup,word);
137
138 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
139 mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
140
141 MB_TIMEVAL_SET(&start, 0, 300000);
142 MB_TIMEVAL_SET(&playing, 0, 0);
143 word = mb_progm_next_word(progm, &start, &playing);
144 snprintf(name, sizeof(name),"item%d", items[8]);
145 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
146 mb_visibility_new(VIS_VISIBLE, textgroup,word);
147
148 mb_progm_free_completed(progm);
149 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
150 rdman_redraw_changed(MBAPP_RDMAN(myApp));
151 tmp = items[8];
152 for(i=8;i>0;i--) {
153 items[i] = items[i-1];
154 }
155 items[0] = tmp;
156 }
157 static void fillMenuContentDown()
158 {
159 int i;
160 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
161 mb_sprite_t *sprite=myApp->rootsprite;
162 coord_t *textgroup;
163 shape_t *text;
164 coord_t *group;
165 coord_t *lightbar;
166 char name[255];
167 int tmp;
168 mb_timeval_t start, playing, now;
169 mb_progm_t *progm;
170 mb_word_t *word;
171
172
173 // fill new item
174 snprintf(name, sizeof(name),"item%dtext", items[8]);
175 text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
176 sh_text_set_text(text, menus[data->top+7]);
177
178 progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
179 MB_TIMEVAL_SET(&start, 0, 0);
180 MB_TIMEVAL_SET(&playing, 0, 300000);
181 word = mb_progm_next_word(progm, &start, &playing);
182 get_now(&now);
183
184 for(i=1;i<8;i++) {
185 //shift to the next item
186 snprintf(name, sizeof(name),"item%d", items[i]);
187 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
188 mb_shift_new(0,menus_y[i-1]-coord_y(textgroup), textgroup,word);
189 }
190 // fade out the item[0]
191 snprintf(name, sizeof(name),"item%d", items[0]);
192 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
193 mb_shift_new(0,-100, textgroup,word);
194
195 // fade in the item[8]
196 snprintf(name, sizeof(name),"item%d", items[8]);
197 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
198 coord_y(textgroup) = menus_y[7]+100;
199 coord_show(textgroup);
200 mb_shift_new(0,-100, textgroup,word);
201
202 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
203 mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
204
205 MB_TIMEVAL_SET(&start, 0, 300001);
206 MB_TIMEVAL_SET(&playing, 0, 0);
207 word = mb_progm_next_word(progm, &start, &playing);
208 snprintf(name, sizeof(name),"item%d", items[0]);
209 textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
210 mb_visibility_new(VIS_VISIBLE, textgroup,word);
211
212 mb_progm_free_completed(progm);
213 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
214 rdman_redraw_changed(MBAPP_RDMAN(myApp));
215 tmp = items[0];
216 for(i=0;i<8;i++) {
217 items[i] = items[i+1];
218 }
219 items[8] = tmp;
220 }
221
222 MoveLightBar()
223 {
224 mb_sprite_t *sprite=myApp->rootsprite;
225 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
226 mb_timeval_t start, playing, now;
227 mb_progm_t *progm;
228 mb_word_t *word;
229 coord_t *group;
230 coord_t *lightbar;
231 char name[255];
232
233 progm = mb_progm_new(1, MBAPP_RDMAN(myApp));
234 MB_TIMEVAL_SET(&start, 0, 0);
235 MB_TIMEVAL_SET(&playing, 0, 200000);
236 word = mb_progm_next_word(progm, &start, &playing);
237 get_now(&now);
238
239 lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
240 snprintf(name,sizeof(name),"item%d", items[data->cur]);
241 group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
242 mb_shift_new(coord_x(group)-coord_x(lightbar),coord_y(group)-coord_y(lightbar),lightbar,word);
243 mb_progm_free_completed(progm);
244 mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
245 rdman_redraw_changed(MBAPP_RDMAN(myApp));
246 }
247
80 void menu_up() 248 void menu_up()
81 { 249 {
82 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 250 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
83 251
84 if (data->cur > 5) 252 if (data->cur > 5) {
85 data->cur--; 253 data->cur--;
86 else { 254 MoveLightBar();
255 } else {
87 if (data->top > 0) { 256 if (data->top > 0) {
88 data->top--; 257 data->top--;
258 fillMenuContentUp();
89 } else { 259 } else {
90 if (data->cur == 0) 260 if (data->cur == 0)
91 return; 261 return;
92 data->cur--; 262 data->cur--;
263 MoveLightBar();
93 } 264 }
94 } 265 }
95 fillMenuContent();
96 } 266 }
97 void menu_down() 267 void menu_down()
98 { 268 {
99 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 269 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
100 270
101 if (data->cur < 5) { 271 if (data->cur < 4) {
102 if (data->top+data->cur <= data->max) 272 if (data->top+data->cur <= data->max) {
103 data->cur++; 273 data->cur++;
274 MoveLightBar();
275 }
104 } else { 276 } else {
105 if ((data->top+8) < data->max) { 277 if ((data->top+8) < data->max) {
106 data->top++; 278 data->top++;
279 fillMenuContentDown();
107 } else { 280 } else {
108 if (data->cur+data->top < data->max-1) 281 if (data->cur+data->top < data->max-1) {
109 data->cur++; 282 data->cur++;
110 else 283 MoveLightBar();
284 } else
111 return; 285 return;
112 } 286 }
113 printf("top=%d\n",data->top); 287 }
114 }
115 fillMenuContent();
116 } 288 }
117 void menu_select() 289 void menu_select()
118 { 290 {
119 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 291 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
120 292
152 324
153 MyApp_InitContent() 325 MyApp_InitContent()
154 { 326 {
155 MyAppData *data = MBAPP_DATA(myApp,MyAppData); 327 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
156 subject_t *key = MBAPP_keySubject(myApp); 328 subject_t *key = MBAPP_keySubject(myApp);
329 char name[255];
330 coord_t *l;
331 int i;
332 mb_sprite_t *sprite=myApp->rootsprite;
157 333
158 data->top = 0; 334 data->top = 0;
159 data->cur = 0; 335 data->cur = 0;
160 data->max = sizeof(menus)/sizeof(int)-1; 336 data->max = sizeof(menus)/sizeof(int)-1;
337 for(i=0;i<9;i++) {
338 items[i] = i+1;
339 snprintf(name,255,"item%d", i+1);
340 l = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
341 menus_y[i] = coord_y(l);
342 }
161 343
162 fillMenuContent(); 344 fillMenuContent();
163 subject_add_observer(key, keyHandler,NULL); 345 subject_add_observer(key, keyHandler,NULL);
164 } 346 }
165 347