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