Merge branch 'gles'
[neverball] / ball / st_name.c
index 6e9efe3..44051c8 100644 (file)
@@ -1,5 +1,5 @@
-/*   
- * Copyright (C) 2003 Robert Kooima
+/*
+ * Copyright (C) 2003 Robert Kooima - 2006 Jean Privat
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  * it under the  terms of the GNU General  Public License as published
  */
 
 #include <string.h>
+#include <ctype.h>
 
+#include "common.h"
 #include "gui.h"
-#include "set.h"
-#include "game.h"
 #include "util.h"
-#include "level.h"
 #include "audio.h"
 #include "config.h"
+#include "video.h"
+#include "text.h"
+#include "geom.h"
+
+#include "game_common.h"
+#include "game_server.h"
+#include "game_client.h"
 
 #include "st_name.h"
+#include "st_shared.h"
 
 /*---------------------------------------------------------------------------*/
 
-extern struct state st_name;
-
-static struct state * next_state;
 static char player[MAXNAM];
 
-int goto_name(struct state * nextstate)
+/*---------------------------------------------------------------------------*/
+
+static struct state *ok_state, *cancel_state;
+static unsigned int draw_back;
+
+int goto_name(struct state *ok, struct state *cancel, unsigned int back)
 {
-    next_state = nextstate;
+    SAFECPY(player, config_get_s(CONFIG_PLAYER));
+
+    ok_state     = ok;
+    cancel_state = cancel;
+    draw_back    = back;
+
     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)
-          return 1;
+        if (strlen(player) == 0)
+           return 1;
+
         config_set_s(CONFIG_PLAYER, player);
-       /* no break, thus continue */
-       
-    case NAME_BACK:
+
+        return goto_state(ok_state);
+
     case NAME_CANCEL:
-       return goto_state(next_state);
-       
+        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] = gui_keyboard_char((char) i);
-            player[l + 1] = '\0';
+        if (text_add_char(i, player, sizeof (player)))
             gui_set_label(name_id, player);
-        }
     }
     return 1;
 }
 
-static int name_enter(void)
+static int enter_id;
+
+static int name_gui(void)
 {
     int id, jd;
 
-    config_get_s(CONFIG_PLAYER, player, MAXNAM);
-
     if ((id = gui_vstack(0)))
     {
-        int gid;
+        gui_label(id, _("Player Name"), GUI_MED, GUI_ALL, 0, 0);
+        gui_space(id);
 
-       gid = gui_label(id, _("Player Name"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
+        name_id = gui_label(id, " ", GUI_MED, GUI_ALL, gui_yel, gui_yel);
 
-       gui_space(id);
         gui_space(id);
-       
-       name_id = gui_label(id, player, GUI_MED, GUI_ALL, gui_yel, gui_yel);
-
+        gui_keyboard(id);
         gui_space(id);
 
-       if ((jd = gui_harray(id)))
-       {
-           gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
-           gui_start(jd, _("OK"),     GUI_SML, NAME_OK,     0);
-       }
-       gui_keyboard(id);
+        if ((jd = gui_harray(id)))
+        {
+            enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
+            gui_space(jd);
+            gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
+        }
+
+        gui_layout(id, 0, 0);
 
-       gui_layout(id, 0, 0);
+        gui_set_trunc(name_id, TRUNC_HEAD);
+        gui_set_label(name_id, player);
     }
 
     return id;
 }
 
-static void name_leave(int id)
+static int name_enter(struct state *st, struct state *prev)
 {
-    gui_delete(id);
-}
+    if (draw_back)
+    {
+        game_client_free(NULL);
+        back_init("back/gui.png");
+    }
 
-static void name_paint(int id, float st)
-{
-    game_draw(0, st);
-    gui_paint(id);
-}
+    SDL_EnableUNICODE(1);
 
-static void name_timer(int id, float dt)
-{
-    gui_timer(id, dt);
-    audio_timer(dt);
+    return name_gui();
 }
 
-static void name_point(int id, int x, int y, int dx, int dy)
+static void name_leave(struct state *st, struct state *next, int id)
 {
-    gui_pulse(gui_point(id, x, y), 1.2f);
+    if (draw_back)
+        back_free();
+
+    SDL_EnableUNICODE(0);
+    gui_delete(id);
 }
 
-static void name_stick(int id, int a, int v)
+static void name_paint(int id, float t)
 {
-    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 (draw_back)
+    {
+        video_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
+        {
+            back_draw_easy();
+        }
+        video_pop_matrix();
+    }
+    else
+        game_client_draw(0, t);
+
+    gui_paint(id);
 }
 
-static int name_click(int b, int d)
+static int name_keybd(int c, int d)
 {
-    if (b <= 0 && d == 1)
-        return name_action(gui_token(gui_click()));
+    if (d)
+    {
+        gui_focus(enter_id);
+
+        if (c == '\b' || c == 0x7F)
+            return name_action(GUI_BS);
+        if (c >= ' ')
+            return name_action(c);
+    }
     return 1;
 }
 
@@ -161,9 +181,16 @@ static int name_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return name_click(0, 1);
+        {
+            int c = gui_token(gui_click());
+
+            if (c >= 0 && !GUI_ISMSK(c))
+                return name_action(gui_keyboard_char(c));
+            else
+                return name_action(c);
+        }
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-           name_action(NAME_BACK);
+            name_action(NAME_CANCEL);
     }
     return 1;
 }
@@ -174,12 +201,12 @@ struct state st_name = {
     name_enter,
     name_leave,
     name_paint,
-    name_timer,
-    name_point,
-    name_stick,
-    name_click,
-    NULL,
-    name_buttn,
-    1, 0
+    shared_timer,
+    shared_point,
+    shared_stick,
+    shared_angle,
+    shared_click,
+    name_keybd,
+    name_buttn
 };