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