Add a new widget: a 'maybe' button :)
[neverball] / ball / st_demo.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 "hud.h"
19 #include "set.h"
20 #include "game.h"
21 #include "demo.h"
22 #include "levels.h"
23 #include "audio.h"
24 #include "solid.h"
25 #include "config.h"
26 #include "st_shared.h"
27 #include "util.h"
28
29 #include "st_demo.h"
30 #include "st_title.h"
31
32 extern struct state st_demo_play;
33 extern struct state st_demo_end;
34 extern struct state st_demo_del;
35
36 /*---------------------------------------------------------------------------*/
37
38 #define DEMO_LINE 4
39 #define DEMO_STEP 8
40
41 static int first = 0;
42 static int total = 0;
43
44 static float replay_time;
45 static float global_time;
46
47 /*---------------------------------------------------------------------------*/
48
49 static int demo_action(int i)
50 {
51     audio_play(AUD_MENU, 1.0f);
52
53     switch (i)
54     {
55     case GUI_BACK:
56         return goto_state(&st_title);
57
58     case GUI_NEXT:
59         first += DEMO_STEP;
60         return goto_state(&st_demo);
61         break;
62
63     case GUI_PREV:
64         first -= DEMO_STEP;
65         return goto_state(&st_demo);
66         break;
67
68     default:
69         if (level_replay(get_demo(i)->filename))
70             return goto_demo_play(0);
71     }
72     return 1;
73 }
74
75 static void demo_replay(int id, int i)
76 {
77     int w = config_get_d(CONFIG_WIDTH);
78     int h = config_get_d(CONFIG_HEIGHT);
79     int jd;
80
81     if ((jd = gui_vstack(id)))
82     {
83         gui_space(jd);
84
85         gui_image(jd, get_demo(i)->shot, w / 6, h / 6);
86         gui_state(jd, get_demo(i)->name, GUI_SML, i, 0);
87
88         gui_active(jd, i, 0);
89     }
90 }
91
92 static int name_id;
93 static int time_id;
94 static int coin_id;
95 static int date_id;
96 static int level_id;
97 static int mode_id;
98 static int state_id;
99 static int player_id;
100
101 static int gui_demo_status(int id, const struct demo * d)
102 /* Create a layout for some demo info, if d is NULL, try to reserve enought space */
103 {
104     char noname[MAXNAM];
105     const char *mode, *state;
106     int i, j, k;
107     int jd, kd, ld, md;
108
109     if (d == NULL)
110     {
111         /* Build a long name */
112         memset(noname, 'M', MAXNAM-1);
113         noname[MAXNAM-1] = '\0';
114
115         /* Get a long mode */
116         mode = mode_to_str(0);
117         j = strlen(mode);
118         for(i=1; i<=MODE_SINGLE; i++)
119         {
120             k = strlen(mode_to_str(i));
121             if (k > j)
122             {
123                 j = k;
124                 mode = mode_to_str(i);
125             }
126         }
127
128         /* Get a long state */
129         state = state_to_str(0);
130         j = strlen(state);
131         for(i=1; i<=GAME_FALL; i++)
132         {
133             k = strlen(state_to_str(i));
134             if (k > j)
135             {
136                 j = k;
137                 state = state_to_str(i);
138             }
139         }
140     }
141     else
142     {
143         mode  = mode_to_str(d->mode);
144         state = state_to_str(d->state);
145     }
146     
147     if ((jd = gui_hstack(id)))
148     {
149         if((kd = gui_vstack(jd)))
150         {
151             if ((ld = gui_harray(kd)))
152             {
153                 if ((md = gui_vstack(ld)))
154                 {
155                     player_id = gui_label(md, (d?d->player:noname), GUI_SML, GUI_RGT, 0, 0);
156                     coin_id = gui_count(md, (d?d->coins:100),       GUI_SML, GUI_RGT);
157                     state_id = gui_label(md, state,                 GUI_SML, GUI_RGT, gui_red, gui_red);
158                 }
159                 if ((md = gui_vstack(ld)))
160                 {
161                     gui_label(md, _("Player"),                      GUI_SML, GUI_LFT, gui_wht, gui_wht);
162                     gui_label(md, _("Coins"),                       GUI_SML, GUI_LFT, gui_wht, gui_wht);
163                     gui_label(md, _("State"),                       GUI_SML, GUI_LFT, gui_wht, gui_wht);
164                 }
165                 if ((md = gui_vstack(ld)))
166                 {
167                     name_id = gui_label(md, (d?d->name:noname),     GUI_SML, GUI_RGT, 0, 0);
168                     time_id = gui_clock(md, (d?d->timer:35000),     GUI_SML, GUI_RGT);
169                     mode_id = gui_label(md, mode,                   GUI_SML, GUI_RGT, 0, 0);
170                 }
171             }
172             level_id = gui_label(kd, (d?d->file:"M"),             GUI_SML, GUI_RGT, gui_wht, gui_wht);
173             date_id = gui_label(kd, (d?date_to_str(d->date):"M"), GUI_SML, GUI_RGT, 0, 0);
174         }
175         if((kd = gui_vstack(jd)))
176         {
177             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
178             gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
179             gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
180             gui_label(kd, _("Level"),  GUI_SML, GUI_LFT, gui_wht, gui_wht);
181             gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
182         }
183         if(d && d->state == GAME_GOAL)
184             gui_set_color(state_id, gui_grn, gui_grn);
185     }
186     return jd;
187 }
188
189 static void gui_demo_update_status(int i)
190 {
191     const struct demo * d = get_demo(i);
192
193     gui_set_label(name_id,   d->name);
194     gui_set_label(date_id,   date_to_str(d->date));
195     gui_set_label(level_id,   d->file);
196     gui_set_label(player_id, d->player);
197     gui_set_label(mode_id,   mode_to_str(d->mode));
198     if (d->state == GAME_GOAL)
199         gui_set_color(state_id, gui_grn, gui_grn);
200     else
201         gui_set_color(state_id, gui_red, gui_red);
202     gui_set_label(state_id,  state_to_str(d->state));
203     gui_set_count(coin_id,   d->coins);
204     gui_set_clock(time_id,   d->timer);
205 }
206
207 static int demo_enter(void)
208 {
209     int i, j;
210     int id, jd, kd, ld;
211
212     total = demo_scan();
213
214     id = gui_vstack(0);
215     if (total == 0)
216     {
217         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
218         gui_filler(id);
219         gui_multi(id, _("You can save replays of your games.\\"
220                         "Currently, there are no replays saved."),
221                   GUI_SML, GUI_ALL, gui_wht, gui_wht);
222         gui_filler(id);
223         gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
224         gui_layout(id, 0, 0);
225     }
226     else
227     {
228         if ((jd = gui_hstack(id)))
229         {
230
231             ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
232             gui_filler(jd);
233             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
234         }
235         if ((jd = gui_varray(id)))
236             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
237                 if ((kd = gui_harray(jd)))
238                 {
239                     for (j = i + DEMO_LINE - 1; j >= i; j--)
240                         if (j < total)
241                             demo_replay(kd, j);
242                         else
243                             gui_space(kd);
244                 }
245         gui_filler(id);
246         gui_demo_status(id, NULL);
247         gui_layout(id, 0, 0);
248         gui_demo_update_status(0);
249     }
250
251     audio_music_fade_to(0.5f, "bgm/inter.ogg");
252
253     return id;
254 }
255
256 static void demo_point(int id, int x, int y, int dx, int dy)
257 {
258     int jd = shared_point_basic(id, x, y);
259     int i  = gui_token(jd);
260     if (jd && i>=0)
261         gui_demo_update_status(i);
262 }
263
264 static void demo_stick(int id, int a, int v)
265 {
266     int jd = shared_stick_basic(id, a, v);
267     int i  = gui_token(jd);
268     if (jd && i>=0)
269         gui_demo_update_status(i);
270 }
271
272 static int demo_buttn(int b, int d)
273 {
274     if (d)
275     {
276         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
277             return demo_action(gui_token(gui_click()));
278         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
279             return demo_action(GUI_BACK);
280     }
281     return 1;
282 }
283
284 /*---------------------------------------------------------------------------*/
285
286 static int simple_play;
287 int goto_demo_play(int simple)
288 {
289     simple_play = simple;
290     return goto_state(&st_demo_play);
291 }
292
293 static int demo_play_enter(void)
294 {
295     int id;
296
297     if ((id = gui_vstack(0)))
298     {
299         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
300         gui_layout(id, 0, 0);
301         gui_pulse(id, 1.2f);
302     }
303
304     global_time = -1.f;
305     replay_time =  0.f;
306
307     hud_update(0);
308
309     game_set_fly(0.f);
310
311     return id;
312 }
313
314 static void demo_play_paint(int id, float st)
315 {
316     game_draw(0, st);
317     hud_paint();
318
319     if (time_state() < 1.f)
320         gui_paint(id);
321 }
322
323 static void demo_play_timer(int id, float dt)
324 {
325     float t;
326
327     game_step_fade(dt);
328     gui_timer(id, dt);
329     audio_timer(dt);
330
331     global_time += dt;
332     hud_timer(dt);
333
334     /* Spin or skip depending on how fast the demo wants to run. */
335
336     while (replay_time < global_time)
337         if (demo_replay_step(&t))
338         {
339             replay_time += t;
340         }
341         else 
342         {
343             goto_state(&st_demo_end);
344             break;
345         }
346 }
347
348 static int demo_play_buttn(int b, int d)
349 {
350     if (d)
351     {
352         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
353             return goto_state(&st_demo_end);
354     }
355     return 1;
356 }
357
358 /*---------------------------------------------------------------------------*/
359
360 #define DEMO_KEEP    0
361 #define DEMO_DEL     1
362 #define DEMO_QUIT    2
363 #define DEMO_REPLAY  3
364
365 static int demo_end_action(int i)
366 {
367     audio_play(AUD_MENU, 1.0f);
368
369     switch (i)
370     {
371     case DEMO_DEL: 
372         return goto_state(&st_demo_del);
373     case DEMO_KEEP:
374         demo_replay_stop(0);
375         return goto_state(&st_demo);
376     case DEMO_QUIT:
377         demo_replay_stop(0);
378         return 0;
379     case DEMO_REPLAY:
380         demo_replay_stop(0);
381         level_replay(curr_demo_replay()->filename);
382         return goto_state(&st_demo_play);
383     }
384     return 1;
385 }
386
387 static int demo_end_enter(void)
388 {
389     int id, jd, kd;
390
391     if ((id = gui_vstack(0)))
392     {
393         kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
394
395         if ((jd = gui_harray(id)))
396         {
397             gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
398             if (simple_play)
399                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
400             else
401             {
402                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
403                 gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
404             }
405         }
406
407         gui_filler(id);
408
409         if ((jd = gui_hstack(id)))
410         {
411             gui_filler(jd);
412             gui_demo_status(jd, curr_demo_replay());
413             gui_filler(jd);
414         }
415
416         gui_pulse(kd, 1.2f);
417         gui_layout(id, 0, 0);
418     }
419     audio_music_fade_out(2.0f);
420
421     return id;
422 }
423
424 static void demo_end_timer(int id, float dt)
425 {
426     float g[3] = { 0.0f, -9.8f, 0.0f };
427
428     if (time_state() < 2.f)
429         game_step(g, dt, NULL);
430                 
431     gui_timer(id, dt);
432     audio_timer(dt);
433 }
434
435 static int demo_end_buttn(int b, int d)
436 {
437     if (d)
438     {
439         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
440             return demo_end_action(gui_token(gui_click()));
441         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
442             return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
443     }
444     return 1;
445 }
446
447 /*---------------------------------------------------------------------------*/
448
449 static int demo_del_action(int i)
450 {
451     audio_play(AUD_MENU, 1.0f);
452
453     demo_replay_stop(i == DEMO_DEL);
454     return goto_state(&st_demo);
455 }
456
457 static int demo_del_enter(void)
458 {
459     int id, jd, kd;
460
461     if ((id = gui_vstack(0)))
462     {
463         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
464
465         if ((jd = gui_harray(id)))
466         {
467             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
468             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
469         }
470
471         gui_pulse(kd, 1.2f);
472         gui_layout(id, 0, 0);
473     }
474     audio_music_fade_out(2.0f);
475
476     return id;
477 }
478
479 static int demo_del_buttn(int b, int d)
480 {
481     if (d)
482     {
483         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
484             return demo_del_action(gui_token(gui_click()));
485         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
486             return demo_del_action(DEMO_KEEP);
487     }
488     return 1;
489 }
490
491 /*---------------------------------------------------------------------------*/
492
493 struct state st_demo = {
494     demo_enter,
495     shared_leave,
496     shared_paint,
497     shared_timer,
498     demo_point,
499     demo_stick,
500     shared_click,
501     NULL,
502     demo_buttn,
503     0
504 };
505
506 struct state st_demo_play = {
507     demo_play_enter,
508     shared_leave,
509     demo_play_paint,
510     demo_play_timer,
511     NULL,
512     NULL,
513     NULL,
514     NULL,
515     demo_play_buttn,
516     0
517 };
518
519 struct state st_demo_end = {
520     demo_end_enter,
521     shared_leave,
522     shared_paint,
523     demo_end_timer,
524     shared_point,
525     shared_stick,
526     shared_click,
527     NULL,
528     demo_end_buttn,
529     1, 0
530 };
531
532 struct state st_demo_del = {
533     demo_del_enter,
534     shared_leave,
535     shared_paint,
536     shared_timer,
537     shared_point,
538     shared_stick,
539     shared_click,
540     NULL,
541     demo_del_buttn,
542     1, 0
543 };