Un-internationalised the title strings and updated POs once again.
[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 id;
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_label(0, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
298     {
299         gui_layout(id, 0, 0);
300         gui_pulse(id, 1.2f);
301     }
302
303     global_time = -1.f;
304     replay_time =  0.f;
305
306     hud_update(0);
307
308     game_set_fly(0.f);
309
310     return id;
311 }
312
313 static void demo_play_paint(int id, float st)
314 {
315     game_draw(0, st);
316     hud_paint();
317
318     if (time_state() < 1.f)
319         gui_paint(id);
320 }
321
322 static void demo_play_timer(int id, float dt)
323 {
324     float t;
325
326     game_step_fade(dt);
327     gui_timer(id, dt);
328     audio_timer(dt);
329
330     global_time += dt;
331     hud_timer(dt);
332
333     /* Spin or skip depending on how fast the demo wants to run. */
334
335     while (replay_time < global_time)
336         if (demo_replay_step(&t))
337         {
338             replay_time += t;
339         }
340         else 
341         {
342             goto_state(&st_demo_end);
343             break;
344         }
345 }
346
347 static int demo_play_buttn(int b, int d)
348 {
349     if (d)
350     {
351         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
352             return goto_state(&st_demo_end);
353     }
354     return 1;
355 }
356
357 /*---------------------------------------------------------------------------*/
358
359 #define DEMO_KEEP    0
360 #define DEMO_DEL     1
361 #define DEMO_QUIT    2
362 #define DEMO_REPLAY  3
363
364 static int demo_end_action(int i)
365 {
366     audio_play(AUD_MENU, 1.0f);
367
368     switch (i)
369     {
370     case DEMO_DEL: 
371         return goto_state(&st_demo_del);
372     case DEMO_KEEP:
373         demo_replay_stop(0);
374         return goto_state(&st_demo);
375     case DEMO_QUIT:
376         demo_replay_stop(0);
377         return 0;
378     case DEMO_REPLAY:
379         demo_replay_stop(0);
380         level_replay(curr_demo_replay()->filename);
381         return goto_state(&st_demo_play);
382     }
383     return 1;
384 }
385
386 static int demo_end_enter(void)
387 {
388     int id, jd, kd;
389
390     if ((id = gui_vstack(0)))
391     {
392         kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
393
394         if ((jd = gui_harray(id)))
395         {
396             gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
397             if (simple_play)
398                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
399             else
400             {
401                 gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
402                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
403             }
404         }
405
406         gui_filler(id);
407
408         if ((jd = gui_hstack(id)))
409         {
410             gui_filler(jd);
411             gui_demo_status(jd, curr_demo_replay());
412             gui_filler(jd);
413         }
414
415         gui_pulse(kd, 1.2f);
416         gui_layout(id, 0, 0);
417     }
418     audio_music_fade_out(2.0f);
419
420     return id;
421 }
422
423 static void demo_end_timer(int id, float dt)
424 {
425     float g[3] = { 0.0f, -9.8f, 0.0f };
426
427     if (time_state() < 2.f)
428         game_step(g, dt, 0);
429                 
430     gui_timer(id, dt);
431     audio_timer(dt);
432 }
433
434 static int demo_end_buttn(int b, int d)
435 {
436     if (d)
437     {
438         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
439             return demo_end_action(gui_token(gui_click()));
440         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
441             return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
442     }
443     return 1;
444 }
445
446 /*---------------------------------------------------------------------------*/
447
448 static int demo_del_action(int i)
449 {
450     audio_play(AUD_MENU, 1.0f);
451
452     demo_replay_stop(i == DEMO_DEL);
453     return goto_state(&st_demo);
454 }
455
456 static int demo_del_enter(void)
457 {
458     int id, jd, kd;
459
460     if ((id = gui_vstack(0)))
461     {
462         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
463
464         if ((jd = gui_harray(id)))
465         {
466             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
467             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
468         }
469
470         gui_pulse(kd, 1.2f);
471         gui_layout(id, 0, 0);
472     }
473     audio_music_fade_out(2.0f);
474
475     return id;
476 }
477
478 static int demo_del_buttn(int b, int d)
479 {
480     if (d)
481     {
482         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
483             return demo_del_action(gui_token(gui_click()));
484         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
485             return demo_del_action(DEMO_KEEP);
486     }
487     return 1;
488 }
489
490 /*---------------------------------------------------------------------------*/
491
492 struct state st_demo = {
493     demo_enter,
494     shared_leave,
495     shared_paint,
496     shared_timer,
497     demo_point,
498     demo_stick,
499     shared_click,
500     NULL,
501     demo_buttn,
502     0
503 };
504
505 struct state st_demo_play = {
506     demo_play_enter,
507     shared_leave,
508     demo_play_paint,
509     demo_play_timer,
510     NULL,
511     NULL,
512     NULL,
513     NULL,
514     demo_play_buttn,
515     0
516 };
517
518 struct state st_demo_end = {
519     demo_end_enter,
520     shared_leave,
521     shared_paint,
522     demo_end_timer,
523     shared_point,
524     shared_stick,
525     shared_click,
526     NULL,
527     demo_end_buttn,
528     1, 0
529 };
530
531 struct state st_demo_del = {
532     demo_del_enter,
533     shared_leave,
534     shared_paint,
535     shared_timer,
536     shared_point,
537     shared_stick,
538     shared_click,
539     NULL,
540     demo_del_buttn,
541     1, 0
542 };