Don't provide a default name string. As a side effect this makes ticket
[neverball] / ball / st_name.c
1 /*
2  * Copyright (C) 2003 Robert Kooima - 2006 Jean Privat
3  * Part of the Neverball Project http://icculus.org/neverball/
4  *
5  * NEVERBALL is  free software; you can redistribute  it and/or modify
6  * it under the  terms of the GNU General  Public License as published
7  * by the Free  Software Foundation; either version 2  of the License,
8  * or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
12  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
13  * General Public License for more details.
14  */
15
16 #include <string.h>
17 #include <ctype.h>
18
19 #include "gui.h"
20 #include "util.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "game.h"
24 #include "st_shared.h"
25
26 #include "st_name.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 extern struct state st_name;
31
32 static struct state *ok_state, *cancel_state;
33 static char player[MAXNAM];
34
35 int goto_name(struct state *ok, struct state *cancel)
36 {
37     config_get_s(CONFIG_PLAYER, player, MAXNAM);
38
39     if (player[0] == '\0')
40         strcpy(player, " ");
41
42     ok_state     = ok;
43     cancel_state = cancel;
44
45     return goto_state(&st_name);
46 }
47
48 #define NAME_BACK   2
49 #define NAME_CANCEL 3
50 #define NAME_OK     4
51
52 static int name_id;
53
54 static int name_action(int i)
55 {
56     size_t l = strlen(player);
57
58     audio_play(AUD_MENU, 1.0f);
59
60     switch (i)
61     {
62     case NAME_OK:
63         if (l == 0)
64            return 1;
65
66         config_set_s(CONFIG_PLAYER, player);
67         return goto_state(ok_state);
68
69     case NAME_BACK:
70     case NAME_CANCEL:
71         return goto_state(cancel_state);
72
73     case GUI_BS:
74         if (l > 0)
75         {
76             player[l - 1] = '\0';
77             gui_set_label(name_id, player);
78         }
79         break;
80
81     default:
82         if (l < MAXNAM - 1)
83         {
84             player[l + 0] = (char) i;
85             player[l + 1] = '\0';
86
87             gui_set_label(name_id, player);
88         }
89     }
90     return 1;
91 }
92
93 static int enter_id;
94
95 static int name_enter(void)
96 {
97     int id, jd;
98
99     if ((id = gui_vstack(0)))
100     {
101         gui_label(id, _("Player Name"), GUI_MED, GUI_ALL, 0, 0);
102
103         gui_space(id);
104         gui_space(id);
105
106         name_id = gui_label(id, player, GUI_MED, GUI_ALL, gui_yel, gui_yel);
107
108         /* Clear dummy text. */
109
110         if (strcmp(player, " ") == 0)
111             strcpy(player, "");
112
113         gui_space(id);
114
115         gui_keyboard(id);
116         if ((jd = gui_harray(id)))
117         {
118             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
119             gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
120         }
121
122         gui_layout(id, 0, 0);
123     }
124
125     SDL_EnableUNICODE(1);
126
127     return id;
128 }
129
130 static void name_leave(int id)
131 {
132     SDL_EnableUNICODE(0);
133     gui_delete(id);
134 }
135
136 static int name_keybd(int c, int d)
137 {
138     if (d)
139         if ((c & 0xFF80) == 0)
140         {
141             gui_focus(enter_id);
142             c &= 0x7F;
143             if (c == '\b' || c == 0x7F)
144                 return name_action(GUI_BS);
145             else if (c > ' ')
146                 return name_action(c);
147         }
148     return 1;
149 }
150
151 static int name_buttn(int b, int d)
152 {
153     if (d)
154     {
155         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
156             return name_action(gui_token(gui_click()));
157         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
158             name_action(NAME_BACK);
159     }
160     return 1;
161 }
162
163 /*---------------------------------------------------------------------------*/
164
165 struct state st_name = {
166     name_enter,
167     name_leave,
168     shared_paint,
169     shared_timer,
170     shared_point,
171     shared_stick,
172     shared_click,
173     name_keybd,
174     name_buttn,
175     1, 0
176 };
177