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