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