Formatting stuff. Convert tabs to spaces, shorten lines to 80, etc. First
[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_BACK -1
31 #define SET_PREV -2
32 #define SET_NEXT -3
33 #define SET_NULL -4
34
35 #define SET_GROUP 5 /* number of sets in one screen */
36
37 static int shot_id;
38 static int desc_id;
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         return goto_state(&st_title);
48
49     case GUI_PREV:
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     case GUI_NEXT:
54         config_set_d(CONFIG_LAST_SET, ((config_get_d(CONFIG_LAST_SET)/SET_GROUP)+1)*SET_GROUP);
55         return goto_state(&st_set);
56
57     case GUI_NULL:
58         return 1;
59     
60     default:
61         if (set_exists(i))
62         {
63             config_set_d(CONFIG_LAST_SET, i);
64             set_goto(i);
65             return goto_state(&st_start);
66         }
67     }
68     return 1;
69 }
70
71 static void gui_set(int id, int i)
72 {
73     const struct set *s = get_set(i);
74     int jd;
75     
76     if (set_completed(s)) 
77         jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_yel, gui_wht);
78     else if (set_unlocked(s)) 
79         jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_grn, gui_wht);
80     else
81         jd = gui_label(id, _(s->name), GUI_SML, GUI_ALL, gui_wht, gui_wht);
82     
83     gui_active(jd, i, 0);
84 }
85
86 static int set_enter(void)
87 {
88     int w = config_get_d(CONFIG_WIDTH);
89     int h = config_get_d(CONFIG_HEIGHT);
90     int last_set = config_get_d(CONFIG_LAST_SET);
91     int b = last_set / SET_GROUP;
92     int i;
93
94     int id, jd, kd;
95
96
97     set_init();
98     
99     /* Reset last set if it do not exists */
100     if (!set_exists(last_set))
101     {
102         b = 0;
103         last_set = 0;
104         config_set_d(CONFIG_LAST_SET, 0);
105     }
106
107     audio_music_fade_to(0.5f, "bgm/inter.ogg");
108     audio_play(AUD_START, 1.f);
109
110     if ((id = gui_vstack(0)))
111     {
112         if ((jd = gui_hstack(id)))
113         {
114             gui_label(jd, _("Level Set"), GUI_SML, GUI_ALL, gui_yel, gui_red);
115             gui_filler(jd);
116             gui_back_prev_next(jd, b>0, set_exists((b+1)*SET_GROUP));
117         }
118
119         if ((jd = gui_harray(id)))
120         {
121             shot_id = gui_image(jd, get_set(last_set)->shot, 7 * w / 16, 7 * h / 16);
122
123             if ((kd = gui_varray(jd)))
124             {
125                 /* Display levels */
126                 for(i=b*SET_GROUP; i<(b+1)*SET_GROUP && set_exists(i); i++)
127                     gui_set(kd, i);
128                 
129                 /* Display empty slots */
130                 for(; i<(b+1)*SET_GROUP; i++)
131                     gui_filler(kd);
132             }          
133         }
134
135         gui_space(id);
136         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL, gui_yel, gui_wht);
137
138         gui_layout(id, 0, 0);
139     }
140     return id;
141 }
142
143 static void set_over(int i)
144 {
145     gui_set_image(shot_id, get_set(i)->shot);
146     gui_set_multi(desc_id, _(get_set(i)->desc));
147 }
148
149 static void set_point(int id, int x, int y, int dx, int dy)
150 {
151     int jd = shared_point_basic(id, x, y);
152     int i  = gui_token(jd);
153     if (jd && set_exists(i))
154         set_over(i);
155 }
156
157 static void set_stick(int id, int a, int v)
158 {
159     int jd = shared_stick_basic(id, a, v);
160     int i  = gui_token(jd);
161     if (jd && set_exists(i))
162         set_over(i);
163 }
164
165 static int set_buttn(int b, int d)
166 {
167     if (d)
168     {
169         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
170             return set_action(gui_token(gui_click()));
171         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
172             return set_action(GUI_BACK);
173     }
174     return 1;
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 struct state st_set = {
180     set_enter,
181     shared_leave,
182     shared_paint,
183     shared_timer,
184     set_point,
185     set_stick,
186     shared_click,
187     NULL,
188     set_buttn,
189     1, 0
190 };