Debian Codegraph @

src/menu.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2014, 2016-2021, 2025 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "menu.h"
50
51 #include <assert.h>
52
53 #include <cdogs/draw/drawtools.h>
54 #include <cdogs/font.h>
55 #include <cdogs/gamedata.h>
56 #include <cdogs/grafx_bg.h>
57 #include <cdogs/log.h>
58 #include <cdogs/mission.h>
59 #include <cdogs/music.h>
60 #include <cdogs/pic_manager.h>
61 #include <cdogs/sounds.h>
62 #include <cdogs/utils.h>
63
64 #define MS_CENTER_X(ms, w) CENTER_X((ms).pos, (ms).size, w)
65 #define MS_CENTER_Y(ms, h) CENTER_Y((ms).pos, (ms).size, h)
66
67 void MenuSystemInit(
68 MenuSystem *ms, EventHandlers *handlers, GraphicsDevice *graphics,
69 struct vec2i pos, struct vec2i size)
70 {
71 memset(ms, 0, sizeof *ms);
72 ms->root = ms->current = NULL;
73 CArrayInit(&ms->exitTypes, sizeof(menu_type_e));
74 CArrayInit(&ms->customDisplayFuncs, sizeof(MenuCustomDisplayFunc));
75 ms->handlers = handlers;
76 ms->graphics = graphics;
77 ms->pos = pos;
78 ms->size = size;
79 ms->align = MENU_ALIGN_CENTER;
80 }
81
82 static void MenuTerminate(menu_t *menu);
83
84 void MenuSystemTerminate(MenuSystem *ms)
85 {
86 MenuTerminate(ms->root);
87 CFREE(ms->root);
88 CArrayTerminate(&ms->exitTypes);
89 CArrayTerminate(&ms->customDisplayFuncs);
90 memset(ms, 0, sizeof *ms);
91 }
92
93 void MenuSetCreditsDisplayer(
94 MenuSystem *menu, credits_displayer_t *creditsDisplayer)
95 {
96 menu->creditsDisplayer = creditsDisplayer;
97 }
98
99 int MenuHasExitType(MenuSystem *menu, menu_type_e exitType)
100 {
101 CA_FOREACH(menu_type_e, m, menu->exitTypes)
102 if (*m == exitType)
103 {
104 return 1;
105 }
106 CA_FOREACH_END()
107 return 0;
108 }
109
110 void MenuAddExitType(MenuSystem *menu, menu_type_e exitType)
111 {
112 if (MenuHasExitType(menu, exitType))
113 {
114 return;
115 }
116 CArrayPushBack(&menu->exitTypes, &exitType);
117 }
118
119 void MenuSystemAddCustomDisplay(
120 MenuSystem *ms, MenuDisplayFunc func, void *data)
121 {
122 MenuCustomDisplayFunc cdf;
123 cdf.Func = func;
124 cdf.Data = data;
125 CArrayPushBack(&ms->customDisplayFuncs, &cdf);
126 }
127
128 int MenuIsExit(MenuSystem *ms)
129 {
130 return ms->current == NULL || MenuHasExitType(ms, ms->current->type);
131 }
132
133 void MenuProcessChangeKey(menu_t *menu);
134
135 bool MenuTypeHasSubMenus(const menu_type_e type)
136 {
137 return type == MENU_TYPE_NORMAL || type == MENU_TYPE_OPTIONS;
138 }
139
140 static struct vec2i SubmenuGetSize(
141 const MenuSystem *ms, const menu_t *menu, const int idx)
142 {
143 const menu_t *subMenu = CArrayGet(&menu->u.normal.subMenus, idx);
144 int maxWidth = 0;
145 CA_FOREACH(const menu_t, subMenu2, menu->u.normal.subMenus)
146 const int width = FontStrW(subMenu2->name);
147 if (width > maxWidth)
148 {
149 maxWidth = width;
150 }
151 CA_FOREACH_END()
152 // Limit max width if it is larger than the menu system size
153 maxWidth = MIN(ms->size.x, maxWidth);
154 // Add extra width for options menus
155 switch (subMenu->type)
156 {
157 case MENU_TYPE_SET_OPTION_RANGE:
158 case MENU_TYPE_SET_OPTION_SEED: // fallthrough
159 case MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID: // fallthrough
160 case MENU_TYPE_SET_OPTION_RANGE_GET_SET: // fallthrough
161 switch (subMenu->u.option.displayStyle)
162 {
163 case MENU_OPTION_DISPLAY_STYLE_NONE:
164 // Do nothing
165 break;
166 case MENU_OPTION_DISPLAY_STYLE_STR_FUNC:
167 case MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC: // fallthrough
168 maxWidth += 80;
169 break;
170 default:
171 CASSERT(false, "unknown menu display type");
172 break;
173 }
174 break;
175 case MENU_TYPE_SET_OPTION_TOGGLE:
176 case MENU_TYPE_SET_OPTION_CHANGE_KEY: // fallthrough
177 maxWidth += 80;
178 break;
179 default:
180 // do nothing
181 break;
182 }
183
184 return svec2i(maxWidth, FontStrH(subMenu->name));
185 }
186 static Rect2i MenuGetSubmenuBounds(const MenuSystem *ms, const int idx)
187 {
188 const menu_t *menu = ms->current;
189 if (menu == NULL || !MenuTypeHasSubMenus(menu->type))
190 {
191 return Rect2iZero();
192 }
193 // Calculate first/last indices
194 const int maxItems = menu->u.normal.maxItems;
195 const int iStart = maxItems > 0 ? menu->u.normal.scroll : 0;
196 if (idx < iStart)
197 {
198 return Rect2iZero();
199 }
200 const int maxIEnd =
201 MIN(maxItems > 0 ? iStart + maxItems : 99,
202 (int)menu->u.normal.subMenus.size);
203 int numMenuLines = 0;
204 // Count the number of menu items that can fit
205 // This is to account for multi-line items
206 int iEnd;
207 for (iEnd = iStart; iEnd < maxIEnd; iEnd++)
208 {
209 const menu_t *subMenu = CArrayGet(&menu->u.normal.subMenus, iEnd);
210 const int numLines = FontStrNumLines(subMenu->name);
211 if (menu->u.normal.maxItems != 0 &&
212 numMenuLines + numLines > menu->u.normal.maxItems)
213 {
214 break;
215 }
216 numMenuLines += numLines;
217 }
218 if (iEnd <= idx)
219 {
220 return Rect2iZero();
221 }
222
223 int maxWidth = 0;
224 CA_FOREACH(const menu_t, subMenu, menu->u.normal.subMenus)
225 const int width = FontStrW(subMenu->name);
226 if (width > maxWidth)
227 {
228 maxWidth = width;
229 }
230 CA_FOREACH_END()
231 // Limit max width if it is larger than the menu system size
232 maxWidth = MIN(ms->size.x, maxWidth);
233
234 struct vec2i pos;
235 const bool isCentered = menu->type == MENU_TYPE_NORMAL;
236 switch (ms->align)
237 {
238 case MENU_ALIGN_CENTER:
239 pos.x = MS_CENTER_X(*ms, maxWidth);
240 if (!isCentered)
241 {
242 pos.x -= 20;
243 }
244 break;
245 case MENU_ALIGN_LEFT:
246 pos.x = ms->pos.x;
247 break;
248 default:
249 CASSERT(false, "unknown alignment");
250 return Rect2iZero();
251 }
252
253 pos.y = MS_CENTER_Y(*ms, numMenuLines * FontH());
254 for (int i = iStart; i < idx; i++)
255 {
256 const struct vec2i size = SubmenuGetSize(ms, menu, i);
257 pos.y += size.y;
258 }
259 return Rect2iNew(pos, SubmenuGetSize(ms, menu, idx));
260 }
261
262 static void MoveIndexToNextEnabledSubmenu(menu_t *menu, const bool isDown)
263 {
264 if (menu->u.normal.index >= (int)menu->u.normal.subMenus.size)
265 {
266 menu->u.normal.index = (int)menu->u.normal.subMenus.size - 1;
267 }
268 const int firstIndex = menu->u.normal.index;
269 bool isFirst = true;
270 // Move the selection to the next non-disabled submenu
271 for (;;)
272 {
273 const menu_t *currentSubmenu =
274 CArrayGet(&menu->u.normal.subMenus, menu->u.normal.index);
275 if (!currentSubmenu->isDisabled)
276 {
277 break;
278 }
279 if (menu->u.normal.index == firstIndex && !isFirst)
280 {
281 break;
282 }
283 isFirst = false;
284 if (isDown)
285 {
286 menu->u.normal.index++;
287 if (menu->u.normal.index == (int)menu->u.normal.subMenus.size)
288 {
289 menu->u.normal.index = 0;
290 }
291 }
292 else
293 {
294 menu->u.normal.index--;
295 if (menu->u.normal.index == -1)
296 {
297 menu->u.normal.index = (int)menu->u.normal.subMenus.size - 1;
298 }
299 }
300 }
301 }
302 // Constrain the menu scroll so that the selected item is always in view
303 // and we don't scroll the menu off the screen
304 static void MenuClampScroll(menu_t *menu)
305 {
306 const int nMenuItems = MenuGetNumMenuItemsShown(menu);
307 menu->u.normal.scroll = CLAMP(
308 menu->u.normal.scroll, MAX(0, menu->u.normal.index - nMenuItems + 1),
309 MIN((int)menu->u.normal.subMenus.size - nMenuItems,
310 menu->u.normal.index));
311 if (menu->u.normal.index < menu->u.normal.scroll)
312 {
313 menu->u.normal.scroll = menu->u.normal.index;
314 }
315 }
316 static void MenuChangeIndex(menu_t *menu, const int d)
317 {
318 if (d < 0)
319 {
320 menu->u.normal.index--;
321 if (menu->u.normal.index == -1)
322 {
323 menu->u.normal.index = (int)menu->u.normal.subMenus.size - 1;
324 }
325 MoveIndexToNextEnabledSubmenu(menu, false);
326 MenuPlaySound(MENU_SOUND_SWITCH);
327 }
328 else if (d > 0)
329 {
330 menu->u.normal.index++;
331 if (menu->u.normal.index == (int)menu->u.normal.subMenus.size)
332 {
333 menu->u.normal.index = 0;
334 }
335 MoveIndexToNextEnabledSubmenu(menu, true);
336 MenuPlaySound(MENU_SOUND_SWITCH);
337 }
338 }
339
340 static GameLoopResult DefaultMenuUpdate(GameLoopData *data, LoopRunner *l);
341 static void DefaultMenuDraw(GameLoopData *data);
342 GameLoopData *MenuLoop(MenuSystem *menu)
343 {
344 return GameLoopDataNew(
345 menu, NULL, NULL, NULL, NULL, DefaultMenuUpdate, DefaultMenuDraw);
346 }
347 static GameLoopResult DefaultMenuUpdate(GameLoopData *data, LoopRunner *l)
348 {
349 UNUSED(l);
350 return MenuUpdate(data->Data);
351 }
352 static void DefaultMenuDraw(GameLoopData *data)
353 {
354 MenuDraw(data->Data);
355 }
356 void MenuUpdateMouse(MenuSystem *ms)
357 {
358 menu_t *menu = ms->current;
359 if (menu == NULL || !MenuTypeHasSubMenus(menu->type))
360 {
361 return;
362 }
363 if (MouseHasMoved(&ms->handlers->mouse))
364 {
365 // Get mouse position and change menu
366 menu->mouseHover = false;
367 for (int i = 0; i < (int)menu->u.normal.subMenus.size; i++)
368 {
369 const Rect2i bounds = MenuGetSubmenuBounds(ms, i);
370 if (!Rect2iIsInside(bounds, ms->handlers->mouse.currentPos))
371 {
372 continue;
373 }
374 menu->mouseHover = true;
375 if (menu->u.normal.index != i)
376 {
377 const menu_t *subMenu = CArrayGet(&menu->u.normal.subMenus, i);
378 if (!subMenu->isDisabled)
379 {
380 menu->u.normal.index = i;
381 MenuPlaySound(MENU_SOUND_SWITCH);
382 }
383 }
384 break;
385 }
386 }
387 // Scroll menu with mouse wheel
388 const int dWheel = MouseWheel(&ms->handlers->mouse).y;
389 if (dWheel != 0)
390 {
391 const int dd = dWheel > 0 ? 1 : -1;
392 for (int i = 0; i != dWheel; i += dd)
393 {
394 const int origScroll = menu->u.normal.scroll;
395 menu->u.normal.scroll += dd;
396 MenuClampScroll(menu);
397 if (menu->u.normal.scroll == origScroll)
398 {
399 break;
400 }
401 MenuChangeIndex(menu, dd);
402 }
403 }
404 }
405 GameLoopResult MenuUpdate(MenuSystem *ms)
406 {
407 if (ms->current->type == MENU_TYPE_OPTIONS &&
408 ms->current->u.normal.changeKeyMenu != NULL)
409 {
410 MenuProcessChangeKey(ms->current);
411 }
412 else
413 {
414 const int cmd = GetMenuCmd(ms->handlers, ms->current->mouseHover);
415 if (cmd)
416 {
417 MenuProcessCmd(ms, cmd);
418 }
419 else
420 {
421 MenuUpdateMouse(ms);
422 }
423 }
424 // Check if anyone pressed escape, or we need a hard exit
425 int cmds[MAX_LOCAL_PLAYERS];
426 memset(cmds, 0, sizeof cmds);
427 GetPlayerCmds(ms->handlers, &cmds);
428 const bool aborted =
429 ms->allowAborts &&
430 EventIsEscape(ms->handlers, cmds, GetMenuCmd(ms->handlers, false));
431 if (aborted || ms->handlers->HasQuit)
432 {
433 ms->hasAbort = true;
434 return UPDATE_RESULT_OK;
435 }
436 if (MenuIsExit(ms))
437 {
438 return UPDATE_RESULT_OK;
439 }
440 if (ms->current->customPostUpdateFunc)
441 {
442 ms->current->customPostUpdateFunc(
443 ms->current, ms->current->customPostUpdateData);
444 }
445 return UPDATE_RESULT_DRAW;
446 }
447 void MenuDraw(const MenuSystem *ms)
448 {
449 BlitClearBuf(ms->graphics);
450 ShowControls();
451 MenuDisplay(ms);
452 BlitUpdateFromBuf(ms->graphics, ms->graphics->screen);
453 }
454
455 void MenuReset(MenuSystem *menu)
456 {
457 menu->current = menu->root;
458 }
459
460 void MenuDisableSubmenu(menu_t *menu, int idx)
461 {
462 menu_t *subMenu = CArrayGet(&menu->u.normal.subMenus, idx);
463 MenuSetDisabled(subMenu, true);
464 }
465 void MenuEnableSubmenu(menu_t *menu, int idx)
466 {
467 menu_t *subMenu = CArrayGet(&menu->u.normal.subMenus, idx);
468 MenuSetDisabled(subMenu, false);
469 }
470 void MenuSetDisabled(menu_t *menu, const bool isDisabled)
471 {
472 menu->isDisabled = isDisabled;
473 if (isDisabled && menu->parentMenu != NULL)
474 {
475 MoveIndexToNextEnabledSubmenu(menu->parentMenu, true);
476 }
477 }
478
479 menu_t *MenuGetSubmenuByName(menu_t *menu, const char *name)
480 {
481 CASSERT(menu->type == MENU_TYPE_NORMAL, "invalid menu type");
482 CA_FOREACH(menu_t, submenu, menu->u.normal.subMenus)
483 if (strcmp(submenu->name, name) == 0)
484 return submenu;
485 CA_FOREACH_END()
486 return NULL;
487 }
488
489 int MenuGetNumMenuItemsShown(const menu_t *menu)
490 {
491 CASSERT(MenuTypeHasSubMenus(menu->type), "invalid menu type");
492 return menu->u.normal.maxItems > 0 ? MIN(menu->u.normal.maxItems,
493 (int)menu->u.normal.subMenus.size)
494 : (int)menu->u.normal.subMenus.size;
495 }
496
497 void ShowControls(void)
498 {
499 FontOpts opts = FontOptsNew();
500 opts.HAlign = ALIGN_CENTER;
501 opts.VAlign = ALIGN_END;
502 opts.Area = gGraphicsDevice.cachedConfig.Res;
503 opts.Pad.y = 10;
504 #ifdef __GCWZERO__
505 FontStrOpt(
506 "(use joystick or D pad + START + SELECT)", svec2i_zero(), opts);
507 #else
508 FontStrOpt(
509 "(use joystick 1 or arrow keys + Enter/Backspace)", svec2i_zero(),
510 opts);
511 #endif
512 }
513
514 struct vec2i DisplayMenuItem(
515 GraphicsDevice *g, const Rect2i bounds, const char *s, const bool selected,
516 const bool isDisabled, const color_t color)
517 {
518 if (isDisabled)
519 {
520 return FontStrMask(s, bounds.Pos, colorDarkGray);
521 }
522 if (selected)
523 {
524 // Add 1px padding
525 const struct vec2i bgPos = svec2i_subtract(bounds.Pos, svec2i_one());
526 const struct vec2i bgSize = svec2i_add(bounds.Size, svec2i(2, 2));
527 DrawRectangle(g, bgPos, bgSize, colorSelectedBG, true);
528 return FontStrMask(s, bounds.Pos, colorRed);
529 }
530 if (!ColorEquals(color, colorTransparent))
531 {
532 return FontStrMask(s, bounds.Pos, color);
533 }
534 return FontStr(s, bounds.Pos);
535 }
536
537 menu_t *MenuCreate(const char *name, menu_type_e type)
538 {
539 menu_t *menu;
540 CCALLOC(menu, sizeof(menu_t));
541 CSTRDUP(menu->name, name);
542 menu->type = type;
543 menu->parentMenu = NULL;
544 menu->enterSound = MENU_SOUND_ENTER;
545 return menu;
546 }
547
548 menu_t *MenuCreateNormal(
549 const char *name, const char *title, menu_type_e type, int displayItems)
550 {
551 menu_t *menu = MenuCreate(name, type);
552 strcpy(menu->u.normal.title, title);
553 menu->u.normal.isSubmenusAlt = false;
554 menu->u.normal.displayItems = displayItems;
555 menu->u.normal.changeKeyMenu = NULL;
556 menu->u.normal.index = 0;
557 menu->u.normal.scroll = 0;
558 menu->u.normal.maxItems = 0;
559 menu->u.normal.align = MENU_ALIGN_LEFT;
560 menu->u.normal.quitMenuIndex = -1;
561 CArrayInit(&menu->u.normal.subMenus, sizeof(menu_t));
562 return menu;
563 }
564
565 static void UpdateSubmenuParentPtrs(menu_t *menu)
566 {
567 CA_FOREACH(menu_t, subMenu, menu->u.normal.subMenus)
568 subMenu->parentMenu = menu;
569 if (MenuTypeHasSubMenus(subMenu->type))
570 {
571 UpdateSubmenuParentPtrs(subMenu);
572 }
573 CA_FOREACH_END()
574 }
575 menu_t *MenuAddSubmenu(menu_t *menu, menu_t *subMenu)
576 {
577 CArrayPushBack(&menu->u.normal.subMenus, subMenu);
578 if (subMenu->type == MENU_TYPE_QUIT)
579 {
580 menu->u.normal.quitMenuIndex = (int)menu->u.normal.subMenus.size - 1;
581 }
582 CFREE(subMenu);
583
584 // update all parent pointers, in child menus
585 UpdateSubmenuParentPtrs(menu);
586
587 // move cursor in case first menu item(s) are disabled
588 MoveIndexToNextEnabledSubmenu(menu, true);
589
590 return CArrayGet(
591 &menu->u.normal.subMenus, menu->u.normal.subMenus.size - 1);
592 }
593
594 void MenuSetPostInputFunc(menu_t *menu, MenuPostInputFunc func, void *data)
595 {
596 menu->customPostInputFunc = func;
597 menu->customPostInputData = data;
598 }
599
600 void MenuSetPostEnterFunc(
601 menu_t *menu, MenuFunc func, void *data, const bool isDynamicData)
602 {
603 menu->customPostEnterFunc = func;
604 menu->customPostEnterData = data;
605 menu->isCustomPostEnterDataDynamic = isDynamicData;
606 }
607
608 void MenuSetPostUpdateFunc(
609 menu_t *menu, MenuFunc func, void *data, const bool isDynamicData)
610 {
611 menu->customPostUpdateFunc = func;
612 menu->customPostUpdateData = data;
613 menu->isCustomPostUpdateDataDynamic = isDynamicData;
614 }
615
616 void MenuSetCustomDisplay(menu_t *menu, MenuDisplayFunc func, const void *data)
617 {
618 menu->customDisplayFunc = func;
619 menu->customDisplayData = data;
620 }
621
622 void MenuAddConfigOptionsItem(menu_t *menu, Config *c)
623 {
624 char nameBuf[256];
625 CASSERT(strlen(c->Name) < sizeof nameBuf, "buffer too small");
626 CamelToTitle(nameBuf, c->Name);
627 switch (c->Type)
628 {
629 case CONFIG_TYPE_STRING:
630 CASSERT(false, "Unimplemented");
631 break;
632 case CONFIG_TYPE_INT:
633 MenuAddSubmenu(
634 menu,
635 MenuCreateOptionRange(
636 nameBuf, (int *)&c->u.Int.Value, c->u.Int.Min, c->u.Int.Max,
637 c->u.Int.Increment, MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC,
638 (void (*)(void))c->u.Int.IntToStr));
639 break;
640 case CONFIG_TYPE_FLOAT:
641 CASSERT(false, "Unimplemented");
642 break;
643 case CONFIG_TYPE_BOOL:
644 MenuAddSubmenu(
645 menu, MenuCreateOptionToggle(nameBuf, &c->u.Bool.Value));
646 break;
647 case CONFIG_TYPE_ENUM:
648 MenuAddSubmenu(
649 menu,
650 MenuCreateOptionRange(
651 nameBuf, (int *)&c->u.Enum.Value, c->u.Enum.Min, c->u.Enum.Max,
652 1, MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC,
653 (void (*)(void))c->u.Enum.EnumToStr));
654 break;
655 case CONFIG_TYPE_GROUP:
656 // Do nothing
657 break;
658 default:
659 CASSERT(false, "Unknown config type");
660 break;
661 }
662 }
663
664 menu_t *MenuCreateOptionToggle(const char *name, bool *config)
665 {
666 menu_t *menu = MenuCreate(name, MENU_TYPE_SET_OPTION_TOGGLE);
667 menu->u.option.uHook.optionToggle = config;
668 menu->u.option.displayStyle = MENU_OPTION_DISPLAY_STYLE_NONE;
669 return menu;
670 }
671
672 menu_t *MenuCreateOptionRange(
673 const char *name, int *config, int low, int high, int increment,
674 menu_option_display_style_e style, void (*func)(void))
675 {
676 menu_t *menu = MenuCreate(name, MENU_TYPE_SET_OPTION_RANGE);
677 menu->u.option.uHook.optionRange.option = config;
678 menu->u.option.uHook.optionRange.low = low;
679 menu->u.option.uHook.optionRange.high = high;
680 menu->u.option.uHook.optionRange.increment = increment;
681 menu->u.option.displayStyle = style;
682 if (style == MENU_OPTION_DISPLAY_STYLE_STR_FUNC)
683 {
684 menu->u.option.uFunc.str = (char *(*)(void))func;
685 }
686 else if (style == MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC)
687 {
688 menu->u.option.uFunc.intToStr = (const char *(*)(int))func;
689 }
690 return menu;
691 }
692
693 menu_t *MenuCreateOptionSeed(const char *name, unsigned int *seed)
694 {
695 menu_t *menu = MenuCreate(name, MENU_TYPE_SET_OPTION_SEED);
696 menu->u.option.uHook.seed = seed;
697 menu->u.option.displayStyle = MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC;
698 return menu;
699 }
700
701 menu_t *MenuCreateOptionUpDownFunc(
702 const char *name, void (*upFunc)(void), void (*downFunc)(void),
703 menu_option_display_style_e style, char *(*strFunc)(void))
704 {
705 menu_t *menu =
706 MenuCreate(name, MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID);
707 menu->u.option.uHook.upDownFuncs.upFunc = upFunc;
708 menu->u.option.uHook.upDownFuncs.downFunc = downFunc;
709 menu->u.option.displayStyle = style;
710 menu->u.option.uFunc.str = strFunc;
711 return menu;
712 }
713
714 menu_t *MenuCreateVoidFunc(const char *name, void (*func)(void *), void *data)
715 {
716 menu_t *menu = MenuCreate(name, MENU_TYPE_VOID_FUNC);
717 menu->u.option.uHook.voidFunc.func = func;
718 menu->u.option.uHook.voidFunc.data = data;
719 menu->u.option.displayStyle = MENU_OPTION_DISPLAY_STYLE_NONE;
720 return menu;
721 }
722
723 menu_t *MenuCreateOptionRangeGetSet(
724 const char *name, int (*getFunc)(void), void (*setFunc)(int), int low,
725 int high, int increment, menu_option_display_style_e style,
726 void (*func)(void))
727 {
728 menu_t *menu = MenuCreate(name, MENU_TYPE_SET_OPTION_RANGE_GET_SET);
729 menu->u.option.uHook.optionRangeGetSet.getFunc = getFunc;
730 menu->u.option.uHook.optionRangeGetSet.setFunc = setFunc;
731 menu->u.option.uHook.optionRangeGetSet.low = low;
732 menu->u.option.uHook.optionRangeGetSet.high = high;
733 menu->u.option.uHook.optionRangeGetSet.increment = increment;
734 menu->u.option.displayStyle = style;
735 // TODO: refactor saving of function based on style
736 if (style == MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC)
737 {
738 menu->u.option.uFunc.intToStr = (const char *(*)(int))func;
739 }
740 return menu;
741 }
742
743 menu_t *MenuCreateSeparator(const char *name)
744 {
745 menu_t *menu = MenuCreate(name, MENU_TYPE_BASIC);
746 menu->isDisabled = 1;
747 return menu;
748 }
749
750 menu_t *MenuCreateBack(const char *name)
751 {
752 return MenuCreate(name, MENU_TYPE_BACK);
753 }
754
755 menu_t *MenuCreateReturn(const char *name, int returnCode)
756 {
757 menu_t *menu = MenuCreate(name, MENU_TYPE_RETURN);
758 menu->u.returnCode = returnCode;
759 return menu;
760 }
761
762 menu_t *MenuCreateCustom(
763 const char *name, MenuDisplayFunc displayFunc, MenuInputFunc inputFunc,
764 void *data)
765 {
766 menu_t *menu = MenuCreate(name, MENU_TYPE_CUSTOM);
767 menu->u.customData.displayFunc = displayFunc;
768 menu->u.customData.inputFunc = inputFunc;
769 menu->u.customData.data = data;
770 return menu;
771 }
772
773 static void MenuDisplayItems(const MenuSystem *ms);
774 static void MenuDisplaySubmenus(const MenuSystem *ms);
775 void MenuDisplay(const MenuSystem *ms)
776 {
777 const menu_t *menu = ms->current;
778 if (menu != NULL)
779 {
780 if (menu->type == MENU_TYPE_CUSTOM)
781 {
782 menu->u.customData.displayFunc(
783 menu, ms->graphics, ms->pos, ms->size,
784 menu->u.customData.data);
785 }
786 else
787 {
788 MenuDisplayItems(ms);
789
790 if (strlen(menu->u.normal.title) != 0)
791 {
792 FontOpts opts = FontOptsNew();
793 opts.HAlign = ALIGN_CENTER;
794 opts.Area = ms->size;
795 opts.Pad = svec2i(20, 20);
796 FontStrOpt(menu->u.normal.title, ms->pos, opts);
797 }
798
799 MenuDisplaySubmenus(ms);
800 }
801 }
802 CA_FOREACH(MenuCustomDisplayFunc, cdf, ms->customDisplayFuncs)
803 cdf->Func(NULL, ms->graphics, ms->pos, ms->size, cdf->Data);
804 CA_FOREACH_END()
805 if (menu != NULL && menu->customDisplayFunc)
806 {
807 menu->customDisplayFunc(
808 menu, ms->graphics, ms->pos, ms->size, menu->customDisplayData);
809 }
810 }
811 static void MenuDisplayItems(const MenuSystem *ms)
812 {
813 int d = ms->current->u.normal.displayItems;
814 if ((d & MENU_DISPLAY_ITEMS_CREDITS) && ms->creditsDisplayer != NULL)
815 {
816 ShowCredits(ms->creditsDisplayer);
817 }
818 if (d & MENU_DISPLAY_ITEMS_AUTHORS)
819 {
820 const Pic *logo = PicManagerGetPic(&gPicManager, "logo");
821 const struct vec2i pos = svec2i(
822 MS_CENTER_X(*ms, logo->size.x), ms->pos.y + ms->size.y / 12);
823 PicRender(
824 logo, ms->graphics->gameWindow.renderer, pos, colorWhite, 0,
825 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
826
827 FontOpts opts = FontOptsNew();
828 opts.HAlign = ALIGN_END;
829 opts.Area = ms->size;
830 opts.Pad = svec2i(20, 20);
831 FontStrOpt("Version: " CDOGS_SDL_VERSION, ms->pos, opts);
832 }
833 }
834 static int MenuOptionGetIntValue(const menu_t *menu);
835 static void MenuDisplaySubmenus(const MenuSystem *ms)
836 {
837 const menu_t *menu = ms->current;
838 if (!MenuTypeHasSubMenus(menu->type))
839 {
840 return;
841 }
842
843 int maxWidth = 0;
844 CA_FOREACH(const menu_t, subMenu, menu->u.normal.subMenus)
845 const int width = FontStrW(subMenu->name);
846 if (width > maxWidth)
847 {
848 maxWidth = width;
849 }
850 CA_FOREACH_END()
851
852 CA_FOREACH(const menu_t, subMenu, menu->u.normal.subMenus)
853 const Rect2i bounds = MenuGetSubmenuBounds(ms, _ca_index);
854 if (Rect2iIsZero(bounds))
855 {
856 continue;
857 }
858
859 const int xOptions = bounds.Pos.x + maxWidth + 10;
860 char nameBuf[512];
861 if (subMenu->type == MENU_TYPE_NORMAL && subMenu->u.normal.isSubmenusAlt)
862 {
863 snprintf(nameBuf, sizeof(nameBuf), "%s " ARROW_RIGHT, subMenu->name);
864 }
865 else
866 {
867 snprintf(nameBuf, sizeof(nameBuf), "%s", subMenu->name);
868 }
869
870 const bool isSelected = _ca_index == menu->u.normal.index;
871 DisplayMenuItem(
872 ms->graphics, bounds, nameBuf, isSelected,
873 menu->isDisabled || subMenu->isDisabled, subMenu->color);
874
875 // display option value
876 const int optionInt = MenuOptionGetIntValue(subMenu);
877 const struct vec2i valuePos = svec2i(xOptions, bounds.Pos.y);
878 const char *option = NULL;
879 if (subMenu->type == MENU_TYPE_SET_OPTION_RANGE ||
880 subMenu->type == MENU_TYPE_SET_OPTION_SEED ||
881 subMenu->type == MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID ||
882 subMenu->type == MENU_TYPE_SET_OPTION_RANGE_GET_SET)
883 {
884 switch (subMenu->u.option.displayStyle)
885 {
886 case MENU_OPTION_DISPLAY_STYLE_NONE:
887 // Do nothing
888 break;
889 case MENU_OPTION_DISPLAY_STYLE_STR_FUNC:
890 option = subMenu->u.option.uFunc.str();
891 break;
892 case MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC:
893 option = subMenu->u.option.uFunc.intToStr(optionInt);
894 break;
895 default:
896 CASSERT(false, "unknown menu display type");
897 break;
898 }
899 }
900 else if (subMenu->type == MENU_TYPE_SET_OPTION_TOGGLE)
901 {
902 option = optionInt ? "Yes" : "No";
903 }
904 else if (subMenu->type == MENU_TYPE_SET_OPTION_CHANGE_KEY)
905 {
906 if (menu->u.normal.changeKeyMenu == subMenu)
907 {
908 option = "Press a key";
909 }
910 else
911 {
912 const int pi = subMenu->u.changeKey.playerIndex;
913 const InputKeys *keys = &ms->handlers->keyboard.PlayerKeys[pi];
914 const SDL_Scancode sc = KeyGet(keys, subMenu->u.changeKey.code);
915 option = SDL_GetScancodeName(sc);
916 if (sc == SDL_SCANCODE_UNKNOWN || option == NULL)
917 {
918 option = "Unset";
919 }
920 }
921 }
922 if (option != NULL)
923 {
924 char buf[256];
925 if (isSelected)
926 {
927 sprintf(buf, ARROW_LEFT " %s " ARROW_RIGHT, option);
928 }
929 else
930 {
931 strcpy(buf, option);
932 }
933 FontStr(buf, valuePos);
934 }
935 CA_FOREACH_END()
936 }
937
938 void MenuPlaySound(MenuSound s)
939 {
940 switch (s)
941 {
942 case MENU_SOUND_ENTER:
943 SoundPlay(&gSoundDevice, StrSound("menu_enter"));
944 break;
945 case MENU_SOUND_BACK:
946 SoundPlay(&gSoundDevice, StrSound("menu_back"));
947 break;
948 case MENU_SOUND_SWITCH:
949 SoundPlay(&gSoundDevice, StrSound("menu_switch"));
950 break;
951 case MENU_SOUND_START:
952 SoundPlay(&gSoundDevice, StrSound("menu_start"));
953 break;
954 case MENU_SOUND_ERROR:
955 SoundPlay(&gSoundDevice, StrSound("menu_error"));
956 break;
957 default:
958 break;
959 }
960 }
961
962 static void MenuTerminateSubmenus(menu_t *menu);
963 static void MenuTerminate(menu_t *menu)
964 {
965 if (menu == NULL)
966 {
967 return;
968 }
969 CFREE(menu->name);
970 if (menu->isCustomPostUpdateDataDynamic)
971 {
972 CFREE(menu->customPostUpdateData);
973 }
974 if (menu->isCustomPostEnterDataDynamic)
975 {
976 CFREE(menu->customPostEnterData);
977 }
978 MenuTerminateSubmenus(menu);
979 }
980 static void MenuTerminateSubmenus(menu_t *menu)
981 {
982 if (!MenuTypeHasSubMenus(menu->type))
983 {
984 return;
985 }
986 CA_FOREACH(menu_t, subMenu, menu->u.normal.subMenus)
987 MenuTerminate(subMenu);
988 CA_FOREACH_END()
989 CArrayTerminate(&menu->u.normal.subMenus);
990 }
991
992 void MenuClearSubmenus(menu_t *menu)
993 {
994 if (!MenuTypeHasSubMenus(menu->type))
995 {
996 CASSERT(false, "attempt to clear submenus for invalid menu type");
997 return;
998 }
999 MenuTerminateSubmenus(menu);
1000 CArrayInit(&menu->u.normal.subMenus, sizeof(menu_t));
1001 }
1002
1003 static int MenuOptionGetIntValue(const menu_t *menu)
1004 {
1005 switch (menu->type)
1006 {
1007 case MENU_TYPE_SET_OPTION_TOGGLE:
1008 return *menu->u.option.uHook.optionToggle;
1009 case MENU_TYPE_SET_OPTION_RANGE:
1010 return *menu->u.option.uHook.optionRange.option;
1011 case MENU_TYPE_SET_OPTION_SEED:
1012 return (int)*menu->u.option.uHook.seed;
1013 case MENU_TYPE_SET_OPTION_RANGE_GET_SET:
1014 return menu->u.option.uHook.optionRangeGetSet.getFunc();
1015 default:
1016 return 0;
1017 }
1018 }
1019
1020 // returns menu to change to, NULL if no change
1021 menu_t *MenuProcessEscCmd(menu_t *menu);
1022 menu_t *MenuProcessButtonCmd(MenuSystem *ms, menu_t *menu, int cmd);
1023 static void MenuChangeIndexCmd(menu_t *menu, const int cmd);
1024
1025 void MenuProcessCmd(MenuSystem *ms, int cmd)
1026 {
1027 menu_t *menu = ms->current;
1028 menu_t *menuToChange = NULL;
1029 if (menu == NULL)
1030 {
1031 goto bail;
1032 }
1033 if (cmd == CMD_ESC || Button2(cmd) ||
1034 (Left(cmd) && menu->u.normal.isSubmenusAlt))
1035 {
1036 menuToChange = MenuProcessEscCmd(menu);
1037 if (menuToChange != NULL)
1038 {
1039 MenuPlaySound(MENU_SOUND_BACK);
1040 ms->current = menuToChange;
1041 goto bail;
1042 }
1043 }
1044 if (menu->type == MENU_TYPE_CUSTOM)
1045 {
1046 if (menu->u.customData.inputFunc == NULL ||
1047 menu->u.customData.inputFunc(cmd, menu->u.customData.data))
1048 {
1049 ms->current = menu->parentMenu;
1050 goto bail;
1051 }
1052 }
1053 else if (cmd != 0)
1054 {
1055 menuToChange = MenuProcessButtonCmd(ms, menu, cmd);
1056 if (menuToChange != NULL)
1057 {
1058 MenuPlaySound(menuToChange->enterSound);
1059 ms->current = menuToChange;
1060 goto bail;
1061 }
1062 MenuChangeIndexCmd(menu, cmd);
1063 }
1064
1065 bail:
1066 if (menu && menu->customPostInputFunc)
1067 {
1068 menu->customPostInputFunc(menu, cmd, menu->customPostInputData);
1069 }
1070 if (menuToChange && menuToChange->customPostEnterFunc)
1071 {
1072 menuToChange->customPostEnterFunc(
1073 menuToChange, menuToChange->customPostEnterData);
1074 }
1075 }
1076
1077 menu_t *MenuProcessEscCmd(menu_t *menu)
1078 {
1079 menu_t *menuToChange = NULL;
1080 int quitMenuIndex = menu->u.normal.quitMenuIndex;
1081 if (quitMenuIndex != -1)
1082 {
1083 if (menu->u.normal.index != quitMenuIndex)
1084 {
1085 MenuPlaySound(MENU_SOUND_SWITCH);
1086 menu->u.normal.index = quitMenuIndex;
1087 }
1088 else if (menu->u.normal.subMenus.size > 0)
1089 {
1090 menuToChange = CArrayGet(&menu->u.normal.subMenus, quitMenuIndex);
1091 }
1092 }
1093 else
1094 {
1095 menuToChange = menu->parentMenu;
1096 }
1097 return menuToChange;
1098 }
1099
1100 void MenuActivate(MenuSystem *ms, menu_t *menu, int cmd);
1101
1102 menu_t *MenuProcessButtonCmd(MenuSystem *ms, menu_t *menu, int cmd)
1103 {
1104 if (AnyButton(cmd) || Left(cmd) || Right(cmd))
1105 {
1106 // Ignore if menu contains no submenus
1107 if (menu->u.normal.subMenus.size == 0)
1108 {
1109 return NULL;
1110 }
1111 menu_t *subMenu =
1112 CArrayGet(&menu->u.normal.subMenus, menu->u.normal.index);
1113
1114 // Only allow menu switching on button 1
1115
1116 switch (subMenu->type)
1117 {
1118 case MENU_TYPE_NORMAL:
1119 case MENU_TYPE_OPTIONS:
1120 case MENU_TYPE_CUSTOM:
1121 if (subMenu->u.normal.isSubmenusAlt ? Right(cmd) : (Button1(cmd)))
1122 {
1123 return subMenu;
1124 }
1125 break;
1126 case MENU_TYPE_BACK:
1127 if (Button1(cmd))
1128 {
1129 return menu->parentMenu;
1130 }
1131 break;
1132 case MENU_TYPE_QUIT:
1133 if (Button1(cmd))
1134 {
1135 return subMenu; // caller will check if subMenu type is QUIT
1136 }
1137 break;
1138 case MENU_TYPE_RETURN:
1139 if (Button1(cmd))
1140 {
1141 return subMenu;
1142 }
1143 break;
1144 default:
1145 MenuActivate(ms, subMenu, cmd);
1146 break;
1147 }
1148 }
1149 return NULL;
1150 }
1151
1152 static bool KeyAvailable(
1153 const SDL_Scancode key, const key_code_e code, const int playerIndex)
1154 {
1155 if (key == SDL_SCANCODE_ESCAPE || key == SDL_SCANCODE_F9 ||
1156 key == SDL_SCANCODE_F10)
1157 {
1158 return false;
1159 }
1160 if (key == SDL_SCANCODE_UNKNOWN)
1161 {
1162 return true;
1163 }
1164 if (key == (SDL_Scancode)ConfigGetInt(&gConfig, "Input.PlayerCodes0.map"))
1165 {
1166 return false;
1167 }
1168
1169 // Check if the key is being used by another control
1170 char buf[256];
1171 sprintf(buf, "Input.PlayerCodes%d", playerIndex);
1172 const InputKeys keys = KeyLoadPlayerKeys(ConfigGet(&gConfig, buf));
1173 for (key_code_e i = 0; i < KEY_CODE_MAP; i++)
1174 {
1175 if (i != code && KeyGet(&keys, i) == key)
1176 {
1177 return false;
1178 }
1179 }
1180
1181 // Check if the other player is using the key
1182 sprintf(buf, "Input.PlayerCodes%d", 1 - playerIndex);
1183 const InputKeys keysOther = KeyLoadPlayerKeys(ConfigGet(&gConfig, buf));
1184 if (keysOther.left == key || keysOther.right == key ||
1185 keysOther.up == key || keysOther.down == key ||
1186 keysOther.button1 == key || keysOther.button2 == key)
1187 {
1188 return false;
1189 }
1190
1191 return true;
1192 }
1193
1194 static void ChangeKey(
1195 const SDL_Scancode key, const key_code_e code, const int playerIndex);
1196 void MenuProcessChangeKey(menu_t *menu)
1197 {
1198 // wait until user has pressed a new button
1199 SDL_Scancode key = KeyGetPressed(&gEventHandlers.keyboard);
1200 if (key == SDL_SCANCODE_UNKNOWN)
1201 {
1202 return;
1203 }
1204 const menu_t *changeKeyMenu = menu->u.normal.changeKeyMenu;
1205 const key_code_e code = changeKeyMenu->u.changeKey.code;
1206 const int pi = changeKeyMenu->u.changeKey.playerIndex;
1207 if (key == SDL_SCANCODE_ESCAPE)
1208 {
1209 if (changeKeyMenu->u.changeKey.isOptional)
1210 {
1211 // Unset the key
1212 key = SDL_SCANCODE_UNKNOWN;
1213 }
1214 else
1215 {
1216 MenuPlaySound(MENU_SOUND_BACK);
1217 }
1218 }
1219 ChangeKey(key, code, pi);
1220 menu->u.normal.changeKeyMenu = NULL;
1221 }
1222 static void ChangeKey(
1223 const SDL_Scancode key, const key_code_e code, const int playerIndex)
1224 {
1225 if (!KeyAvailable(key, code, playerIndex))
1226 {
1227 MenuPlaySound(MENU_SOUND_ERROR);
1228 return;
1229 }
1230 // Players share map key
1231 const int changePlayerIndex = code == KEY_CODE_MAP ? 0 : playerIndex;
1232 char buf[256];
1233 sprintf(
1234 buf, "Input.PlayerCodes%d.%s", changePlayerIndex, KeycodeStr(code));
1235 ConfigGet(&gConfig, buf)->u.Int.Value = key;
1236 sprintf(buf, "Input.PlayerCodes%d", changePlayerIndex);
1237 gEventHandlers.keyboard.PlayerKeys[changePlayerIndex] =
1238 KeyLoadPlayerKeys(ConfigGet(&gConfig, buf));
1239 MenuPlaySound(MENU_SOUND_ENTER);
1240 }
1241
1242 static void MenuChangeIndexCmd(menu_t *menu, const int cmd)
1243 {
1244 // Ignore if no submenus
1245 if (menu->u.normal.subMenus.size == 0)
1246 {
1247 return;
1248 }
1249
1250 if (Up(cmd))
1251 {
1252 MenuChangeIndex(menu, -1);
1253 }
1254 else if (Down(cmd))
1255 {
1256 MenuChangeIndex(menu, 1);
1257 }
1258 MenuClampScroll(menu);
1259 }
1260
1261 void MenuActivate(MenuSystem *ms, menu_t *menu, int cmd)
1262 {
1263 UNUSED(ms);
1264 MenuPlaySound(MENU_SOUND_SWITCH);
1265 switch (menu->type)
1266 {
1267 case MENU_TYPE_BASIC:
1268 // do nothing
1269 return;
1270 case MENU_TYPE_SET_OPTION_TOGGLE:
1271 *menu->u.option.uHook.optionToggle =
1272 !*menu->u.option.uHook.optionToggle;
1273 break;
1274 case MENU_TYPE_SET_OPTION_RANGE: {
1275 int option = *menu->u.option.uHook.optionRange.option;
1276 int increment = menu->u.option.uHook.optionRange.increment;
1277 int low = menu->u.option.uHook.optionRange.low;
1278 int high = menu->u.option.uHook.optionRange.high;
1279 if (Left(cmd))
1280 {
1281 if (low == option)
1282 {
1283 option = high;
1284 }
1285 else if (low + increment > option)
1286 {
1287 option = low;
1288 }
1289 else
1290 {
1291 option -= increment;
1292 }
1293 }
1294 else if (Right(cmd))
1295 {
1296 if (high == option)
1297 {
1298 option = low;
1299 }
1300 else if (high - increment < option)
1301 {
1302 option = high;
1303 }
1304 else
1305 {
1306 option += increment;
1307 }
1308 }
1309 *menu->u.option.uHook.optionRange.option = option;
1310 }
1311 break;
1312 case MENU_TYPE_SET_OPTION_SEED: {
1313 unsigned int seed = *menu->u.option.uHook.seed;
1314 unsigned int increment = 1;
1315 if (Button1(cmd))
1316 {
1317 increment *= 10;
1318 }
1319 if (Button2(cmd))
1320 {
1321 increment *= 100;
1322 }
1323 if (Left(cmd))
1324 {
1325 if (increment > seed)
1326 {
1327 seed = 0;
1328 }
1329 else
1330 {
1331 seed -= increment;
1332 }
1333 }
1334 else if (Right(cmd))
1335 {
1336 if (UINT_MAX - increment < seed)
1337 {
1338 seed = UINT_MAX;
1339 }
1340 else
1341 {
1342 seed += increment;
1343 }
1344 }
1345 *menu->u.option.uHook.seed = seed;
1346 }
1347 break;
1348 case MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID:
1349 if (Left(cmd))
1350 {
1351 menu->u.option.uHook.upDownFuncs.upFunc();
1352 }
1353 else if (Right(cmd))
1354 {
1355 menu->u.option.uHook.upDownFuncs.downFunc();
1356 }
1357 break;
1358 case MENU_TYPE_SET_OPTION_RANGE_GET_SET: {
1359 int option = menu->u.option.uHook.optionRangeGetSet.getFunc();
1360 int increment = menu->u.option.uHook.optionRangeGetSet.increment;
1361 if (Left(cmd))
1362 {
1363 if (menu->u.option.uHook.optionRangeGetSet.low + increment >
1364 option)
1365 {
1366 option = menu->u.option.uHook.optionRangeGetSet.low;
1367 }
1368 else
1369 {
1370 option -= increment;
1371 }
1372 }
1373 else if (Right(cmd))
1374 {
1375 if (menu->u.option.uHook.optionRangeGetSet.high - increment <
1376 option)
1377 {
1378 option = menu->u.option.uHook.optionRangeGetSet.high;
1379 }
1380 else
1381 {
1382 option += increment;
1383 }
1384 }
1385 menu->u.option.uHook.optionRangeGetSet.setFunc(option);
1386 }
1387 break;
1388 case MENU_TYPE_VOID_FUNC:
1389 if (AnyButton(cmd))
1390 {
1391 menu->u.option.uHook.voidFunc.func(
1392 menu->u.option.uHook.voidFunc.data);
1393 }
1394 break;
1395 case MENU_TYPE_SET_OPTION_CHANGE_KEY:
1396 menu->parentMenu->u.normal.changeKeyMenu = menu;
1397 break;
1398 default:
1399 printf("Error unhandled menu type %d\n", menu->type);
1400 assert(0);
1401 break;
1402 }
1403 }