fix del key on mac
[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 #include "st_shared.h"
24
25 #include "st_save.h"
26
27 extern struct state st_save;
28 extern struct state st_clobber;
29 static char filename[MAXNAM];
30
31 /*---------------------------------------------------------------------------*/
32
33 static struct state * ok_state;
34 static struct state * cancel_state;
35
36 int goto_save(struct state * ok, struct state * cancel)
37 {
38     demo_unique(filename);
39     ok_state     = ok;
40     cancel_state = cancel;
41     return goto_state(&st_save);
42 }
43
44 /*---------------------------------------------------------------------------*/
45
46 #define SAVE_SAVE   2
47 #define SAVE_CANCEL 3
48
49 static int file_id;
50
51 static int save_action(int i)
52 {
53     size_t l = strlen(filename);
54
55     audio_play(AUD_MENU, 1.0f);
56
57     switch (i)
58     {
59     case SAVE_SAVE:
60         if (strcmp(filename, "") == 0)
61             return 1;
62         if (demo_exists(filename))
63             return goto_state(&st_clobber);
64         else
65         {
66             demo_play_save(filename);
67             return goto_state(ok_state);
68         }
69
70     case SAVE_CANCEL:
71         return goto_state(cancel_state);
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] = (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     if ((id = gui_vstack(0)))
97     {
98         gui_label(id, _("Replay Name"), GUI_MED, GUI_ALL, 0, 0);
99         
100         gui_space(id);
101         gui_space(id);
102         
103         file_id = gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
104         
105         gui_space(id);
106
107         gui_keyboard(id);
108         if ((jd = gui_harray(id)))
109         {
110             gui_state(jd, _("Cancel"), GUI_SML, SAVE_CANCEL, 0);
111             gui_start(jd, _("Save"),   GUI_SML, SAVE_SAVE,   0);
112         }
113         
114         gui_layout(id, 0, 0);
115     }
116     
117     SDL_EnableUNICODE(1);
118
119     return id;
120 }
121
122 static void save_leave(int id)
123 {
124     SDL_EnableUNICODE(0);
125     gui_delete(id);
126 }
127
128 static int save_keybd(int c, int d)
129 {
130     if (d)
131         if ((c & 0xFF80) == 0)
132         {
133             c &= 0x7F;
134             if (c == '\b')
135                 return save_action(GUI_BS);
136             else if (c > ' ')
137                 return save_action(c);
138         }
139     return 1;
140 }
141
142 static int save_buttn(int b, int d)
143 {
144     if (d)
145     {
146         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
147             return save_action(gui_token(gui_click()));
148         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
149             return save_action(SAVE_CANCEL);
150     }
151     return 1;
152 }
153
154 /*---------------------------------------------------------------------------*/
155
156 static int clobber_action(int i)
157 {
158     audio_play(AUD_MENU, 1.0f);
159
160     if (i == SAVE_SAVE)
161     {
162         demo_play_save(filename);
163         return goto_state(ok_state);    
164     }
165     return goto_state(&st_save);
166 }
167
168 static int clobber_enter(void)
169 {
170     int id, jd, kd;
171
172     if ((id = gui_vstack(0)))
173     {
174         kd = gui_label(id, _("Overwrite?"), GUI_MED, GUI_ALL, gui_red, gui_red);
175
176         gui_label(id, filename, GUI_MED, GUI_ALL, gui_yel, gui_yel);
177
178         if ((jd = gui_harray(id)))
179         {
180             gui_state(jd, _("Yes"), GUI_SML, SAVE_SAVE,   0);
181             gui_start(jd, _("No"),  GUI_SML, SAVE_CANCEL, 1);
182         }
183
184         gui_pulse(kd, 1.2f);
185         gui_layout(id, 0, 0);
186     }
187
188     return id;
189 }
190
191 static int clobber_buttn(int b, int d)
192 {
193     if (d)
194     {
195         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
196             return clobber_action(gui_token(gui_click()));
197         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
198             return clobber_action(SAVE_CANCEL);
199     }
200     return 1;
201 }
202
203 /*---------------------------------------------------------------------------*/
204
205 struct state st_save = {
206     save_enter,
207     save_leave,
208     shared_paint,
209     shared_timer,
210     shared_point,
211     shared_stick,
212     shared_click,
213     save_keybd,
214     save_buttn,
215     1, 0
216 };
217
218 struct state st_clobber = {
219     clobber_enter,
220     shared_leave,
221     shared_paint,
222     shared_timer,
223     shared_point,
224     shared_stick,
225     shared_click,
226     NULL,
227     clobber_buttn,
228     1, 0
229 };