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