Correct logic of BSP back/front tests
[neverball] / ball / st_save.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
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 "game.h"
20 #include "util.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "demo.h"
24 #include "progress.h"
25 #include "text.h"
26
27 #include "st_shared.h"
28 #include "st_save.h"
29
30 extern struct state st_save;
31 extern struct state st_clobber;
32
33 static char filename[MAXSTR];
34
35 /*---------------------------------------------------------------------------*/
36
37 static struct state *ok_state;
38 static struct state *cancel_state;
39
40 int goto_save(struct state *ok, struct state *cancel)
41 {
42     char fmt[MAXSTR] = "";
43     const char *name;
44
45     config_get_s(CONFIG_REPLAY_NAME, fmt, sizeof (fmt) - 1);
46     name = demo_format_name(fmt, set_id(curr_set()), level_name(curr_level()));
47
48     strncpy(filename, name, sizeof (filename) - 1);
49
50     ok_state     = ok;
51     cancel_state = cancel;
52
53     return goto_state(&st_save);
54 }
55
56 /*---------------------------------------------------------------------------*/
57
58 static int file_id;
59
60 #define SAVE_SAVE   -1
61 #define SAVE_CANCEL -2
62
63 static int save_action(int i)
64 {
65     char *n;
66
67     audio_play(AUD_MENU, 1.0f);
68
69     switch (i)
70     {
71     case SAVE_SAVE:
72         n = text_to_locale(filename);
73
74         if (strlen(n) == 0)
75             return 1;
76
77         if (demo_exists(n))
78             return goto_state(&st_clobber);
79         else
80         {
81             demo_rename(n);
82             return goto_state(ok_state);
83         }
84
85     case SAVE_CANCEL:
86         return goto_state(cancel_state);
87
88     case GUI_CL:
89         gui_keyboard_lock();
90         break;
91
92     case GUI_BS:
93         if (text_del_char(filename))
94             gui_set_label(file_id, filename);
95         break;
96
97     default:
98         if (text_add_char(i, filename, sizeof (filename) - 1,
99                                        sizeof (filename) - 1))
100             gui_set_label(file_id, filename);
101     }
102     return 1;
103 }
104
105 static int enter_id;
106
107 static int save_enter(void)
108 {
109     int id, jd;
110
111     if ((id = gui_vstack(0)))
112     {
113         gui_label(id, _("Replay Name"), GUI_MED, GUI_ALL, 0, 0);
114         gui_space(id);
115
116         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
117
118         gui_space(id);
119         gui_keyboard(id);
120         gui_space(id);
121
122         if ((jd = gui_harray(id)))
123         {
124             enter_id = gui_start(jd, _("Save"), GUI_SML, SAVE_SAVE, 0);
125             gui_space(jd);
126             gui_state(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
127         }
128
129         gui_layout(id, 0, 0);
130     }
131
132     SDL_EnableUNICODE(1);
133
134     return id;
135 }
136
137 static void save_leave(int id)
138 {
139     SDL_EnableUNICODE(0);
140     gui_delete(id);
141 }
142
143 static int save_keybd(int c, int d)
144 {
145     if (d)
146     {
147         gui_focus(enter_id);
148
149         if (c == '\b' || c == 0x7F)
150             return save_action(GUI_BS);
151         if (c >= ' ')
152             return save_action(c);
153     }
154     return 1;
155 }
156
157 static int save_buttn(int b, int d)
158 {
159     if (d)
160     {
161         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
162         {
163             int c = gui_token(gui_click());
164
165             if (c >= 0 && !GUI_ISMSK(c))
166                 return save_action(gui_keyboard_char(c));
167             else
168                 return save_action(c);
169         }
170         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
171             return save_action(SAVE_CANCEL);
172     }
173     return 1;
174 }
175
176 /*---------------------------------------------------------------------------*/
177
178 static int clobber_action(int i)
179 {
180     audio_play(AUD_MENU, 1.0f);
181
182     if (i == SAVE_SAVE)
183     {
184         demo_rename(text_to_locale(filename));
185         return goto_state(ok_state);
186     }
187     return goto_state(&st_save);
188 }
189
190 static int clobber_enter(void)
191 {
192     int id, jd, kd;
193
194     if ((id = gui_vstack(0)))
195     {
196         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
197
198         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
199
200         if ((jd = gui_harray(id)))
201         {
202             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
203             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
204         }
205
206         gui_pulse(kd, 1.2f);
207         gui_layout(id, 0, 0);
208     }
209
210     return id;
211 }
212
213 static int clobber_buttn(int b, int d)
214 {
215     if (d)
216     {
217         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
218             return clobber_action(gui_token(gui_click()));
219         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
220             return clobber_action(SAVE_CANCEL);
221     }
222     return 1;
223 }
224
225 /*---------------------------------------------------------------------------*/
226
227 struct state st_save = {
228     save_enter,
229     save_leave,
230     shared_paint,
231     shared_timer,
232     shared_point,
233     shared_stick,
234     shared_angle,
235     shared_click,
236     save_keybd,
237     save_buttn,
238     1, 0
239 };
240
241 struct state st_clobber = {
242     clobber_enter,
243     shared_leave,
244     shared_paint,
245     shared_timer,
246     shared_point,
247     shared_stick,
248     shared_angle,
249     shared_click,
250     NULL,
251     clobber_buttn,
252     1, 0
253 };