Debian Codegraph @

src/util_menu.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2023 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "util_menu.h"
30
31 #include <assert.h>
32
33 #include <cdogs/draw/draw_actor.h>
34 #include <cdogs/draw/drawtools.h>
35 #include <cdogs/draw/nine_slice.h>
36 #include <cdogs/font.h>
37
38 #define MENU_WIDTH 80
39 #define EQUIP_MENU_SLOT_HEIGHT 40
40 #define MENU_MAX_ROWS 4
41 #define BUTTON_BG_W 20
42 #define SCROLL_H 12
43 #define SLOT_BORDER 3
44 #define LEVEL_W 2
45 #define ROW_H 10
46 #define HP_DELTA 5
47 #define HP_PRICE 2000
48 #define LIFE_PRICE 4000
49
50 typedef enum
51 {
52 OPTION_HP,
53 OPTION_LIVES,
54 OPTION_COUNT
55 } Option;
56
57 static Option GetSelectedOption(const UtilMenu *menu)
58 {
59 return MIN(menu->idx / 2, OPTION_COUNT);
60 }
61
62 int UtilMenuSelectedCostDiff(const UtilMenu *menu)
63 {
64 const PlayerData *pData = PlayerDataGetByUID(menu->PlayerUID);
65 const Option option = GetSelectedOption(menu);
66 if (option < OPTION_COUNT)
67 {
68 int amount = 0;
69 int max = 0;
70 int price = 0;
71 switch (option)
72 {
73 case OPTION_HP:
74 amount = pData->HP;
75 max = CampaignGetMaxHP(&gCampaign);
76 price = HP_PRICE;
77 break;
78 case OPTION_LIVES:
79 amount = pData->Lives;
80 max = CampaignGetMaxLives(&gCampaign);
81 price = LIFE_PRICE;
82 break;
83 default:
84 CASSERT(false, "unknown option");
85 break;
86 }
87 const bool buy = (menu->idx & 1) == 0;
88 if (buy && max > amount)
89 {
90 return price;
91 }
92 else if (amount > 0)
93 {
94 return -price;
95 }
96 }
97 return 0;
98 }
99
100 static void OnSelect(menu_t *menu, int cmd, void *data)
101 {
102 UNUSED(menu);
103 UtilMenu *d = data;
104 const PlayerData *pData = PlayerDataGetByUID(d->PlayerUID);
105
106 d->SelectResult = UTIL_MENU_NONE;
107 if (Button1(cmd))
108 {
109 const Option option = GetSelectedOption(d);
110 const bool buy = (d->idx & 1) == 0;
111 int amount = 0;
112 int max = 0;
113 int delta = 0;
114 int price = 0;
115 const char *sound = NULL;
116 switch (option)
117 {
118 case OPTION_HP:
119 amount = pData->HP;
120 max = CampaignGetMaxHP(&gCampaign);
121 delta = HP_DELTA;
122 price = HP_PRICE;
123 sound = "health";
124 break;
125 case OPTION_LIVES:
126 amount = pData->Lives;
127 max = CampaignGetMaxLives(&gCampaign);
128 delta = 1;
129 price = LIFE_PRICE;
130 sound = "1up";
131 break;
132 case OPTION_COUNT:
133 d->SelectResult = UTIL_MENU_CANCEL;
134 MenuPlaySound(MENU_SOUND_BACK);
135 break;
136 default:
137 CASSERT(false, "unknown option");
138 break;
139 }
140 if (buy && amount < max && price <= pData->Totals.Score)
141 {
142 SoundPlay(&gSoundDevice, StrSound(sound));
143 d->SelectResult = UTIL_MENU_SELECT;
144 }
145 else if (!buy && amount > delta)
146 {
147 SoundPlay(&gSoundDevice, StrSound(sound));
148 d->SelectResult = UTIL_MENU_SELECT;
149 }
150 else if (option != OPTION_COUNT)
151 {
152 SoundPlay(&gSoundDevice, StrSound("ammo_none"));
153 }
154 }
155 else if (Button2(cmd))
156 {
157 d->SelectResult = UTIL_MENU_CANCEL;
158 MenuPlaySound(MENU_SOUND_BACK);
159 }
160 }
161
162 static menu_t *CreateMenu(UtilMenu *data);
163 void UtilMenuCreate(
164 UtilMenu *menu, const int playerUID, const struct vec2i pos,
165 const struct vec2i size, EventHandlers *handlers, GraphicsDevice *graphics)
166 {
167 menu->PlayerUID = playerUID;
168 menu->buttonBG = PicManagerGetPic(&gPicManager, "hud/button_bg");
169 menu->idx = -1;
170 UtilMenuReset(menu);
171
172 MenuSystemInit(&menu->ms, handlers, graphics, pos, size);
173 menu->ms.align = MENU_ALIGN_LEFT;
174 menu->ms.root = menu->ms.current = CreateMenu(menu);
175 MenuSystemAddCustomDisplay(
176 &menu->ms, MenuDisplayPlayerControls, &menu->PlayerUID);
177 }
178 static void DrawMenu(
179 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
180 const struct vec2i size, const void *data);
181 static int HandleInputMenu(int cmd, void *data);
182 static menu_t *CreateMenu(UtilMenu *data)
183 {
184 menu_t *menu = MenuCreateCustom("", DrawMenu, HandleInputMenu, data);
185
186 MenuSetPostInputFunc(menu, OnSelect, data);
187
188 return menu;
189 }
190
191 static void DrawUtilMenuItem(
192 const UtilMenu *data, GraphicsDevice *g, const Option option,
193 const struct vec2i pos, const struct vec2i bgSize);
194 static void DrawMenu(
195 const menu_t *menu, GraphicsDevice *g, const struct vec2i pos,
196 const struct vec2i size, const void *data)
197 {
198 UNUSED(menu);
199 const UtilMenu *d = data;
200 const int ammoHeight = EQUIP_MENU_SLOT_HEIGHT * 2 + FontH();
201 const int ammoY = CENTER_Y(pos, size, ammoHeight) - 12;
202 const struct vec2i bgSize =
203 svec2i(CLAMP(d->size.x * 3 / 4, 40 * 2, 40 * 4), FontH() * 3 + 4);
204
205 for (Option option = OPTION_HP; option < OPTION_COUNT; option++)
206 {
207 DrawUtilMenuItem(d, g, option, svec2i(pos.x, pos.y + ammoY), bgSize);
208 }
209
210 // Draw back item
211 DrawUtilMenuItem(d, g, OPTION_COUNT, svec2i(pos.x, pos.y + ammoY), bgSize);
212 }
213 static void DrawUtilMenuItem(
214 const UtilMenu *data, GraphicsDevice *g, const Option option,
215 const struct vec2i pos, const struct vec2i bgSize)
216 {
217 const bool selected = data->idx / 2 == (int)option;
218 const PlayerData *pData = PlayerDataGetByUID(data->PlayerUID);
219 int amount = 0;
220 int max = 0;
221 int delta = 0;
222 int price = 0;
223 const char *name = NULL;
224 const Pic *pic = NULL;
225 switch (option)
226 {
227 case OPTION_HP:
228 amount = pData->HP;
229 max = CampaignGetMaxHP(&gCampaign);
230 delta = HP_DELTA;
231 price = HP_PRICE;
232 name = "Armor";
233 pic = PicManagerGetPic(&gPicManager, "health");
234 break;
235 case OPTION_LIVES:
236 amount = pData->Lives;
237 max = CampaignGetMaxLives(&gCampaign);
238 delta = 1;
239 price = LIFE_PRICE;
240 name = "Life";
241 pic = GetHeadPic(
242 pData->Char.Class, DIRECTION_DOWN, false, &pData->Char.Colors);
243 break;
244 default:
245 name = "Back";
246 break;
247 }
248 const struct vec2i bgPos = svec2i(pos.x, pos.y + option * bgSize.y);
249 color_t color = colorWhite;
250 if (selected && data->Active)
251 {
252 DrawRectangle(g, bgPos, bgSize, colorSelectedBG, true);
253 color = colorRed;
254 }
255
256 // Draw:
257 // <name> <price>
258 // <icon> <buy> <sell>
259 // <amount>/<max>
260 // With: amount/max coloured rectangle
261
262 int x = bgPos.x + 4;
263 int y = bgPos.y + ROW_H * 2;
264
265 // Amount BG
266 if (max > 0 && amount > 0)
267 {
268 DrawRectangle(
269 g, svec2i_add(svec2i(x, y), svec2i_one()),
270 svec2i(amount * (bgSize.x - 8) / max, FontH()), colorBlue, true);
271 }
272
273 y = bgPos.y + ROW_H;
274
275 // Sell/buy buttons
276 if (max > 0)
277 {
278 const bool sellSelected =
279 selected && data->Active && (data->idx & 1) == 1;
280 const bool canSell = amount > delta;
281 const FontOpts foptsSell = {
282 ALIGN_CENTER, ALIGN_START, svec2i(BUTTON_BG_W, FontH()),
283 svec2i(2, 2), sellSelected ? colorRed : colorGray};
284 x = bgPos.x + bgSize.x - BUTTON_BG_W - 2;
285 const struct vec2i sellPos = svec2i(x, y);
286 Draw9Slice(
287 g, data->buttonBG,
288 Rect2iNew(sellPos, svec2i(BUTTON_BG_W, FontH() + 4)), 3, 3, 3, 3,
289 true,
290 canSell ? (sellSelected ? colorRed : colorMaroon) : colorGray,
291 SDL_FLIP_NONE);
292 FontStrOpt("Sell", sellPos, foptsSell);
293
294 x -= BUTTON_BG_W + 3;
295 const bool buySelected =
296 selected && data->Active && (data->idx & 1) == 0;
297 const bool canBuy = amount < max && price <= pData->Totals.Score;
298 const FontOpts foptsBuy = {
299 ALIGN_CENTER, ALIGN_START, svec2i(BUTTON_BG_W, FontH()),
300 svec2i(2, 2), buySelected ? colorGreen : colorGray};
301 const struct vec2i buyPos = svec2i(x, y);
302 Draw9Slice(
303 g, data->buttonBG,
304 Rect2iNew(buyPos, svec2i(BUTTON_BG_W, FontH() + 4)), 3, 3, 3, 3,
305 true,
306 canBuy ? (buySelected ? colorGreen : colorOfficeGreen) : colorGray,
307 SDL_FLIP_NONE);
308 FontStrOpt("Buy", buyPos, foptsBuy);
309 }
310
311 y = bgPos.y;
312 x = bgPos.x + 4;
313
314 const FontOpts fopts = {
315 ALIGN_START, ALIGN_START, bgSize, svec2i(2, 2), color};
316
317 // Name
318 FontStrOpt(name, svec2i(x, y), fopts);
319
320 // Price
321 if (price > 0)
322 {
323 const FontOpts foptsP = {
324 ALIGN_END, ALIGN_START, bgSize, svec2i(8, 2),
325 selected ? colorRed : colorGray};
326 char buf[256];
327 sprintf(buf, "$%d", price);
328 FontStrOpt(buf, svec2i(x, y), foptsP);
329 }
330
331 y += ROW_H * 2;
332 x = bgPos.x + 4;
333
334 // Amount BG
335 if (max > 0 && amount > 0)
336 {
337 DrawRectangle(
338 g, svec2i_add(svec2i(x, y), svec2i_one()),
339 svec2i(amount * (bgSize.x - 8) / max, FontH()), colorBlue, true);
340 }
341
342 // Amount
343 if (max > 0)
344 {
345 char buf[256];
346 sprintf(buf, "%d/%d", amount, max);
347 const FontOpts foptsA = {
348 ALIGN_END, ALIGN_START, bgSize, svec2i(8, 2), color};
349 FontStrOpt(buf, svec2i(x, y), foptsA);
350 }
351
352 y -= ROW_H;
353
354 // Icon
355 if (pic != NULL)
356 {
357 x = bgPos.x + 12;
358 const struct vec2i picPos = svec2i_subtract(
359 svec2i(x, bgPos.y + bgSize.y / 2),
360 svec2i_scale_divide(pic->size, 2));
361 PicRender(
362 pic, g->gameWindow.renderer, picPos, colorWhite, 0, svec2_one(),
363 SDL_FLIP_NONE, Rect2iZero());
364 }
365 }
366 static int HandleInputMenu(int cmd, void *data)
367 {
368 UtilMenu *d = data;
369
370 if (Button1(cmd))
371 {
372 // Do nothing; don't switch away from menu
373 }
374 else if (Button2(cmd))
375 {
376 return 1;
377 }
378 else if (Left(cmd))
379 {
380 if ((d->idx % 2) == 1)
381 {
382 d->idx--;
383 MenuPlaySound(MENU_SOUND_SWITCH);
384 }
385 }
386 else if (Right(cmd))
387 {
388 if (d->idx < OPTION_COUNT * 2 && (d->idx % 2) == 0)
389 {
390 d->idx++;
391 MenuPlaySound(MENU_SOUND_SWITCH);
392 }
393 }
394 else if (Up(cmd))
395 {
396 if (d->idx >= 2)
397 {
398 d->idx -= 2;
399 MenuPlaySound(MENU_SOUND_SWITCH);
400 }
401 }
402 else if (Down(cmd))
403 {
404 if (d->idx + 2 < OPTION_COUNT * 2 + 2)
405 {
406 d->idx = MIN(OPTION_COUNT * 2 + 2, d->idx + 2);
407 MenuPlaySound(MENU_SOUND_SWITCH);
408 }
409 }
410
411 return 0;
412 }
413
414 void UtilMenuTerminate(UtilMenu *menu)
415 {
416 MenuSystemTerminate(&menu->ms);
417 }
418
419 void UtilMenuReset(UtilMenu *menu)
420 {
421 menu->idx = CLAMP(menu->idx, 0, OPTION_COUNT * 2);
422 }
423
424 void UtilMenuActivate(UtilMenu *menu)
425 {
426 menu->Active = true;
427 menu->SelectResult = UTIL_MENU_NONE;
428 }
429
430 bool UtilMenuUpdate(UtilMenu *menu, const int cmd)
431 {
432 PlayerData *p = PlayerDataGetByUID(menu->PlayerUID);
433 MenuProcessCmd(&menu->ms, cmd);
434 switch (menu->SelectResult)
435 {
436 case UTIL_MENU_NONE:
437 break;
438 case UTIL_MENU_SELECT: {
439 const Option option = GetSelectedOption(menu);
440 const bool buy = (menu->idx & 1) == 0;
441 switch (option)
442 {
443 case OPTION_HP:
444 PlayerSetHP(p, p->HP + (buy ? HP_DELTA : -HP_DELTA));
445 PlayerScore(p, buy ? -HP_PRICE : HP_PRICE);
446 break;
447 case OPTION_LIVES:
448 PlayerSetLives(p, p->Lives + (buy ? 1 : -1));
449 PlayerScore(p, buy ? -LIFE_PRICE : LIFE_PRICE);
450 break;
451 default:
452 break;
453 }
454 return true;
455 }
456 break;
457 case UTIL_MENU_CANCEL:
458 menu->ms.current = menu->ms.root;
459 // Switch back to equip menu
460 menu->Active = false;
461 break;
462 default:
463 CASSERT(false, "unhandled case");
464 break;
465 }
466 return false;
467 }
468
469 void UtilMenuDraw(const UtilMenu *menu)
470 {
471 MenuDisplay(&menu->ms);
472 }