Debian Codegraph @

src/menu/menu.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file menu.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd of August 2005: Created menu.c
10 * - 3rd of February 2009: Renamed menu.c to menu.cpp
11 * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp
12 * - 18th July 2009: Created menugame.cpp from parts of menu.cpp
13 * - 18th July 2009: Created menuutil.cpp from parts of menu.cpp
14 * - 18th July 2009: Created menusetup.cpp from parts of menu.cpp
15 * - 19th July 2009: Created menumain.cpp from parts of menu.cpp
16 * - 23rd June 2010: Merged menuutil.cpp into menu.cpp
17 *
18 * @par Licence:
19 * Copyright (c) 2005-2017 AJ Thomson
20 *
21 * OpenJazz is distributed under the terms of
22 * the GNU General Public License, version 2.0
23 *
24 * @par Description:
25 * Provides various generic menus.
26 *
27 */
28
29
30 #include "menu.h"
31
32 #include "io/controls.h"
33 #include "io/gfx/font.h"
34 #include "io/gfx/video.h"
35 #include "io/sound.h"
36 #include "loop.h"
37 #include "util.h"
38 #include "platforms/platforms.h"
39
40 #include <string.h>
41
42
43 /**
44 * Show the "(esc) quits" string.
45 */
46 void Menu::showEscString (bool alignLeft) {
47
48 if(alignLeft)
49 fontbig->showString(ESCAPE_STRING, 3, canvasH, alignX::Left, alignY::Bottom);
50 else
51 fontbig->showString(ESCAPE_STRING, canvasW - 3, canvasH, alignX::Right, alignY::Bottom);
52
53 }
54
55
56 /**
57 * Display a message to the user.
58 *
59 * @param text The message to display
60 *
61 * @return Error code
62 */
63 int Menu::message (const char* text) {
64
65 video.setPalette(menuPalette);
66
67 while (true) {
68
69 if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT;
70
71 if (controls.release(C_ENTER) || controls.release(C_ESCAPE) || controls.wasCursorReleased())
72 return E_NONE;
73
74 SDL_Delay(T_MENU_FRAME);
75
76 video.clearScreen(15);
77
78 // Draw the message
79 fontmn2->showStringCentered(text);
80
81 }
82
83 return E_NONE;
84
85 }
86
87
88 /**
89 * Let the user select from a menu of the given options.
90 *
91 * @param title Optional title, can be nullptr
92 * @param optionNames Array of option names
93 * @param options The number of options (and size of the names array)
94 * @param chosen Which option is selected
95 *
96 * @return Error code
97 */
98 int Menu::generic (const char *title, const char** optionNames, int options, int& chosen) {
99
100 int x, y;
101
102 if (chosen >= options) chosen = 0;
103
104 // calculate the longest string length for centering
105 int xOffset = (canvasW >> 1);
106 int maxWidth = 0;
107 for (int i = 0; i < options; i++) {
108 int w = fontmn2->getStringWidth(optionNames[i]);
109 if(w > maxWidth) maxWidth = w;
110 }
111 xOffset -= maxWidth >> 1;
112
113 while (true) {
114
115 if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT;
116
117 if (controls.release(C_ESCAPE)) return E_RETURN;
118
119 if (controls.release(C_UP)) chosen = (chosen + options - 1) % options;
120
121 if (controls.release(C_DOWN)) chosen = (chosen + 1) % options;
122
123 if (controls.release(C_ENTER)) {
124
125 playConfirmSound();
126 return E_NONE;
127
128 }
129
130 if (controls.getCursor(x, y)) {
131
132 if ((x < 100) && (y >= canvasH - 12) && controls.wasCursorReleased()) return E_RETURN;
133
134 x -= canvasW >> 2;
135 y -= (canvasH >> 1) - (options << 3);
136
137 if ((x >= 0) && (x < 256) && (y >= 0) && (y < (options << 4))) {
138
139 chosen = y >> 4;
140
141 if (controls.wasCursorReleased()) {
142
143 playConfirmSound();
144 return E_NONE;
145
146 }
147
148 }
149
150 }
151
152 SDL_Delay(T_MENU_FRAME);
153
154 video.clearScreen(0);
155
156 if(title)
157 fontmn2->showString(title, canvasW >> 1, (canvasH >> 1) - 80, alignX::Center);
158
159 for (int i = 0; i < options; i++) {
160
161 if (i == chosen) fontmn2->mapPalette(240, 8, 114, 16);
162
163 fontmn2->showString(optionNames[i], xOffset,
164 (canvasH >> 1) + (i << 4) - (options << 3));
165
166 if (i == chosen) fontmn2->restorePalette();
167
168 }
169
170 showEscString();
171
172 }
173
174 return E_NONE;
175
176 }
177
178
179 /**
180 * Let the user edit a text string
181 *
182 * @param request Description of the text string
183 * @param text The text string to be edited
184 *
185 * @return Error code
186 */
187 int Menu::textInput (const char* request, char*& text, bool ip) {
188
189 char *input;
190
191 // TODO: simplify
192
193 #if (defined(__3DS__) || defined(__SWITCH__))
194
195 bool res;
196
197 if (ip)
198 res = platform->InputIP(text, input);
199 else
200 res = platform->InputString(request, text, input);
201
202 if (res) {
203
204 playConfirmSound();
205
206 delete[] text;
207 text = input;
208
209 return E_NONE;
210
211 }
212
213 #elif defined(__vita__)
214
215 if (platform->InputString(request, text, input)) {
216
217 playConfirmSound();
218
219 delete[] text;
220 text = input;
221
222 return E_NONE;
223
224 }
225
226 #else
227
228 int count, terminate, added, x, y;
229
230 video.setPalette(menuPalette);
231
232 // Create input string
233 input = createEditableString(text);
234
235 unsigned int cursor = strlen(input);
236
237 while (true) {
238
239 int character = loop(TYPING_LOOP);
240
241 if (character == E_QUIT) {
242
243 delete[] input;
244
245 return E_QUIT;
246
247 }
248
249 // Ensure there is space for another character
250 if (cursor < STRING_LENGTH) {
251
252 terminate = (input[cursor] == 0);
253
254 // If the character is valid, add it to the input string
255
256 added = 0;
257
258 if (!ip) {
259
260 if (((character >= 'a') && (character <= 'z'))
261 || (character == ' ')) {
262
263 input[cursor] = character;
264 added = 1;
265
266 } else if ((character >= 'A') && (character <= 'Z')) {
267
268 input[cursor] = character | 32;
269 added = 1;
270
271 }
272
273 }
274
275 if (((character >= '0') && (character <= '9'))
276 || (character == '.')) {
277
278 input[cursor] = character;
279 added = 1;
280
281 }
282
283 if (added) {
284
285 cursor++;
286 if (terminate) input[cursor] = 0;
287
288 }
289
290 }
291
292 if ((character == SDLK_DELETE) && (cursor < strlen(input))) {
293
294 for (count = cursor; count < STRING_LENGTH; count++)
295 input[count] = input[count + 1];
296
297 }
298
299 if ((character == SDLK_BACKSPACE) && (cursor > 0)) {
300
301 for (count = cursor - 1; count < STRING_LENGTH; count++)
302 input[count] = input[count + 1];
303
304 cursor--;
305
306 }
307
308
309 if (controls.release(C_ESCAPE) ||
310 (controls.getCursor(x, y) && (x < 100) && (y >= canvasH - 12) && controls.wasCursorReleased())) {
311
312 delete[] input;
313
314 return E_RETURN;
315
316 }
317
318
319 SDL_Delay(T_MENU_FRAME);
320
321 video.clearScreen(15);
322
323 // Draw the prompt
324 fontmn2->showStringCentered(request);
325
326 // Draw the section of the text before the cursor
327 fontmn2->mapPalette(240, 8, 114, 16);
328 terminate = input[cursor];
329 input[cursor] = 0;
330 Point pos = fontmn2->showString(input, (canvasW >> 1) - 152, (canvasH >> 1) + 16);
331
332 // Draw the cursor
333 video.drawRect(pos.x, (canvasH >> 1) + 26, 8, 2, 79);
334
335 // Draw the section of text after the cursor
336 input[cursor] = terminate;
337 fontmn2->showString(input + cursor, pos.x, (canvasH >> 1) + 16);
338 fontmn2->restorePalette();
339
340 showEscString();
341
342
343 if (controls.release(C_LEFT) && (cursor > 0)) cursor--;
344
345 if (controls.release(C_RIGHT) && (cursor < strlen(input))) cursor++;
346
347 if (controls.release(C_ENTER)) {
348
349 playConfirmSound();
350
351 // Replace the original string with the input string
352 delete[] text;
353 text = input;
354
355 return E_NONE;
356
357 }
358
359 }
360
361 delete[] input;
362
363 #endif
364
365 return E_RETURN;
366
367 }
368