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