Reformat code to fit in 80 columns. Jean, I cannot answer your
[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     if (goal)
226         gui_set_label(time_l, _("Unlock Goal"));
227     else
228         gui_set_label(time_l, _("Best Times"));
229
230     if (s == NULL)
231     {
232         for (j = 0; j < NSCORE + time_extrarow ; j++)
233         {
234             gui_set_clock(time_t2[j], -1);
235             gui_set_label(time_n[j], "");
236             gui_set_count(time_c[j], -1);
237         }
238     }
239     else
240     {
241         for (j = 0; j < NSCORE + time_extrarow; j++)
242         {
243             name = s->player[j];
244             spe = is_special_name(name);
245
246             if (spe)
247                 gui_set_color(time_n[j], 0, 0);
248             else if (j != hilight)
249                 gui_set_color(time_n[j], gui_yel, gui_wht);
250             else if (j>= NSCORE)
251                 gui_set_color(time_n[j], gui_red, gui_red);
252             else
253                 gui_set_color(time_n[j], gui_grn, gui_grn);
254
255             gui_set_clock(time_t2[j], s->timer[j]);
256             gui_set_label(time_n[j], spe ? _(name) : name);
257             gui_set_count(time_c[j], s->coins[j]);
258         }
259     }
260 }
261
262 /*---------------------------------------------------------------------------*/
263
264 static int lock = 1;
265 static int keyd[127];
266
267 static void gui_fill(int id, char *line)
268 {
269     char l[2];
270     l[1] = '\0';
271
272     while(*line)
273     {
274         *l = *(line++);
275         keyd[(int)*l] = gui_state(id, l, GUI_SML, *l, 0);
276     }
277 }
278
279 void gui_keyboard(int id)
280 {
281     int jd, kd, ld;
282
283     lock = 1;
284
285     if ((jd = gui_hstack(id)))
286     {
287         gui_filler(jd);
288
289         if ((kd = gui_vstack(jd)))
290         {
291             if ((ld = gui_harray(kd)))
292             {
293                 gui_filler(ld);
294                 gui_fill(ld, "9876543210");
295                 gui_filler(ld);
296             }
297             if ((ld = gui_harray(kd)))
298             {
299                 gui_filler(ld);
300                 gui_fill(ld, "JIHGFEDCBA");
301                 gui_filler(ld);
302             }
303             if ((ld = gui_harray(kd)))
304             {
305                 gui_filler(ld);
306                 gui_fill(ld, "TSRQPONMLK");
307                 gui_filler(ld);
308             }
309             if ((ld = gui_harray(kd)))
310             {
311                 gui_filler(ld);
312                 gui_fill(ld, "!.-_ZYXWVU");
313                 gui_filler(ld);
314             }
315             if ((ld = gui_harray(kd)))
316             {
317                 gui_filler(ld);
318                 gui_fill(ld, "jihgfedcba");
319                 gui_filler(ld);
320             }
321             if ((ld = gui_harray(kd)))
322             {
323                 gui_filler(ld);
324                 gui_fill(ld, "tsrqponmlk");
325                 gui_filler(ld);
326             }
327             if ((ld = gui_hstack(kd)))
328             {
329                 gui_filler(ld);
330                 gui_state(ld, _("del"), GUI_SML, GUI_BS, 0);
331                 gui_fill(ld, "zyxwvu");
332                 gui_filler(ld);
333             }
334         }
335         gui_filler(jd);
336     }
337 }
338
339 /*---------------------------------------------------------------------------*/
340
341 int gui_back_prev_next(int id, int prev, int next)
342 {
343     int jd;
344
345     if ((jd = gui_hstack(id)))
346     {
347         if (next || prev)
348         {
349             gui_maybe(jd, _("Next"), GUI_NEXT, next);
350             gui_maybe(jd, _("Prev"), GUI_PREV, prev);
351         }
352
353         gui_start(jd, _("Back"),  GUI_SML, GUI_BACK, 0);
354     }
355     return jd;
356 }
357
358 int gui_maybe(int id, const char *label, int token, int enabled)
359 {
360     int bd;
361     if (enabled)
362         bd = gui_state(id, label, GUI_SML, token,    0);
363     else
364     {
365         bd = gui_state(id, label, GUI_SML, GUI_NULL, 0);
366         gui_set_color(bd, gui_gry, gui_gry);
367     }
368     return bd;
369 }
370
371 /*---------------------------------------------------------------------------*/