share/gui: don't use less of widget width for truncation than available
[neverball] / putt / st_conf.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERPUTT 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 "hud.h"
17 #include "back.h"
18 #include "geom.h"
19 #include "ball.h"
20 #include "part.h"
21 #include "game.h"
22 #include "audio.h"
23 #include "config.h"
24 #include "video.h"
25
26 #include "st_conf.h"
27 #include "st_all.h"
28 #include "st_resol.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 enum {
33     CONF_FULL = 1,
34     CONF_WIN,
35     CONF_TEXHI,
36     CONF_TEXLO,
37     CONF_GEOHI,
38     CONF_GEOLO,
39     CONF_SHDON,
40     CONF_SHDOF,
41     CONF_BACK,
42     CONF_RESOL
43 };
44
45 static int music_id[11];
46 static int sound_id[11];
47
48 static int conf_action(int i)
49 {
50     int w = config_get_d(CONFIG_WIDTH);
51     int h = config_get_d(CONFIG_HEIGHT);
52     int s = config_get_d(CONFIG_SOUND_VOLUME);
53     int m = config_get_d(CONFIG_MUSIC_VOLUME);
54     int r = 1;
55
56     audio_play(AUD_MENU, 1.0f);
57
58     switch (i)
59     {
60     case CONF_FULL:
61         goto_state(&st_null);
62         r = video_mode(1, w, h);
63         goto_state(&st_conf);
64         break;
65
66     case CONF_WIN:
67         goto_state(&st_null);
68         r = video_mode(0, w, h);
69         goto_state(&st_conf);
70         break;
71
72     case CONF_TEXHI:
73         goto_state(&st_null);
74         config_set_d(CONFIG_TEXTURES, 1);
75         goto_state(&st_conf);
76         break;
77
78     case CONF_TEXLO:
79         goto_state(&st_null);
80         config_set_d(CONFIG_TEXTURES, 2);
81         goto_state(&st_conf);
82         break;
83
84     case CONF_GEOHI:
85         goto_state(&st_null);
86         config_set_d(CONFIG_GEOMETRY, 1);
87         goto_state(&st_conf);
88         break;
89
90     case CONF_GEOLO:
91         goto_state(&st_null);
92         config_set_d(CONFIG_GEOMETRY, 0);
93         goto_state(&st_conf);
94         break;
95
96     case CONF_SHDON:
97         goto_state(&st_null);
98         config_set_d(CONFIG_SHADOW, 1);
99         goto_state(&st_conf);
100         break;
101
102     case CONF_SHDOF:
103         goto_state(&st_null);
104         config_set_d(CONFIG_SHADOW, 0);
105         goto_state(&st_conf);
106         break;
107
108     case CONF_BACK:
109         goto_state(&st_title);
110         break;
111
112     case CONF_RESOL:
113         goto_state(&st_resol);
114         break;
115
116     default:
117         if (100 <= i && i <= 110)
118         {
119             int n = i - 100;
120
121             config_set_d(CONFIG_SOUND_VOLUME, n);
122             audio_volume(n, m);
123             audio_play(AUD_BUMP, 1.f);
124
125             gui_toggle(sound_id[n]);
126             gui_toggle(sound_id[s]);
127         }
128         if (200 <= i && i <= 210)
129         {
130             int n = i - 200;
131
132             config_set_d(CONFIG_MUSIC_VOLUME, n);
133             audio_volume(s, n);
134             audio_play(AUD_BUMP, 1.f);
135
136             gui_toggle(music_id[n]);
137             gui_toggle(music_id[m]);
138         }
139     }
140
141     return r;
142 }
143
144 static int conf_enter(void)
145 {
146     int id, jd, kd;
147
148     back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
149
150     /* Initialize the configuration GUI. */
151
152     if ((id = gui_vstack(0)))
153     {
154         int f = config_get_d(CONFIG_FULLSCREEN);
155         int t = config_get_d(CONFIG_TEXTURES);
156         int g = config_get_d(CONFIG_GEOMETRY);
157         int h = config_get_d(CONFIG_SHADOW);
158         int s = config_get_d(CONFIG_SOUND_VOLUME);
159         int m = config_get_d(CONFIG_MUSIC_VOLUME);
160
161         char resolution[20];
162
163         sprintf(resolution, "%d x %d",
164                 config_get_d(CONFIG_WIDTH),
165                 config_get_d(CONFIG_HEIGHT));
166
167         if ((jd = gui_harray(id)))
168         {
169             gui_label(jd, _("Options"), GUI_SML, GUI_ALL, 0, 0);
170             gui_space(jd);
171             gui_start(jd, _("Back"),    GUI_SML, CONF_BACK, 0);
172         }
173
174         gui_space(id);
175
176         if ((jd = gui_harray(id)) &&
177             (kd = gui_harray(jd)))
178         {
179             gui_state(kd, _("No"),  GUI_SML, CONF_WIN,  (f == 0));
180             gui_state(kd, _("Yes"), GUI_SML, CONF_FULL, (f == 1));
181
182             gui_label(jd, _("Fullscreen"), GUI_SML, GUI_ALL, 0, 0);
183         }
184
185         if ((jd = gui_harray(id)) &&
186             (kd = gui_harray(jd)))
187         {
188             gui_state(kd, resolution, GUI_SML, CONF_RESOL, 0);
189
190             gui_label(jd, _("Resolution"), GUI_SML, GUI_ALL, 0, 0);
191         }
192
193         gui_space(id);
194
195         if ((jd = gui_harray(id)) &&
196             (kd = gui_harray(jd)))
197         {
198             gui_state(kd, _("Low"),  GUI_SML, CONF_TEXLO, (t == 2));
199             gui_state(kd, _("High"), GUI_SML, CONF_TEXHI, (t == 1));
200
201             gui_label(jd, _("Textures"), GUI_SML, GUI_ALL, 0, 0);
202         }
203
204         if ((jd = gui_harray(id)) &&
205             (kd = gui_harray(jd)))
206         {
207             gui_state(kd, _("Low"),  GUI_SML, CONF_GEOLO, (g == 0));
208             gui_state(kd, _("High"), GUI_SML, CONF_GEOHI, (g == 1));
209
210             gui_label(jd, _("Geometry"), GUI_SML, GUI_ALL, 0, 0);
211         }
212
213         if ((jd = gui_harray(id)) &&
214             (kd = gui_harray(jd)))
215         {
216             gui_state(kd, _("Off"),  GUI_SML, CONF_SHDOF, (h == 0));
217             gui_state(kd, _("On"),   GUI_SML, CONF_SHDON, (h == 1));
218
219             gui_label(jd, _("Shadow"), GUI_SML, GUI_ALL, 0, 0);
220         }
221
222         gui_space(id);
223
224         if ((jd = gui_harray(id)) &&
225             (kd = gui_harray(jd)))
226         {
227             /* A series of empty buttons forms the sound volume control. */
228
229             sound_id[10] = gui_state(kd, NULL, GUI_SML, 110, (s == 10));
230             sound_id[ 9] = gui_state(kd, NULL, GUI_SML, 109, (s ==  9));
231             sound_id[ 8] = gui_state(kd, NULL, GUI_SML, 108, (s ==  8));
232             sound_id[ 7] = gui_state(kd, NULL, GUI_SML, 107, (s ==  7));
233             sound_id[ 6] = gui_state(kd, NULL, GUI_SML, 106, (s ==  6));
234             sound_id[ 5] = gui_state(kd, NULL, GUI_SML, 105, (s ==  5));
235             sound_id[ 4] = gui_state(kd, NULL, GUI_SML, 104, (s ==  4));
236             sound_id[ 3] = gui_state(kd, NULL, GUI_SML, 103, (s ==  3));
237             sound_id[ 2] = gui_state(kd, NULL, GUI_SML, 102, (s ==  2));
238             sound_id[ 1] = gui_state(kd, NULL, GUI_SML, 101, (s ==  1));
239             sound_id[ 0] = gui_state(kd, NULL, GUI_SML, 100, (s ==  0));
240
241             gui_label(jd, _("Sound Volume"), GUI_SML, GUI_ALL, 0, 0);
242         }
243
244         if ((jd = gui_harray(id)) &&
245             (kd = gui_harray(jd)))
246         {
247             /* A series of empty buttons forms the music volume control. */
248
249             music_id[10] = gui_state(kd, NULL, GUI_SML, 210, (m == 10));
250             music_id[ 9] = gui_state(kd, NULL, GUI_SML, 209, (m ==  9));
251             music_id[ 8] = gui_state(kd, NULL, GUI_SML, 208, (m ==  8));
252             music_id[ 7] = gui_state(kd, NULL, GUI_SML, 207, (m ==  7));
253             music_id[ 6] = gui_state(kd, NULL, GUI_SML, 206, (m ==  6));
254             music_id[ 5] = gui_state(kd, NULL, GUI_SML, 205, (m ==  5));
255             music_id[ 4] = gui_state(kd, NULL, GUI_SML, 204, (m ==  4));
256             music_id[ 3] = gui_state(kd, NULL, GUI_SML, 203, (m ==  3));
257             music_id[ 2] = gui_state(kd, NULL, GUI_SML, 202, (m ==  2));
258             music_id[ 1] = gui_state(kd, NULL, GUI_SML, 201, (m ==  1));
259             music_id[ 0] = gui_state(kd, NULL, GUI_SML, 200, (m ==  0));
260
261             gui_label(jd, _("Music Volume"), GUI_SML, GUI_ALL, 0, 0);
262         }
263
264         gui_layout(id, 0, 0);
265     }
266
267     audio_music_fade_to(0.5f, "bgm/inter.ogg");
268
269     return id;
270 }
271
272 static void conf_leave(int id)
273 {
274     gui_delete(id);
275 }
276
277 static void conf_paint(int id, float st)
278 {
279     video_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
280     {
281         back_draw(0);
282     }
283     video_pop_matrix();
284     gui_paint(id);
285 }
286
287 static void conf_timer(int id, float dt)
288 {
289     gui_timer(id, dt);
290 }
291
292 static void conf_point(int id, int x, int y, int dx, int dy)
293 {
294     gui_pulse(gui_point(id, x, y), 1.2f);
295 }
296
297 static void conf_stick(int id, int a, int v)
298 {
299     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
300         gui_pulse(gui_stick(id, v, 0), 1.2f);
301     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
302         gui_pulse(gui_stick(id, 0, v), 1.2f);
303 }
304
305 static int conf_click(int b, int d)
306 {
307     if (b == SDL_BUTTON_LEFT && d == 1)
308         return conf_action(gui_token(gui_click()));
309     return 1;
310 }
311
312 static int conf_keybd(int c, int d)
313 {
314     return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
315 }
316
317 static int conf_buttn(int b, int d)
318 {
319     if (d)
320     {
321         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
322             return conf_action(gui_token(gui_click()));
323         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
324             return goto_state(&st_title);
325         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
326             return goto_state(&st_title);
327     }
328     return 1;
329 }
330
331 /*---------------------------------------------------------------------------*/
332
333 static int null_enter(void)
334 {
335     gui_free();
336     swch_free();
337     jump_free();
338     flag_free();
339     mark_free();
340     ball_free();
341     shad_free();
342
343     return 0;
344 }
345
346 static void null_leave(int id)
347 {
348     int g = config_get_d(CONFIG_GEOMETRY);
349
350     shad_init();
351     ball_init();
352     mark_init(g);
353     flag_init(g);
354     jump_init(g);
355     swch_init(g);
356     gui_init();
357 }
358
359 /*---------------------------------------------------------------------------*/
360
361 struct state st_conf = {
362     conf_enter,
363     conf_leave,
364     conf_paint,
365     conf_timer,
366     conf_point,
367     conf_stick,
368     NULL,
369     conf_click,
370     conf_keybd,
371     conf_buttn,
372     1, 0
373 };
374
375 struct state st_null = {
376     null_enter,
377     null_leave,
378     NULL,
379     NULL,
380     NULL,
381     NULL,
382     NULL,
383     NULL,
384     NULL,
385     NULL,
386     1, 0
387 };