Replay pause/end screen: removed info and reordered buttons.
[neverball] / ball / st_demo.c
index 68287de..d612ffe 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  * General Public License for more details.
  */
 
+#include <string.h>
+
 #include "gui.h"
 #include "hud.h"
 #include "set.h"
 #include "game.h"
 #include "demo.h"
-#include "level.h"
+#include "levels.h"
 #include "audio.h"
 #include "solid.h"
 #include "config.h"
+#include "st_shared.h"
+#include "util.h"
 
 #include "st_demo.h"
 #include "st_title.h"
 
 /*---------------------------------------------------------------------------*/
 
-#define DEMO_BACK -1
-#define DEMO_NEXT -2
-#define DEMO_PREV -3
-
 #define DEMO_LINE 4
 #define DEMO_STEP 8
 
@@ -48,38 +48,56 @@ static int demo_action(int i)
 
     switch (i)
     {
-    case DEMO_BACK:
+    case GUI_BACK:
         return goto_state(&st_title);
 
-    case DEMO_NEXT:
+    case GUI_NEXT:
         first += DEMO_STEP;
         return goto_state(&st_demo);
         break;
 
-    case DEMO_PREV:
+    case GUI_PREV:
         first -= DEMO_STEP;
         return goto_state(&st_demo);
         break;
 
+    case GUI_NULL:
+        return 1;
+        break;
+
     default:
-        if (level_replay(demo_name(i)))
+        if (level_replay(demo_get(i)->filename))
+            demo_play_goto(0);
             return goto_state(&st_demo_play);
     }
     return 1;
 }
 
+/*---------------------------------------------------------------------------*/
+
 static void demo_replay(int id, int i)
 {
     int w = config_get_d(CONFIG_WIDTH);
     int h = config_get_d(CONFIG_HEIGHT);
     int jd;
 
+    char nam[MAXNAM + 3];
+
     if ((jd = gui_vstack(id)))
     {
         gui_space(jd);
+        gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
 
-        gui_image(jd, demo_shot(i), w / 6, h / 6);
-        gui_state(jd, demo_name(i), GUI_SML, i, 0);
+        nam[MAXNAM - 1] = '\0';
+        strncpy(nam, demo_get(i)->name, MAXNAM);
+        if (nam[MAXNAM - 1] != '\0')
+        {
+            nam[MAXNAM - 2] = '.';
+            nam[MAXNAM - 1] = '.';
+            nam[MAXNAM + 0] = '.';
+            nam[MAXNAM + 1] = '\0';
+        }
+        gui_state(jd, nam, GUI_SML, i, 0);
 
         gui_active(jd, i, 0);
     }
@@ -90,53 +108,146 @@ static int time_id;
 static int coin_id;
 static int date_id;
 static int mode_id;
+static int status_id;
 static int player_id;
 
-static void demo_status(int i)
+/* Create a layout for some demo info.  If d is NULL, try to reserve enough
+ * space. */
+
+static int gui_demo_status(int id, const struct demo *d)
 {
-    gui_set_label(name_id,   demo_name(i));
-    gui_set_label(date_id,   demo_str_date(i));
-    gui_set_label(player_id, demo_player(i));
-    gui_set_label(mode_id,   mode_to_str(demo_mode(i)));
-    gui_set_count(coin_id,   demo_coins(i));
-    gui_set_clock(time_id,   demo_clock(i));
+    char noname[MAXNAM];
+    const char *mode, *status;
+    int i, j, k;
+    int jd, kd, ld, md;
+
+    if (d == NULL)
+    {
+        /* Build a long name */
+        memset(noname, 'M', MAXNAM - 1);
+        noname[MAXNAM - 1] = '\0';
+
+        /* Get a long mode */
+        mode = mode_to_str(0, 0);
+        j = strlen(mode);
+
+        for (i = 1; i <= MODE_COUNT; i++)
+        {
+            k = strlen(mode_to_str(i, 0));
+
+            if (k > j)
+            {
+                j = k;
+                mode = mode_to_str(i, 0);
+            }
+        }
+
+        /* Get a long status */
+        status = status_to_str(0);
+        j = strlen(status);
+        for (i = 1; i <= GAME_FALL; i++)
+        {
+            k = strlen(status_to_str(i));
+            if (k > j)
+            {
+                j = k;
+                status = status_to_str(i);
+            }
+        }
+    }
+    else
+    {
+        mode = mode_to_str(d->mode, 0);
+        status = status_to_str(d->status);
+    }
+
+    if ((jd = gui_hstack(id)))
+    {
+        if ((kd = gui_vstack(jd)))
+        {
+            if ((ld = gui_harray(kd)))
+            {
+                if ((md = gui_vstack(ld)))
+                {
+                    player_id = gui_label(md, (d ? d->player : noname),
+                                          GUI_SML, GUI_RGT, 0, 0);
+                    coin_id = gui_count(md, (d ? d->coins : 100),
+                                        GUI_SML, GUI_RGT);
+                    status_id = gui_label(md, status, GUI_SML, GUI_RGT,
+                                         gui_red, gui_red);
+                }
+                if ((md = gui_vstack(ld)))
+                {
+                    gui_label(md, _("Player"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("State"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                }
+                if ((md = gui_vstack(ld)))
+                {
+                    name_id = gui_label(md, (d ? d->name : noname),
+                                        GUI_SML, GUI_RGT, 0, 0);
+                    time_id = gui_clock(md, (d ? d->timer : 35000),
+                                        GUI_SML, GUI_RGT);
+                    mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
+                }
+            }
+            date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
+                                GUI_SML, GUI_RGT, 0, 0);
+        }
+        if ((kd = gui_vstack(jd)))
+        {
+            gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+        }
+        if (d && d->status == GAME_GOAL)
+            gui_set_color(status_id, gui_grn, gui_grn);
+    }
+    return jd;
 }
 
+static void gui_demo_update_status(int i)
+{
+    const struct demo *d = demo_get(i);
+
+    gui_set_label(name_id,   d->name);
+    gui_set_label(date_id,   date_to_str(d->date));
+    gui_set_label(player_id, d->player);
+    gui_set_label(mode_id,   mode_to_str(d->mode, 0));
+
+    if (d->status == GAME_GOAL)
+        gui_set_color(status_id, gui_grn, gui_grn);
+    else
+        gui_set_color(status_id, gui_red, gui_red);
+
+    gui_set_label(status_id, status_to_str(d->status));
+    gui_set_count(coin_id, d->coins);
+    gui_set_clock(time_id, d->timer);
+}
+
+/*---------------------------------------------------------------------------*/
+
 static int demo_enter(void)
 {
     int i, j;
-    int id, jd, kd, ld;
+    int id, jd, kd;
 
-    total = demo_scan();
+    id = gui_vstack(0);
 
-    if ((id = gui_vstack(0)))
+    if ((total = demo_scan()))
     {
-        if (total == 0)
-        {
-           gui_label(id, _("No Replay"), GUI_MED, GUI_ALL, 0,0);
-           gui_filler(id);
-           gui_multi(id, _("You can save replay of you games.\\Currently, there is no replay saved."), GUI_SML, GUI_ALL, gui_wht, gui_wht);
-           gui_filler(id);
-           gui_start(id, _("Back"), GUI_SML, DEMO_BACK, 0);
-       }
-    else
-    {
-       if ((jd = gui_hstack(id)))
+        if ((jd = gui_hstack(id)))
         {
 
-            ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
+            gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
             gui_filler(jd);
-
-           
-           
-           if (first + DEMO_STEP < total)
-               gui_state(jd, _("Next"), GUI_SML, DEMO_NEXT, 0);
-
-           if (first > 0)
-                gui_state(jd, _("Prev"), GUI_SML, DEMO_PREV, 0);
-           
-           gui_start(jd, _("Back"), GUI_SML, DEMO_BACK, 0);
+            gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
         }
+
         if ((jd = gui_varray(id)))
             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
                 if ((kd = gui_harray(jd)))
@@ -147,37 +258,14 @@ static int demo_enter(void)
                         else
                             gui_space(kd);
                 }
-       gui_filler(id);
-       if ((jd = gui_hstack(id)))
-       {
-           if((kd = gui_vstack(jd)))
-           {
-               if ((ld = gui_harray(kd)))
-               {
-                   coin_id = gui_count(ld, 100,          GUI_SML, GUI_RGT);
-                   gui_label(ld, _("Coins"),             GUI_SML, GUI_LFT, gui_wht, gui_wht);
-                   time_id = gui_clock(ld, 35000,        GUI_SML, GUI_RGT);
-                   gui_label(ld, _("Time"),              GUI_SML, GUI_LFT, gui_wht, gui_wht);
-                   name_id = gui_label(ld, demo_name(0), GUI_SML, GUI_RGT, 0, 0);
-               }
-               if ((ld = gui_harray(kd)))
-               {
-                   mode_id = gui_label(ld, "..............", GUI_SML, GUI_RGT, 0, 0);
-                   gui_label(ld, _("Mode"),                  GUI_SML, GUI_LFT, gui_wht, gui_wht);
-                   player_id = gui_label(ld, demo_player(0), GUI_SML, GUI_RGT, 0, 0);
-               }
-               date_id = gui_label(kd, demo_str_date(0),     GUI_SML, GUI_RGT, 0, 0);
-           }
-           if((kd = gui_vstack(jd)))
-           {
-               gui_label(kd, _("Name"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-               gui_label(kd, _("Player"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-               gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-           }
-       }
-        demo_status(0);
+        gui_filler(id);
+        gui_demo_status(id, NULL);
+        gui_layout(id, 0, 0);
+        gui_demo_update_status(0);
     }
-
+    else
+    {
+        gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
         gui_layout(id, 0, 0);
     }
 
@@ -186,46 +274,31 @@ static int demo_enter(void)
     return id;
 }
 
-static void demo_leave(int id)
-{
-    gui_delete(id);
-}
-
-static void demo_paint(int id, float st)
-{
-    game_draw(0, st);
-    config_pop_matrix();
-    gui_paint(id);
-}
-
 static void demo_timer(int id, float dt)
 {
+    if (total == 0 && time_state() > 4.0f)
+        goto_state(&st_title);
+
     gui_timer(id, dt);
     audio_timer(dt);
 }
 
 static void demo_point(int id, int x, int y, int dx, int dy)
 {
-    int jd = gui_point(id, x, y);
+    int jd = shared_point_basic(id, x, y);
     int i  = gui_token(jd);
-    gui_pulse(jd, 1.2f);
-    if (jd && i>=0)
-       demo_status(i);
+
+    if (jd && i >= 0)
+        gui_demo_update_status(i);
 }
 
 static void demo_stick(int id, int a, int v)
 {
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
-        gui_pulse(gui_stick(id, v, 0), 1.2f);
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
-        gui_pulse(gui_stick(id, 0, v), 1.2f);
-}
+    int jd = shared_stick_basic(id, a, v);
+    int i  = gui_token(jd);
 
-static int demo_click(int b, int d)
-{
-    if (b <= 0 && d == 1)
-        return demo_action(gui_token(gui_click()));
-    return 1;
+    if (jd && i >= 0)
+        gui_demo_update_status(i);
 }
 
 static int demo_buttn(int b, int d)
@@ -233,21 +306,39 @@ static int demo_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return demo_click(0, 1);
+            return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-            return demo_action(DEMO_BACK);
+            return demo_action(GUI_BACK);
     }
     return 1;
 }
 
 /*---------------------------------------------------------------------------*/
 
+/* Play demo from command line. */
+static int simple_play;
+
+static int demo_paused;
+
+void demo_play_goto(int simple)
+{
+    simple_play = simple;
+}
+
 static int demo_play_enter(void)
 {
     int id;
 
-    if ((id = gui_label(0, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
+    if (demo_paused)
     {
+        demo_paused = 0;
+        audio_music_fade_in(0.5f);
+        return 0;
+    }
+
+    if ((id = gui_vstack(0)))
+    {
+        gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
         gui_layout(id, 0, 0);
         gui_pulse(id, 1.2f);
     }
@@ -255,16 +346,13 @@ static int demo_play_enter(void)
     global_time = -1.f;
     replay_time =  0.f;
 
+    hud_update(0);
+
     game_set_fly(0.f);
 
     return id;
 }
 
-static void demo_play_leave(int id)
-{
-    gui_delete(id);
-}
-
 static void demo_play_paint(int id, float st)
 {
     game_draw(0, st);
@@ -292,40 +380,71 @@ static void demo_play_timer(int id, float dt)
         {
             replay_time += t;
         }
-        else 
+        else
         {
+            demo_paused = 0;
             goto_state(&st_demo_end);
             break;
         }
 }
 
+static int demo_play_keybd(int c, int d)
+{
+    if (d)
+    {
+        if (config_tst_d(CONFIG_KEY_PAUSE, c))
+        {
+            demo_paused = 1;
+            return goto_state(&st_demo_end);
+        }
+    }
+    return 1;
+}
+
 static int demo_play_buttn(int b, int d)
 {
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
+        {
+            if (! (SDL_GetModState() & (KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_META)))
+                demo_paused = 1;
             return goto_state(&st_demo_end);
+        }
     }
     return 1;
 }
 
 /*---------------------------------------------------------------------------*/
 
-#define DEMO_KEEP 0
-#define DEMO_DEL  1
+#define DEMO_KEEP    0
+#define DEMO_DEL     1
+#define DEMO_QUIT    2
+#define DEMO_REPLAY  3
+#define DEMO_CONTINUE  4
 
 static int demo_end_action(int i)
 {
     audio_play(AUD_MENU, 1.0f);
 
-    if (i == DEMO_DEL)
-        return goto_state(&st_demo_del);
-
-    else
+    switch (i)
     {
+    case DEMO_DEL:
+        return goto_state(&st_demo_del);
+    case DEMO_KEEP:
         demo_replay_stop(0);
         return goto_state(&st_demo);
+    case DEMO_QUIT:
+        demo_replay_stop(0);
+        return 0;
+    case DEMO_REPLAY:
+        demo_replay_stop(0);
+        level_replay(curr_demo_replay()->filename);
+        return goto_state(&st_demo_play);
+    case DEMO_CONTINUE:
+        return goto_state(&st_demo_play);
     }
+    return 1;
 }
 
 static int demo_end_enter(void)
@@ -334,61 +453,61 @@ static int demo_end_enter(void)
 
     if ((id = gui_vstack(0)))
     {
-        kd = gui_label(id, _("Replay Ends"), GUI_MED, GUI_ALL, gui_gry, gui_red);
+        if (demo_paused)
+            kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
+                           gui_gry, gui_red);
+        else
+            kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
+                           gui_gry, gui_red);
 
         if ((jd = gui_harray(id)))
         {
-            gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL,  0);
-            gui_start(jd, _("Keep"),   GUI_SML, DEMO_KEEP, 1);
+            int start_id = 0;
+
+            if (simple_play)
+            {
+                start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
+            }
+            else
+            {
+                start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
+                gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
+            }
+
+            if (demo_paused)
+            {
+                gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
+                gui_toggle(start_id);
+            }
+            else
+                gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
         }
 
         gui_pulse(kd, 1.2f);
         gui_layout(id, 0, 0);
     }
-    audio_music_fade_out(2.0f);
 
-    return id;
-}
+    audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
 
-static void demo_end_leave(int id)
-{
-    gui_delete(id);
+    return id;
 }
 
 static void demo_end_paint(int id, float st)
 {
     game_draw(0, st);
     gui_paint(id);
-}
-
-static void demo_end_timer(int id, float dt)
-{
-    float g[3] = { 0.0f, -9.8f, 0.0f };
-
-    if (time_state() < 2.f)
-       game_step(g, dt, 0);
-               
-    gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void demo_end_point(int id, int x, int y, int dx, int dy)
-{
-    gui_pulse(gui_point(id, x, y), 1.2f);
-}
 
-static void demo_end_stick(int id, int a, int v)
-{
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
-        gui_pulse(gui_stick(id, v, 0), 1.2f);
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
-        gui_pulse(gui_stick(id, 0, v), 1.2f);
+    if (demo_paused)
+        hud_paint();
 }
 
-static int demo_end_click(int b, int d)
+static int demo_end_keybd(int c, int d)
 {
-    if (d && b < 0)
-        return demo_end_action(gui_token(gui_click()));
+    if (d)
+    {
+        if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
+            return demo_end_action(DEMO_CONTINUE);
+    }
     return 1;
 }
 
@@ -399,7 +518,9 @@ static int demo_end_buttn(int b, int d)
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
             return demo_end_action(gui_token(gui_click()));
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-            return demo_end_action(DEMO_KEEP);
+            return demo_end_action(demo_paused
+                                   ? DEMO_CONTINUE
+                                   : (simple_play ? DEMO_QUIT : DEMO_KEEP));
     }
     return 1;
 }
@@ -424,8 +545,8 @@ static int demo_del_enter(void)
 
         if ((jd = gui_harray(id)))
         {
-            gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
+            gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
         }
 
         gui_pulse(kd, 1.2f);
@@ -436,43 +557,6 @@ static int demo_del_enter(void)
     return id;
 }
 
-static void demo_del_leave(int id)
-{
-    gui_delete(id);
-}
-
-static void demo_del_paint(int id, float st)
-{
-    game_draw(0, st);
-    gui_paint(id);
-}
-
-static void demo_del_timer(int id, float dt)
-{
-    gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void demo_del_point(int id, int x, int y, int dx, int dy)
-{
-    gui_pulse(gui_point(id, x, y), 1.2f);
-}
-
-static void demo_del_stick(int id, int a, int v)
-{
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
-        gui_pulse(gui_stick(id, v, 0), 1.2f);
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
-        gui_pulse(gui_stick(id, 0, v), 1.2f);
-}
-
-static int demo_del_click(int b, int d)
-{
-    if (d && b < 0)
-        return demo_del_action(gui_token(gui_click()));
-    return 1;
-}
-
 static int demo_del_buttn(int b, int d)
 {
     if (d)
@@ -489,51 +573,51 @@ static int demo_del_buttn(int b, int d)
 
 struct state st_demo = {
     demo_enter,
-    demo_leave,
-    demo_paint,
+    shared_leave,
+    shared_paint,
     demo_timer,
     demo_point,
     demo_stick,
-    demo_click,
+    shared_click,
     NULL,
     demo_buttn,
-    0
+    1, 0
 };
 
 struct state st_demo_play = {
     demo_play_enter,
-    demo_play_leave,
+    shared_leave,
     demo_play_paint,
     demo_play_timer,
     NULL,
     NULL,
     NULL,
-    NULL,
+    demo_play_keybd,
     demo_play_buttn,
-    0
+    1, 0
 };
 
 struct state st_demo_end = {
     demo_end_enter,
-    demo_end_leave,
+    shared_leave,
     demo_end_paint,
-    demo_end_timer,
-    demo_end_point,
-    demo_end_stick,
-    demo_end_click,
-    NULL,
+    shared_timer,
+    shared_point,
+    shared_stick,
+    shared_click,
+    demo_end_keybd,
     demo_end_buttn,
     1, 0
 };
 
 struct state st_demo_del = {
     demo_del_enter,
-    demo_del_leave,
-    demo_del_paint,
-    demo_del_timer,
-    demo_del_point,
-    demo_del_stick,
-    demo_del_click,
+    shared_leave,
+    shared_paint,
+    shared_timer,
+    shared_point,
+    shared_stick,
+    shared_click,
     NULL,
     demo_del_buttn,
     1, 0