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