Don't internationalise player name. Temporary solution to ticket #82.
[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 void name_default(void)
36 {
37     char *login = getenv("LOGNAME");
38
39     if (login == NULL || login[0] == '\0')
40         login = "Player";
41
42     strncpy(player, login, MAXNAM);
43     player[MAXNAM - 1] = '\0';
44 }
45
46 int goto_name(struct state *ok, struct state *cancel)
47 {
48     config_get_s(CONFIG_PLAYER, player, MAXNAM);
49
50     if (player[0] == '\0')
51         name_default();
52
53     ok_state     = ok;
54     cancel_state = cancel;
55
56     return goto_state(&st_name);
57 }
58
59 #define NAME_BACK   2
60 #define NAME_CANCEL 3
61 #define NAME_OK     4
62
63 static int name_id;
64
65 static int name_action(int i)
66 {
67     size_t l;
68
69     audio_play(AUD_MENU, 1.0f);
70
71     l = strlen(player);
72
73     switch (i)
74     {
75     case NAME_OK:
76         if (l == 0)
77            return 1;
78         config_set_s(CONFIG_PLAYER, player);
79         return goto_state(ok_state);
80
81     case NAME_BACK:
82     case NAME_CANCEL:
83         return goto_state(cancel_state);
84
85     case GUI_BS:
86         if (l > 0)
87         {
88             player[l - 1] = '\0';
89             gui_set_label(name_id, player);
90         }
91         break;
92
93     default:
94         if (l < MAXNAM - 1)
95         {
96             player[l + 0] = (char) i;
97             player[l + 1] = '\0';
98
99             gui_set_label(name_id, player);
100         }
101     }
102     return 1;
103 }
104
105 static int enter_id;
106
107 static int name_enter(void)
108 {
109     int id, jd;
110
111     if ((id = gui_vstack(0)))
112     {
113         gui_label(id, _("Player Name"), GUI_MED, GUI_ALL, 0, 0);
114
115         gui_space(id);
116         gui_space(id);
117
118         name_id = gui_label(id, player, GUI_MED, GUI_ALL, gui_yel, gui_yel);
119
120         gui_space(id);
121
122         gui_keyboard(id);
123         if ((jd = gui_harray(id)))
124         {
125             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
126             gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
127         }
128
129         gui_layout(id, 0, 0);
130     }
131
132     SDL_EnableUNICODE(1);
133
134     return id;
135 }
136
137 static void name_leave(int id)
138 {
139     SDL_EnableUNICODE(0);
140     gui_delete(id);
141 }
142
143 static int name_keybd(int c, int d)
144 {
145     if (d)
146         if ((c & 0xFF80) == 0)
147         {
148             gui_focus(enter_id);
149             c &= 0x7F;
150             if (c == '\b' || c == 0x7F)
151                 return name_action(GUI_BS);
152             else if (c > ' ')
153                 return name_action(c);
154         }
155     return 1;
156 }
157
158 static int name_buttn(int b, int d)
159 {
160     if (d)
161     {
162         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
163             return name_action(gui_token(gui_click()));
164         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
165             name_action(NAME_BACK);
166     }
167     return 1;
168 }
169
170 /*---------------------------------------------------------------------------*/
171
172 struct state st_name = {
173     name_enter,
174     name_leave,
175     shared_paint,
176     shared_timer,
177     shared_point,
178     shared_stick,
179     shared_click,
180     name_keybd,
181     name_buttn,
182     1, 0
183 };
184