5525087b754a27f05296101ca36fcd3def805e1c
[neverball] / ball / st_set.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 "game.h"
18 #include "audio.h"
19 #include "config.h"
20
21 #include "st_set.h"
22 #include "st_title.h"
23 #include "st_start.h"
24
25 /*---------------------------------------------------------------------------*/
26
27 #define SET_BACK -1
28 #define SET_PREV -2
29 #define SET_NEXT -3
30
31 #define SET_GROUP 4 /* number of sets in one screen */
32
33 static int shot_id;
34 static int desc_id;
35
36 static int last_set; /* TODO: Use config instead of a global variable */
37
38 static int set_action(int i)
39 {
40     audio_play(AUD_MENU, 1.0f);
41     
42     if (i == SET_BACK)
43         return goto_state(&st_title);
44     else if (i == SET_PREV)
45     {
46         last_set = ((last_set/SET_GROUP)-1)*SET_GROUP;
47         return goto_state(&st_set);
48     }
49     else if (i == SET_NEXT)
50     {
51         last_set = ((last_set/SET_GROUP)+1)*SET_GROUP;
52         return goto_state(&st_set);
53     }
54     else if (set_exists(i))
55     {
56         last_set = i;
57         set_goto(i);
58         return goto_state(&st_start);
59     }
60     return 1;
61 }
62
63 static int set_enter(void)
64 {
65     int w = config_get_d(CONFIG_WIDTH);
66     int h = config_get_d(CONFIG_HEIGHT);
67     int b = last_set / SET_GROUP;
68     int i;
69
70     int id, jd, kd, ld;
71
72     set_init();
73
74     audio_music_fade_to(0.5f, "bgm/inter.ogg");
75     audio_play(AUD_START, 1.f);
76
77     if ((id = gui_vstack(0)))
78     {
79         if ((jd = gui_hstack(id)))
80         {
81             gui_label(jd, "Level Set", GUI_SML, GUI_ALL, gui_yel, gui_red);
82             gui_filler(jd);
83             gui_state(jd, "Back",  GUI_SML, SET_BACK, 0);
84         }
85
86         if ((jd = gui_harray(id)))
87         {
88             shot_id = gui_image(jd, set_shot(last_set), 7 * w / 16, 7 * h / 16);
89
90             if ((kd = gui_varray(jd)))
91             {
92                 /* Display levels */
93                 for(i=b*SET_GROUP; i<(b+1)*SET_GROUP && set_exists(i); i++)
94                 {
95                     if(last_set == i)
96                         gui_start(kd, set_name(i), GUI_SML, i, 0);
97                     else
98                         gui_state(kd, set_name(i), GUI_SML, i, 0);
99                 }
100                 
101                 /* Display Prev/Next buttons */ 
102                 ld = gui_harray(kd);
103                 if (set_exists(i))
104                     gui_state(ld, "Next", GUI_SML, SET_NEXT, 0);
105                 else
106                     gui_label(ld, "Next", GUI_SML, GUI_ALL, gui_gry, gui_gry);
107                 if (b>0)
108                     gui_state(ld, "Prev", GUI_SML, SET_PREV, 0);
109                 else
110                     gui_label(ld, "Prev", GUI_SML, GUI_ALL, gui_gry, gui_gry);
111
112                 /* Display empty slots */
113                 for(; i<(b+1)*SET_GROUP; i++)
114                     gui_filler(kd);
115             }          
116         }
117
118         gui_space(id);
119         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL, gui_yel, gui_wht);
120
121         gui_layout(id, 0, 0);
122     }
123     return id;
124 }
125
126 static void set_leave(int id)
127 {
128     gui_delete(id);
129 }
130
131 static void set_paint(int id, float st)
132 {
133     game_draw(0, st);
134     config_pop_matrix();
135     gui_paint(id);
136 }
137
138 static void set_timer(int id, float dt)
139 {
140     gui_timer(id, dt);
141     audio_timer(dt);
142 }
143
144 static void set_point(int id, int x, int y, int dx, int dy)
145 {
146     int jd;
147
148     if ((jd = gui_point(id, x, y)))
149     {
150         int i = gui_token(jd);
151         if (set_exists(i))
152         {
153             gui_set_image(shot_id, set_shot(i));
154             gui_set_multi(desc_id, set_desc(i));
155             gui_pulse(jd, 1.2f);
156         }
157     }
158 }
159
160 static void set_stick(int id, int a, int v)
161 {
162     int jd;
163
164     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
165     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
166
167     if ((jd = gui_stick(id, x, y)))
168     {
169         int i = gui_token(jd);
170
171         if (set_exists(i))
172         {
173             gui_set_image(shot_id, set_shot(i));
174             gui_set_multi(desc_id, set_desc(i));
175             gui_pulse(jd, 1.2f);
176         }
177     }
178 }
179
180 static int set_click(int b, int d)
181 {
182     return (b < 0 && d == 1) ? set_action(gui_token(gui_click())) : 1;
183 }
184
185 static int set_keybd(int c, int d)
186 {
187     return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
188 }
189
190 static int set_buttn(int b, int d)
191 {
192     if (d)
193     {
194         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
195             return set_action(gui_token(gui_click()));
196         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
197             return goto_state(&st_title);
198         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
199             return goto_state(&st_title);
200     }
201     return 1;
202 }
203
204 /*---------------------------------------------------------------------------*/
205
206 struct state st_set = {
207     set_enter,
208     set_leave,
209     set_paint,
210     set_timer,
211     set_point,
212     set_stick,
213     set_click,
214     set_keybd,
215     set_buttn,
216     1, 0
217 };