ball/set: clean up some excessive macro usage
[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 #include <ctype.h>
17
18 #include "gui.h"
19 #include "util.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "demo.h"
23 #include "progress.h"
24 #include "text.h"
25
26 #include "game_common.h"
27
28 #include "st_shared.h"
29 #include "st_save.h"
30
31 extern struct state st_save;
32 extern struct state st_clobber;
33
34 static char filename[MAXSTR];
35
36 /*---------------------------------------------------------------------------*/
37
38 static struct state *ok_state;
39 static struct state *cancel_state;
40
41 int goto_save(struct state *ok, struct state *cancel)
42 {
43     const char *name;
44
45     name = demo_format_name(config_get_s(CONFIG_REPLAY_NAME),
46                             set_id(curr_set()),
47                             level_name(curr_level()));
48
49     strncpy(filename, name, sizeof (filename) - 1);
50
51     ok_state     = ok;
52     cancel_state = cancel;
53
54     return goto_state(&st_save);
55 }
56
57 /*---------------------------------------------------------------------------*/
58
59 static int file_id;
60
61 #define SAVE_SAVE   -1
62 #define SAVE_CANCEL -2
63
64 static int save_action(int i)
65 {
66     char *n;
67
68     audio_play(AUD_MENU, 1.0f);
69
70     switch (i)
71     {
72     case SAVE_SAVE:
73         n = filename;
74
75         if (strlen(n) == 0)
76             return 1;
77
78         if (demo_exists(n))
79             return goto_state(&st_clobber);
80         else
81         {
82             demo_rename(n);
83             return goto_state(ok_state);
84         }
85
86     case SAVE_CANCEL:
87         return goto_state(cancel_state);
88
89     case GUI_CL:
90         gui_keyboard_lock();
91         break;
92
93     case GUI_BS:
94         if (text_del_char(filename))
95             gui_set_label(file_id, filename);
96         break;
97
98     default:
99         if (text_add_char(i, filename, sizeof (filename)))
100             gui_set_label(file_id, filename);
101     }
102     return 1;
103 }
104
105 static int enter_id;
106
107 static int save_enter(void)
108 {
109     int id, jd;
110
111     if ((id = gui_vstack(0)))
112     {
113         gui_label(id, _("Replay Name"), GUI_MED, GUI_ALL, 0, 0);
114         gui_space(id);
115
116         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
117
118         gui_space(id);
119         gui_keyboard(id);
120         gui_space(id);
121
122         if ((jd = gui_harray(id)))
123         {
124             enter_id = gui_start(jd, _("Save"), GUI_SML, SAVE_SAVE, 0);
125             gui_space(jd);
126             gui_state(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
127         }
128
129         gui_layout(id, 0, 0);
130     }
131
132     SDL_EnableUNICODE(1);
133
134     return id;
135 }
136
137 static void save_leave(int id)
138 {
139     SDL_EnableUNICODE(0);
140     gui_delete(id);
141 }
142
143 static int save_keybd(int c, int d)
144 {
145     if (d)
146     {
147         gui_focus(enter_id);
148
149         if (c == '\b' || c == 0x7F)
150             return save_action(GUI_BS);
151         if (c >= ' ')
152             return save_action(c);
153     }
154     return 1;
155 }
156
157 static int save_buttn(int b, int d)
158 {
159     if (d)
160     {
161         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
162         {
163             int c = gui_token(gui_click());
164
165             if (c >= 0 && !GUI_ISMSK(c))
166                 return save_action(gui_keyboard_char(c));
167             else
168                 return save_action(c);
169         }
170         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
171             return save_action(SAVE_CANCEL);
172     }
173     return 1;
174 }
175
176 /*---------------------------------------------------------------------------*/
177
178 static int clobber_action(int i)
179 {
180     audio_play(AUD_MENU, 1.0f);
181
182     if (i == SAVE_SAVE)
183     {
184         demo_rename(filename);
185         return goto_state(ok_state);
186     }
187     return goto_state(&st_save);
188 }
189
190 static int clobber_enter(void)
191 {
192     int id, jd, kd;
193
194     if ((id = gui_vstack(0)))
195     {
196         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
197
198         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
199
200         if ((jd = gui_harray(id)))
201         {
202             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
203             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
204         }
205
206         gui_pulse(kd, 1.2f);
207         gui_layout(id, 0, 0);
208     }
209
210     return id;
211 }
212
213 static int clobber_buttn(int b, int d)
214 {
215     if (d)
216     {
217         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
218             return clobber_action(gui_token(gui_click()));
219         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
220             return clobber_action(SAVE_CANCEL);
221     }
222     return 1;
223 }
224
225 /*---------------------------------------------------------------------------*/
226
227 struct state st_save = {
228     save_enter,
229     save_leave,
230     shared_paint,
231     shared_timer,
232     shared_point,
233     shared_stick,
234     shared_angle,
235     shared_click,
236     save_keybd,
237     save_buttn,
238     1, 0
239 };
240
241 struct state st_clobber = {
242     clobber_enter,
243     shared_leave,
244     shared_paint,
245     shared_timer,
246     shared_point,
247     shared_stick,
248     shared_angle,
249     shared_click,
250     NULL,
251     clobber_buttn,
252     1, 0
253 };