add infos in Modes and Secrets tabs
[neverball] / ball / util.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 <ctype.h>
16 #include <string.h>
17
18 #include "gui.h"
19 #include "util.h"
20 #include "config.h"
21
22 /*---------------------------------------------------------------------------*/
23
24 static int is_special_name(const char *n)
25 {
26     return (strcmp(n, N_("Hard")) == 0 || strcmp(n, N_("Medium")) == 0
27             || strcmp(n, N_("Easy")) == 0);
28 }
29
30 /*---------------------------------------------------------------------------*/
31
32 static int coin_c[4];
33 static int coin_n[4];
34 static int coin_t[4];
35 static int coin_extrarow;
36
37 /* Build a Most Coins top three list with default values. */
38
39 void gui_most_coins(int id, int e)
40 {
41     const char *s = "1234567";
42
43     int j, jd, kd, ld, md;
44
45     coin_extrarow = e;
46
47     if ((jd = gui_hstack(id)))
48     {
49         gui_filler(jd);
50
51         if ((kd = gui_vstack(jd)))
52         {
53             gui_label(kd, _("Most Coins"), GUI_SML, GUI_TOP, 0, 0);
54
55             if ((ld = gui_hstack(kd)))
56             {
57                 if ((md = gui_vstack(ld)))
58                 {
59                     for (j = 0; j < NSCORE - 1; j++)
60                         coin_c[j] = gui_count(md, 1000, GUI_SML, 0);
61
62                     coin_c[j++] = gui_count(md, 1000, GUI_SML, GUI_SE);
63
64                     if (e)
65                     {
66                        gui_space(md);
67                        coin_c[j++] = gui_count(md, 1000, GUI_SML, GUI_RGT);
68                     }
69                 }
70
71                 if ((md = gui_vstack(ld)))
72                 {
73                     for (j = 0; j < NSCORE ; j++)
74                         coin_n[j] = gui_label(md, s, GUI_SML, 0,
75                                               gui_yel, gui_wht);
76
77                     if (e)
78                     {
79                         gui_space(md);
80                         coin_n[j++] = gui_label(md, s, GUI_SML, 0,
81                                                 gui_yel, gui_wht);
82                     }
83                 }
84
85                 if ((md = gui_vstack(ld)))
86                 {
87                     for (j = 0; j < NSCORE - 1; j++)
88                         coin_t[j] = gui_clock(md, 359999, GUI_SML, 0);
89
90                     coin_t[j++] = gui_clock(md, 359999,  GUI_SML, GUI_SW);
91
92                     if (e)
93                     {
94                        gui_space(md);
95                        coin_t[j++] = gui_clock(md, 359999,  GUI_SML, GUI_LFT);
96                     }
97                 }
98             }
99         }
100         gui_filler(jd);
101     }
102 }
103
104 /* Set the Most Coins top three list values for level i. */
105
106 void set_most_coins(const struct score *s, int hilight)
107 {
108     int j, spe;
109     const char *name;
110
111     if (s == NULL)
112     {
113         for (j = 0; j < NSCORE + coin_extrarow ; j++)
114         {
115             gui_set_count(coin_c[j], -1);
116             gui_set_label(coin_n[j], "");
117             gui_set_clock(coin_t[j], -1);
118         }
119     }
120     else
121     {
122         for (j = 0; j < NSCORE + coin_extrarow; j++)
123         {
124             name = s->player[j];
125             spe = is_special_name(name);
126
127             if (spe)
128                 gui_set_color(coin_n[j], 0, 0);
129             else if (j != hilight)
130                 gui_set_color(coin_n[j], gui_yel, gui_wht);
131             else if (j>= NSCORE)
132                 gui_set_color(coin_n[j], gui_red, gui_red);
133             else
134                 gui_set_color(coin_n[j], gui_grn, gui_grn);
135
136             gui_set_count(coin_c[j], s->coins[j]);
137             gui_set_label(coin_n[j], spe ? _(name) : name);
138             gui_set_clock(coin_t[j], s->timer[j]);
139         }
140     }
141 }
142
143 /*---------------------------------------------------------------------------*/
144
145 static int time_l;
146 static int time_c[4];
147 static int time_n[4];
148 static int time_t2[4]; /* Renamed to time_t2 to avoid multiple declaration (in sys/types.h) */
149 static int time_extrarow;
150
151 /* Build a Best Times top three list with default values. */
152
153 void gui_best_times(int id, int e)
154 {
155     const char *s = "1234567";
156
157     int j, jd, kd, ld, md;
158
159     time_extrarow = e;
160
161     if ((jd = gui_hstack(id)))
162     {
163         gui_filler(jd);
164
165         if ((kd = gui_vstack(jd)))
166         {
167             time_l = gui_label(kd, "XXX", GUI_SML, GUI_TOP, 0, 0);
168
169             if ((ld = gui_hstack(kd)))
170             {
171                 if ((md = gui_vstack(ld)))
172                 {
173                     for (j = 0; j < NSCORE - 1; j++)
174                         time_t2[j] = gui_clock(md, 359999, GUI_SML, 0);
175
176                     time_t2[j++] = gui_clock(md, 359999, GUI_SML, GUI_SE);
177
178                     if (e)
179                     {
180                         gui_space(md);
181                         time_t2[j++] = gui_clock(md, 359999, GUI_SML, GUI_RGT);
182                     }
183                 }
184
185                 if ((md = gui_vstack(ld)))
186                 {
187                     for (j = 0; j < NSCORE; j++)
188                         time_n[j] = gui_label(md, s, GUI_SML, 0,
189                                               gui_yel, gui_wht);
190
191                     if (e)
192                     {
193                         gui_space(md);
194                         time_n[j++] = gui_label(md, s, GUI_SML, 0,
195                                                 gui_yel, gui_wht);
196                     }
197                 }
198
199                 if ((md = gui_vstack(ld)))
200                 {
201                     for (j = 0; j < NSCORE - 1; j++)
202                         time_c[j] = gui_count(md, 1000, GUI_SML, 0);
203
204                     time_c[j++] = gui_count(md, 1000, GUI_SML, GUI_SW);
205
206                     if (e)
207                     {
208                         gui_space(md);
209                         time_c[j++] = gui_count(md, 1000, GUI_SML, GUI_LFT);
210                     }
211                 }
212             }
213         }
214         gui_filler(jd);
215     }
216 }
217
218 /* Set the Best Times top three list values for level i. */
219
220 void set_best_times(const struct score *s, int hilight, int goal)
221 {
222     int j, spe;
223     const char *name;
224
225     gui_set_label(time_l, goal ? _("Unlock Goal") : _("Best Times"));
226
227     if (s == NULL)
228     {
229         for (j = 0; j < NSCORE + time_extrarow ; j++)
230         {
231             gui_set_clock(time_t2[j], -1);
232             gui_set_label(time_n[j], "");
233             gui_set_count(time_c[j], -1);
234         }
235     }
236     else
237     {
238         for (j = 0; j < NSCORE + time_extrarow; j++)
239         {
240             name = s->player[j];
241             spe = is_special_name(name);
242
243             if (spe)
244                 gui_set_color(time_n[j], 0, 0);
245             else if (j != hilight)
246                 gui_set_color(time_n[j], gui_yel, gui_wht);
247             else if (j>= NSCORE)
248                 gui_set_color(time_n[j], gui_red, gui_red);
249             else
250                 gui_set_color(time_n[j], gui_grn, gui_grn);
251
252             gui_set_clock(time_t2[j], s->timer[j]);
253             gui_set_label(time_n[j], spe ? _(name) : name);
254             gui_set_count(time_c[j], s->coins[j]);
255         }
256     }
257 }
258
259 /*---------------------------------------------------------------------------*/
260
261 static int lock = 1;
262 static int keyd[127];
263
264 static void gui_fill(int id, char *line)
265 {
266     char l[2];
267     l[1] = '\0';
268
269     while (*line)
270     {
271         *l = *(line++);
272         keyd[(int) *l] = gui_state(id, l, GUI_SML, *l, 0);
273     }
274 }
275
276 void gui_keyboard(int id)
277 {
278     int jd, kd, ld;
279
280     lock = 1;
281
282     if ((jd = gui_hstack(id)))
283     {
284         gui_filler(jd);
285
286         if ((kd = gui_vstack(jd)))
287         {
288             if ((ld = gui_harray(kd)))
289             {
290                 gui_filler(ld);
291                 gui_fill(ld, "9876543210");
292                 gui_filler(ld);
293             }
294             if ((ld = gui_harray(kd)))
295             {
296                 gui_filler(ld);
297                 gui_fill(ld, "JIHGFEDCBA");
298                 gui_filler(ld);
299             }
300             if ((ld = gui_harray(kd)))
301             {
302                 gui_filler(ld);
303                 gui_fill(ld, "TSRQPONMLK");
304                 gui_filler(ld);
305             }
306             if ((ld = gui_harray(kd)))
307             {
308                 gui_filler(ld);
309                 gui_fill(ld, "!.-_ZYXWVU");
310                 gui_filler(ld);
311             }
312             if ((ld = gui_harray(kd)))
313             {
314                 gui_filler(ld);
315                 gui_fill(ld, "jihgfedcba");
316                 gui_filler(ld);
317             }
318             if ((ld = gui_harray(kd)))
319             {
320                 gui_filler(ld);
321                 gui_fill(ld, "tsrqponmlk");
322                 gui_filler(ld);
323             }
324             if ((ld = gui_hstack(kd)))
325             {
326                 gui_filler(ld);
327                 gui_state(ld, _("del"), GUI_SML, GUI_BS, 0);
328                 gui_fill(ld, "zyxwvu");
329                 gui_filler(ld);
330             }
331         }
332         gui_filler(jd);
333     }
334 }
335
336 /*---------------------------------------------------------------------------*/
337
338 int gui_back_prev_next(int id, int prev, int next)
339 {
340     int jd;
341
342     if ((jd = gui_hstack(id)))
343     {
344         if (next || prev)
345         {
346             gui_maybe(jd, _("Next"), GUI_NEXT, next);
347             gui_maybe(jd, _("Prev"), GUI_PREV, prev);
348         }
349
350         gui_start(jd, _("Back"),  GUI_SML, GUI_BACK, 0);
351     }
352     return jd;
353 }
354
355 int gui_maybe(int id, const char *label, int token, int enabled)
356 {
357     int bd;
358     if (enabled)
359         bd = gui_state(id, label, GUI_SML, token,    0);
360     else
361     {
362         bd = gui_state(id, label, GUI_SML, token >= 0 ? token | GUI_NULL_MASK : GUI_NULL, 0);
363         gui_set_color(bd, gui_gry, gui_gry);
364     }
365     return bd;
366 }
367
368 /*---------------------------------------------------------------------------*/