Debian Codegraph @

src/menu.h

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-2018, 2020-2021 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 #pragma once
50
51 #include <cdogs/c_array.h>
52 #include <cdogs/campaigns.h>
53 #include <cdogs/events.h>
54 #include <cdogs/gamedata.h>
55 #include <cdogs/grafx.h>
56
57 #include "credits.h"
58 #include "game_loop.h"
59
60 typedef enum
61 {
62 MENU_TYPE_NORMAL, // normal menu with items, up/down/left/right moves
63 // cursor
64 MENU_TYPE_OPTIONS, // menu with items, only up/down moves
65 MENU_TYPE_BASIC, // no items, does nothing (use custom callbacks)
66 MENU_TYPE_SET_OPTION_TOGGLE, // no items, sets option on/off
67 MENU_TYPE_SET_OPTION_RANGE, // no items, sets option range low-high
68 MENU_TYPE_SET_OPTION_SEED, // set random seed
69 MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID, // set option using up/down
70 // functions
71 MENU_TYPE_SET_OPTION_RANGE_GET_SET, // set option range low-high using
72 // get/set functions
73 MENU_TYPE_SET_OPTION_CHANGE_KEY, // redefine key
74 MENU_TYPE_VOID_FUNC, // call a void (*f)(void *) function
75 MENU_TYPE_BACK,
76 MENU_TYPE_QUIT,
77 MENU_TYPE_RETURN, // Return with a code
78 MENU_TYPE_CUSTOM // use custom callbacks for input and drawing
79 } menu_type_e;
80
81 typedef enum
82 {
83 MENU_OPTION_DISPLAY_STYLE_NONE,
84 MENU_OPTION_DISPLAY_STYLE_STR_FUNC, // use a function that returns string
85 MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC, // function that converts int to
86 // string
87 } menu_option_display_style_e;
88
89 typedef enum
90 {
91 MENU_DISPLAY_ITEMS_CREDITS = 0x01,
92 MENU_DISPLAY_ITEMS_AUTHORS = 0x02
93 } menu_display_items_e;
94
95 typedef struct menu menu_t;
96
97 // Callback for drawing custom menu
98 // menu, graphics, pos, size, data
99 typedef void (*MenuDisplayFunc)(
100 const menu_t *, GraphicsDevice *, const struct vec2i, const struct vec2i,
101 const void *);
102 // Callback for handling user input
103 // cmd, data
104 // returns: 0 if stay on menu, 1 if exit from menu
105 typedef int (*MenuInputFunc)(int, void *);
106 // Callback for processing custom function on menu
107 typedef void (*MenuFunc)(menu_t *, void *);
108 // Callback for processing custom menu function post input
109 // menu, cmd, data
110 typedef void (*MenuPostInputFunc)(menu_t *, int cmd, void *);
111
112 typedef enum
113 {
114 MENU_ALIGN_CENTER,
115 MENU_ALIGN_LEFT
116 } MenuAlignStyle;
117
118 typedef enum
119 {
120 MENU_SOUND_ENTER,
121 MENU_SOUND_BACK,
122 MENU_SOUND_SWITCH,
123 MENU_SOUND_START,
124 MENU_SOUND_ERROR
125 } MenuSound;
126
127 struct menu
128 {
129 char *name;
130 menu_type_e type;
131 struct menu *parentMenu;
132 bool isDisabled;
133 color_t color;
134 MenuFunc customPostEnterFunc;
135 void *customPostEnterData;
136 bool isCustomPostEnterDataDynamic;
137 MenuFunc customPostUpdateFunc;
138 void *customPostUpdateData;
139 bool isCustomPostUpdateDataDynamic;
140 MenuPostInputFunc customPostInputFunc;
141 void *customPostInputData;
142 MenuDisplayFunc customDisplayFunc;
143 const void *customDisplayData;
144 MenuSound enterSound;
145 bool mouseHover; // whether mouse is hovered over a submenu
146 union {
147 // normal menu, with sub menus
148 struct
149 {
150 char title[64];
151 CArray subMenus; // of menu_t
152 // whether to use alternate control to enter submenus
153 bool isSubmenusAlt;
154 int index;
155 int scroll;
156 int maxItems; // 0 means unlimited
157 MenuAlignStyle align;
158 int quitMenuIndex;
159 int displayItems;
160 int setOptions;
161 struct menu
162 *changeKeyMenu; // if in change key mode, and which item
163 } normal;
164 // menu item only
165 struct
166 {
167 union {
168 bool *optionToggle;
169 struct
170 {
171 int *option;
172 int low;
173 int high;
174 int increment;
175 } optionRange;
176 unsigned int *seed;
177 // function to call
178 struct
179 {
180 void (*upFunc)(void);
181 void (*downFunc)(void);
182 } upDownFuncs;
183 struct
184 {
185 int (*getFunc)(void);
186 void (*setFunc)(int);
187 int low;
188 int high;
189 int increment;
190 } optionRangeGetSet;
191 struct
192 {
193 void (*func)(void *);
194 void *data;
195 } voidFunc;
196 struct
197 {
198 input_device_e *device0;
199 input_device_e *device1;
200 } changeControl;
201 } uHook;
202 menu_option_display_style_e displayStyle;
203 union {
204 char *(*str)(void);
205 const char *(*intToStr)(int);
206 } uFunc;
207 } option;
208 // change key
209 struct
210 {
211 key_code_e code;
212 int playerIndex;
213 // If key is optional, allow it to be unset
214 bool isOptional;
215 } changeKey;
216 int returnCode;
217 struct
218 {
219 MenuDisplayFunc displayFunc;
220 MenuInputFunc inputFunc;
221 void *data;
222 } customData;
223 } u;
224 };
225
226 typedef struct
227 {
228 MenuDisplayFunc Func;
229 void *Data;
230 } MenuCustomDisplayFunc;
231 typedef struct
232 {
233 menu_t *root;
234 menu_t *current;
235 CArray exitTypes; // of menu_type_e
236 credits_displayer_t *creditsDisplayer;
237 EventHandlers *handlers;
238 GraphicsDevice *graphics;
239 struct vec2i pos;
240 struct vec2i size;
241 MenuAlignStyle align;
242 // Allow exiting menu via escape key
243 bool allowAborts;
244 bool hasAbort;
245 CArray customDisplayFuncs; // of MenuCustomDisplayFunc
246 } MenuSystem;
247
248 void MenuSystemInit(
249 MenuSystem *ms, EventHandlers *handlers, GraphicsDevice *graphics,
250 struct vec2i pos, struct vec2i size);
251 void MenuSystemTerminate(MenuSystem *ms);
252 void MenuSetCreditsDisplayer(
253 MenuSystem *menu, credits_displayer_t *creditsDisplayer);
254 void MenuAddExitType(MenuSystem *menu, menu_type_e exitType);
255 void MenuSystemAddCustomDisplay(
256 MenuSystem *ms, MenuDisplayFunc func, void *data);
257 int MenuIsExit(MenuSystem *ms);
258 bool MenuTypeHasSubMenus(const menu_type_e type);
259 GameLoopData *MenuLoop(MenuSystem *menu);
260 void MenuUpdateMouse(MenuSystem *ms);
261 GameLoopResult MenuUpdate(MenuSystem *ms);
262 void MenuDraw(const MenuSystem *ms);
263 void MenuDisplay(const MenuSystem *ms);
264 void MenuProcessCmd(MenuSystem *ms, int cmd);
265 void MenuReset(MenuSystem *menu);
266 void MenuDisableSubmenu(menu_t *menu, int index);
267 void MenuEnableSubmenu(menu_t *menu, int index);
268 void MenuSetDisabled(menu_t *menu, const bool isDisabled);
269 menu_t *MenuGetSubmenuByName(menu_t *menu, const char *name);
270 int MenuGetNumMenuItemsShown(const menu_t *menu);
271
272 void ShowControls(void);
273 struct vec2i DisplayMenuItem(
274 GraphicsDevice *g, const Rect2i bounds, const char *s, const bool selected,
275 const bool isDisabled, const color_t color);
276
277 menu_t *MenuCreate(const char *name, menu_type_e type);
278 menu_t *MenuCreateNormal(
279 const char *name, const char *title, menu_type_e type, int displayItems);
280 menu_t *MenuAddSubmenu(menu_t *menu, menu_t *subMenu);
281 void MenuSetPostEnterFunc(
282 menu_t *menu, MenuFunc func, void *data, const bool isDynamicData);
283 void MenuSetPostUpdateFunc(
284 menu_t *menu, MenuFunc func, void *data, const bool isDynamicData);
285 void MenuSetPostInputFunc(menu_t *menu, MenuPostInputFunc func, void *data);
286 void MenuSetCustomDisplay(
287 menu_t *menu, MenuDisplayFunc func, const void *data);
288
289 void MenuAddConfigOptionsItem(menu_t *menu, Config *c);
290 menu_t *MenuCreateOptionToggle(const char *name, bool *config);
291 menu_t *MenuCreateOptionRange(
292 const char *name, int *config, int low, int high, int increment,
293 menu_option_display_style_e style, void (*func)(void));
294 menu_t *MenuCreateOptionSeed(const char *name, unsigned int *seed);
295 menu_t *MenuCreateOptionUpDownFunc(
296 const char *name, void (*upFunc)(void), void (*downFunc)(void),
297 menu_option_display_style_e style, char *(*strFunc)(void));
298 menu_t *MenuCreateVoidFunc(const char *name, void (*func)(void *), void *data);
299 menu_t *MenuCreateOptionRangeGetSet(
300 const char *name, int (*getFunc)(void), void (*setFunc)(int), int low,
301 int high, int increment, menu_option_display_style_e style,
302 void (*func)(void));
303 menu_t *MenuCreateSeparator(const char *name);
304 menu_t *MenuCreateBack(const char *name);
305 // Create a menu entry that exits the menu while setting a return code
306 // The return code can be retrieved from the menu system via
307 // ms.current->u.returnCode
308 menu_t *MenuCreateReturn(const char *name, int returnCode);
309 menu_t *MenuCreateCustom(
310 const char *name, MenuDisplayFunc displayFunc, MenuInputFunc inputFunc,
311 void *data);
312
313 void MenuPlaySound(MenuSound s);
314
315 void MenuClearSubmenus(menu_t *menu);