| 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"cxx . . $ `<file>/src/menu/menu.h`/ |
| 31 | |
| 32 | #include "io/controls.h"cxx . . $ `<file>/src/io/controls.h`/ |
| 33 | #include "io/gfx/font.h"cxx . . $ `<file>/src/io/gfx/font.h`/ |
| 34 | #include "io/gfx/video.h"cxx . . $ `<file>/src/io/gfx/video.h`/ |
| 35 | #include "io/sound.h"cxx . . $ `<file>/src/io/sound.h`/ |
| 36 | #include "loop.h"cxx . . $ `<file>/src/loop.h`/ |
| 37 | #include "util.h"cxx . . $ `<file>/src/util.h`/ |
| 38 | #include "platforms/platforms.h"cxx . . $ `<file>/src/platforms/platforms.h`/ |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Show the "(esc) quits" string. |
| 45 | */ |
| 46 | void Menucxx . . $ Menu#::showEscStringcxx . . $ Menu#showEscString(b45ddcbc8423892c). (bool alignLeft) { |
| 47 | |
| 48 | if(alignLeft) |
| 49 | fontbigcxx . . $ fontbig.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(ESCAPE_STRINGcxx . . $ `src/menu/menu.h:39:9`!, 3, canvasHcxx . . $ canvasH., alignXcxx . . $ alignX#::Leftcxx . . $ alignX#Left., alignYcxx . . $ alignY#::Bottomcxx . . $ alignY#Bottom.); |
| 50 | else |
| 51 | fontbigcxx . . $ fontbig.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(ESCAPE_STRINGcxx . . $ `src/menu/menu.h:39:9`!, canvasWcxx . . $ canvasW. - 3, canvasHcxx . . $ canvasH., alignXcxx . . $ alignX#::Rightcxx . . $ alignX#Right., alignYcxx . . $ alignY#::Bottomcxx . . $ 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 Menucxx . . $ Menu#::messagecxx . . $ Menu#message(5c4306d8553b3696). (const char* text) { |
| 64 | |
| 65 | videocxx . . $ video..setPalettecxx . . $ Video#setPalette(d5421d17e1a4b2b2).(menuPalettecxx . . $ menuPalette.); |
| 66 | |
| 67 | while (true) { |
| 68 | |
| 69 | if (loopcxx . . $ loop(fd873fd06cfe1135).(NORMAL_LOOPcxx . . $ NORMAL_LOOP.) == E_QUITcxx . . $ `src/OpenJazz.h:67:9`!) return E_QUITcxx . . $ `src/OpenJazz.h:67:9`!; |
| 70 | |
| 71 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ENTERcxx . . $ `src/io/controls.h:38:9`!) || controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ESCAPEcxx . . $ `src/io/controls.h:39:9`!) || controlscxx . . $ controls..wasCursorReleasedcxx . . $ Controls#wasCursorReleased(c9857a73d68e59ba).()) |
| 72 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 73 | |
| 74 | SDL_Delay(T_MENU_FRAMEcxx . . $ `src/io/gfx/video.h:78:9`!); |
| 75 | |
| 76 | videocxx . . $ video..clearScreencxx . . $ Video#clearScreen(d4f767463ce0a6b3).(15); |
| 77 | |
| 78 | // Draw the message |
| 79 | fontmn2cxx . . $ fontmn2.->showStringCenteredcxx . . $ Font#showStringCentered(dffc78c69bf84bb8).(text); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 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 Menucxx . . $ Menu#::genericcxx . . $ Menu#generic(9f6ec59569c98226). (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 = (canvasWcxx . . $ canvasW. >> 1); |
| 106 | int maxWidth = 0; |
| 107 | for (int i = 0; i < options; i++) { |
| 108 | int w = fontmn2cxx . . $ fontmn2.->getStringWidthcxx . . $ Font#getStringWidth(5c4306d8553b3696).(optionNames[i]); |
| 109 | if(w > maxWidth) maxWidth = w; |
| 110 | } |
| 111 | xOffset -= maxWidth >> 1; |
| 112 | |
| 113 | while (true) { |
| 114 | |
| 115 | if (loopcxx . . $ loop(fd873fd06cfe1135).(NORMAL_LOOPcxx . . $ NORMAL_LOOP.) == E_QUITcxx . . $ `src/OpenJazz.h:67:9`!) return E_QUITcxx . . $ `src/OpenJazz.h:67:9`!; |
| 116 | |
| 117 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ESCAPEcxx . . $ `src/io/controls.h:39:9`!)) return E_RETURNcxx . . $ `src/OpenJazz.h:66:9`!; |
| 118 | |
| 119 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_UPcxx . . $ `src/io/controls.h:30:9`!)) chosen = (chosen + options - 1) % options; |
| 120 | |
| 121 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_DOWNcxx . . $ `src/io/controls.h:31:9`!)) chosen = (chosen + 1) % options; |
| 122 | |
| 123 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ENTERcxx . . $ `src/io/controls.h:38:9`!)) { |
| 124 | |
| 125 | playConfirmSoundcxx . . $ playConfirmSound(49f6e7a06ebc5aa8).(); |
| 126 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | if (controlscxx . . $ controls..getCursorcxx . . $ Controls#getCursor(8a990ad1ed500b42).(x, y)) { |
| 131 | |
| 132 | if ((x < 100) && (y >= canvasHcxx . . $ canvasH. - 12) && controlscxx . . $ controls..wasCursorReleasedcxx . . $ Controls#wasCursorReleased(c9857a73d68e59ba).()) return E_RETURNcxx . . $ `src/OpenJazz.h:66:9`!; |
| 133 | |
| 134 | x -= canvasWcxx . . $ canvasW. >> 2; |
| 135 | y -= (canvasHcxx . . $ canvasH. >> 1) - (options << 3); |
| 136 | |
| 137 | if ((x >= 0) && (x < 256) && (y >= 0) && (y < (options << 4))) { |
| 138 | |
| 139 | chosen = y >> 4; |
| 140 | |
| 141 | if (controlscxx . . $ controls..wasCursorReleasedcxx . . $ Controls#wasCursorReleased(c9857a73d68e59ba).()) { |
| 142 | |
| 143 | playConfirmSoundcxx . . $ playConfirmSound(49f6e7a06ebc5aa8).(); |
| 144 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | SDL_Delay(T_MENU_FRAMEcxx . . $ `src/io/gfx/video.h:78:9`!); |
| 153 | |
| 154 | videocxx . . $ video..clearScreencxx . . $ Video#clearScreen(d4f767463ce0a6b3).(0); |
| 155 | |
| 156 | if(title) |
| 157 | fontmn2cxx . . $ fontmn2.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(title, canvasWcxx . . $ canvasW. >> 1, (canvasHcxx . . $ canvasH. >> 1) - 80, alignXcxx . . $ alignX#::Centercxx . . $ alignX#Center.); |
| 158 | |
| 159 | for (int i = 0; i < options; i++) { |
| 160 | |
| 161 | if (i == chosen) fontmn2cxx . . $ fontmn2.->mapPalettecxx . . $ Font#mapPalette(748afa4755bc9595).(240, 8, 114, 16); |
| 162 | |
| 163 | fontmn2cxx . . $ fontmn2.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(optionNames[i], xOffset, |
| 164 | (canvasHcxx . . $ canvasH. >> 1) + (i << 4) - (options << 3)); |
| 165 | |
| 166 | if (i == chosen) fontmn2cxx . . $ fontmn2.->restorePalettecxx . . $ Font#restorePalette(49f6e7a06ebc5aa8).(); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | showEscStringcxx . . $ Menu#showEscString(b45ddcbc8423892c).(); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 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 Menucxx . . $ Menu#::textInputcxx . . $ Menu#textInput(bc30cf002e76fcd0). (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 | videocxx . . $ video..setPalettecxx . . $ Video#setPalette(d5421d17e1a4b2b2).(menuPalettecxx . . $ menuPalette.); |
| 231 | |
| 232 | // Create input string |
| 233 | input = createEditableStringcxx . . $ createEditableString(a8b8e32347182212).(text); |
| 234 | |
| 235 | unsigned int cursor = strlen(input); |
| 236 | |
| 237 | while (true) { |
| 238 | |
| 239 | int character = loopcxx . . $ loop(fd873fd06cfe1135).(TYPING_LOOPcxx . . $ TYPING_LOOP.); |
| 240 | |
| 241 | if (character == E_QUITcxx . . $ `src/OpenJazz.h:67:9`!) { |
| 242 | |
| 243 | delete[] input; |
| 244 | |
| 245 | return E_QUITcxx . . $ `src/OpenJazz.h:67:9`!; |
| 246 | |
| 247 | } |
| 248 | |
| 249 | // Ensure there is space for another character |
| 250 | if (cursor < STRING_LENGTHcxx . . $ `src/OpenJazz.h:46:9`!) { |
| 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_DELETEcxx . . $ SDLK_DELETE.) && (cursor < strlen(input))) { |
| 293 | |
| 294 | for (count = cursor; count < STRING_LENGTHcxx . . $ `src/OpenJazz.h:46:9`!; count++) |
| 295 | input[count] = input[count + 1]; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | if ((character == SDLK_BACKSPACEcxx . . $ SDLK_BACKSPACE.) && (cursor > 0)) { |
| 300 | |
| 301 | for (count = cursor - 1; count < STRING_LENGTHcxx . . $ `src/OpenJazz.h:46:9`!; count++) |
| 302 | input[count] = input[count + 1]; |
| 303 | |
| 304 | cursor--; |
| 305 | |
| 306 | } |
| 307 | |
| 308 | |
| 309 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ESCAPEcxx . . $ `src/io/controls.h:39:9`!) || |
| 310 | (controlscxx . . $ controls..getCursorcxx . . $ Controls#getCursor(8a990ad1ed500b42).(x, y) && (x < 100) && (y >= canvasHcxx . . $ canvasH. - 12) && controlscxx . . $ controls..wasCursorReleasedcxx . . $ Controls#wasCursorReleased(c9857a73d68e59ba).())) { |
| 311 | |
| 312 | delete[] input; |
| 313 | |
| 314 | return E_RETURNcxx . . $ `src/OpenJazz.h:66:9`!; |
| 315 | |
| 316 | } |
| 317 | |
| 318 | |
| 319 | SDL_Delay(T_MENU_FRAMEcxx . . $ `src/io/gfx/video.h:78:9`!); |
| 320 | |
| 321 | videocxx . . $ video..clearScreencxx . . $ Video#clearScreen(d4f767463ce0a6b3).(15); |
| 322 | |
| 323 | // Draw the prompt |
| 324 | fontmn2cxx . . $ fontmn2.->showStringCenteredcxx . . $ Font#showStringCentered(dffc78c69bf84bb8).(request); |
| 325 | |
| 326 | // Draw the section of the text before the cursor |
| 327 | fontmn2cxx . . $ fontmn2.->mapPalettecxx . . $ Font#mapPalette(748afa4755bc9595).(240, 8, 114, 16); |
| 328 | terminate = input[cursor]; |
| 329 | input[cursor] = 0; |
| 330 | Pointcxx . . $ Point# pos = fontmn2cxx . . $ fontmn2.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(input, (canvasWcxx . . $ canvasW. >> 1) - 152, (canvasHcxx . . $ canvasH. >> 1) + 16); |
| 331 | |
| 332 | // Draw the cursor |
| 333 | videocxx . . $ video..drawRectcxx . . $ Video#drawRect(a4376a5627a47301).(pos.xcxx . . $ Point#x., (canvasHcxx . . $ canvasH. >> 1) + 26, 8, 2, 79); |
| 334 | |
| 335 | // Draw the section of text after the cursor |
| 336 | input[cursor] = terminate; |
| 337 | fontmn2cxx . . $ fontmn2.->showStringcxx . . $ Font#showString(7e50a619d5a117f1).(input + cursor, pos.xcxx . . $ Point#x., (canvasHcxx . . $ canvasH. >> 1) + 16); |
| 338 | fontmn2cxx . . $ fontmn2.->restorePalettecxx . . $ Font#restorePalette(49f6e7a06ebc5aa8).(); |
| 339 | |
| 340 | showEscStringcxx . . $ Menu#showEscString(b45ddcbc8423892c).(); |
| 341 | |
| 342 | |
| 343 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_LEFTcxx . . $ `src/io/controls.h:32:9`!) && (cursor > 0)) cursor--; |
| 344 | |
| 345 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_RIGHTcxx . . $ `src/io/controls.h:33:9`!) && (cursor < strlen(input))) cursor++; |
| 346 | |
| 347 | if (controlscxx . . $ controls..releasecxx . . $ Controls#release(83c267b20bac841a).(C_ENTERcxx . . $ `src/io/controls.h:38:9`!)) { |
| 348 | |
| 349 | playConfirmSoundcxx . . $ playConfirmSound(49f6e7a06ebc5aa8).(); |
| 350 | |
| 351 | // Replace the original string with the input string |
| 352 | delete[] text; |
| 353 | text = input; |
| 354 | |
| 355 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 356 | |
| 357 | } |
| 358 | |
| 359 | } |
| 360 | |
| 361 | delete[] input; |
| 362 | |
| 363 | #endif |
| 364 | |
| 365 | return E_RETURNcxx . . $ `src/OpenJazz.h:66:9`!; |
| 366 | |
| 367 | } |
| 368 |