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