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 "common.h"
19 #include "gui.h"
20 #include "util.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "video.h"
24 #include "text.h"
25 #include "geom.h"
26 
27 #include "game_common.h"
28 #include "game_server.h"
29 #include "game_client.h"
30 
31 #include "st_name.h"
32 #include "st_shared.h"
33 
34 /*---------------------------------------------------------------------------*/
35 
36 static struct state *ok_state;
37 static struct state *cancel_state;
38 
39 static unsigned int draw_back;
40 
goto_name(struct state * ok,struct state * cancel,unsigned int back)41 int goto_name(struct state *ok, struct state *cancel, unsigned int back)
42 {
43     ok_state     = ok;
44     cancel_state = cancel;
45     draw_back    = back;
46 
47     return goto_state(&st_name);
48 }
49 
50 /*---------------------------------------------------------------------------*/
51 
52 enum
53 {
54     NAME_OK = GUI_LAST
55 };
56 
57 static int name_id;
58 
name_action(int tok,int val)59 static int name_action(int tok, int val)
60 {
61     audio_play(AUD_MENU, 1.0f);
62 
63     switch (tok)
64     {
65     case GUI_BACK:
66         return goto_state(cancel_state);
67 
68     case NAME_OK:
69         if (strlen(text_input) == 0)
70            return 1;
71 
72         config_set_s(CONFIG_PLAYER, text_input);
73 
74         return goto_state(ok_state);
75 
76     case GUI_CL:
77         gui_keyboard_lock();
78         break;
79 
80     case GUI_BS:
81         text_input_del();
82         break;
83 
84     case GUI_CHAR:
85         text_input_char(val);
86         break;
87     }
88     return 1;
89 }
90 
91 static int enter_id;
92 
name_gui(void)93 static int name_gui(void)
94 {
95     int id, jd;
96 
97     if ((id = gui_vstack(0)))
98     {
99         gui_label(id, _("Player Name"), GUI_MED, 0, 0);
100         gui_space(id);
101 
102         name_id = gui_label(id, " ", GUI_MED, gui_yel, gui_yel);
103 
104         gui_space(id);
105         gui_keyboard(id);
106         gui_space(id);
107 
108         if ((jd = gui_harray(id)))
109         {
110             enter_id = gui_start(jd, _("OK"), GUI_SML, NAME_OK, 0);
111             gui_space(jd);
112             gui_state(jd, _("Cancel"), GUI_SML, GUI_BACK, 0);
113         }
114 
115         gui_layout(id, 0, 0);
116 
117         gui_set_trunc(name_id, TRUNC_HEAD);
118         gui_set_label(name_id, text_input);
119     }
120 
121     return id;
122 }
123 
on_text_input(int typing)124 static void on_text_input(int typing)
125 {
126     if (name_id)
127     {
128         gui_set_label(name_id, text_input);
129 
130         if (typing)
131             audio_play(AUD_MENU, 1.0f);
132     }
133 }
134 
name_enter(struct state * st,struct state * prev)135 static int name_enter(struct state *st, struct state *prev)
136 {
137     if (draw_back)
138     {
139         game_client_free(NULL);
140         back_init("back/gui.png");
141     }
142 
143     text_input_start(on_text_input);
144     text_input_str(config_get_s(CONFIG_PLAYER), 0);
145 
146     return name_gui();
147 }
148 
name_leave(struct state * st,struct state * next,int id)149 static void name_leave(struct state *st, struct state *next, int id)
150 {
151     if (draw_back)
152         back_free();
153 
154     text_input_stop();
155 
156     gui_delete(id);
157 }
158 
name_paint(int id,float t)159 static void name_paint(int id, float t)
160 {
161     if (draw_back)
162     {
163         video_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
164         {
165             back_draw_easy();
166         }
167         video_pop_matrix();
168     }
169     else
170         game_client_draw(0, t);
171 
172     gui_paint(id);
173 }
174 
name_keybd(int c,int d)175 static int name_keybd(int c, int d)
176 {
177     if (d)
178     {
179         if (c == KEY_EXIT)
180             return name_action(GUI_BACK, 0);
181 
182         if (c == '\b' || c == 0x7F)
183         {
184             gui_focus(enter_id);
185             return name_action(GUI_BS, 0);
186         }
187         else
188         {
189             gui_focus(enter_id);
190             return 1;
191         }
192     }
193     return 1;
194 }
195 
name_buttn(int b,int d)196 static int name_buttn(int b, int d)
197 {
198     if (d)
199     {
200         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
201         {
202             int tok = gui_token(gui_active());
203             int val = gui_value(gui_active());
204 
205             return name_action(tok, (tok == GUI_CHAR ?
206                                      gui_keyboard_char(val) :
207                                      val));
208         }
209         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
210             name_action(GUI_BACK, 0);
211     }
212     return 1;
213 }
214 
215 /*---------------------------------------------------------------------------*/
216 
217 struct state st_name = {
218     name_enter,
219     name_leave,
220     name_paint,
221     shared_timer,
222     shared_point,
223     shared_stick,
224     shared_angle,
225     shared_click,
226     name_keybd,
227     name_buttn
228 };
229 
230