I18N
[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
55                 return goto_state(&st_done);
56         }
57
58     case SAVE_CANCEL:
59         if (level_exit(NULL, 1))
60             return goto_state(&st_level);
61         else
62             return goto_state(&st_done);
63
64     case GUI_CL:
65         gui_keyboard_lock();
66         break;
67
68     case GUI_BS:
69         if (l > 0)
70         {
71             filename[l - 1] = 0;
72             gui_set_label(file_id, filename);
73         }
74         break;
75
76     default:
77         if (l < MAXNAM - 1)
78         {
79             filename[l + 0] = gui_keyboard_char((char) i);
80             filename[l + 1] = 0;
81             gui_set_label(file_id, filename);
82         }
83     }
84     return 1;
85 }
86
87 static int save_enter(void)
88 {
89     int id, jd;
90
91     demo_unique(filename);
92
93     if ((id = gui_vstack(0)))
94     {
95         gui_space(id);
96         if ((jd = gui_hstack(id)))
97         {
98             gui_filler(jd);
99             gui_count(jd, curr_level(), GUI_LRG, GUI_NE | GUI_SE);
100             gui_label(jd, _("Level "),     GUI_LRG, GUI_NW | GUI_SW, 0, 0);
101             gui_filler(jd);
102         }
103         gui_space(id);
104
105         gui_space(id);
106         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
107         gui_space(id);
108
109         if ((jd = gui_harray(id)))
110         {
111             gui_start(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_keybd(int c, int d)
160 {
161     if (d && c == SDLK_ESCAPE)
162         goto_state(&st_over);
163     return 1;
164 }
165
166 static int save_buttn(int b, int d)
167 {
168     if (d)
169     {
170         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
171             return save_click(0, 1);
172         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
173             return goto_state(&st_over);
174     }
175     return 1;
176 }
177
178 /*---------------------------------------------------------------------------*/
179
180 static int clobber_action(int i)
181 {
182     audio_play(AUD_MENU, 1.0f);
183
184     if (i == SAVE_SAVE)
185     {
186             if (level_exit(filename, 1))
187                 return goto_state(&st_level);
188             else
189                 return goto_state(&st_title);
190     }
191     return goto_state(&st_save);
192 }
193
194 static int clobber_enter(void)
195 {
196     int id, jd, kd;
197
198     if ((id = gui_vstack(0)))
199     {
200         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
201
202         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
203
204         if ((jd = gui_harray(id)))
205         {
206             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
207             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
208         }
209
210         gui_pulse(kd, 1.2f);
211         gui_layout(id, 0, 0);
212     }
213
214     return id;
215 }
216
217 static void clobber_leave(int id)
218 {
219     gui_delete(id);
220 }
221
222 static void clobber_paint(int id, float st)
223 {
224     game_draw(0, st);
225     gui_paint(id);
226 }
227
228 static void clobber_timer(int id, float dt)
229 {
230     gui_timer(id, dt);
231     audio_timer(dt);
232 }
233
234 static int clobber_keybd(int c, int d)
235 {
236     return (d && c == SDLK_ESCAPE) ? clobber_action(SAVE_CANCEL) : 1;
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     save_keybd,
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     clobber_keybd,
295     clobber_buttn,
296     1, 0
297 };