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