Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[neverball] / ball / st_time_out.c
1 /*
2  * Copyright (C) 2007 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 "game.h"
16 #include "util.h"
17 #include "levels.h"
18 #include "demo.h"
19 #include "audio.h"
20 #include "gui.h"
21 #include "config.h"
22
23 #include "st_over.h"
24 #include "st_start.h"
25 #include "st_save.h"
26 #include "st_time_out.h"
27 #include "st_level.h"
28 #include "st_shared.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 #define TIME_OUT_NEXT 1
33 #define TIME_OUT_SAME 2
34 #define TIME_OUT_SAVE 3
35 #define TIME_OUT_BACK 4
36 #define TIME_OUT_OVER 5
37
38 static int time_out_action(int i)
39 {
40     audio_play(AUD_MENU, 1.0f);
41
42     switch (i)
43     {
44     case TIME_OUT_BACK:
45         /* Fall through. */
46
47     case TIME_OUT_OVER:
48         level_stop();
49         return goto_state(&st_over);
50
51     case TIME_OUT_SAVE:
52         level_stop();
53         return goto_save(&st_time_out, &st_time_out);
54
55     case TIME_OUT_NEXT:
56         level_next();
57         return goto_state(&st_level);
58
59     case TIME_OUT_SAME:
60         level_same();
61         return goto_state(&st_level);
62     }
63
64     return 1;
65 }
66
67 static int time_out_enter(void)
68 {
69     int id, jd, kd;
70
71     const struct level_game *lg = curr_lg();
72
73     if ((id = gui_vstack(0)))
74     {
75         kd = gui_label(id, _("Time's Up!"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
76
77         gui_space(id);
78
79         if ((jd = gui_harray(id)))
80         {
81             int next_id = 0, retry_id = 0;
82
83             next_id = gui_maybe(jd, _("Next Level"),  TIME_OUT_NEXT,
84                                 lg->next_level != NULL);
85
86             if (lg->dead)
87                 gui_start(jd, _("Game Over"), GUI_SML, TIME_OUT_OVER, 0);
88             else
89             {
90                 retry_id = gui_state(jd, _("Retry Level"), GUI_SML,
91                                      TIME_OUT_SAME, 0);
92             }
93
94             gui_maybe(jd, _("Save Replay"), TIME_OUT_SAVE, demo_saved());
95
96             /* Default is next if the next level is newly unlocked. */
97
98             if (next_id && lg->unlock)
99                 gui_focus(next_id);
100             else if (retry_id)
101                 gui_focus(retry_id);
102         }
103         gui_space(id);
104
105         gui_pulse(kd, 1.2f);
106         gui_layout(id, 0, 0);
107     }
108
109     audio_music_fade_out(2.0f);
110     /* audio_play(AUD_TIME, 1.0f); */
111
112     config_clr_grab();
113
114     return id;
115 }
116
117 static int time_out_buttn(int b, int d)
118 {
119     if (d)
120     {
121         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
122             return time_out_action(gui_token(gui_click()));
123         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
124             return time_out_action(TIME_OUT_BACK);
125     }
126     return 1;
127 }
128
129 /*---------------------------------------------------------------------------*/
130
131 struct state st_time_out = {
132     time_out_enter,
133     shared_leave,
134     shared_paint,
135     shared_timer,
136     shared_point,
137     shared_stick,
138     shared_angle,
139     shared_click,
140     NULL,
141     time_out_buttn,
142     1, 0
143 };
144