Fix #22
[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
17 #include "gui.h"
18 #include "game.h"
19 #include "util.h"
20 #include "demo.h"
21 #include "level.h"
22 #include "audio.h"
23 #include "config.h"
24
25 #include "st_save.h"
26 #include "st_over.h"
27 #include "st_done.h"
28 #include "st_level.h"
29 #include "st_title.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 #define SAVE_SAVE   2
34 #define SAVE_CANCEL 3
35
36 static int  file_id;
37 static char filename[MAXNAM];
38
39 static int save_action(int i)
40 {
41     size_t l = strlen(filename);
42
43     audio_play(AUD_MENU, 1.0f);
44
45     switch (i)
46     {
47     case SAVE_SAVE:
48         if (demo_exists(filename))
49             return goto_state(&st_clobber);
50         else
51         {
52             if (level_exit(filename, 1))
53                 return goto_state(&st_level);
54             else if (level_dead())
55                 return goto_state(&st_title);
56             else
57                 return goto_state(&st_done);
58         }
59
60     case SAVE_CANCEL:
61         if (level_exit(NULL, 1))
62             return goto_state(&st_level);
63         else if (level_dead())
64             return goto_state(&st_title);
65         else
66             return goto_state(&st_done);
67
68     case GUI_CL:
69         gui_keyboard_lock();
70         break;
71
72     case GUI_BS:
73         if (l > 0)
74         {
75             filename[l - 1] = 0;
76             gui_set_label(file_id, filename);
77         }
78         break;
79
80     default:
81         if (l < MAXNAM - 1)
82         {
83             filename[l + 0] = gui_keyboard_char((char) i);
84             filename[l + 1] = 0;
85             gui_set_label(file_id, filename);
86         }
87     }
88     return 1;
89 }
90
91 static int save_enter(void)
92 {
93     int id, jd;
94
95     demo_unique(filename);
96
97     if ((id = gui_vstack(0)))
98     {
99         gui_space(id);
100         if ((jd = gui_hstack(id)))
101         {
102             gui_filler(jd);
103             gui_count(jd, curr_level(), GUI_LRG, GUI_NE | GUI_SE);
104             gui_label(jd, _("Level "),     GUI_LRG, GUI_NW | GUI_SW, 0, 0);
105             gui_filler(jd);
106         }
107         gui_space(id);
108
109         gui_space(id);
110         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
111         gui_space(id);
112
113         if ((jd = gui_harray(id)))
114         {
115             gui_start(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
116             gui_start(jd, _("Save"),   GUI_SML, SAVE_SAVE,   0);
117         }
118         gui_keyboard(id);
119         
120         gui_layout(id, 0, 0);
121     }
122
123     return id;
124 }
125
126 static void save_leave(int id)
127 {
128     gui_delete(id);
129 }
130
131 static void save_paint(int id, float st)
132 {
133     game_draw(0, st);
134     gui_paint(id);
135 }
136
137 static void save_timer(int id, float dt)
138 {
139     gui_timer(id, dt);
140     audio_timer(dt);
141 }
142
143 static void save_point(int id, int x, int y, int dx, int dy)
144 {
145     gui_pulse(gui_point(id, x, y), 1.2f);
146 }
147
148 static void save_stick(int id, int a, int v)
149 {
150     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
151         gui_pulse(gui_stick(id, v, 0), 1.2f);
152     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
153         gui_pulse(gui_stick(id, 0, v), 1.2f);
154 }
155
156 static int save_click(int b, int d)
157 {
158     if (b <= 0 && d == 1)
159         return save_action(gui_token(gui_click()));
160     return 1;
161 }
162
163 static int save_keybd(int c, int d)
164 {
165     if (d && c == SDLK_ESCAPE)
166         goto_state(&st_over);
167     return 1;
168 }
169
170 static int save_buttn(int b, int d)
171 {
172     if (d)
173     {
174         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
175             return save_click(0, 1);
176         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
177             return goto_state(&st_over);
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             if (level_exit(filename, 1))
191                 return goto_state(&st_level);
192             else
193                 return goto_state(&st_title);
194     }
195     return goto_state(&st_save);
196 }
197
198 static int clobber_enter(void)
199 {
200     int id, jd, kd;
201
202     if ((id = gui_vstack(0)))
203     {
204         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
205
206         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
207
208         if ((jd = gui_harray(id)))
209         {
210             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
211             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
212         }
213
214         gui_pulse(kd, 1.2f);
215         gui_layout(id, 0, 0);
216     }
217
218     return id;
219 }
220
221 static void clobber_leave(int id)
222 {
223     gui_delete(id);
224 }
225
226 static void clobber_paint(int id, float st)
227 {
228     game_draw(0, st);
229     gui_paint(id);
230 }
231
232 static void clobber_timer(int id, float dt)
233 {
234     gui_timer(id, dt);
235     audio_timer(dt);
236 }
237
238 static int clobber_keybd(int c, int d)
239 {
240     return (d && c == SDLK_ESCAPE) ? clobber_action(SAVE_CANCEL) : 1;
241 }
242
243 static void clobber_point(int id, int x, int y, int dx, int dy)
244 {
245     gui_pulse(gui_point(id, x, y), 1.2f);
246 }
247
248 static void clobber_stick(int id, int a, int v)
249 {
250     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
251         gui_pulse(gui_stick(id, v, 0), 1.2f);
252     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
253         gui_pulse(gui_stick(id, 0, v), 1.2f);
254 }
255
256 static int clobber_click(int b, int d)
257 {
258     if (d && b < 0)
259         return clobber_action(gui_token(gui_click()));
260     return 1;
261 }
262
263 static int clobber_buttn(int b, int d)
264 {
265     if (d)
266     {
267         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
268             return clobber_action(gui_token(gui_click()));
269         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
270             return clobber_action(SAVE_CANCEL);
271     }
272     return 1;
273 }
274
275 /*---------------------------------------------------------------------------*/
276
277 struct state st_save = {
278     save_enter,
279     save_leave,
280     save_paint,
281     save_timer,
282     save_point,
283     save_stick,
284     save_click,
285     save_keybd,
286     save_buttn,
287     1, 0
288 };
289
290 struct state st_clobber = {
291     clobber_enter,
292     clobber_leave,
293     clobber_paint,
294     clobber_timer,
295     clobber_point,
296     clobber_stick,
297     clobber_click,
298     clobber_keybd,
299     clobber_buttn,
300     1, 0
301 };