Merged source:branches/pause (revisions 954:1064) into trunk (replace the old pause...
[neverball] / ball / st_pause.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 "config.h"
17 #include "game.h"
18 #include "levels.h"
19 #include "level.h"
20 #include "audio.h"
21 #include "hud.h"
22
23 #include "st_play.h"
24 #include "st_start.h"
25 #include "st_shared.h"
26 #include "st_pause.h"
27
28 #define PAUSE_CONTINUE 1
29 #define PAUSE_RESTART  2
30 #define PAUSE_EXIT     3
31
32 /*---------------------------------------------------------------------------*/
33
34 static struct state *st_continue;
35 static int paused;
36
37 int goto_pause(void)
38 {
39     st_continue = curr_state();
40     paused = 1;
41     return goto_state(&st_pause);
42 }
43
44 int is_paused(void)
45 {
46     return paused;
47 }
48
49 void clear_pause(void)
50 {
51     paused = 0;
52 }
53
54 /*---------------------------------------------------------------------------*/
55
56 static int pause_action(int i)
57 {
58     audio_play(AUD_MENU, 1.0f);
59
60     switch(i)
61     {
62     case PAUSE_CONTINUE:
63         audio_music_fade_in(0.5f);
64         config_set_grab(0);
65         return goto_state(st_continue);
66
67     case PAUSE_RESTART:
68         level_stop(GAME_NONE, 0, curr_clock(), curr_coins());
69         clear_pause();
70         level_play_go();
71         audio_music_fade_in (2.0f);
72         config_set_grab(1);
73         return goto_state(&st_play_set);
74
75     case PAUSE_EXIT:
76         level_stop(GAME_NONE, 0, curr_clock(), curr_coins());
77         clear_pause();
78         return curr_lg()->mode == MODE_SINGLE ? 0 : goto_state(&st_start);
79     }
80
81     return 1;
82 }
83
84
85 static int pause_enter(void)
86 {
87     int id, jd, title_id;
88
89     config_clr_grab();
90
91     audio_music_fade_out(0.2f);
92
93     /* Build the pause GUI. */
94
95     if ((id = gui_vstack(0)))
96     {
97         title_id = gui_label(id, _("Paused"), GUI_LRG, GUI_ALL, 0, 0);
98
99         gui_space(id);
100
101         if ((jd = gui_harray(id)))
102         {
103             gui_state(jd, _("Quit"), GUI_SML, PAUSE_EXIT, 0);
104
105             if (curr_lg()->mode != MODE_CHALLENGE)
106                 gui_state(jd, _("Restart"), GUI_SML, PAUSE_RESTART, 0);
107             else
108             {
109                 int ld = gui_state(jd, _("Restart"), GUI_SML, 0, 0);
110                 gui_set_color(ld, gui_gry, gui_gry);
111             }
112
113             gui_start(jd, _("Continue"), GUI_SML, PAUSE_CONTINUE, 1);
114         }
115
116         gui_pulse(title_id, 1.2f);
117         gui_layout(id, 0, 0);
118     }
119
120     hud_update(0);
121
122     return id;
123 }
124
125 void pause_paint(int id, float st)
126 {
127     shared_paint(id, st);
128     hud_paint();
129 }
130
131 void pause_timer(int id, float dt)
132 {
133     shared_timer (id, dt);
134     hud_timer (dt);
135 }
136
137 static int pause_keybd(int c, int d)
138 {
139     if (d)
140     {
141         if (config_tst_d(CONFIG_KEY_PAUSE, c))
142             return pause_action(PAUSE_CONTINUE);
143
144         if (config_tst_d(CONFIG_KEY_RESTART, c)
145             && curr_lg()->mode != MODE_CHALLENGE)
146             return pause_action(PAUSE_RESTART);
147     }
148     return 1;
149 }
150
151 static int pause_buttn(int b, int d)
152 {
153     if (d)
154     {
155         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
156             return pause_action(gui_token(gui_click()));
157
158         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
159         {
160             if (SDL_GetModState() & (KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_META))
161                 return pause_action(PAUSE_EXIT);
162             else
163                 return pause_action(PAUSE_CONTINUE);
164         }
165     }
166     return 1;
167 }
168
169 /*---------------------------------------------------------------------------*/
170
171 struct state st_pause = {
172     pause_enter,
173     shared_leave,
174     pause_paint,
175     pause_timer,
176     shared_point,
177     shared_stick,
178     shared_click,
179     pause_keybd,
180     pause_buttn,
181     1, 0
182 };