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