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

Implement animation menu
author wycc
date Wed, 04 Feb 2009 01:23:45 +0800
parents 2e97e8082d83
children 959c4ac544a1
line wrap: on
line diff
--- a/examples/menu/main.c	Sun Feb 01 16:28:28 2009 +0800
+++ b/examples/menu/main.c	Wed Feb 04 01:23:45 2009 +0800
@@ -1,6 +1,14 @@
 /*! \file
  *
  *  This is the demo program for the animated menu. We will use to test the MBAF API.
+ *  We need to have group item1-item9 in the SVG file. Initially, we will show
+ *  item1-item8 only. When a up/down key is pressed, we will draw the next item in item9 and 
+ *  add two words to move item1-item9 smoothly. The first word move items to the 3/4 position 
+ *  fastly. The second will move it from 3/4 to the final position slowly to make retard effect.
+ *
+ *  If we press another key before the second words finish, we will delete the word and replace 
+ *  it with a word to move it fastly to the final position and then we will repeat the procedure
+ *  to add another two words to move it to the next position.
  */
 #include <stdio.h>
 #include <mb.h>
@@ -29,6 +37,10 @@
 	"Item 18",
 };
 
+int menus_y[10];
+int items[10];
+
+
 typedef struct {
     int top;
     int cur;
@@ -42,38 +54,194 @@
     int i;
     MyAppData *data = MBAPP_DATA(myApp,MyAppData);
     mb_sprite_t *sprite=myApp->rootsprite;
+    coord_t *textgroup;
     shape_t *text;
     coord_t *group;
     coord_t *lightbar;
     char name[255];
+    int tmp;
+    mb_timeval_t start, playing, now;
+    mb_progm_t *progm;
+    mb_word_t *word;
 
+
+    // fill new item
     for(i=0;i<8;i++) {
-	if (i+data->top > data->max) break;
-        snprintf(name,sizeof(name),"item%dtext", i+1);
-        text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name);
-	if (text == NULL) {
-		printf("Can not find object %s\n",name);
-		continue;
-	}
-	sh_text_set_text(text,menus[i+data->top]);
-        rdman_shape_changed(MBAPP_RDMAN(myApp), text);
+        snprintf(name, sizeof(name),"item%dtext", items[i]);
+        text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
+        sh_text_set_text(text, menus[data->top+i]);
+	rdman_shape_changed(MBAPP_RDMAN(myApp),text);
     }
-    for(;i<8;i++) {
-        snprintf(name,sizeof(name),"item%dtext", i+1);
-        text = (shape_t *) MB_SPRITE_GET_OBJ(sprite, name);
-	if (text == NULL) {
-		printf("Can not find object %s\n",name);
-		continue;
-	}
-	sh_text_set_text(text,"");
-        rdman_shape_changed(MBAPP_RDMAN(myApp), text);
-    }
+
+
+    snprintf(name, sizeof(name),"item%d", items[i]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    coord_hide(textgroup);
+    rdman_coord_changed(MBAPP_RDMAN(myApp),textgroup);
+
     lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
     snprintf(name,sizeof(name),"item%d", data->cur+1);
     group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
     coord_x(lightbar) = coord_x(group);
     coord_y(lightbar) = coord_y(group);
-    rdman_coord_changed(MBAPP_RDMAN(myApp), lightbar);
+    rdman_redraw_changed(MBAPP_RDMAN(myApp));
+}
+
+static void fillMenuContentUp()
+{
+    int i;
+    MyAppData *data = MBAPP_DATA(myApp,MyAppData);
+    mb_sprite_t *sprite=myApp->rootsprite;
+    coord_t *textgroup;
+    shape_t *text;
+    coord_t *group;
+    coord_t *lightbar;
+    char name[255];
+    int tmp;
+    mb_timeval_t start, playing, now;
+    mb_progm_t *progm;
+    mb_word_t *word;
+
+
+    // fill new item
+    snprintf(name, sizeof(name),"item%dtext", items[8]);
+    text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    sh_text_set_text(text, menus[data->top]);
+
+    progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
+    MB_TIMEVAL_SET(&start, 0, 0);
+    MB_TIMEVAL_SET(&playing, 0, 300000);
+    word = mb_progm_next_word(progm, &start, &playing);
+    get_now(&now);
+
+    for(i=0;i<7;i++) {
+	//shift to the next item
+    	snprintf(name, sizeof(name),"item%d", items[i]);
+        textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+	mb_shift_new(0,menus_y[i+1]-coord_y(textgroup), textgroup,word);
+    }
+    // fade out the item[7]
+    snprintf(name, sizeof(name),"item%d", items[7]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    mb_shift_new(0,100, textgroup,word);
+    mb_visibility_new(VIS_HIDDEN, textgroup,word);
+
+    // fade in the item[8]
+    snprintf(name, sizeof(name),"item%d", items[8]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    snprintf(name,sizeof(name),"item%d", items[0]);
+    group = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    coord_y(textgroup) = menus_y[0]-100;
+    coord_show(textgroup);
+    mb_shift_new(0,100, textgroup,word);
+
+    lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
+    mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
+    
+    MB_TIMEVAL_SET(&start, 0, 300000);
+    MB_TIMEVAL_SET(&playing, 0, 0);
+    word = mb_progm_next_word(progm, &start, &playing);
+    snprintf(name, sizeof(name),"item%d", items[8]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    mb_visibility_new(VIS_VISIBLE, textgroup,word);
+
+    mb_progm_free_completed(progm);
+    mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
+    rdman_redraw_changed(MBAPP_RDMAN(myApp));
+    tmp = items[8];
+    for(i=8;i>0;i--) {
+	items[i] = items[i-1];
+    }
+    items[0] = tmp;
+}
+static void fillMenuContentDown()
+{
+    int i;
+    MyAppData *data = MBAPP_DATA(myApp,MyAppData);
+    mb_sprite_t *sprite=myApp->rootsprite;
+    coord_t *textgroup;
+    shape_t *text;
+    coord_t *group;
+    coord_t *lightbar;
+    char name[255];
+    int tmp;
+    mb_timeval_t start, playing, now;
+    mb_progm_t *progm;
+    mb_word_t *word;
+
+
+    // fill new item
+    snprintf(name, sizeof(name),"item%dtext", items[8]);
+    text = (shape_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    sh_text_set_text(text, menus[data->top+7]);
+
+    progm = mb_progm_new(2, MBAPP_RDMAN(myApp));
+    MB_TIMEVAL_SET(&start, 0, 0);
+    MB_TIMEVAL_SET(&playing, 0, 300000);
+    word = mb_progm_next_word(progm, &start, &playing);
+    get_now(&now);
+
+    for(i=1;i<8;i++) {
+	//shift to the next item
+    	snprintf(name, sizeof(name),"item%d", items[i]);
+        textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+	mb_shift_new(0,menus_y[i-1]-coord_y(textgroup), textgroup,word);
+    }
+    // fade out the item[0]
+    snprintf(name, sizeof(name),"item%d", items[0]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    mb_shift_new(0,-100, textgroup,word);
+
+    // fade in the item[8]
+    snprintf(name, sizeof(name),"item%d", items[8]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    coord_y(textgroup) = menus_y[7]+100;
+    coord_show(textgroup);
+    mb_shift_new(0,-100, textgroup,word);
+
+    lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
+    mb_shift_new(0,menus_y[data->cur]-coord_y(lightbar),lightbar,word);
+
+    MB_TIMEVAL_SET(&start, 0, 300001);
+    MB_TIMEVAL_SET(&playing, 0, 0);
+    word = mb_progm_next_word(progm, &start, &playing);
+    snprintf(name, sizeof(name),"item%d", items[0]);
+    textgroup = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+    mb_visibility_new(VIS_VISIBLE, textgroup,word);
+
+    mb_progm_free_completed(progm);
+    mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
+    rdman_redraw_changed(MBAPP_RDMAN(myApp));
+    tmp = items[0];
+    for(i=0;i<8;i++) {
+	items[i] = items[i+1];
+    }
+    items[8] = tmp;
+}
+
+MoveLightBar()
+{
+    mb_sprite_t *sprite=myApp->rootsprite;
+    MyAppData *data = MBAPP_DATA(myApp,MyAppData);
+    mb_timeval_t start, playing, now;
+    mb_progm_t *progm;
+    mb_word_t *word;
+    coord_t *group;
+    coord_t *lightbar;
+    char name[255];
+
+    progm = mb_progm_new(1, MBAPP_RDMAN(myApp));
+    MB_TIMEVAL_SET(&start, 0, 0);
+    MB_TIMEVAL_SET(&playing, 0, 200000);
+    word = mb_progm_next_word(progm, &start, &playing);
+    get_now(&now);
+
+    lightbar = (coord_t *) MB_SPRITE_GET_OBJ(sprite, "lightbar");
+    snprintf(name,sizeof(name),"item%d", items[data->cur]);
+    group = (coord_t *) MB_SPRITE_GET_OBJ(sprite, name);
+    mb_shift_new(coord_x(group)-coord_x(lightbar),coord_y(group)-coord_y(lightbar),lightbar,word);
+    mb_progm_free_completed(progm);
+    mb_progm_start(progm, X_MB_tman(MBAPP_RDMAN(myApp)->rt), &now);
     rdman_redraw_changed(MBAPP_RDMAN(myApp));
 }
 
@@ -81,38 +249,42 @@
 {
     MyAppData *data = MBAPP_DATA(myApp,MyAppData);
 
-    if (data->cur > 5)
+    if (data->cur > 5) {
 	data->cur--;
-    else {
+        MoveLightBar();
+    } else {
         if (data->top > 0) {
 	    data->top--;
+            fillMenuContentUp();
         } else {
 	    if (data->cur == 0) 
 	        return;
 	    data->cur--;
+            MoveLightBar();
 	}
     }
-    fillMenuContent();
 }
 void menu_down()
 {
     MyAppData *data = MBAPP_DATA(myApp,MyAppData);
 
-    if (data->cur < 5) {
-	if (data->top+data->cur <= data->max)
+    if (data->cur < 4) {
+	if (data->top+data->cur <= data->max) {
 	    data->cur++;
+	    MoveLightBar();
+	}
     } else  {
         if ((data->top+8) < data->max) {
 	    data->top++;
+            fillMenuContentDown();
         } else {
-   	    if (data->cur+data->top < data->max-1) 
+   	    if (data->cur+data->top < data->max-1) {
 	        data->cur++;
-	    else
+	        MoveLightBar();
+	    } else
 	        return;
 	}
-	printf("top=%d\n",data->top);
     }
-    fillMenuContent();
 }
 void menu_select()
 {
@@ -154,10 +326,20 @@
 {
     MyAppData *data = MBAPP_DATA(myApp,MyAppData);
     subject_t *key = MBAPP_keySubject(myApp);
+    char name[255];
+    coord_t *l;
+    int i;
+    mb_sprite_t *sprite=myApp->rootsprite;
     
     data->top = 0;
     data->cur = 0;
     data->max = sizeof(menus)/sizeof(int)-1;
+    for(i=0;i<9;i++) {
+        items[i] = i+1;
+	snprintf(name,255,"item%d", i+1);
+	l = (coord_t *) MB_SPRITE_GET_OBJ(sprite,name);
+	menus_y[i] = coord_y(l);
+    }
 
     fillMenuContent();
     subject_add_observer(key, keyHandler,NULL);