| 1 | #include <stdint.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <string.h> |
| 4 | #include "pt2_textout.h" |
| 5 | #include "pt2_visuals.h" |
| 6 | #include "pt2_mouse.h" |
| 7 | #include "pt2_structs.h" |
| 8 | #include "pt2_visuals_sync.h" |
| 9 | #include "pt2_keyboard.h" |
| 10 | #include "pt2_diskop.h" |
| 11 | #include "pt2_mod2wav.h" |
| 12 | #include "pt2_pat2smp.h" |
| 13 | #include "pt2_askbox.h" |
| 14 | #include "pt2_posed.h" |
| 15 | |
| 16 | #define INIT_BUTTONS(num, abortRetVal, defRetVal) \ |
| 17 | d.numButtons = num; \ |
| 18 | d.abortReturnValue = abortRetVal; \ |
| 19 | d.defaultReturnValue = defRetVal; |
| 20 | |
| 21 | #define SET_DIALOG(xx, yy, ww, hh) \ |
| 22 | d.x = xx; \ |
| 23 | d.y = yy; \ |
| 24 | d.w = ww; \ |
| 25 | d.h = hh; |
| 26 | |
| 27 | #define SET_BUTTON(i, invert, label, key, retVal, xx1, xx2, yy1, yy2) \ |
| 28 | strcpy(d.buttons[i].text, label); \ |
| 29 | d.buttons[i].repeat = false; \ |
| 30 | d.buttons[i].inverted = invert; \ |
| 31 | d.buttons[i].callback = NULL; \ |
| 32 | d.buttons[i].keyBinding = key; \ |
| 33 | d.buttons[i].returnValue = retVal; \ |
| 34 | d.buttons[i].clickableWidth = 0; \ |
| 35 | d.buttons[i].x1 = xx1; \ |
| 36 | d.buttons[i].x2 = xx2; \ |
| 37 | d.buttons[i].y1 = yy1; \ |
| 38 | d.buttons[i].y2 = yy2; |
| 39 | |
| 40 | #define SET_CALLBACK_BUTTON(i, label, func, clickWidth, xx1, xx2, yy1, yy2, doRepeat) \ |
| 41 | strcpy(d.buttons[i].text, label); \ |
| 42 | d.buttons[i].repeat = true; \ |
| 43 | d.buttons[i].inverted = false; \ |
| 44 | d.buttons[i].callback = func; \ |
| 45 | d.buttons[i].clickableWidth = clickWidth; \ |
| 46 | d.buttons[i].x1 = xx1; \ |
| 47 | d.buttons[i].x2 = xx2; \ |
| 48 | d.buttons[i].y1 = yy1; \ |
| 49 | d.buttons[i].y2 = yy2; |
| 50 | |
| 51 | typedef struct choice_t |
| 52 | { |
| 53 | void (*callback)(void); |
| 54 | bool inverted, repeat; |
| 55 | int16_t x1, y1, x2, y2, clickableWidth; |
| 56 | SDL_Keycode keyBinding; |
| 57 | int16_t returnValue; |
| 58 | char text[32]; |
| 59 | } button_t; |
| 60 | |
| 61 | typedef struct dialog_t |
| 62 | { |
| 63 | bool bigDialog; |
| 64 | int16_t x, y, w, h, numButtons, defaultReturnValue, abortReturnValue; |
| 65 | button_t buttons[16]; |
| 66 | } dialog_t; |
| 67 | |
| 68 | typedef struct askBoxData_t // for thread-safe version of askBox() |
| 69 | { |
| 70 | volatile bool active; |
| 71 | uint32_t dialogType, returnValue; |
| 72 | const char *statusText; |
| 73 | } askBoxData_t; |
| 74 | |
| 75 | static int32_t buttonToRepeat = -1; |
| 76 | static askBoxData_t askBoxData; |
| 77 | |
| 78 | static void drawDialog(dialog_t *d) |
| 79 | { |
| 80 | // render dialog |
| 81 | drawFramework3(d->x, d->y, d->w, d->h); |
| 82 | |
| 83 | // render buttons |
| 84 | for (int32_t i = 0; i < d->numButtons; i++) |
| 85 | { |
| 86 | button_t *b = &d->buttons[i]; |
| 87 | if (b->inverted) |
| 88 | drawButton2(b->x1, b->y1, (b->x2 - b->x1) + 1, (b->y2 - b->y1) + 1, b->text); |
| 89 | else |
| 90 | drawButton1(b->x1, b->y1, (b->x2 - b->x1) + 1, (b->y2 - b->y1) + 1, b->text); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void removeAskBox(void) |
| 95 | { |
| 96 | ui.disableVisualizer = false; |
| 97 | ui.askBoxShown = false; |
| 98 | |
| 99 | if (ui.diskOpScreenShown) |
| 100 | { |
| 101 | renderDiskOpScreen(); // also sets update flags |
| 102 | updateDiskOp(); // redraw requested updates |
| 103 | } |
| 104 | else if (ui.posEdScreenShown) |
| 105 | { |
| 106 | renderPosEdScreen(); // also sets update flags |
| 107 | updatePosEd(); // redraw requested updates |
| 108 | } |
| 109 | else if (ui.editOpScreenShown) |
| 110 | { |
| 111 | renderEditOpScreen(); // also sets update flags |
| 112 | updateEditOp(); // redraw requested updates |
| 113 | } |
| 114 | else if (ui.aboutScreenShown) |
| 115 | { |
| 116 | renderAboutScreen(); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | if (ui.visualizerMode == VISUAL_QUADRASCOPE) |
| 121 | renderQuadrascopeBg(); |
| 122 | else if (ui.visualizerMode == VISUAL_SPECTRUM) |
| 123 | renderSpectrumAnalyzerBg(); |
| 124 | |
| 125 | updateVisualizer(); // will draw one frame of the visualizer in use |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void handleThreadedAskBox(void) // rain from main input/video loop |
| 130 | { |
| 131 | if (askBoxData.active) |
| 132 | { |
| 133 | askBoxData.returnValue = askBox(askBoxData.dialogType, askBoxData.statusText); |
| 134 | askBoxData.active = false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | uint32_t askBoxThreadSafe(uint32_t dialogType, const char *statusText) |
| 139 | { |
| 140 | if (!editor.mainLoopOngoing) |
| 141 | return 0; // main loop was not even started yet, bail out. |
| 142 | |
| 143 | // block multiple calls before they are completed (for safety) |
| 144 | while (askBoxData.active) |
| 145 | SDL_Delay(1000 / VBLANK_HZ); // accuracy is not important here |
| 146 | |
| 147 | askBoxData.dialogType = dialogType; |
| 148 | askBoxData.statusText = statusText; |
| 149 | askBoxData.active = true; |
| 150 | |
| 151 | while (askBoxData.active) |
| 152 | SDL_Delay(1000 / VBLANK_HZ); // accuracy is not important here |
| 153 | |
| 154 | return askBoxData.returnValue; |
| 155 | } |
| 156 | |
| 157 | uint32_t askBox(uint32_t dialogType, const char *statusText) |
| 158 | { |
| 159 | uint32_t returnValue = 0; |
| 160 | dialog_t d; |
| 161 | |
| 162 | ASSERT(dialogType < ASKBOX_NUM); |
| 163 | if (dialogType >= ASKBOX_NUM) |
| 164 | return ASKBOX_NO; |
| 165 | |
| 166 | editor.errorMsgActive = false; |
| 167 | editor.errorMsgBlock = false; |
| 168 | editor.errorMsgCounter = 0; |
| 169 | |
| 170 | editor.blockMarkFlag = false; |
| 171 | ui.disableVisualizer = true; |
| 172 | ui.askBoxShown = true; |
| 173 | |
| 174 | SDL_EventState(SDL_DROPFILE, SDL_DISABLE); |
| 175 | |
| 176 | // setup dialog based on type |
| 177 | switch (dialogType) |
| 178 | { |
| 179 | default: |
| 180 | case ASKBOX_YES_NO: |
| 181 | { |
| 182 | // input: x, y, width, height |
| 183 | SET_DIALOG(160, 51, 104, 39); |
| 184 | |
| 185 | // input: number of buttons, abort return-value (esc / right mouse button), default return-value (enter/return) |
| 186 | INIT_BUTTONS(2, ASKBOX_NO, ASKBOX_YES); |
| 187 | |
| 188 | // input: button number, inverted type, keybinding, return-value, x1, x2, y1, y2 |
| 189 | SET_BUTTON(0, true, "YES", SDLK_y, ASKBOX_YES, 171, 196, 71, 81); |
| 190 | SET_BUTTON(1, true, "NO", SDLK_n, ASKBOX_NO, 234, 252, 71, 81); |
| 191 | |
| 192 | drawDialog(&d); |
| 193 | |
| 194 | textOut2(166, 59, "ARE YOU SURE?"); |
| 195 | } |
| 196 | break; |
| 197 | |
| 198 | case ASKBOX_CLEAR: |
| 199 | { |
| 200 | SET_DIALOG(160, 51, 104, 39); |
| 201 | INIT_BUTTONS(4, ASKBOX_CLEAR_CANCEL, ASKBOX_CLEAR_CANCEL); |
| 202 | SET_BUTTON(0, true, "SONG", SDLK_o, ASKBOX_CLEAR_SONG, 166, 198, 57, 67); |
| 203 | SET_BUTTON(1, true, "SAMPLES", SDLK_s, ASKBOX_CLEAR_SAMPLES, 204, 257, 57, 67); |
| 204 | SET_BUTTON(2, true, "ALL", SDLK_a, ASKBOX_CLEAR_ALL, 166, 198, 73, 83); |
| 205 | SET_BUTTON(3, true, "CANCEL", SDLK_c, ASKBOX_CLEAR_CANCEL, 204, 257, 73, 83); |
| 206 | drawDialog(&d); |
| 207 | } |
| 208 | break; |
| 209 | |
| 210 | case ASKBOX_PAT2SMP: |
| 211 | { |
| 212 | SET_DIALOG(120, 44, 200, 55); |
| 213 | INIT_BUTTONS(10, 0, 1); |
| 214 | |
| 215 | SET_BUTTON(0, false, "RENDER TO SAMPLE", SDLK_r, 1, 124, 246, 84, 94); |
| 216 | SET_BUTTON(1, false, "EXIT", SDLK_e, 0, 258, 315, 84, 94); |
| 217 | |
| 218 | // input: button number, text, keybinding, return-value, x1, x2, y1, y2, repeat button |
| 219 | SET_CALLBACK_BUTTON(2, ARROW_UP_STR, pat2SmpNoteUp, 0, 191, 201, 48, 58, true); |
| 220 | SET_CALLBACK_BUTTON(3, ARROW_DOWN_STR, pat2SmpNoteDown, 0, 202, 212, 48, 58, true); |
| 221 | SET_CALLBACK_BUTTON(4, ARROW_UP_STR, pat2SmpStartRowUp, 0, 294, 304, 48, 58, true); |
| 222 | SET_CALLBACK_BUTTON(5, ARROW_DOWN_STR, pat2SmpStartRowDown, 0, 305, 315, 48, 58, true); |
| 223 | SET_CALLBACK_BUTTON(6, ARROW_UP_STR, pat2SmpFinetuneUp, 0, 191, 201, 59, 69, true); |
| 224 | SET_CALLBACK_BUTTON(7, ARROW_DOWN_STR, pat2SmpFinetuneDown, 0, 202, 212, 59, 69, true); |
| 225 | SET_CALLBACK_BUTTON(8, ARROW_UP_STR, pat2SmpRowsUp, 0, 294, 304, 59, 69, true); |
| 226 | SET_CALLBACK_BUTTON(9, ARROW_DOWN_STR, pat2SmpRowsDown, 0, 305, 315, 59, 69, true); |
| 227 | |
| 228 | drawDialog(&d); |
| 229 | |
| 230 | drawFramework1(163, 48, 28, 11); |
| 231 | drawFramework1(274, 48, 20, 11); |
| 232 | drawFramework1(163, 59, 28, 11); |
| 233 | drawFramework1(274, 59, 20, 11); |
| 234 | |
| 235 | pat2SmpDrawNote(); |
| 236 | pat2SmpDrawStartRow(); |
| 237 | pat2SmpDrawRows(); |
| 238 | pat2SmpDrawFinetune(); |
| 239 | pat2SmpCalculateFreq(); |
| 240 | |
| 241 | textOut2(127, 50, "NOTE"); |
| 242 | textOut2(217, 50, "ROW BEG."); |
| 243 | textOut2(124, 61, "FTUNE"); |
| 244 | textOut2(223, 61, "# ROWS"); |
| 245 | textOut2(124, 74, "FREQ."); |
| 246 | } |
| 247 | break; |
| 248 | |
| 249 | case ASKBOX_DOWNSAMPLE: // sample loader (AIFF/WAV w/ rate >22kHz) |
| 250 | { |
| 251 | SET_DIALOG(120, 44, 200, 55); |
| 252 | INIT_BUTTONS(2, ASKBOX_NO, ASKBOX_YES); |
| 253 | SET_BUTTON(0, true, "YES", SDLK_y, ASKBOX_YES, 179, 204, 83, 93); |
| 254 | SET_BUTTON(1, true, "NO", SDLK_n, ASKBOX_NO, 242, 260, 83, 93); |
| 255 | drawDialog(&d); |
| 256 | |
| 257 | textOut2(133, 49, "THE SAMPLE'S FREQUENCY IS"); |
| 258 | textOut2(154, 57, "HIGH (ABOVE 22KHZ)."); |
| 259 | textOut2(133, 65, "DO YOU WANT TO DOWNSAMPLE"); |
| 260 | textOut2(156, 73, "BEFORE LOADING IT?"); |
| 261 | } |
| 262 | break; |
| 263 | |
| 264 | case ASKBOX_MOD2WAV: |
| 265 | { |
| 266 | SET_DIALOG(138, 44, 165, 55); |
| 267 | INIT_BUTTONS(7, ASKBOX_NO, ASKBOX_YES); |
| 268 | |
| 269 | SET_BUTTON(0, false, "RENDER TO WAV", SDLK_r, ASKBOX_YES, 142, 246, 84, 94); |
| 270 | SET_BUTTON(1, false, "EXIT", SDLK_e, ASKBOX_NO, 257, 298, 84, 94); |
| 271 | |
| 272 | SET_CALLBACK_BUTTON(2, " ", toggleMod2WavFadeout, 152, 142, 151, 48, 56, false); |
| 273 | SET_CALLBACK_BUTTON(3, ARROW_UP_STR, mod2WavFadeoutUp, 0, 277, 287, 59, 69, true); |
| 274 | SET_CALLBACK_BUTTON(4, ARROW_DOWN_STR, mod2WavFadeoutDown, 0, 288, 298, 59, 69, true); |
| 275 | SET_CALLBACK_BUTTON(5, ARROW_UP_STR, mod2WavLoopCountUp, 0, 277, 287, 70, 80, true); |
| 276 | SET_CALLBACK_BUTTON(6, ARROW_DOWN_STR, mod2WavLoopCountDown, 0, 288, 298, 70, 80, true); |
| 277 | |
| 278 | drawDialog(&d); |
| 279 | |
| 280 | drawFramework1(257, 59, 20, 11); |
| 281 | drawFramework1(257, 70, 20, 11); |
| 282 | |
| 283 | textOut2(154, 50, "FADE OUT END OF SONG"); |
| 284 | textOut2(142, 62, "FADEOUT SECONDS"); |
| 285 | textOut2(142, 73, "SONG LOOP COUNT"); |
| 286 | |
| 287 | mod2WavDrawFadeoutToggle(); |
| 288 | mod2WavDrawFadeoutSeconds(); |
| 289 | mod2WavDrawLoopCount(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | pointerSetMode(POINTER_MODE_MSG1, NO_CARRY); |
| 294 | if (statusText != NULL) |
| 295 | setStatusMessage(statusText, NO_CARRY); |
| 296 | |
| 297 | mouse.leftButtonPressed = false; |
| 298 | mouse.buttonWaiting = false; |
| 299 | mouse.buttonWaitCounter = 0; |
| 300 | mouse.repeatCounter = 0; |
| 301 | |
| 302 | // main loop here |
| 303 | // XXX: if you change anything in the main rendering loop, make sure it goes in here too (if needed) |
| 304 | |
| 305 | bool loopRunning = true; |
| 306 | while (loopRunning) |
| 307 | { |
| 308 | SDL_Event event; |
| 309 | |
| 310 | beginFPSCounter(); |
| 311 | sinkVisualizerBars(); |
| 312 | updateChannelSyncBuffer(); |
| 313 | readMouseXY(); |
| 314 | |
| 315 | while (SDL_PollEvent(&event)) |
| 316 | { |
| 317 | if (event.type == SDL_WINDOWEVENT) |
| 318 | { |
| 319 | if (event.window.event == SDL_WINDOWEVENT_HIDDEN) |
| 320 | video.windowHidden = true; |
| 321 | else if (event.window.event == SDL_WINDOWEVENT_SHOWN) |
| 322 | video.windowHidden = false; |
| 323 | |
| 324 | // reset vblank end time if we minimize window |
| 325 | if (event.window.event == SDL_WINDOWEVENT_MINIMIZED || event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) |
| 326 | hpc_ResetCounters(&video.vblankHpc); |
| 327 | } |
| 328 | else if (event.type == SDL_KEYDOWN) |
| 329 | { |
| 330 | SDL_Keycode key = event.key.keysym.sym; |
| 331 | |
| 332 | // abort (escape key) |
| 333 | if (key == SDLK_ESCAPE) |
| 334 | { |
| 335 | returnValue = d.abortReturnValue; |
| 336 | |
| 337 | loopRunning = false; |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | // default action (return/enter key) |
| 342 | if (key == SDLK_RETURN || key == SDLK_KP_ENTER) |
| 343 | { |
| 344 | returnValue = d.defaultReturnValue; |
| 345 | |
| 346 | loopRunning = false; |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | // test keybindings |
| 351 | for (int32_t i = 0; i < d.numButtons; i++) |
| 352 | { |
| 353 | button_t *b = &d.buttons[i]; |
| 354 | if (b->callback == NULL && key == b->keyBinding) |
| 355 | { |
| 356 | returnValue = b->returnValue; |
| 357 | |
| 358 | loopRunning = false; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | else if (event.type == SDL_MOUSEBUTTONDOWN) |
| 364 | { |
| 365 | if (event.button.button == SDL_BUTTON_LEFT) |
| 366 | { |
| 367 | mouse.leftButtonPressed = true; |
| 368 | mouse.buttonWaiting = true; |
| 369 | mouse.repeatCounter = 0; |
| 370 | } |
| 371 | |
| 372 | // abort (right mouse button) |
| 373 | if (event.button.button == SDL_BUTTON_RIGHT) |
| 374 | { |
| 375 | returnValue = d.abortReturnValue; |
| 376 | |
| 377 | loopRunning = false; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | // test buttons |
| 382 | if (event.button.button == SDL_BUTTON_LEFT) |
| 383 | { |
| 384 | for (int32_t i = 0; i < d.numButtons; i++) |
| 385 | { |
| 386 | button_t *b = &d.buttons[i]; |
| 387 | |
| 388 | const int32_t x2 = (b->clickableWidth > 0) ? (b->x1 + b->clickableWidth) : b->x2; |
| 389 | if (mouse.x >= b->x1 && mouse.x <= x2 && mouse.y >= b->y1 && mouse.y <= b->y2) |
| 390 | { |
| 391 | if (b->callback != NULL) |
| 392 | { |
| 393 | b->callback(); |
| 394 | if (b->repeat) |
| 395 | { |
| 396 | buttonToRepeat = i; |
| 397 | } |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | returnValue = b->returnValue; |
| 402 | |
| 403 | loopRunning = false; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | else if (event.type == SDL_MOUSEBUTTONUP) |
| 411 | { |
| 412 | #if defined __APPLE__ && defined __aarch64__ |
| 413 | armMacGhostMouseCursorFix(); |
| 414 | #endif |
| 415 | mouse.buttonWaitCounter = 0; |
| 416 | mouse.buttonWaiting = false; |
| 417 | buttonToRepeat = -1; |
| 418 | |
| 419 | if (event.button.button == SDL_BUTTON_LEFT) |
| 420 | mouse.leftButtonPressed = false; |
| 421 | } |
| 422 | #if defined __APPLE__ && defined __aarch64__ |
| 423 | else if (event.type == SDL_MOUSEMOTION) |
| 424 | { |
| 425 | armMacGhostMouseCursorFix(); |
| 426 | } |
| 427 | #endif |
| 428 | } |
| 429 | |
| 430 | if (!mouse.buttonWaiting && buttonToRepeat > -1) |
| 431 | { |
| 432 | button_t *b = &d.buttons[buttonToRepeat]; |
| 433 | |
| 434 | if (++mouse.repeatCounter >= 3) |
| 435 | { |
| 436 | const int32_t x2 = (b->clickableWidth > 0) ? (b->x1 + b->clickableWidth) : b->x2; |
| 437 | if (mouse.x >= b->x1 && mouse.x <= x2 && mouse.y >= b->y1 && mouse.y <= b->y2) |
| 438 | { |
| 439 | if (b->callback != NULL) |
| 440 | b->callback(); |
| 441 | } |
| 442 | |
| 443 | mouse.repeatCounter = 0; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | updateMouseCounters(); |
| 448 | renderFrame2(); |
| 449 | flipFrame(); |
| 450 | endFPSCounter(); |
| 451 | } |
| 452 | |
| 453 | mouse.leftButtonPressed = false; |
| 454 | mouse.buttonWaiting = false; |
| 455 | mouse.buttonWaitCounter = 0; |
| 456 | mouse.repeatCounter = 0; |
| 457 | |
| 458 | removeAskBox(); |
| 459 | pointerSetPreviousMode(); |
| 460 | setPrevStatusMessage(); |
| 461 | |
| 462 | SDL_EventState(SDL_DROPFILE, SDL_ENABLE); |
| 463 | return returnValue; |
| 464 | } |