Simplify code in set_enter() and gui_set().
[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_GROUP 5 /* number of sets in one screen */
31
32 static int last_set = 0;
33
34 static int shot_id;
35 static int desc_id;
36
37 static int set_action(int i)
38 {
39     audio_play(AUD_MENU, 1.0f);
40
41     switch (i)
42     {
43     case GUI_BACK:
44         return goto_state(&st_title);
45
46     case GUI_PREV:
47         last_set = (last_set / SET_GROUP - 1) * SET_GROUP;
48         return goto_state(&st_set);
49
50     case GUI_NEXT:
51         last_set = (last_set / SET_GROUP + 1) * SET_GROUP;
52         return goto_state(&st_set);
53
54     case GUI_NULL:
55         return 1;
56
57     default:
58         if (set_exists(i))
59         {
60             last_set = i;
61             set_goto(i);
62             return goto_state(&st_start);
63         }
64     }
65     return 1;
66 }
67
68 static void gui_set(int id, int i)
69 {
70     const struct set *s;
71
72     if ((s = get_set(i)))
73         gui_state(id, _(s->name), GUI_SML, i, 0);
74     else
75         gui_label(id, "", GUI_SML, GUI_ALL, 0, 0);
76 }
77
78 static int set_enter(void)
79 {
80     int w = config_get_d(CONFIG_WIDTH);
81     int h = config_get_d(CONFIG_HEIGHT);
82     int b = last_set / SET_GROUP;
83     int i;
84
85     int id, jd, kd;
86
87     set_init();
88
89     /* Reset last set if it does not exist */
90     if (!set_exists(last_set))
91     {
92         b = 0;
93         last_set = 0;
94     }
95
96     audio_music_fade_to(0.5f, "bgm/inter.ogg");
97     audio_play(AUD_START, 1.f);
98
99     if ((id = gui_vstack(0)))
100     {
101         if ((jd = gui_hstack(id)))
102         {
103             gui_label(jd, _("Level Set"), GUI_SML, GUI_ALL, gui_yel, gui_red);
104             gui_filler(jd);
105             gui_back_prev_next(jd, b > 0, set_exists((b + 1) * SET_GROUP));
106         }
107
108         if ((jd = gui_harray(id)))
109         {
110             shot_id = gui_image(jd, get_set(last_set)->shot,
111                                 7 * w / 16, 7 * h / 16);
112
113             if ((kd = gui_varray(jd)))
114             {
115                 for (i = b * SET_GROUP; i < (b + 1) * SET_GROUP; i++)
116                     gui_set(kd, i);
117             }
118         }
119
120         gui_space(id);
121         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL,
122                             gui_yel, gui_wht);
123
124         gui_layout(id, 0, 0);
125     }
126     return id;
127 }
128
129 static void set_over(int i)
130 {
131     gui_set_image(shot_id, get_set(i)->shot);
132     gui_set_multi(desc_id, _(get_set(i)->desc));
133 }
134
135 static void set_point(int id, int x, int y, int dx, int dy)
136 {
137     int jd = shared_point_basic(id, x, y);
138     int i  = gui_token(jd);
139     if (jd && set_exists(i))
140         set_over(i);
141 }
142
143 static void set_stick(int id, int a, int v)
144 {
145     int jd = shared_stick_basic(id, a, v);
146     int i  = gui_token(jd);
147     if (jd && set_exists(i))
148         set_over(i);
149 }
150
151 static int set_buttn(int b, int d)
152 {
153     if (d)
154     {
155         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
156             return set_action(gui_token(gui_click()));
157         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
158             return set_action(GUI_BACK);
159     }
160     return 1;
161 }
162
163 /*---------------------------------------------------------------------------*/
164
165 struct state st_set = {
166     set_enter,
167     shared_leave,
168     shared_paint,
169     shared_timer,
170     set_point,
171     set_stick,
172     shared_click,
173     NULL,
174     set_buttn,
175     1, 0
176 };