-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
341 lines (275 loc) · 7.4 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* file created on Mon Aug 26 15:30:05 CEST 2002 by doom */
#include <stdbool.h>
#include "menu.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
struct _MENU
{
MenuItems items;
int active_item;
int nb_items;
IIM_Surface * menuselector;
int sep_size;
Sound *sound_move;
Sound *sound_valid;
int appears;
int visible;
int hide;
int hauteur;
};
void menu_update_hauteur (Menu * menu);
Menu *
menu_new (MenuItems items,IIM_Surface * selimage)
{
Menu *m = (Menu *)malloc (sizeof (Menu));
m->items = items;
m->nb_items = 0;
while (items[m->nb_items++].name);
m->sep_size = 20; // taille d'un separateur.
m->active_item = -1;
m->sound_move = m->sound_valid = NULL;
m->visible = m->appears = m->hide = 0;
menu_update_hauteur (m);
menu_next_item (m);
m->menuselector = selimage;;
return m;
}
int menu_get_active_item (Menu * menu)
{
return menu->active_item;
}
const char * menu_get_active_item_string (Menu * menu)
{
return menu->items[menu_get_active_item (menu)].name;
}
int menu_active_is (Menu * menu, const char *it_name)
{
return (!strcmp (it_name, menu_get_active_item_string (menu)));
}
void menu_change_item_done (Menu * menu)
{
menu->items[menu->active_item].priv_must_anim = 1;
if (menu->sound_move) {
audio_sound_play (menu->sound_move);
}
}
void
menu_next_item (Menu * menu)
{
do {
menu->active_item++;
menu->active_item %= menu->nb_items;
}
while (menu_items_is_delimiter (menu->items, menu->active_item));
menu_change_item_done (menu);
}
void
menu_prev_item (Menu * menu)
{
do {
if (menu->active_item)
menu->active_item--;
else
menu->active_item = menu->nb_items - 1;
}
while (menu_items_is_delimiter (menu->items, menu->active_item));
menu_change_item_done (menu);
}
void menu_update (Menu * menu, SDL_Surface * surf)
{
int c;
if (menu->visible) {
if (menu->hide) {
menu->hide *= 2;
}
if (menu->hide > surf->h) {
menu->visible = 0;
menu->hide = 0;
}
if (menu->appears) {
for (c = 0; c < menu->nb_items; c++) {
menu->items[c].priv_vx = 0;
menu->items[c].priv_x =
((rand () % 2) ? -1 : 1) * (16 + (rand () % 4)) * surf->w / 16;
}
menu->appears = 0;
}
for (c = 0; c < menu->nb_items; c++) {
SDL_Surface *item_surf = menu->items[c].bitmap;
SoFont *item_font = menu->items[c].font;
if (item_surf || item_font) {
if (menu->items[c].priv_must_anim) {
static int alt = -1;
menu->items[c].priv_vx += alt * 15;
menu->items[c].priv_must_anim = 0;
alt = -alt;
}
if ((fabs (menu->items[c].priv_vx) < 0.02)
&& (fabs (menu->items[c].priv_x) < 0.1))
menu->items[c].priv_x = menu->items[c].priv_vx = 0;
menu->items[c].priv_vx -= menu->items[c].priv_x / 8;
menu->items[c].priv_vx *= 0.85;
menu->items[c].priv_x += menu->items[c].priv_vx;
}
}
}
}
void menu_draw (Menu * menu, SDL_Surface * surf)
{
if (menu->visible) {
int c; // compteur d'items
int center = surf->w / 2;
int hauteur = (surf->h - menu->hauteur) / 2;
hauteur += menu->hide;
for (c = 0; c < menu->nb_items; c++) {
SDL_Surface *item_surf = menu->items[c].bitmap;
SoFont *item_font = menu->items[c].font;
if (item_surf || item_font) {
int center_x;
center_x = (int)(center + menu->items[c].priv_x);
if (item_surf != NULL) {
SDL_Rect rect;
rect.x = center_x - item_surf->w / 2;
rect.y = hauteur;
rect.w = item_surf->w;
rect.h = item_surf->h;
SDL_BlitSurface (item_surf, NULL, surf, &rect);
hauteur += item_surf->h;
}
else { // font..
static char item_name[1024];
if (c == menu->active_item) {
SDL_Rect rect;
if (menu->menuselector==NULL)
{
rect.x = center - 140; rect.w = 280;
rect.y = hauteur; rect.h = SoFont_FontHeight (item_font);
SDL_FillRect (surf, &rect, 0);
rect.x = center - 150; rect.w = 300;
rect.y = hauteur + 3; rect.h = SoFont_FontHeight (item_font) - 6;
SDL_FillRect (surf, &rect, 0);
}
else
{
rect.x = center - 150; rect.w = menu->menuselector->w;
rect.y = hauteur - 3; rect.h = menu->menuselector->h;
SDL_BlitSurface (menu->menuselector->surf, NULL, surf, &rect);
}
}
strcpy (item_name, menu->items[c].name);
if (menu->items[c].value) {
strcat (item_name, menu->items[c].value);
}
char *pc = item_name;
int nbTab = 0;
for (; *pc; pc++)
{
if (*pc == '\t') {
while (*pc == '\t') {
*pc = 0;
pc ++;
nbTab ++;
}
break;
}
}
if (nbTab > 0)
{
int w = 80 + nbTab * 50;
SoFont_PutString (item_font, surf, center_x-w, hauteur, item_name, NULL);
SoFont_PutString (item_font, surf, center_x+w - SoFont_TextWidth(item_font, pc), hauteur, pc, NULL);
}
else
{
SoFont_PutString (item_font, surf,
center_x - SoFont_TextWidth (item_font,
item_name) / 2,
hauteur, item_name, NULL);
}
hauteur += SoFont_FontHeight (item_font);
}
}
else
hauteur += menu->sep_size;
}
}
}
void menu_update_hauteur (Menu * menu)
{
int c; // compteur d'items
int hauteur = 0;
for (c = 0; c < menu->nb_items; c++) {
SDL_Surface *item_surf = menu->items[c].bitmap;
SoFont *item_font = menu->items[c].font;
if (item_surf || item_font) {
if (item_surf != NULL) {
hauteur += item_surf->h;
}
else { // font..
hauteur += SoFont_FontHeight (item_font);
}
}
else
hauteur += menu->sep_size;
}
menu->hauteur = hauteur;
}
void
menu_set_sounds (Menu * menu, Sound * s_move, Sound * s_validate)
{
menu->sound_move = s_move;
menu->sound_valid = s_validate;
}
void
menu_validate (Menu * menu)
{
if (menu->sound_valid)
audio_sound_play (menu->sound_valid);
menu->items[menu->active_item].priv_must_anim = 1;
}
void
menu_show (Menu * menu)
{
if (!menu_visible (menu)) {
menu->appears = 1;
menu->visible = 1;
menu->hide = 0;
}
}
void
menu_hide (Menu * menu)
{
if (menu_visible (menu))
menu->hide = 1;
}
int
menu_visible (Menu * menu)
{
return (menu->visible) && (!menu->hide);
}
int
menu_switch_on_off (Menu * menu, const char *name)
{
const char *value = menu_items_get_value (menu->items, name);
if (!strcmp (value, "ON")) {
menu_set_value (menu, name, "OFF", 1);
return 0;
}
else {
menu_set_value (menu, name, "ON", 1);
return 1;
}
}
const char *
menu_get_value (Menu * menu, const char *name)
{
return menu_items_get_value (menu->items, name);
}
void
menu_set_value (Menu * menu, const char *name, const char *value, int doValide)
{
menu_items_set_value_for (menu->items, name, value);
if (doValide)
menu_validate (menu);
}