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