Ported a couple of changesets from cosmetic-gui branch.
[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 #include "back.h"
25
26 #include "st_name.h"
27 #include "st_shared.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 static char player[MAXNAM];
32
33 /*---------------------------------------------------------------------------*/
34
35 static struct state *ok_state, *cancel_state;
36 static unsigned int draw_back;
37
38 int goto_name(struct state *ok, struct state *cancel, unsigned int back)
39 {
40     config_get_s(CONFIG_PLAYER, player, MAXNAM);
41
42     ok_state     = ok;
43     cancel_state = cancel;
44     draw_back    = back;
45
46     return goto_state(&st_name);
47 }
48
49 /*---------------------------------------------------------------------------*/
50
51 #define NAME_OK     1
52 #define NAME_CANCEL 2
53
54 static int name_id;
55
56 static int name_action(int i)
57 {
58     audio_play(AUD_MENU, 1.0f);
59
60     switch (i)
61     {
62     case NAME_OK:
63         if (strlen(player) == 0)
64            return 1;
65
66         config_set_s(CONFIG_PLAYER, player);
67
68         return goto_state(ok_state);
69
70     case NAME_CANCEL:
71         return goto_state(cancel_state);
72
73     case GUI_CL:
74         gui_keyboard_lock();
75         break;
76
77     case GUI_BS:
78         if (text_del_char(player))
79             gui_set_label(name_id, player);
80         break;
81
82     default:
83         if (text_add_char(i, player, MAXNAM, 17))
84             gui_set_label(name_id, player);
85     }
86     return 1;
87 }
88
89 static int enter_id;
90
91 static int name_enter(void)
92 {
93     int id, jd;
94
95     if (draw_back)
96     {
97         game_free();
98         back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
99     }
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         gui_space(id);
112
113         if ((jd = gui_harray(id)))
114         {
115             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 1);
116             gui_space(jd);
117             gui_state(jd, _("Cancel"), GUI_SML, NAME_CANCEL, 0);
118         }
119
120         gui_layout(id, 0, 0);
121     }
122
123     SDL_EnableUNICODE(1);
124
125     return id;
126 }
127
128 static void name_leave(int id)
129 {
130     if (draw_back)
131         back_free();
132
133     SDL_EnableUNICODE(0);
134     gui_delete(id);
135 }
136
137 static void name_paint(int id, float st)
138 {
139     if (draw_back)
140     {
141         config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
142         {
143             back_draw(0);
144         }
145         config_pop_matrix();
146     }
147     else
148         game_draw(0, st);
149
150     gui_paint(id);
151 }
152
153 static int name_keybd(int c, int d)
154 {
155     if (d)
156     {
157         gui_focus(enter_id);
158
159         if (c == '\b' || c == 0x7F)
160             return name_action(GUI_BS);
161         if (c > ' ')
162             return name_action(c);
163     }
164     return 1;
165 }
166
167 static int name_buttn(int b, int d)
168 {
169     if (d)
170     {
171         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
172         {
173             int c = gui_token(gui_click());
174
175             /* Ugh.  This is such a hack. */
176
177             return name_action(isupper(c) ? gui_keyboard_char(c) : c);
178         }
179         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
180             name_action(NAME_CANCEL);
181     }
182     return 1;
183 }
184
185 /*---------------------------------------------------------------------------*/
186
187 struct state st_name = {
188     name_enter,
189     name_leave,
190     name_paint,
191     shared_timer,
192     shared_point,
193     shared_stick,
194     shared_click,
195     name_keybd,
196     name_buttn,
197     1, 0
198 };
199