Add a basic ball configuration screen
[neverball] / ball / st_start.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 "gui.h"
16 #include "set.h"
17 #include "util.h"
18 #include "progress.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "st_shared.h"
22 #include "common.h"
23
24 #include "game_common.h"
25
26 #include "st_set.h"
27 #include "st_over.h"
28 #include "st_level.h"
29 #include "st_start.h"
30 #include "st_title.h"
31
32 /*---------------------------------------------------------------------------*/
33
34 #define START_BACK        -1
35 #define START_CHALLENGE   -2
36 #define START_OPEN_GOALS  -3
37 #define START_LOCK_GOALS  -4
38
39 static int shot_id;
40 static int file_id;
41 static int challenge_id;
42
43 /*---------------------------------------------------------------------------*/
44
45 /* Create a level selector button based upon its existence and status. */
46
47 static void gui_level(int id, int i)
48 {
49     const GLfloat *fore = 0, *back = 0;
50
51     int jd;
52
53     if (!level_exists(i))
54     {
55         gui_label(id, " ", GUI_SML, GUI_ALL, gui_blk, gui_blk);
56         return;
57     }
58
59     if (level_opened(i))
60     {
61         fore = level_bonus(i)     ? gui_grn : gui_wht;
62         back = level_completed(i) ? fore    : gui_yel;
63     }
64
65     jd = gui_label(id, level_name(i), GUI_SML, GUI_ALL, back, fore);
66
67     if (level_opened(i) || config_cheat())
68         gui_active(jd, i, 0);
69 }
70
71 static void start_over_level(int i)
72 {
73     if (level_opened(i) || config_cheat())
74     {
75         gui_set_image(shot_id, level_shot(i));
76
77         set_score_board(&get_level(i)->score.most_coins,  -1,
78                         &get_level(i)->score.best_times,  -1,
79                         &get_level(i)->score.fast_unlock, -1);
80
81         if (file_id)
82             gui_set_label(file_id, level_file(i));
83     }
84 }
85
86 static void start_over(int id, int pulse)
87 {
88     int i;
89
90     if (id == 0)
91         return;
92
93     if (pulse)
94         gui_pulse(id, 1.2f);
95
96     i = gui_token(id);
97
98     if (i == START_CHALLENGE || i == START_BACK)
99     {
100         gui_set_image(shot_id, set_shot(curr_set()));
101
102         set_score_board(set_coin_score(curr_set()), -1,
103                         set_time_score(curr_set()), -1,
104                         NULL, -1);
105     }
106
107     if (i >= 0 && !GUI_ISMSK(i))
108         start_over_level(i);
109 }
110
111 /*---------------------------------------------------------------------------*/
112
113 static int start_action(int i)
114 {
115     audio_play(AUD_MENU, 1.0f);
116
117     switch (i)
118     {
119     case START_BACK:
120         return goto_state(&st_set);
121
122     case START_CHALLENGE:
123         if (config_cheat())
124         {
125             progress_init(curr_mode() == MODE_CHALLENGE ?
126                           MODE_NORMAL : MODE_CHALLENGE);
127             gui_toggle(challenge_id);
128             return 1;
129         }
130         else
131         {
132             progress_init(MODE_CHALLENGE);
133             return start_action(0);
134         }
135         break;
136
137     case GUI_MOST_COINS:
138     case GUI_BEST_TIMES:
139     case GUI_FAST_UNLOCK:
140         gui_score_set(i);
141         return goto_state(&st_start);
142
143     case START_OPEN_GOALS:
144         config_set_d(CONFIG_LOCK_GOALS, 0);
145         return goto_state(&st_start);
146
147     case START_LOCK_GOALS:
148         config_set_d(CONFIG_LOCK_GOALS, 1);
149         return goto_state(&st_start);
150
151     default:
152         if (progress_play(i))
153             return goto_state(&st_level);
154         break;
155     }
156
157     return 1;
158 }
159
160 static int start_enter(void)
161 {
162     int w = config_get_d(CONFIG_WIDTH);
163     int h = config_get_d(CONFIG_HEIGHT);
164     int i, j;
165
166     int id, jd, kd, ld;
167
168     progress_init(MODE_NORMAL);
169
170     if ((id = gui_vstack(0)))
171     {
172         if ((jd = gui_hstack(id)))
173         {
174
175             gui_label(jd, set_name(curr_set()), GUI_SML, GUI_ALL,
176                       gui_yel, gui_red);
177             gui_filler(jd);
178             gui_start(jd, _("Back"),  GUI_SML, START_BACK, 0);
179         }
180
181         if ((jd = gui_harray(id)))
182         {
183             if (config_cheat())
184             {
185                 if ((kd = gui_vstack(jd)))
186                 {
187                     shot_id = gui_image(kd, set_shot(curr_set()),
188                                         7 * w / 16, 7 * h / 16);
189                     file_id = gui_label(kd, " ", GUI_SML, GUI_ALL,
190                                         gui_yel, gui_red);
191                 }
192             }
193             else
194             {
195                 shot_id = gui_image(jd, set_shot(curr_set()),
196                                     7 * w / 16, 7 * h / 16);
197             }
198
199             if ((kd = gui_varray(jd)))
200             {
201                 for (i = 0; i < 5; i++)
202                     if ((ld = gui_harray(kd)))
203                         for (j = 4; j >= 0; j--)
204                             gui_level(ld, i * 5 + j);
205
206                 challenge_id = gui_state(kd, _("Challenge"),
207                                          GUI_SML, START_CHALLENGE,
208                                          curr_mode() == MODE_CHALLENGE);
209             }
210         }
211         gui_space(id);
212
213         if ((jd = gui_hstack(id)))
214             gui_score_board(jd, GUI_MOST_COINS |
215                                 GUI_BEST_TIMES |
216                                 GUI_FAST_UNLOCK, 0, 0);
217
218         gui_space(id);
219
220         if ((jd = gui_hstack(id)))
221         {
222             gui_filler(jd);
223
224             if ((kd = gui_harray(jd)))
225             {
226                 /* TODO, replace the whitespace hack with something sane. */
227
228                 gui_state(kd,
229                           /* Translators: adjust the amount of whitespace here
230                            * as necessary for the buttons to look good. */
231                           _("   No   "), GUI_SML, START_OPEN_GOALS,
232                           config_get_d(CONFIG_LOCK_GOALS) == 0);
233
234                 gui_state(kd, _("Yes"), GUI_SML, START_LOCK_GOALS,
235                           config_get_d(CONFIG_LOCK_GOALS) == 1);
236             }
237
238             gui_space(jd);
239
240             gui_label(jd, _("Lock Goals of Completed Levels?"),
241                       GUI_SML, GUI_ALL, 0, 0);
242
243             gui_filler(jd);
244         }
245
246         gui_layout(id, 0, 0);
247
248         set_score_board(NULL, -1, NULL, -1, NULL, -1);
249     }
250
251     audio_music_fade_to(0.5f, "bgm/inter.ogg");
252
253     return id;
254 }
255
256 static void start_point(int id, int x, int y, int dx, int dy)
257 {
258     start_over(gui_point(id, x, y), 1);
259 }
260
261 static void start_stick(int id, int a, int v)
262 {
263     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
264     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
265
266     start_over(gui_stick(id, x, y), 1);
267 }
268
269 static int start_keybd(int c, int d)
270 {
271     if (d)
272     {
273         if (c == SDLK_c && config_cheat())
274         {
275             set_cheat();
276             return goto_state(&st_start);
277         }
278         else if (c == SDLK_F12 && config_cheat())
279         {
280             char *dir = concat_string("Screenshots/shot-",
281                                       set_id(curr_set()), NULL);
282             int i;
283
284             fs_mkdir(dir);
285
286             /* Iterate over all levels, taking a screenshot of each. */
287
288             for (i = 0; i < MAXLVL; i++)
289                 if (level_exists(i))
290                     level_snap(i, dir);
291
292             free(dir);
293         }
294         else if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
295         {
296             int active = gui_click();
297
298             if (start_action(gui_score_next(gui_score_get())))
299             {
300                 /* HACK ALERT
301                  *
302                  * This assumes that 'active' is a valid widget ID even after
303                  * the above start_action has recreated the entire widget
304                  * hierarchy.  Maybe it is.  Maybe it isn't.
305                  */
306                 gui_focus(active);
307                 start_over(active, 0);
308
309                 return 1;
310             }
311             else
312                 return 0;
313         }
314     }
315
316     return 1;
317 }
318
319 static int start_buttn(int b, int d)
320 {
321     if (d)
322     {
323         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
324             return start_action(gui_token(gui_click()));
325         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
326             return start_action(START_BACK);
327     }
328     return 1;
329 }
330
331 /*---------------------------------------------------------------------------*/
332
333 struct state st_start = {
334     start_enter,
335     shared_leave,
336     shared_paint,
337     shared_timer,
338     start_point,
339     start_stick,
340     shared_angle,
341     shared_click,
342     start_keybd,
343     start_buttn,
344     1, 0
345 };