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