Added a new module to be used as a dump of generic utility functions.
[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 "levels.h"
18 #include "game.h"
19 #include "audio.h"
20 #include "config.h"
21 #include "util.h"
22 #include "st_shared.h"
23
24 #include "st_set.h"
25 #include "st_title.h"
26 #include "st_start.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 #define SET_STEP    5
31
32 static int total = 0;
33 static int first = 0;
34
35 static int shot_id;
36 static int desc_id;
37
38 static int should_prompt = 1;
39
40 static int set_action(int i)
41 {
42     audio_play(AUD_MENU, 1.0f);
43
44     switch (i)
45     {
46     case GUI_BACK:
47         set_free();
48         return goto_state(&st_title);
49         break;
50
51     case GUI_PREV:
52
53         first -= SET_STEP;
54
55         should_prompt = 0;
56         return goto_state(&st_set);
57
58         break;
59
60     case GUI_NEXT:
61
62         first += SET_STEP;
63
64         should_prompt = 0;
65         return goto_state(&st_set);
66
67         break;
68
69     case GUI_NULL:
70         return 1;
71         break;
72
73     default:
74         if (set_exists(i))
75         {
76             set_goto(i);
77             return goto_state(&st_start);
78         }
79     }
80
81     return 1;
82 }
83
84 static void gui_set(int id, int i)
85 {
86     const struct set *s;
87
88     if ((s = get_set(i)))
89         gui_state(id, _(s->name), GUI_SML, i, 0);
90     else
91         gui_label(id, "", GUI_SML, GUI_ALL, 0, 0);
92 }
93
94 static int set_enter(void)
95 {
96     int w = config_get_d(CONFIG_WIDTH);
97     int h = config_get_d(CONFIG_HEIGHT);
98
99     int id, jd, kd;
100
101     int i;
102
103     total = set_init();
104
105     audio_music_fade_to(0.5f, "bgm/inter.ogg");
106
107     if (should_prompt)
108         audio_play(AUD_START, 1.f);
109     else should_prompt = 1;
110
111     if ((id = gui_vstack(0)))
112     {
113         if ((jd = gui_hstack(id)))
114         {
115             gui_label(jd, _("Level Set"), GUI_SML, GUI_ALL, gui_yel, gui_red);
116             gui_filler(jd);
117             gui_back_prev_next(jd, first > 0, first + SET_STEP < total);
118         }
119
120         if ((jd = gui_harray(id)))
121         {
122             shot_id = gui_image(jd, get_set(first)->shot,
123                                 7 * w / 16, 7 * h / 16);
124
125             if ((kd = gui_varray(jd)))
126             {
127                 for (i = first; i < first + SET_STEP; i++)
128                     gui_set(kd, i);
129             }
130         }
131
132         gui_space(id);
133         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL,
134                             gui_yel, gui_wht);
135
136         gui_layout(id, 0, 0);
137     }
138     return id;
139 }
140
141 static void set_over(int i)
142 {
143     gui_set_image(shot_id, get_set(i)->shot);
144     gui_set_multi(desc_id, _(get_set(i)->desc));
145 }
146
147 static void set_point(int id, int x, int y, int dx, int dy)
148 {
149     int jd = shared_point_basic(id, x, y);
150     int i  = gui_token(jd);
151     if (jd && set_exists(i))
152         set_over(i);
153 }
154
155 static void set_stick(int id, int a, int v)
156 {
157     int jd = shared_stick_basic(id, a, v);
158     int i  = gui_token(jd);
159     if (jd && set_exists(i))
160         set_over(i);
161 }
162
163 static int set_buttn(int b, int d)
164 {
165     if (d)
166     {
167         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
168             return set_action(gui_token(gui_click()));
169         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
170             return set_action(GUI_BACK);
171     }
172     return 1;
173 }
174
175 /*---------------------------------------------------------------------------*/
176
177 struct state st_set = {
178     set_enter,
179     shared_leave,
180     shared_paint,
181     shared_timer,
182     set_point,
183     set_stick,
184     shared_angle,
185     shared_click,
186     NULL,
187     set_buttn,
188     1, 0
189 };