Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[neverball] / ball / st_save.c
index f7aa379..ea4abe3 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  */
 
 #include <string.h>
+#include <ctype.h>
 
 #include "gui.h"
 #include "game.h"
 #include "util.h"
-#include "demo.h"
-#include "set.h"
-#include "level.h"
 #include "audio.h"
 #include "config.h"
+#include "demo.h"
+#include "levels.h"
+#include "text.h"
 
+#include "st_shared.h"
 #include "st_save.h"
-#include "st_done.h"
-#include "st_level.h"
-#include "st_title.h"
 
 extern struct state st_save;
 extern struct state st_clobber;
 
+static char filename[MAXNAM];
+
 /*---------------------------------------------------------------------------*/
 
-static struct state * next_state;
+static struct state *ok_state;
+static struct state *cancel_state;
 
-int goto_save(struct state * nextstate)
+int goto_save(struct state *ok, struct state *cancel)
 {
-    next_state = nextstate;
+    demo_unique(filename);
+
+    ok_state     = ok;
+    cancel_state = cancel;
+
     return goto_state(&st_save);
 }
 
 /*---------------------------------------------------------------------------*/
 
-#define SAVE_SAVE   2
-#define SAVE_CANCEL 3
+static int file_id;
 
-static int  file_id;
-static char filename[MAXNAM];
+#define SAVE_SAVE   1
+#define SAVE_CANCEL 2
 
 static int save_action(int i)
 {
-    size_t l = strlen(filename);
+    char *n;
 
     audio_play(AUD_MENU, 1.0f);
 
     switch (i)
     {
     case SAVE_SAVE:
-       if (strcmp(filename, "") == 0)
-           return 1;
-        if (demo_exists(filename))
+        n = text_to_locale(filename);
+
+        if (strlen(n) == 0)
+            return 1;
+
+        if (demo_exists(n))
             return goto_state(&st_clobber);
         else
         {
-           demo_play_save(filename);
-           return goto_state(next_state);
+            demo_rename(n);
+            return goto_state(ok_state);
         }
 
     case SAVE_CANCEL:
-       return goto_state(next_state);
+        return goto_state(cancel_state);
 
     case GUI_CL:
         gui_keyboard_lock();
         break;
 
     case GUI_BS:
-        if (l > 0)
-        {
-            filename[l - 1] = 0;
+        if (text_del_char(filename))
             gui_set_label(file_id, filename);
-        }
         break;
 
     default:
-        if (l < MAXNAM - 1)
-        {
-            filename[l + 0] = gui_keyboard_char((char) i);
-            filename[l + 1] = 0;
+        if (text_add_char(i, filename, MAXNAM, 17))
             gui_set_label(file_id, filename);
-        }
     }
     return 1;
 }
 
+static int enter_id;
+
 static int save_enter(void)
 {
-    int id, jd, kd, ld;
-
-    demo_unique(filename);
+    int id, jd;
 
     if ((id = gui_vstack(0)))
     {
-        gui_space(id);
-        if ((jd = gui_hstack(id)))
-        {
-            gui_filler(jd);
-           if ((kd = gui_vstack(jd)))
-           {
-               if ((ld = gui_hstack(kd)))
-               {
-                   gui_count(ld, curr_level(), GUI_LRG, GUI_NE);
-                   gui_label(ld, _("Level "),  GUI_LRG, GUI_NW, 0, 0);
-               }
-               gui_label(kd, _(set_name(set_curr())),  GUI_SML, GUI_BOT, gui_wht, gui_wht);
-           }
-           gui_filler(jd);
-        }
-        gui_space(id);
+        gui_label(id, _("Replay Name"), GUI_MED, GUI_ALL, 0, 0);
 
         gui_space(id);
+
         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
+
         gui_space(id);
 
+        gui_keyboard(id);
+
         if ((jd = gui_harray(id)))
         {
-            gui_start(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
-            gui_start(jd, _("Save"),   GUI_SML, SAVE_SAVE,   0);
+            enter_id = gui_start(jd, _("Save"), GUI_SML, SAVE_SAVE, 0);
+            gui_state(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
         }
-        gui_keyboard(id);
-        
+
         gui_layout(id, 0, 0);
     }
 
+    SDL_EnableUNICODE(1);
+
     return id;
 }
 
 static void save_leave(int id)
 {
+    SDL_EnableUNICODE(0);
     gui_delete(id);
 }
 
-static void save_paint(int id, float st)
-{
-    game_draw(0, st);
-    gui_paint(id);
-}
-
-static void save_timer(int id, float dt)
+static int save_keybd(int c, int d)
 {
-    gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void save_point(int id, int x, int y, int dx, int dy)
-{
-    gui_pulse(gui_point(id, x, y), 1.2f);
-}
-
-static void save_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 (d)
+    {
+        gui_focus(enter_id);
 
-static int save_click(int b, int d)
-{
-    if (b <= 0 && d == 1)
-        return save_action(gui_token(gui_click()));
+        if (c == '\b' || c == 0x7F)
+            return save_action(GUI_BS);
+        if (c > ' ')
+            return save_action(c);
+    }
     return 1;
 }
 
@@ -178,9 +152,15 @@ static int save_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return save_click(0, 1);
+        {
+            int c = gui_token(gui_click());
+
+            /* Ugh.  This is such a hack. */
+
+            return save_action(isupper(c) ? gui_keyboard_char(c) : c);
+        }
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-           return save_action(SAVE_CANCEL);
+            return save_action(SAVE_CANCEL);
     }
     return 1;
 }
@@ -193,8 +173,8 @@ static int clobber_action(int i)
 
     if (i == SAVE_SAVE)
     {
-       demo_play_save(filename);
-       return goto_state(next_state);    
+        demo_rename(text_to_locale(filename));
+        return goto_state(ok_state);
     }
     return goto_state(&st_save);
 }
@@ -211,8 +191,8 @@ static int clobber_enter(void)
 
         if ((jd = gui_harray(id)))
         {
-            gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
+            gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
         }
 
         gui_pulse(kd, 1.2f);
@@ -222,43 +202,6 @@ static int clobber_enter(void)
     return id;
 }
 
-static void clobber_leave(int id)
-{
-    gui_delete(id);
-}
-
-static void clobber_paint(int id, float st)
-{
-    game_draw(0, st);
-    gui_paint(id);
-}
-
-static void clobber_timer(int id, float dt)
-{
-    gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void clobber_point(int id, int x, int y, int dx, int dy)
-{
-    gui_pulse(gui_point(id, x, y), 1.2f);
-}
-
-static void clobber_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 clobber_click(int b, int d)
-{
-    if (d && b < 0)
-        return clobber_action(gui_token(gui_click()));
-    return 1;
-}
-
 static int clobber_buttn(int b, int d)
 {
     if (d)
@@ -276,24 +219,26 @@ static int clobber_buttn(int b, int d)
 struct state st_save = {
     save_enter,
     save_leave,
-    save_paint,
-    save_timer,
-    save_point,
-    save_stick,
-    save_click,
-    NULL,
+    shared_paint,
+    shared_timer,
+    shared_point,
+    shared_stick,
+    shared_angle,
+    shared_click,
+    save_keybd,
     save_buttn,
     1, 0
 };
 
 struct state st_clobber = {
     clobber_enter,
-    clobber_leave,
-    clobber_paint,
-    clobber_timer,
-    clobber_point,
-    clobber_stick,
-    clobber_click,
+    shared_leave,
+    shared_paint,
+    shared_timer,
+    shared_point,
+    shared_stick,
+    shared_angle,
+    shared_click,
     NULL,
     clobber_buttn,
     1, 0