set shots from the first level, not the second
[neverball] / ball / st_set.c
index e2177c9..5816d85 100644 (file)
 
 #include "gui.h"
 #include "set.h"
+#include "levels.h"
 #include "game.h"
 #include "audio.h"
 #include "config.h"
+#include "util.h"
+#include "st_shared.h"
 
 #include "st_set.h"
 #include "st_title.h"
@@ -27,6 +30,7 @@
 #define SET_BACK -1
 #define SET_PREV -2
 #define SET_NEXT -3
+#define SET_NULL -4
 
 #define SET_GROUP 5 /* number of sets in one screen */
 
@@ -39,16 +43,19 @@ static int set_action(int i)
 
     switch(i)
     {
-    case SET_BACK:
+    case GUI_BACK:
         return goto_state(&st_title);
 
-    case SET_PREV:
+    case GUI_PREV:
        config_set_d(CONFIG_LAST_SET, ((config_get_d(CONFIG_LAST_SET)/SET_GROUP)-1)*SET_GROUP);
        return goto_state(&st_set);
     
-    case SET_NEXT:
+    case GUI_NEXT:
        config_set_d(CONFIG_LAST_SET, ((config_get_d(CONFIG_LAST_SET)/SET_GROUP)+1)*SET_GROUP);
        return goto_state(&st_set);
+
+    case GUI_NULL:
+       return 1;
     
     default:
        if (set_exists(i))
@@ -61,6 +68,21 @@ static int set_action(int i)
     return 1;
 }
 
+static void gui_set(int id, int i)
+{
+    const struct set *s = get_set(i);
+    int jd;
+    
+    if (set_completed(s)) 
+       jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_yel, gui_wht);
+    else if (set_unlocked(s)) 
+       jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_grn, gui_wht);
+    else
+       jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_wht, gui_wht);
+    
+    gui_active(jd, i, 0);
+}
+
 static int set_enter(void)
 {
     int w = config_get_d(CONFIG_WIDTH);
@@ -69,9 +91,18 @@ static int set_enter(void)
     int b = last_set / SET_GROUP;
     int i;
 
-    int id, jd, kd, ld;
+    int id, jd, kd;
+
 
     set_init();
+    
+    /* Reset last set if it do not exists */
+    if (!set_exists(last_set))
+    {
+       b = 0;
+       last_set = 0;
+       config_set_d(CONFIG_LAST_SET, 0);
+    }
 
     audio_music_fade_to(0.5f, "bgm/inter.ogg");
     audio_play(AUD_START, 1.f);
@@ -82,35 +113,19 @@ static int set_enter(void)
         {
             gui_label(jd, _("Level Set"), GUI_SML, GUI_ALL, gui_yel, gui_red);
             gui_filler(jd);
-            gui_state(jd, _("Back"),  GUI_SML, SET_BACK, 0);
+           gui_back_prev_next(jd, b>0, set_exists((b+1)*SET_GROUP));
         }
 
         if ((jd = gui_harray(id)))
         {
-            shot_id = gui_image(jd, set_shot(last_set), 7 * w / 16, 7 * h / 16);
+            shot_id = gui_image(jd, get_set(last_set)->shot, 7 * w / 16, 7 * h / 16);
 
             if ((kd = gui_varray(jd)))
            {
                /* Display levels */
                for(i=b*SET_GROUP; i<(b+1)*SET_GROUP && set_exists(i); i++)
-               {
-                   if(last_set == i)
-                       gui_start(kd, _(set_name(i)), GUI_SML, i, 0);
-                   else
-                       gui_state(kd, _(set_name(i)), GUI_SML, i, 0);
-               }
+                   gui_set(kd, i);
                
-               /* Display Prev/Next buttons */ 
-               ld = gui_harray(kd);
-               if (set_exists(i))
-                   gui_state(ld, _("Next"), GUI_SML, SET_NEXT, 0);
-               else
-                   gui_label(ld, _("Next"), GUI_SML, GUI_ALL, gui_gry, gui_gry);
-               if (b>0)
-                   gui_state(ld, _("Prev"), GUI_SML, SET_PREV, 0);
-               else
-                   gui_label(ld, _("Prev"), GUI_SML, GUI_ALL, gui_gry, gui_gry);
-
                /* Display empty slots */
                for(; i<(b+1)*SET_GROUP; i++)
                    gui_filler(kd);
@@ -125,64 +140,26 @@ static int set_enter(void)
     return id;
 }
 
-static void set_leave(int id)
-{
-    gui_delete(id);
-}
-
-static void set_paint(int id, float st)
+static void set_over(int i)
 {
-    game_draw(0, st);
-    config_pop_matrix();
-    gui_paint(id);
-}
-
-static void set_timer(int id, float dt)
-{
-    gui_timer(id, dt);
-    audio_timer(dt);
+    gui_set_image(shot_id, get_set(i)->shot);
+    gui_set_multi(desc_id, _(get_set(i)->desc));
 }
 
 static void set_point(int id, int x, int y, int dx, int dy)
 {
-    int jd;
-
-    if ((jd = gui_point(id, x, y)))
-    {
-        int i = gui_token(jd);
-       
-       if (set_exists(i))
-       {
-            gui_set_image(shot_id, set_shot(i));
-           gui_set_multi(desc_id, _(set_desc(i)));
-           gui_pulse(jd, 1.2f);
-       }
-    }
+    int jd = shared_point_basic(id, x, y);
+    int i  = gui_token(jd);
+    if (jd && set_exists(i))
+       set_over(i);
 }
 
 static void set_stick(int id, int a, int v)
 {
-    int jd;
-
-    int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
-    int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
-
-    if ((jd = gui_stick(id, x, y)))
-    {
-        int i = gui_token(jd);
-
-       if (set_exists(i))
-       {
-            gui_set_image(shot_id, set_shot(i));
-           gui_set_multi(desc_id, _(set_desc(i)));
-           gui_pulse(jd, 1.2f);
-       }
-    }
-}
-
-static int set_click(int b, int d)
-{
-    return (b < 0 && d == 1) ? set_action(gui_token(gui_click())) : 1;
+    int jd = shared_stick_basic(id, a, v);
+    int i  = gui_token(jd);
+    if (jd && set_exists(i))
+       set_over(i);
 }
 
 static int set_buttn(int b, int d)
@@ -192,7 +169,7 @@ static int set_buttn(int b, int d)
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
             return set_action(gui_token(gui_click()));
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-            return goto_state(&st_title);
+            return set_action(GUI_BACK);
     }
     return 1;
 }
@@ -201,12 +178,12 @@ static int set_buttn(int b, int d)
 
 struct state st_set = {
     set_enter,
-    set_leave,
-    set_paint,
-    set_timer,
+    shared_leave,
+    shared_paint,
+    shared_timer,
     set_point,
     set_stick,
-    set_click,
+    shared_click,
     NULL,
     set_buttn,
     1, 0