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