remove disable prec and next button since it break keyboard navigation
[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 5 /* 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             if (set_exists((b+1)*SET_GROUP))
86                 gui_state(jd, _("Next"), GUI_SML, SET_NEXT, 0);
87             if (b>0)
88                 gui_state(jd, _("Prev"), GUI_SML, SET_PREV, 0);
89             gui_state(jd, _("Back"),  GUI_SML, SET_BACK, 0);
90         }
91
92         if ((jd = gui_harray(id)))
93         {
94             shot_id = gui_image(jd, set_shot(last_set), 7 * w / 16, 7 * h / 16);
95
96             if ((kd = gui_varray(jd)))
97             {
98                 /* Display levels */
99                 for(i=b*SET_GROUP; i<(b+1)*SET_GROUP && set_exists(i); i++)
100                 {
101                     if(last_set == i)
102                         gui_start(kd, _(set_name(i)), GUI_SML, i, 0);
103                     else
104                         gui_state(kd, _(set_name(i)), GUI_SML, i, 0);
105                 }
106                 
107                 /* Display empty slots */
108                 for(; i<(b+1)*SET_GROUP; i++)
109                     gui_filler(kd);
110             }          
111         }
112
113         gui_space(id);
114         desc_id = gui_multi(id, " \\ \\ \\ \\ \\", GUI_SML, GUI_ALL, gui_yel, gui_wht);
115
116         gui_layout(id, 0, 0);
117     }
118     return id;
119 }
120
121 static void set_leave(int id)
122 {
123     gui_delete(id);
124 }
125
126 static void set_paint(int id, float st)
127 {
128     game_draw(0, st);
129     config_pop_matrix();
130     gui_paint(id);
131 }
132
133 static void set_timer(int id, float dt)
134 {
135     gui_timer(id, dt);
136     audio_timer(dt);
137 }
138
139 static void set_point(int id, int x, int y, int dx, int dy)
140 {
141     int jd;
142
143     if ((jd = gui_point(id, x, y)))
144     {
145         int i = gui_token(jd);
146         
147         if (set_exists(i))
148         {
149             gui_set_image(shot_id, set_shot(i));
150             gui_set_multi(desc_id, _(set_desc(i)));
151             gui_pulse(jd, 1.2f);
152         }
153     }
154 }
155
156 static void set_stick(int id, int a, int v)
157 {
158     int jd;
159
160     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
161     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
162
163     if ((jd = gui_stick(id, x, y)))
164     {
165         int i = gui_token(jd);
166
167         if (set_exists(i))
168         {
169             gui_set_image(shot_id, set_shot(i));
170             gui_set_multi(desc_id, _(set_desc(i)));
171             gui_pulse(jd, 1.2f);
172         }
173     }
174 }
175
176 static int set_click(int b, int d)
177 {
178     return (b < 0 && d == 1) ? set_action(gui_token(gui_click())) : 1;
179 }
180
181 static int set_buttn(int b, int d)
182 {
183     if (d)
184     {
185         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
186             return set_action(gui_token(gui_click()));
187         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
188             return goto_state(&st_title);
189     }
190     return 1;
191 }
192
193 /*---------------------------------------------------------------------------*/
194
195 struct state st_set = {
196     set_enter,
197     set_leave,
198     set_paint,
199     set_timer,
200     set_point,
201     set_stick,
202     set_click,
203     NULL,
204     set_buttn,
205     1, 0
206 };