Some ball/st_name.c clean-up.
[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
17 #include "gui.h"
18 #include "util.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "game.h"
22
23 #include "st_name.h"
24 #include "st_shared.h"
25
26 /*---------------------------------------------------------------------------*/
27
28 static char player[MAXNAM];
29
30 /*---------------------------------------------------------------------------*/
31
32 static struct state *ok_state, *cancel_state;
33
34 int goto_name(struct state *ok, struct state *cancel)
35 {
36     config_get_s(CONFIG_PLAYER, player, MAXNAM);
37
38     ok_state     = ok;
39     cancel_state = cancel;
40
41     return goto_state(&st_name);
42 }
43
44 /*---------------------------------------------------------------------------*/
45
46 #define NAME_OK     1
47 #define NAME_CANCEL 2
48
49 static int name_id;
50
51 static int name_action(int i)
52 {
53     size_t l = strlen(player);
54
55     audio_play(AUD_MENU, 1.0f);
56
57     switch (i)
58     {
59     case NAME_OK:
60         if (l == 0)
61            return 1;
62
63         config_set_s(CONFIG_PLAYER, player);
64
65         return goto_state(ok_state);
66
67     case NAME_CANCEL:
68         return goto_state(cancel_state);
69
70     case GUI_BS:
71         if (l > 0)
72         {
73             player[l - 1] = '\0';
74             gui_set_label(name_id, player);
75         }
76         break;
77
78     default:
79         if (l < MAXNAM - 1)
80         {
81             player[l + 0] = (char) i;
82             player[l + 1] = '\0';
83
84             gui_set_label(name_id, player);
85         }
86     }
87     return 1;
88 }
89
90 static int enter_id;
91
92 static int name_enter(void)
93 {
94     int id, jd;
95
96     if ((id = gui_vstack(0)))
97     {
98         gui_label(id, _("Player Name"), GUI_MED, GUI_ALL, 0, 0);
99         gui_space(id);
100
101         name_id = gui_label(id, strlen(player) == 0 ? " " : player,
102                             GUI_MED, GUI_ALL, gui_yel, gui_yel);
103
104         gui_space(id);
105         gui_keyboard(id);
106
107         if ((jd = gui_harray(id)))
108         {
109             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
110             gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
111         }
112
113         gui_layout(id, 0, 0);
114     }
115
116     SDL_EnableUNICODE(1);
117
118     return id;
119 }
120
121 static void name_leave(int id)
122 {
123     SDL_EnableUNICODE(0);
124     gui_delete(id);
125 }
126
127 static int name_keybd(int c, int d)
128 {
129     if (d && (c & 0xFF80) == 0)
130     {
131         gui_focus(enter_id);
132
133         if (c == '\b' || c == 0x7F)
134             return name_action(GUI_BS);
135         if (c > ' ')
136             return name_action(c);
137     }
138     return 1;
139 }
140
141 static int name_buttn(int b, int d)
142 {
143     if (d)
144     {
145         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
146             return name_action(gui_token(gui_click()));
147         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
148             name_action(NAME_CANCEL);
149     }
150     return 1;
151 }
152
153 /*---------------------------------------------------------------------------*/
154
155 struct state st_name = {
156     name_enter,
157     name_leave,
158     shared_paint,
159     shared_timer,
160     shared_point,
161     shared_stick,
162     shared_click,
163     name_keybd,
164     name_buttn,
165     1, 0
166 };
167