Added a tweak to place the GUI into "recently moved" mode upon level end. This will...
[neverball] / ball / st_name.c
index 2978210..7b57d73 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Copyright (C) 2003 Robert Kooima - 2006 Jean Privat
- * Part of the Neverball Project http://icculus.org/neverball/
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  * it under the  terms of the GNU General  Public License as published
 #include "audio.h"
 #include "config.h"
 #include "game.h"
-#include "st_shared.h"
+#include "text.h"
 
 #include "st_name.h"
+#include "st_shared.h"
 
 /*---------------------------------------------------------------------------*/
 
-extern struct state st_name;
-
-static struct state *ok_state, *cancel_state;
 static char player[MAXNAM];
 
-void name_default(void)
-{
-    char *login = getenv("LOGNAME");
-
-    if (login == NULL || login[0] == '\0')
-        login = _("Player");
+/*---------------------------------------------------------------------------*/
 
-    strncpy(player, login, MAXNAM);
-    player[MAXNAM-1] = '\0';
-    player[0] = toupper(player[0]);
-}
+static struct state *ok_state, *cancel_state;
 
 int goto_name(struct state *ok, struct state *cancel)
 {
     config_get_s(CONFIG_PLAYER, player, MAXNAM);
-    if (player[0] == '\0')
-        name_default();
 
     ok_state     = ok;
     cancel_state = cancel;
+
     return goto_state(&st_name);
 }
 
-#define NAME_BACK   2
-#define NAME_CANCEL 3
-#define NAME_OK     4
+/*---------------------------------------------------------------------------*/
+
+#define NAME_OK     1
+#define NAME_CANCEL 2
 
 static int name_id;
 
 static int name_action(int i)
 {
-    size_t l;
-
     audio_play(AUD_MENU, 1.0f);
 
-    l = strlen(player);
-
     switch (i)
     {
     case NAME_OK:
-        if (l == 0)
+        if (strlen(player) == 0)
            return 1;
+
         config_set_s(CONFIG_PLAYER, player);
+
         return goto_state(ok_state);
 
-    case NAME_BACK:
     case NAME_CANCEL:
         return goto_state(cancel_state);
 
+    case GUI_CL:
+        gui_keyboard_lock();
+        break;
+
     case GUI_BS:
-        if (l > 0)
-        {
-            player[l - 1] = '\0';
+        if (text_del_char(player))
             gui_set_label(name_id, player);
-        }
         break;
 
     default:
-        if (l < MAXNAM - 1)
-        {
-            player[l + 0] = (char) i;
-            player[l + 1] = '\0';
+        if (text_add_char(i, player, MAXNAM, 17))
             gui_set_label(name_id, player);
-        }
     }
     return 1;
 }
@@ -109,15 +92,14 @@ static int name_enter(void)
     if ((id = gui_vstack(0)))
     {
         gui_label(id, _("Player Name"), GUI_MED, GUI_ALL, 0, 0);
-
-        gui_space(id);
         gui_space(id);
 
-        name_id = gui_label(id, player, GUI_MED, GUI_ALL, gui_yel, gui_yel);
+        name_id = gui_label(id, strlen(player) == 0 ? " " : player,
+                            GUI_MED, GUI_ALL, gui_yel, gui_yel);
 
         gui_space(id);
-
         gui_keyboard(id);
+
         if ((jd = gui_harray(id)))
         {
             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
@@ -141,15 +123,14 @@ static void name_leave(int id)
 static int name_keybd(int c, int d)
 {
     if (d)
-        if ((c & 0xFF80) == 0)
-        {
-            gui_focus(enter_id);
-            c &= 0x7F;
-            if (c == '\b' || c == 0x7F)
-                return name_action(GUI_BS);
-            else if (c > ' ')
-                return name_action(c);
-        }
+    {
+        gui_focus(enter_id);
+
+        if (c == '\b' || c == 0x7F)
+            return name_action(GUI_BS);
+        if (c > ' ')
+            return name_action(c);
+    }
     return 1;
 }
 
@@ -158,9 +139,15 @@ static int name_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return name_action(gui_token(gui_click()));
+        {
+            int c = gui_token(gui_click());
+
+            /* Ugh.  This is such a hack. */
+
+            return name_action(isupper(c) ? gui_keyboard_char(c) : c);
+        }
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-            name_action(NAME_BACK);
+            name_action(NAME_CANCEL);
     }
     return 1;
 }