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