Debian Codegraph @

src/pt2_mouse.c

raw symbols SCIP
1 // for finding memory leaks in debug mode with Visual Studio
2 #if defined _DEBUG && defined _MSC_VER
3 #include <crtdbg.h>
4 #endif
5
6 #include <ctype.h>
7 #include <math.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #ifndef _WIN32
12 #include <unistd.h>
13 #endif
14 #include <stdio.h>
15 #include "pt2_helpers.h"
16 #include "pt2_diskop.h"
17 #include "pt2_sampler.h"
18 #include "pt2_module_saver.h"
19 #include "pt2_edit.h"
20 #include "pt2_sample_saver.h"
21 #include "pt2_visuals.h"
22 #include "pt2_tables.h"
23 #include "pt2_audio.h"
24 #include "pt2_textout.h"
25 #include "pt2_keyboard.h"
26 #include "pt2_config.h"
27 #include "pt2_bmp.h"
28 #include "pt2_sampling.h"
29 #include "pt2_chordmaker.h"
30 #include "pt2_pat2smp.h"
31 #include "pt2_mod2wav.h"
32 #include "pt2_askbox.h"
33 #include "pt2_replayer.h"
34 #include "pt2_posed.h"
35 #include "pt2_textedit.h"
36
37 SDL_Cursor *cursors[NUM_CURSORS]; // globalized
38
39 static int32_t checkGUIButtons(void);
40 static bool handleRightMouseButton(void);
41 static bool handleLeftMouseButton(void);
42 static bool handleGUIButtons(int32_t button);
43 static void handleRepeatedGUIButtons(void);
44 static void handleRepeatedSamplerFilterButtons(void);
45
46 #if defined __APPLE__ && defined __aarch64__
47 void armMacGhostMouseCursorFix(void)
48 {
49 /* M E G A K L U D G E:
50 ** The mouse cursor can sometimes change back to OS stock
51 ** (or show both stock and custom mouse) on Macs with a notch
52 ** (ARM based) in fullscreen mode. Weird, right?!
53 **
54 ** XXX: Can this cause stuttering or performance issues?
55 */
56 if (video.fullscreen)
57 SDL_SetCursor(NULL); // forces redraw
58 }
59 #endif
60
61 void sampleUpButton(void)
62 {
63 if (editor.sampleZero)
64 {
65 editor.sampleZero = false;
66 editor.currSample = 0;
67 }
68 else if (editor.currSample < 30)
69 {
70 editor.currSample++;
71 }
72
73 updateCurrSample();
74 }
75
76 void sampleDownButton(void)
77 {
78 if (!editor.sampleZero && editor.currSample > 0)
79 {
80 editor.currSample--;
81 updateCurrSample();
82 }
83 }
84
85 static void pointerSetColor(uint8_t cursorColorIndex)
86 {
87 ASSERT(cursorColorIndex <= 5);
88
89 video.palette[PAL_MOUSE_1] = cursorColors[cursorColorIndex][0];
90 video.palette[PAL_MOUSE_2] = cursorColors[cursorColorIndex][1];
91 video.palette[PAL_MOUSE_3] = cursorColors[cursorColorIndex][2];
92
93 if (config.hwMouse)
94 setSystemCursor(cursors[cursorColorIndex]);
95 }
96
97 void updatePointerColor(void)
98 {
99 switch (ui.pointerMode)
100 {
101 case POINTER_MODE_IDLE: pointerSetColor(POINTER_GRAY); break;
102 case POINTER_MODE_PLAY: pointerSetColor(POINTER_YELLOW); break;
103 case POINTER_MODE_EDIT: pointerSetColor(POINTER_BLUE); break;
104 case POINTER_MODE_RECORD: pointerSetColor(POINTER_BLUE); break;
105 case POINTER_MODE_MSG1: pointerSetColor(POINTER_PURPLE); break;
106 case POINTER_MODE_MSG2: pointerSetColor(POINTER_GREEN); break;
107 default: break;
108 }
109 }
110
111 void pointerSetMode(uint8_t pointerMode, bool carry)
112 {
113 ASSERT(pointerMode <= 5);
114
115 ui.pointerMode = pointerMode;
116 if (carry)
117 ui.previousPointerMode = ui.pointerMode;
118
119 updatePointerColor();
120 }
121
122 void pointerSetModeThreadSafe(uint8_t pointerMode, bool carry)
123 {
124 ASSERT(pointerMode <= 5);
125
126 ui.pointerMode = pointerMode;
127 if (carry)
128 ui.previousPointerMode = ui.pointerMode;
129
130 // in hardware mouse mode, the pointer color can only be changed from the main thread
131 if (config.hwMouse)
132 mouse.updatePointerColorFlag = true;
133 else
134 updatePointerColor();
135 }
136
137 void pointerSetPreviousMode(void)
138 {
139 if (ui.editTextFlag || ui.askBoxShown)
140 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
141 else
142 pointerSetMode(ui.previousPointerMode, NO_CARRY);
143 }
144
145 void setMsgPointer(void)
146 {
147 pointerSetMode(POINTER_MODE_MSG2, false);
148 }
149
150 void setErrPointer(void)
151 {
152 pointerSetColor(POINTER_RED);
153 }
154
155 bool setSystemCursor(SDL_Cursor *cur)
156 {
157 if (cur == NULL)
158 {
159 SDL_SetCursor(SDL_GetDefaultCursor());
160 return false;
161 }
162
163 SDL_SetCursor(cur);
164 return true;
165 }
166
167 void freeMouseCursors(void)
168 {
169 SDL_SetCursor(SDL_GetDefaultCursor());
170 for (uint32_t i = 0; i < NUM_CURSORS; i++)
171 {
172 if (cursors[i] != NULL)
173 {
174 SDL_FreeCursor(cursors[i]);
175 cursors[i] = NULL;
176 }
177 }
178 }
179
180 bool createMouseCursors(void) // creates scaled SDL surfaces for current mouse pointer shape
181 {
182 freeMouseCursors();
183
184 uint32_t scaleFactor = video.mouseCursorUpscaleFactor;
185 if (scaleFactor > 8) // just in case
186 scaleFactor = 8;
187
188 for (uint32_t i = 0; i < NUM_CURSORS; i++)
189 {
190 SDL_Surface *surface = SDL_CreateRGBSurface(0, POINTER_W*scaleFactor, POINTER_H*scaleFactor, 32, 0, 0, 0, 0);
191 if (surface == NULL)
192 {
193 freeMouseCursors();
194 config.hwMouse = false; // enable software mouse
195 return false;
196 }
197
198 uint32_t color1 = cursorColors[i][0];
199 uint32_t color2 = cursorColors[i][1];
200 uint32_t color3 = cursorColors[i][2];
201 uint32_t colorkey = 0x12345678;
202
203 color1 = SDL_MapRGB(surface->format, R24(color1), G24(color1), B24(color1));
204 color2 = SDL_MapRGB(surface->format, R24(color2), G24(color2), B24(color2));
205 color3 = SDL_MapRGB(surface->format, R24(color3), G24(color3), B24(color3));
206 colorkey = SDL_MapRGB(surface->format, R24(colorkey), G24(colorkey), B24(colorkey));
207
208 SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
209 SDL_SetColorKey(surface, SDL_TRUE, colorkey);
210 SDL_SetSurfaceRLE(surface, SDL_TRUE);
211
212 const uint8_t *srcPixels8 = mousePointerBMP;
213 SDL_LockSurface(surface);
214
215 uint32_t *dstPixels32 = (uint32_t *)surface->pixels;
216 for (int32_t k = 0; k < surface->w*surface->h; k++) // fill surface with colorkey pixels
217 dstPixels32[k] = colorkey;
218
219 // blit upscaled cursor to surface
220 for (uint32_t y = 0; y < POINTER_H; y++)
221 {
222 uint32_t *outX = &dstPixels32[(y * scaleFactor) * surface->w];
223 for (uint32_t yScale = 0; yScale < scaleFactor; yScale++)
224 {
225 for (uint32_t x = 0; x < POINTER_W; x++)
226 {
227 uint8_t srcPix = srcPixels8[(y * POINTER_W) + x];
228 if (srcPix != PAL_COLORKEY)
229 {
230 uint32_t pixel = colorkey; // make compiler happy
231
232 if (srcPix == PAL_MOUSE_1) pixel = color1;
233 else if (srcPix == PAL_MOUSE_2) pixel = color2;
234 else if (srcPix == PAL_MOUSE_3) pixel = color3;
235
236 for (uint32_t xScale = 0; xScale < scaleFactor; xScale++)
237 outX[xScale] = pixel;
238 }
239
240 outX += scaleFactor;
241 }
242 }
243 }
244
245 SDL_UnlockSurface(surface);
246
247 cursors[i] = SDL_CreateColorCursor(surface, 0, 0);
248 if (cursors[i] == NULL)
249 {
250 SDL_FreeSurface(surface);
251 freeMouseCursors();
252 config.hwMouse = false; // enable software mouse
253 return false;
254 }
255
256 SDL_FreeSurface(surface);
257 }
258
259 pointerSetPreviousMode(); // this sets the appropriate the hardware cursor
260 return true;
261 }
262
263 void updateMouseScaling(void)
264 {
265 if (video.renderW > 0) video.dMouseXMul = (double)SCREEN_W / video.renderW;
266 if (video.renderH > 0) video.dMouseYMul = (double)SCREEN_H / video.renderH;
267 }
268
269 void readMouseXY(void)
270 {
271 int32_t mx, my, windowX, windowY;
272
273 if (mouse.updatePointerColorFlag) // used when changing pointer color from other threads
274 {
275 mouse.updatePointerColorFlag = false;
276 updatePointerColor();
277 }
278
279 if (mouse.setPosFlag)
280 {
281 if (!video.windowHidden)
282 SDL_WarpMouseInWindow(video.window, mouse.setPosX, mouse.setPosY);
283
284 mouse.setPosFlag = false;
285 return;
286 }
287
288 if (video.fullscreen)
289 {
290 mouse.buttonState = SDL_GetMouseState(&mx, &my);
291
292 mouse.absX = mx;
293 mouse.absY = my;
294 }
295 else
296 {
297 mouse.buttonState = SDL_GetGlobalMouseState(&mx, &my);
298
299 mouse.absX = mx;
300 mouse.absY = my;
301
302 // convert desktop coords to window coords
303 SDL_GetWindowPosition(video.window, &windowX, &windowY);
304
305 mx -= windowX;
306 my -= windowY;
307 }
308
309 mouse.rawX = mx;
310 mouse.rawY = my;
311
312 if (video.fullscreen)
313 {
314 // if software mouse is enabled, warp mouse inside render space
315 if (!config.hwMouse)
316 {
317 bool warpMouse = false;
318
319 if (mx < video.renderX)
320 {
321 mx = video.renderX;
322 warpMouse = true;
323 }
324 else if (mx >= video.renderX+video.renderW)
325 {
326 mx = (video.renderX + video.renderW) - 1;
327 warpMouse = true;
328 }
329
330 if (my < video.renderY)
331 {
332 my = video.renderY;
333 warpMouse = true;
334 }
335 else if (my >= video.renderY+video.renderH)
336 {
337 my = (video.renderY + video.renderH) - 1;
338 warpMouse = true;
339 }
340
341 if (warpMouse)
342 SDL_WarpMouseInWindow(video.window, mx, my);
343 }
344
345 // convert fullscreen coords to window (centered image) coords
346 mx -= video.renderX;
347 my -= video.renderY;
348 }
349
350 // kludge: this can happen and prevent buttons from pressing on the very first row of pixels
351 if (mx == -1) mx = 0;
352 if (my == -1) my = 0;
353
354 // multiply coords by video upscaling factors
355 mouse.x = (int32_t)floor(mx * video.dMouseXMul);
356 mouse.y = (int32_t)floor(my * video.dMouseYMul);
357
358 if (config.hwMouse)
359 {
360 // hardware mouse mode (OS)
361 hideSprite(SPRITE_MOUSE_POINTER);
362 }
363 else
364 {
365 // software mouse mode (PT mouse)
366 setSpritePos(SPRITE_MOUSE_POINTER, mouse.x, mouse.y);
367 }
368 }
369
370 void mouseButtonUpHandler(uint8_t mouseButton)
371 {
372 mouse.buttonWaitCounter = 0;
373 mouse.buttonWaiting = false;
374
375 if (mouseButton == SDL_BUTTON_LEFT)
376 {
377 mouse.leftButtonPressed = false;
378
379 ui.forceSampleDrag = false;
380 ui.forceVolDrag = false;
381 ui.leftLoopPinMoving = false;
382 ui.rightLoopPinMoving = false;
383 ui.sampleMarkingPos = -1;
384
385 switch (mouse.lastGUIButton)
386 {
387 case PTB_SLENGTHU:
388 case PTB_SLENGTHD:
389 {
390 if (ui.samplerScreenShown)
391 redrawSample();
392
393 recalcChordLength();
394 updateSamplePos();
395
396 ui.updateSongSize = true;
397 }
398 break;
399
400 case PTB_LENGTHU:
401 case PTB_LENGTHD:
402 case PTB_PATTERNU:
403 case PTB_PATTERND:
404 {
405 ui.updateSongSize = true;
406 if (ui.posEdScreenShown)
407 ui.updatePosEd = true;
408 }
409 break;
410
411 default:
412 break;
413 }
414
415 mouse.lastGUIButton = -1;
416 mouse.lastSmpFilterButton = -1;
417 mouse.lastSamplingButton = -1;
418 }
419
420 if (mouseButton == SDL_BUTTON_RIGHT)
421 {
422 mouse.rightButtonPressed = false;
423 ui.forceSampleEdit = false;
424 }
425 }
426
427 void mouseButtonDownHandler(uint8_t mouseButton)
428 {
429 if (mouseButton == SDL_BUTTON_LEFT)
430 {
431 mouse.leftButtonPressed = true;
432 mouse.buttonWaiting = true;
433 }
434
435 if (mouseButton == SDL_BUTTON_RIGHT)
436 mouse.rightButtonPressed = true;
437
438 // when red mouse pointer (error), block further input for a while
439 if (editor.errorMsgActive && editor.errorMsgBlock)
440 return;
441
442 if (handleRightMouseButton() || handleLeftMouseButton())
443 return;
444
445 handleTextEditing(mouseButton);
446 }
447
448 void handleGUIButtonRepeat(void)
449 {
450 if (!mouse.leftButtonPressed)
451 {
452 // left mouse button released, stop repeating buttons
453 mouse.repeatCounter = 0;
454 return;
455 }
456
457 if (ui.samplerFiltersBoxShown)
458 {
459 handleRepeatedSamplerFilterButtons();
460 return;
461 }
462
463 if (ui.samplingBoxShown)
464 {
465 handleRepeatedSamplingButtons();
466 return;
467 }
468
469 if (mouse.lastGUIButton != checkGUIButtons()) // FIXME: This can potentially do a ton of iterations, bad design!
470 {
471 // only repeat the button that was first clicked (e.g. if you hold and move mouse to another button)
472 mouse.repeatCounter = 0;
473 return;
474 }
475
476 handleRepeatedGUIButtons();
477 mouse.repeatCounter++;
478 }
479
480 static void edNote1UpButton(void)
481 {
482 if (mouse.rightButtonPressed)
483 editor.note1 += 12;
484 else
485 editor.note1++;
486
487 if (editor.note1 > 36)
488 editor.note1 = 36;
489
490 ui.updateChordNote1Text = true;
491 recalcChordLength();
492 }
493
494 static void edNote1DownButton(void)
495 {
496 if (mouse.rightButtonPressed)
497 editor.note1 -= 12;
498 else
499 editor.note1--;
500
501 if (editor.note1 < 0)
502 editor.note1 = 0;
503
504 ui.updateChordNote1Text = true;
505 recalcChordLength();
506 }
507
508 static void edNote2UpButton(void)
509 {
510 if (mouse.rightButtonPressed)
511 editor.note2 += 12;
512 else
513 editor.note2++;
514
515 if (editor.note2 > 36)
516 editor.note2 = 36;
517
518 ui.updateChordNote2Text = true;
519 recalcChordLength();
520 }
521
522 static void edNote2DownButton(void)
523 {
524 if (mouse.rightButtonPressed)
525 editor.note2 -= 12;
526 else
527 editor.note2--;
528
529 if (editor.note2 < 0)
530 editor.note2 = 0;
531
532 ui.updateChordNote2Text = true;
533 recalcChordLength();
534 }
535
536 static void edNote3UpButton(void)
537 {
538 if (mouse.rightButtonPressed)
539 editor.note3 += 12;
540 else
541 editor.note3++;
542
543 if (editor.note3 > 36)
544 editor.note3 = 36;
545
546 ui.updateChordNote3Text = true;
547 recalcChordLength();
548 }
549
550 static void edNote3DownButton(void)
551 {
552 if (mouse.rightButtonPressed)
553 editor.note3 -= 12;
554 else
555 editor.note3--;
556
557 if (editor.note3 < 0)
558 editor.note3 = 0;
559
560 ui.updateChordNote3Text = true;
561 recalcChordLength();
562 }
563
564 static void edNote4UpButton(void)
565 {
566 if (mouse.rightButtonPressed)
567 editor.note4 += 12;
568 else
569 editor.note4++;
570
571 if (editor.note4 > 36)
572 editor.note4 = 36;
573
574 ui.updateChordNote4Text = true;
575 recalcChordLength();
576 }
577
578 static void edNote4DownButton(void)
579 {
580 if (mouse.rightButtonPressed)
581 editor.note4 -= 12;
582 else
583 editor.note4--;
584
585 if (editor.note4 < 0)
586 editor.note4 = 0;
587
588 ui.updateChordNote4Text = true;
589 recalcChordLength();
590 }
591
592 static void edPosUpButton(bool fast)
593 {
594 if (mouse.rightButtonPressed)
595 {
596 if (fast)
597 {
598 if (editor.samplePos <= config.maxSampleLength-64)
599 editor.samplePos += 64;
600 else
601 editor.samplePos = config.maxSampleLength;
602 }
603 else
604 {
605 if (editor.samplePos <= config.maxSampleLength-16)
606 editor.samplePos += 16;
607 else
608 editor.samplePos = config.maxSampleLength;
609 }
610 }
611 else
612 {
613 if (fast)
614 {
615 if (editor.samplePos <= config.maxSampleLength-64)
616 editor.samplePos += 64;
617 else
618 editor.samplePos = config.maxSampleLength;
619 }
620 else
621 {
622 if (editor.samplePos < config.maxSampleLength)
623 editor.samplePos++;
624 else
625 editor.samplePos = config.maxSampleLength;
626 }
627 }
628
629 if (editor.samplePos > song->samples[editor.currSample].length)
630 editor.samplePos = song->samples[editor.currSample].length;
631
632 ui.updatePosText = true;
633 }
634
635 static void edPosDownButton(bool fast)
636 {
637 if (mouse.rightButtonPressed)
638 {
639 if (fast)
640 {
641 if (editor.samplePos > 64)
642 editor.samplePos -= 64;
643 else
644 editor.samplePos = 0;
645 }
646 else
647 {
648 if (editor.samplePos > 16)
649 editor.samplePos -= 16;
650 else
651 editor.samplePos = 0;
652 }
653 }
654 else
655 {
656 if (fast)
657 {
658 if (editor.samplePos > 64)
659 editor.samplePos -= 64;
660 else
661 editor.samplePos = 0;
662 }
663 else
664 {
665 if (editor.samplePos > 0)
666 editor.samplePos--;
667 else
668 editor.samplePos = 0;
669 }
670 }
671
672 ui.updatePosText = true;
673 }
674
675 static void edModUpButton(void)
676 {
677 if (mouse.rightButtonPressed)
678 editor.modulateSpeed += 10;
679 else
680 editor.modulateSpeed++;
681
682 if (editor.modulateSpeed > 127)
683 editor.modulateSpeed = 127;
684
685 ui.updateModText = true;
686 }
687
688 static void edModDownButton(void)
689 {
690 if (mouse.rightButtonPressed)
691 {
692 editor.modulateSpeed -= 10;
693 }
694 else
695 {
696 editor.modulateSpeed--;
697 }
698
699 if (editor.modulateSpeed < -128)
700 editor.modulateSpeed = -128;
701
702 ui.updateModText = true;
703 }
704
705 static void edVolUpButton(void)
706 {
707 if (mouse.rightButtonPressed)
708 {
709 if (editor.sampleVol <= 999-10)
710 editor.sampleVol += 10;
711 else
712 editor.sampleVol = 999;
713 }
714 else
715 {
716 if (editor.sampleVol < 999)
717 editor.sampleVol++;
718 }
719
720 ui.updateVolText = true;
721 }
722
723 static void edVolDownButton(void)
724 {
725 if (mouse.rightButtonPressed)
726 {
727 if (editor.sampleVol >= 10)
728 editor.sampleVol -= 10;
729 else
730 editor.sampleVol = 0;
731 }
732 else
733 {
734 if (editor.sampleVol > 0)
735 editor.sampleVol--;
736 }
737
738 ui.updateVolText = true;
739 }
740
741 static void sampleFineTuneUpButton(void)
742 {
743 if (editor.sampleZero)
744 return;
745
746 int8_t finetune = song->samples[editor.currSample].fineTune & 0xF;
747 if (finetune != 7)
748 song->samples[editor.currSample].fineTune = (finetune + 1) & 0xF;
749
750 if (mouse.rightButtonPressed)
751 song->samples[editor.currSample].fineTune = 0;
752
753 recalcChordLength();
754 ui.updateCurrSampleFineTune = true;
755 }
756
757 static void sampleFineTuneDownButton(void)
758 {
759 if (editor.sampleZero)
760 return;
761
762 int8_t finetune = song->samples[editor.currSample].fineTune & 0xF;
763 if (finetune != 8)
764 song->samples[editor.currSample].fineTune = (finetune - 1) & 0xF;
765
766 if (mouse.rightButtonPressed)
767 song->samples[editor.currSample].fineTune = 0;
768
769 recalcChordLength();
770 ui.updateCurrSampleFineTune = true;
771 }
772
773 static void sampleVolumeUpButton(void)
774 {
775 if (editor.sampleZero)
776 return;
777
778 int8_t val = song->samples[editor.currSample].volume;
779
780 if (mouse.rightButtonPressed)
781 val += 16;
782 else
783 val++;
784
785 if (val > 64)
786 val = 64;
787
788 song->samples[editor.currSample].volume = (uint8_t)val;
789 ui.updateCurrSampleVolume = true;
790 }
791
792 static void sampleVolumeDownButton(void)
793 {
794 if (editor.sampleZero)
795 return;
796
797 int8_t val = song->samples[editor.currSample].volume;
798
799 if (mouse.rightButtonPressed)
800 val -= 16;
801 else
802 val--;
803
804 if (val < 0)
805 val = 0;
806
807 song->samples[editor.currSample].volume = (uint8_t)val;
808 ui.updateCurrSampleVolume = true;
809 }
810
811 static void sampleLengthUpButton(bool fast)
812 {
813 if (editor.sampleZero || song->samples[editor.currSample].length == config.maxSampleLength)
814 return;
815
816 int32_t val = song->samples[editor.currSample].length;
817 if (mouse.rightButtonPressed)
818 {
819 if (fast)
820 val += 64;
821 else
822 val += 16;
823 }
824 else
825 {
826 if (fast)
827 val += 10;
828 else
829 val += 2;
830 }
831
832 if (val > config.maxSampleLength)
833 val = config.maxSampleLength;
834
835 song->samples[editor.currSample].length = val;
836 ui.updateCurrSampleLength = true;
837 }
838
839 static void sampleLengthDownButton(bool fast)
840 {
841 if (editor.sampleZero)
842 return;
843
844 moduleSample_t *s = &song->samples[editor.currSample];
845 if (s->loopStart+s->loopLength > 2)
846 {
847 if (s->length == s->loopStart+s->loopLength)
848 return;
849 }
850 else
851 {
852 if (s->length == 0)
853 return;
854 }
855
856 int32_t val = song->samples[editor.currSample].length;
857 if (mouse.rightButtonPressed)
858 {
859 if (fast)
860 val -= 64;
861 else
862 val -= 16;
863 }
864 else
865 {
866 if (fast)
867 val -= 10;
868 else
869 val -= 2;
870 }
871
872 if (val < 0)
873 val = 0;
874
875 if (s->loopStart+s->loopLength > 2)
876 {
877 if (val < s->loopStart+s->loopLength)
878 val = s->loopStart+s->loopLength;
879 }
880
881 s->length = val;
882
883 ui.updateCurrSampleLength = true;
884 }
885
886 static void sampleRepeatUpButton(bool fast)
887 {
888 if (editor.sampleZero)
889 return;
890
891 int32_t val = song->samples[editor.currSample].loopStart;
892 int32_t loopLen = song->samples[editor.currSample].loopLength;
893 int32_t len = song->samples[editor.currSample].length;
894
895 if (len == 0)
896 {
897 song->samples[editor.currSample].loopStart = 0;
898 return;
899 }
900
901 if (mouse.rightButtonPressed)
902 {
903 if (fast)
904 val += 64;
905 else
906 val += 16;
907 }
908 else
909 {
910 if (fast)
911 val += 10;
912 else
913 val += 2;
914 }
915
916 if (val > len-loopLen)
917 val = len-loopLen;
918
919 song->samples[editor.currSample].loopStart = val;
920 ui.updateCurrSampleRepeat = true;
921
922 updatePaulaLoops();
923
924 if (ui.samplerScreenShown)
925 setLoopSprites();
926
927 if (ui.editOpScreenShown && ui.editOpScreen == 3) // sample chord editor
928 ui.updateChordLengthText = true;
929 }
930
931 static void sampleRepeatDownButton(bool fast)
932 {
933 if (editor.sampleZero)
934 return;
935
936 int32_t val = song->samples[editor.currSample].loopStart;
937 int32_t len = song->samples[editor.currSample].length;
938
939 if (len == 0)
940 {
941 song->samples[editor.currSample].loopStart = 0;
942 return;
943 }
944
945 if (mouse.rightButtonPressed)
946 {
947 if (fast)
948 val -= 64;
949 else
950 val -= 16;
951 }
952 else
953 {
954 if (fast)
955 val -= 10;
956 else
957 val -= 2;
958 }
959
960 if (val < 0)
961 val = 0;
962
963 song->samples[editor.currSample].loopStart = val;
964 ui.updateCurrSampleRepeat = true;
965
966 updatePaulaLoops();
967
968 if (ui.samplerScreenShown)
969 setLoopSprites();
970
971 if (ui.editOpScreenShown && ui.editOpScreen == 3) // sample chord editor
972 ui.updateChordLengthText = true;
973 }
974
975 static void sampleRepeatLengthUpButton(bool fast)
976 {
977 if (editor.sampleZero)
978 return;
979
980 int32_t val = song->samples[editor.currSample].loopLength;
981 int32_t loopStart = song->samples[editor.currSample].loopStart;
982 int32_t len = song->samples[editor.currSample].length;
983
984 if (len == 0)
985 {
986 song->samples[editor.currSample].loopLength = 2;
987 return;
988 }
989
990 if (mouse.rightButtonPressed)
991 {
992 if (fast)
993 val += 64;
994 else
995 val += 16;
996 }
997 else
998 {
999 if (fast)
1000 val += 10;
1001 else
1002 val += 2;
1003 }
1004
1005 if (val > len-loopStart)
1006 val = len-loopStart;
1007
1008 song->samples[editor.currSample].loopLength = val;
1009 ui.updateCurrSampleReplen = true;
1010
1011 updatePaulaLoops();
1012
1013 if (ui.samplerScreenShown)
1014 setLoopSprites();
1015
1016 if (ui.editOpScreenShown && ui.editOpScreen == 3) // sample chord editor
1017 ui.updateChordLengthText = true;
1018 }
1019
1020 static void sampleRepeatLengthDownButton(bool fast)
1021 {
1022 if (editor.sampleZero)
1023 return;
1024
1025 int32_t val = song->samples[editor.currSample].loopLength;
1026 int32_t len = song->samples[editor.currSample].length;
1027
1028 if (len == 0)
1029 {
1030 song->samples[editor.currSample].loopLength = 2;
1031 return;
1032 }
1033
1034 if (mouse.rightButtonPressed)
1035 {
1036 if (fast)
1037 val -= 64;
1038 else
1039 val -= 16;
1040 }
1041 else
1042 {
1043 if (fast)
1044 val -= 10;
1045 else
1046 val -= 2;
1047 }
1048
1049 if (val < 2)
1050 val = 2;
1051
1052 song->samples[editor.currSample].loopLength = val;
1053 ui.updateCurrSampleReplen = true;
1054
1055 updatePaulaLoops();
1056
1057 if (ui.samplerScreenShown)
1058 setLoopSprites();
1059
1060 if (ui.editOpScreenShown && ui.editOpScreen == 3) // sample chord editor
1061 ui.updateChordLengthText = true;
1062 }
1063
1064 static void tempoUpButton(void)
1065 {
1066 if (editor.timingMode == TEMPO_MODE_VBLANK)
1067 return;
1068
1069 int32_t val = song->currBPM;
1070 if (mouse.rightButtonPressed)
1071 val += 10;
1072 else
1073 val++;
1074
1075 if (val > 255)
1076 val = 255;
1077
1078 song->currBPM = val;
1079 modSetTempo(song->currBPM, true);
1080 ui.updateSongBPM = true;
1081 }
1082
1083 static void tempoDownButton(void)
1084 {
1085 if (editor.timingMode == TEMPO_MODE_VBLANK)
1086 return;
1087
1088 int32_t val = song->currBPM;
1089 if (mouse.rightButtonPressed)
1090 val -= 10;
1091 else
1092 val--;
1093
1094 if (val < 32)
1095 val = 32;
1096
1097 song->currBPM = val;
1098 modSetTempo(song->currBPM, true);
1099 ui.updateSongBPM = true;
1100 }
1101
1102 static void songLengthUpButton(void)
1103 {
1104 int16_t val = song->header.songLength;
1105 if (mouse.rightButtonPressed)
1106 val += 10;
1107 else
1108 val++;
1109
1110 if (val > 128)
1111 val = 128;
1112
1113 song->header.songLength = (uint8_t)val;
1114
1115 val = song->currPos;
1116 if (val > song->header.songLength-1)
1117 val = song->header.songLength-1;
1118
1119 editor.currPosEdPattDisp = &song->header.patternTable[val];
1120 ui.updateSongLength = true;
1121 }
1122
1123 static void songLengthDownButton(void)
1124 {
1125 int16_t val = song->header.songLength;
1126
1127 if (mouse.rightButtonPressed)
1128 val -= 10;
1129 else
1130 val--;
1131
1132 if (val < 1)
1133 val = 1;
1134
1135 song->header.songLength = (uint8_t)val;
1136
1137 val = song->currPos;
1138 if (val > song->header.songLength-1)
1139 val = song->header.songLength-1;
1140
1141 editor.currPosEdPattDisp = &song->header.patternTable[val];
1142 ui.updateSongLength = true;
1143 }
1144
1145 static void patternUpButton(void)
1146 {
1147 int16_t val = song->header.patternTable[song->currPos];
1148
1149 if (mouse.rightButtonPressed)
1150 val += 10;
1151 else
1152 val++;
1153
1154 if (val > MAX_PATTERNS-1)
1155 val = MAX_PATTERNS-1;
1156
1157 song->header.patternTable[song->currPos] = (uint8_t)val;
1158
1159 if (ui.posEdScreenShown)
1160 ui.updatePosEd = true;
1161
1162 ui.updateSongPattern = true;
1163 }
1164
1165 static void patternDownButton(void)
1166 {
1167 int16_t val = song->header.patternTable[song->currPos];
1168
1169 if (mouse.rightButtonPressed)
1170 val -= 10;
1171 else
1172 val--;
1173
1174 if (val < 0)
1175 val = 0;
1176
1177 song->header.patternTable[song->currPos] = (uint8_t)val;
1178
1179 if (ui.posEdScreenShown)
1180 ui.updatePosEd = true;
1181
1182 ui.updateSongPattern = true;
1183 }
1184
1185 static void positionUpButton(void)
1186 {
1187 int16_t val = song->currPos;
1188
1189 if (mouse.rightButtonPressed)
1190 val += 10;
1191 else
1192 val++;
1193
1194 if (val > 127)
1195 val = 127;
1196
1197 modSetPos(val, DONT_SET_ROW);
1198 }
1199
1200 static void positionDownButton(void)
1201 {
1202 int16_t val = song->currPos;
1203
1204 if (mouse.rightButtonPressed)
1205 val -= 10;
1206 else
1207 val--;
1208
1209 if (val < 0)
1210 val = 0;
1211
1212 modSetPos(val, DONT_SET_ROW);
1213 }
1214
1215 static void handleSamplerVolumeBox(void)
1216 {
1217 if (mouse.rightButtonPressed)
1218 {
1219 if (ui.editTextFlag)
1220 {
1221 leaveTextEditMode(EDIT_TEXT_NO_UPDATE);
1222 }
1223 else
1224 {
1225 ui.samplerVolBoxShown = false;
1226 removeSamplerVolBox();
1227 }
1228
1229 return;
1230 }
1231
1232 if (ui.editTextFlag)
1233 return;
1234
1235 // check buttons
1236 if (mouse.leftButtonPressed)
1237 {
1238 // MAIN SCREEN STOP
1239 if (!ui.diskOpScreenShown && !ui.posEdScreenShown)
1240 {
1241 if (mouse.x >= 182 && mouse.x <= 243 && mouse.y >= 0 && mouse.y <= 10)
1242 {
1243 modStop();
1244 return;
1245 }
1246 }
1247
1248 if (mouse.x >= 32 && mouse.x <= 95)
1249 {
1250 // SAMPLER SCREEN PLAY WAVEFORM
1251 if (mouse.y >= 211 && mouse.y <= 221)
1252 {
1253 samplerPlayWaveform();
1254 return;
1255 }
1256
1257 // SAMPLER SCREEN PLAY DISPLAY
1258 else if (mouse.y >= 222 && mouse.y <= 232)
1259 {
1260 samplerPlayDisplay();
1261 return;
1262 }
1263
1264 // SAMPLER SCREEN PLAY RANGE
1265 else if (mouse.y >= 233 && mouse.y <= 243)
1266 {
1267 samplerPlayRange();
1268 return;
1269 }
1270 }
1271
1272 // SAMPLER SCREEN STOP
1273 if (mouse.x >= 0 && mouse.x <= 31 && mouse.y >= 222 && mouse.y <= 243)
1274 {
1275 turnOffVoices();
1276 return;
1277 }
1278
1279 // VOLUME button (toggle)
1280 if (mouse.x >= 96 && mouse.x <= 135 && mouse.y >= 244 && mouse.y <= 254)
1281 {
1282 ui.samplerVolBoxShown = false;
1283 removeSamplerVolBox();
1284 return;
1285 }
1286
1287 // DRAG BOXES
1288 if (mouse.x >= 72 && mouse.x <= 173 && mouse.y >= 154 && mouse.y <= 175)
1289 {
1290 volBoxBarPressed(MOUSE_BUTTON_NOT_HELD);
1291 return;
1292 }
1293
1294 if (mouse.x >= 174 && mouse.x <= 207)
1295 {
1296 // FROM NUM
1297 if (mouse.y >= 154 && mouse.y <= 164)
1298 {
1299 textEdit.tmpDisp16 = editor.vol1;
1300 editor.vol1Disp = &textEdit.tmpDisp16;
1301 textEdit.numPtr16 = &textEdit.tmpDisp16;
1302 textEdit.numDigits = 3;
1303 textEdit.cursorStartX = 176;
1304 textEdit.cursorStartY = 162;
1305 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_SA_VOL_FROM_NUM);
1306 return;
1307 }
1308
1309 // TO NUM
1310 else if (mouse.y >= 165 && mouse.y <= 175)
1311 {
1312 textEdit.tmpDisp16 = editor.vol2;
1313 editor.vol2Disp = &textEdit.tmpDisp16;
1314 textEdit.numPtr16 = &textEdit.tmpDisp16;
1315 textEdit.numDigits = 3;
1316 textEdit.cursorStartX = 176;
1317 textEdit.cursorStartY = 173;
1318 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_SA_VOL_TO_NUM);
1319 return;
1320 }
1321 }
1322
1323 if (mouse.y >= 176 && mouse.y <= 186)
1324 {
1325 // NORMALIZE
1326 if (mouse.x >= 101 && mouse.x <= 143)
1327 {
1328 if (editor.sampleZero)
1329 {
1330 statusNotSampleZero();
1331 return;
1332 }
1333
1334 moduleSample_t *s = &song->samples[editor.currSample];
1335 if (s->length == 0)
1336 {
1337 statusSampleIsEmpty();
1338 return;
1339 }
1340
1341 int8_t *sampleData = &song->sampleData[s->offset];
1342
1343 int32_t from = 0;
1344 int32_t to = s->length;
1345
1346 if (editor.markStartOfs != -1)
1347 {
1348 from = editor.markStartOfs;
1349 to = editor.markEndOfs;
1350
1351 if (to > s->length)
1352 to = s->length;
1353
1354 if (from == to || from >= s->length || to < from)
1355 {
1356 from = 0;
1357 to = s->length;
1358 }
1359 }
1360
1361 int32_t hi = 0;
1362 for (int32_t i = from; i < to; i++)
1363 {
1364 const int32_t sample = ABS(sampleData[i]);
1365 if (sample > hi)
1366 hi = sample;
1367 }
1368
1369 if (hi <= 0 || hi > 127)
1370 {
1371 editor.vol1 = 100;
1372 editor.vol2 = 100;
1373 }
1374 else if (hi < 64)
1375 {
1376 editor.vol1 = 200;
1377 editor.vol2 = 200;
1378 }
1379 else
1380 {
1381 editor.vol1 = (uint16_t)((100 * 127) / hi);
1382 editor.vol2 = (uint16_t)((100 * 127) / hi);
1383 }
1384
1385 ui.updateVolFromText = true;
1386 ui.updateVolToText = true;
1387
1388 showVolFromSlider();
1389 showVolToSlider();
1390 return;
1391 }
1392
1393 // RAMP DOWN
1394 else if (mouse.x >= 144 && mouse.x <= 153)
1395 {
1396 editor.vol1 = 100;
1397 editor.vol2 = 0;
1398 ui.updateVolFromText = true;
1399 ui.updateVolToText = true;
1400 showVolFromSlider();
1401 showVolToSlider();
1402 return;
1403 }
1404
1405 // RAMP UP
1406 else if (mouse.x >= 154 && mouse.x <= 163)
1407 {
1408 editor.vol1 = 0;
1409 editor.vol2 = 100;
1410 ui.updateVolFromText = true;
1411 ui.updateVolToText = true;
1412 showVolFromSlider();
1413 showVolToSlider();
1414 return;
1415 }
1416
1417 // RAMP UNITY
1418 else if (mouse.x >= 164 && mouse.x <= 173)
1419 {
1420 editor.vol1 = 100;
1421 editor.vol2 = 100;
1422 ui.updateVolFromText = true;
1423 ui.updateVolToText = true;
1424 showVolFromSlider();
1425 showVolToSlider();
1426 return;
1427 }
1428
1429 // CANCEL
1430 else if (mouse.x >= 174 && mouse.x <= 207)
1431 {
1432 ui.samplerVolBoxShown = false;
1433 removeSamplerVolBox();
1434 return;
1435 }
1436
1437 // RAMP
1438 else if (mouse.x >= 72 && mouse.x <= 100)
1439 {
1440 if (editor.sampleZero)
1441 {
1442 statusNotSampleZero();
1443 return;
1444 }
1445
1446 moduleSample_t *s = &song->samples[editor.currSample];
1447 if (s->length == 0)
1448 {
1449 statusSampleIsEmpty();
1450 return;
1451 }
1452
1453 if (editor.vol1 == 100 && editor.vol2 == 100)
1454 {
1455 ui.samplerVolBoxShown = false;
1456 removeSamplerVolBox();
1457 return;
1458 }
1459
1460 int8_t *sampleData = &song->sampleData[s->offset];
1461
1462 int32_t from = 0;
1463 int32_t to = s->length;
1464
1465 if (editor.markStartOfs != -1)
1466 {
1467 from = editor.markStartOfs;
1468 to = editor.markEndOfs;
1469
1470 if (to > s->length)
1471 to = s->length;
1472
1473 if (from == to || from >= s->length || to < from)
1474 {
1475 from = 0;
1476 to = s->length;
1477 }
1478 }
1479
1480 const int32_t markLength = to - from;
1481
1482 double dToFrac = 0.0;
1483 double dFromFrac = editor.vol1;
1484
1485 double dToDelta = (double)editor.vol2 / markLength;
1486 double dFromDelta = (double)editor.vol1 / markLength;
1487
1488 for (int32_t i = from; i < to; i++)
1489 {
1490 double dSmp = ((dToFrac + dFromFrac) * sampleData[i]) * (1.0 / 100.0);
1491 int32_t smp32 = (int32_t)dSmp;
1492 sampleData[i] = (int8_t)(CLAMP(smp32, INT8_MIN, INT8_MAX));
1493
1494 dToFrac += dToDelta;
1495 dFromFrac -= dFromDelta;
1496 }
1497
1498 fixSampleBeep(s);
1499
1500 ui.samplerVolBoxShown = false;
1501 removeSamplerVolBox();
1502
1503 updateWindowTitle(MOD_IS_MODIFIED);
1504 }
1505 }
1506 }
1507 }
1508
1509 static void handleRepeatedSamplerFilterButtons(void)
1510 {
1511 if (!mouse.leftButtonPressed || mouse.lastSmpFilterButton == -1)
1512 return;
1513
1514 switch (mouse.lastSmpFilterButton)
1515 {
1516 case 0: // low-pass cutoff up
1517 {
1518 if (mouse.rightButtonPressed)
1519 {
1520 if (editor.lpCutOff <= 0xFFFF-50)
1521 editor.lpCutOff += 50;
1522 else
1523 editor.lpCutOff = 0xFFFF;
1524 }
1525 else
1526 {
1527 if (editor.lpCutOff < 0xFFFF)
1528 editor.lpCutOff++;
1529 else
1530 editor.lpCutOff = 0xFFFF;
1531 }
1532
1533 if (editor.lpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
1534 editor.lpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
1535
1536 ui.updateLPText = true;
1537 }
1538 break;
1539
1540 case 1: // low-pass cutoff down
1541 {
1542 if (mouse.rightButtonPressed)
1543 {
1544 if (editor.lpCutOff >= 50)
1545 editor.lpCutOff -= 50;
1546 else
1547 editor.lpCutOff = 0;
1548 }
1549 else
1550 {
1551 if (editor.lpCutOff > 0)
1552 editor.lpCutOff--;
1553 else
1554 editor.lpCutOff = 0;
1555 }
1556
1557 ui.updateLPText = true;
1558 }
1559 break;
1560
1561 case 2: // high-pass cutoff up
1562 {
1563 if (mouse.rightButtonPressed)
1564 {
1565 if (editor.hpCutOff <= 0xFFFF-50)
1566 editor.hpCutOff += 50;
1567 else
1568 editor.hpCutOff = 0xFFFF;
1569 }
1570 else
1571 {
1572 if (editor.hpCutOff < 0xFFFF)
1573 editor.hpCutOff++;
1574 else
1575 editor.hpCutOff = 0xFFFF;
1576 }
1577
1578 if (editor.hpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
1579 editor.hpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
1580
1581 ui.updateHPText = true;
1582 }
1583 break;
1584
1585 case 3: // high-pass cutoff down
1586 {
1587 if (mouse.rightButtonPressed)
1588 {
1589 if (editor.hpCutOff >= 50)
1590 editor.hpCutOff -= 50;
1591 else
1592 editor.hpCutOff = 0;
1593 }
1594 else
1595 {
1596 if (editor.hpCutOff > 0)
1597 editor.hpCutOff--;
1598 else
1599 editor.hpCutOff = 0;
1600 }
1601
1602 ui.updateHPText = true;
1603 }
1604 break;
1605
1606 default: break;
1607 }
1608 }
1609
1610 static void handleSamplerFiltersBox(void)
1611 {
1612 if (mouse.rightButtonPressed && ui.editTextFlag)
1613 {
1614 leaveTextEditMode(EDIT_TEXT_NO_UPDATE);
1615 return;
1616 }
1617
1618 if (ui.editTextFlag || mouse.lastSmpFilterButton > -1 || !mouse.leftButtonPressed)
1619 return;
1620
1621 // FILTERS button (toggle)
1622 if (mouse.x >= 211 && mouse.x <= 245 && mouse.y >= 244 && mouse.y <= 254)
1623 {
1624 ui.samplerFiltersBoxShown = false;
1625 removeSamplerFiltersBox();
1626 return;
1627 }
1628
1629 // MAIN SCREEN STOP
1630 if (!ui.diskOpScreenShown && !ui.posEdScreenShown)
1631 {
1632 if (mouse.x >= 182 && mouse.x <= 243 && mouse.y >= 0 && mouse.y <= 10)
1633 {
1634 modStop();
1635 return;
1636 }
1637 }
1638
1639 if (mouse.x >= 32 && mouse.x <= 95)
1640 {
1641 // SAMPLER SCREEN PLAY WAVEFORM
1642 if (mouse.y >= 211 && mouse.y <= 221)
1643 {
1644 samplerPlayWaveform();
1645 return;
1646 }
1647
1648 // SAMPLER SCREEN PLAY DISPLAY
1649 else if (mouse.y >= 222 && mouse.y <= 232)
1650 {
1651 samplerPlayDisplay();
1652 return;
1653 }
1654
1655 // SAMPLER SCREEN PLAY RANGE
1656 else if (mouse.y >= 233 && mouse.y <= 243)
1657 {
1658 samplerPlayRange();
1659 return;
1660 }
1661 }
1662
1663 // SAMPLER SCREEN STOP
1664 if (mouse.x >= 0 && mouse.x <= 31 && mouse.y >= 222 && mouse.y <= 243)
1665 {
1666 turnOffVoices();
1667 return;
1668 }
1669
1670 // UNDO
1671 if (mouse.x >= 65 && mouse.x <= 75 && mouse.y >= 154 && mouse.y <= 184)
1672 {
1673 if (editor.sampleZero)
1674 {
1675 statusNotSampleZero();
1676 return;
1677 }
1678
1679 moduleSample_t *s = &song->samples[editor.currSample];
1680 if (s->length == 0)
1681 {
1682 statusSampleIsEmpty();
1683 }
1684 else
1685 {
1686 memcpy(&song->sampleData[s->offset], sampler.sampleUndoCopy, config.maxSampleLength);
1687 redrawSample();
1688 updateWindowTitle(MOD_IS_MODIFIED);
1689 renderSamplerFiltersBox();
1690 }
1691
1692 return;
1693 }
1694
1695 if (mouse.y >= 154 && mouse.y <= 164)
1696 {
1697 // DO LOW-PASS FILTER
1698 if (mouse.x >= 76 && mouse.x <= 165)
1699 {
1700 lowPassSample(editor.lpCutOff);
1701 renderSamplerFiltersBox();
1702 return;
1703 }
1704
1705 // LOW-PASS CUTOFF
1706 else if (mouse.x >= 166 && mouse.x <= 217)
1707 {
1708 if (mouse.rightButtonPressed)
1709 {
1710 editor.lpCutOff = 0;
1711 ui.updateLPText = true;
1712 }
1713 else
1714 {
1715 textEdit.tmpDisp16 = editor.lpCutOff;
1716 editor.lpCutOffDisp = &textEdit.tmpDisp16;
1717 textEdit.numPtr16 = &textEdit.tmpDisp16;
1718 textEdit.numDigits = 4;
1719 textEdit.cursorStartX = 168;
1720 textEdit.cursorStartY = 162;
1721 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_SA_FIL_LP_CUTOFF);
1722 }
1723
1724 return;
1725 }
1726
1727 // LOW-PASS CUTOFF UP
1728 else if (mouse.x >= 218 && mouse.x <= 228)
1729 {
1730 mouse.lastSmpFilterButton = 0;
1731 if (mouse.rightButtonPressed)
1732 {
1733 if (editor.lpCutOff <= 0xFFFF-100)
1734 editor.lpCutOff += 100;
1735 else
1736 editor.lpCutOff = 0xFFFF;
1737 }
1738 else
1739 {
1740 if (editor.lpCutOff < 0xFFFF)
1741 editor.lpCutOff++;
1742 else
1743 editor.lpCutOff = 0xFFFF;
1744 }
1745
1746 if (editor.lpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
1747 editor.lpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
1748
1749 ui.updateLPText = true;
1750 return;
1751 }
1752
1753 // LOW-PASS CUTOFF DOWN
1754 else if (mouse.x >= 229 && mouse.x <= 239)
1755 {
1756 mouse.lastSmpFilterButton = 1;
1757 if (mouse.rightButtonPressed)
1758 {
1759 if (editor.lpCutOff >= 100)
1760 editor.lpCutOff -= 100;
1761 else
1762 editor.lpCutOff = 0;
1763 }
1764 else
1765 {
1766 if (editor.lpCutOff > 0)
1767 editor.lpCutOff--;
1768 else
1769 editor.lpCutOff = 0;
1770 }
1771
1772 ui.updateLPText = true;
1773 return;
1774 }
1775 }
1776
1777 if (mouse.y >= 164 && mouse.y <= 174)
1778 {
1779 // DO HIGH-PASS FILTER
1780 if (mouse.x >= 76 && mouse.x <= 165)
1781 {
1782 highPassSample(editor.hpCutOff);
1783 renderSamplerFiltersBox();
1784 return;
1785 }
1786
1787 // HIGH-PASS CUTOFF
1788 else if (mouse.x >= 166 && mouse.x <= 217)
1789 {
1790 if (mouse.rightButtonPressed)
1791 {
1792 editor.hpCutOff = 0;
1793 ui.updateHPText = true;
1794 }
1795 else
1796 {
1797 textEdit.tmpDisp16 = editor.hpCutOff;
1798 editor.hpCutOffDisp = &textEdit.tmpDisp16;
1799 textEdit.numPtr16 = &textEdit.tmpDisp16;
1800 textEdit.numDigits = 4;
1801 textEdit.cursorStartX = 168;
1802 textEdit.cursorStartY = 173;
1803 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_SA_FIL_HP_CUTOFF);
1804 }
1805
1806 return;
1807 }
1808
1809 // HIGH-PASS CUTOFF UP
1810 else if (mouse.x >= 218 && mouse.x <= 228)
1811 {
1812 mouse.lastSmpFilterButton = 2;
1813 if (mouse.rightButtonPressed)
1814 {
1815 if (editor.hpCutOff <= 0xFFFF-100)
1816 editor.hpCutOff += 100;
1817 else
1818 editor.hpCutOff = 0xFFFF;
1819 }
1820 else
1821 {
1822 if (editor.hpCutOff < 0xFFFF)
1823 editor.hpCutOff++;
1824 else
1825 editor.hpCutOff = 0xFFFF;
1826 }
1827
1828 if (editor.hpCutOff > (uint16_t)(FILTERS_BASE_FREQ/2))
1829 editor.hpCutOff = (uint16_t)(FILTERS_BASE_FREQ/2);
1830
1831 ui.updateHPText = true;
1832 return;
1833 }
1834
1835 // HIGH-PASS CUTOFF DOWN
1836 else if (mouse.x >= 229 && mouse.x <= 239)
1837 {
1838 mouse.lastSmpFilterButton = 3;
1839 if (mouse.rightButtonPressed)
1840 {
1841 if (editor.hpCutOff >= 100)
1842 editor.hpCutOff -= 100;
1843 else
1844 editor.hpCutOff = 0;
1845 }
1846 else
1847 {
1848 if (editor.hpCutOff > 0)
1849 editor.hpCutOff--;
1850 else
1851 editor.hpCutOff = 0;
1852 }
1853
1854 ui.updateHPText = true;
1855 return;
1856 }
1857 }
1858
1859 // NORMALIZE SAMPLE FLAG
1860 if (mouse.x >= 76 && mouse.x <= 239 && mouse.y >= 174 && mouse.y <= 186)
1861 {
1862 editor.normalizeFiltersFlag ^= 1;
1863 ui.updateNormFlag = true;
1864 return;
1865 }
1866
1867 // EXIT
1868 if (mouse.x >= 240 && mouse.x <= 250 && mouse.y >= 154 && mouse.y <= 186)
1869 {
1870 ui.samplerFiltersBoxShown = false;
1871 removeSamplerFiltersBox();
1872 }
1873 }
1874
1875 static bool withinButtonRect(const guiButton_t *b)
1876 {
1877 if (mouse.x >= b->x1 && mouse.x <= b->x2 && mouse.y >= b->y1 && mouse.y <= b->y2)
1878 return true;
1879
1880 return false;
1881 }
1882
1883 #define TEST_BUTTONS(bStruct, bNum) \
1884 for (uint32_t i = 0; i < bNum; i++) \
1885 if (withinButtonRect(&bStruct[i])) \
1886 return bStruct[i].b; \
1887
1888 static int32_t checkGUIButtons(void)
1889 {
1890 // QUIT (xy 0,0) works on all screens except for ask/clear screen
1891 if (mouse.x == 0 && mouse.y == 0)
1892 return PTB_QUIT;
1893
1894 // top screen buttons
1895 if (ui.diskOpScreenShown)
1896 {
1897 TEST_BUTTONS(bDiskOp, DISKOP_BUTTONS);
1898 }
1899 else
1900 {
1901 if (ui.posEdScreenShown)
1902 {
1903 TEST_BUTTONS(bPosEd, POSED_BUTTONS);
1904 }
1905 else if (ui.editOpScreenShown)
1906 {
1907 switch (ui.editOpScreen)
1908 {
1909 default:
1910 case 0: TEST_BUTTONS(bEditOp1, EDITOP1_BUTTONS); break;
1911 case 1: TEST_BUTTONS(bEditOp2, EDITOP2_BUTTONS); break;
1912 case 2: TEST_BUTTONS(bEditOp3, EDITOP3_BUTTONS); break;
1913 case 3: TEST_BUTTONS(bEditOp4, EDITOP4_BUTTONS); break;
1914 }
1915 }
1916
1917 TEST_BUTTONS(bTopScreen, TOPSCREEN_BUTTONS);
1918 }
1919
1920 // middle buttons (always present)
1921 TEST_BUTTONS(bMidScreen, MIDSCREEN_BUTTONS);
1922
1923 // bottom screen buttons
1924 if (ui.samplerScreenShown)
1925 {
1926 TEST_BUTTONS(bSampler, SAMPLER_BUTTONS);
1927 }
1928 else
1929 {
1930 TEST_BUTTONS(bBotScreen, BOTSCREEN_BUTTONS);
1931 }
1932
1933 return -1;
1934 }
1935
1936 void mouseWheelUpHandler(void)
1937 {
1938 if (ui.editTextFlag || ui.askBoxShown || editor.swapChannelFlag ||
1939 ui.samplingBoxShown || ui.samplerVolBoxShown || ui.samplerFiltersBoxShown)
1940 return;
1941
1942 if (mouse.y < 121)
1943 {
1944 // upper part of screen
1945 if (ui.diskOpScreenShown)
1946 {
1947 if (diskop.scrollOffset > 0)
1948 {
1949 diskop.scrollOffset--;
1950 ui.updateDiskOpFileList = true;
1951 }
1952 }
1953 else if (ui.posEdScreenShown)
1954 {
1955 posEdScrollUp();
1956 }
1957 }
1958 else if (ui.samplerScreenShown) // lower part of screen
1959 {
1960 samplerZoomInMouseWheel();
1961 }
1962 else if (!editor.songPlaying && song->currRow > 0)
1963 {
1964 modSetPos(DONT_SET_ORDER, song->currRow - 1); // pattern data
1965 }
1966 }
1967
1968 void mouseWheelDownHandler(void)
1969 {
1970 if (ui.editTextFlag || ui.askBoxShown || editor.swapChannelFlag ||
1971 ui.samplingBoxShown || ui.samplerVolBoxShown || ui.samplerFiltersBoxShown)
1972 return;
1973
1974 if (mouse.y < 121)
1975 {
1976 // upper part of screen
1977 if (ui.diskOpScreenShown)
1978 {
1979 if (diskop.numEntries > DISKOP_LINES && diskop.scrollOffset < diskop.numEntries-DISKOP_LINES)
1980 {
1981 diskop.scrollOffset++;
1982 ui.updateDiskOpFileList = true;
1983 }
1984 }
1985 else if (ui.posEdScreenShown)
1986 {
1987 posEdScrollDown();
1988 }
1989 }
1990 else if (ui.samplerScreenShown) // lower part of screen
1991 {
1992 if (!ui.samplerVolBoxShown && !ui.samplerFiltersBoxShown)
1993 samplerZoomOutMouseWheel();
1994 }
1995 else if (!editor.songPlaying && song->currRow < MOD_ROWS)
1996 {
1997 modSetPos(DONT_SET_ORDER, song->currRow + 1); // pattern data
1998 }
1999 }
2000
2001 static bool handleRightMouseButton(void)
2002 {
2003 if (!mouse.rightButtonPressed)
2004 return false;
2005
2006 // exit sample swap mode with right mouse button (if present)
2007 if (editor.swapChannelFlag)
2008 {
2009 editor.swapChannelFlag = false;
2010 pointerSetPreviousMode();
2011 setPrevStatusMessage();
2012 return true;
2013 }
2014
2015 // toggle channel muting with right mouse button
2016 if (ui.visualizerMode == VISUAL_QUADRASCOPE && mouse.y >= 55 && mouse.y <= 87)
2017 {
2018 if (!ui.posEdScreenShown && !ui.editOpScreenShown && !ui.diskOpScreenShown &&
2019 !ui.aboutScreenShown && !ui.samplerVolBoxShown &&
2020 !ui.samplerFiltersBoxShown && !ui.samplingBoxShown &&
2021 !editor.mod2WavOngoing)
2022 {
2023 if (mouse.x > 127 && mouse.x <= 167) editor.muted[0] ^= 1;
2024 else if (mouse.x > 175 && mouse.x <= 215) editor.muted[1] ^= 1;
2025 else if (mouse.x > 223 && mouse.x <= 263) editor.muted[2] ^= 1;
2026 else if (mouse.x > 271 && mouse.x <= 311) editor.muted[3] ^= 1;
2027
2028 renderMuteButtons();
2029 }
2030 }
2031
2032 // sample hand drawing
2033 if (mouse.y >= 138 && mouse.y <= 201 && ui.samplerScreenShown &&
2034 !ui.samplerVolBoxShown && !ui.samplerFiltersBoxShown && !ui.samplingBoxShown)
2035 {
2036 samplerEditSample(false);
2037 }
2038
2039 return false;
2040 }
2041
2042 static bool handleLeftMouseButton(void)
2043 {
2044 if (editor.swapChannelFlag || ui.editTextFlag)
2045 return false;
2046
2047 // handle volume toolbox in sampler screen
2048 if (ui.samplerVolBoxShown)
2049 {
2050 handleSamplerVolumeBox();
2051 return true;
2052 }
2053
2054 // handle filters toolbox in sampler screen
2055 else if (ui.samplerFiltersBoxShown)
2056 {
2057 handleSamplerFiltersBox();
2058 return true;
2059 }
2060
2061 // handle sampling toolbox in sampler screen
2062 else if (ui.samplingBoxShown)
2063 {
2064 handleSamplingBox();
2065 return true;
2066 }
2067
2068 // cancel note input gadgets with left/right mouse button
2069 if (ui.changingSmpResample || ui.changingChordNote || ui.changingDrumPadNote || ui.changingSamplingNote)
2070 {
2071 if (mouse.leftButtonPressed || mouse.rightButtonPressed)
2072 {
2073 ui.changingSmpResample = false;
2074 ui.changingSamplingNote = false;
2075 ui.changingChordNote = false;
2076 ui.changingDrumPadNote = false;
2077
2078 ui.updateResampleNote = true;
2079 ui.updateChordNote1Text = true;
2080 ui.updateChordNote2Text = true;
2081 ui.updateChordNote3Text = true;
2082 ui.updateChordNote4Text = true;
2083
2084 setPrevStatusMessage();
2085 pointerSetPreviousMode();
2086 }
2087
2088 return true;
2089 }
2090
2091 if (!mouse.leftButtonPressed)
2092 return false;
2093
2094 // if MOD2WAV is ongoing, only check CANCEL button
2095 if (editor.mod2WavOngoing)
2096 {
2097 if (mouse.x >= MOD2WAV_CANCEL_BTN_X1 && mouse.x <= MOD2WAV_CANCEL_BTN_X2 &&
2098 mouse.y >= MOD2WAV_CANCEL_BTN_Y1 && mouse.y <= MOD2WAV_CANCEL_BTN_Y2)
2099 {
2100 editor.abortMod2Wav = true;
2101 }
2102
2103 return true; // don't handle other buttons
2104 }
2105
2106 // if in fullscreen mode and the image isn't filling the whole screen, handle top left corner as quit
2107 if (video.fullscreen && (video.renderX > 0 || video.renderY > 0) && (mouse.rawX == 0 && mouse.rawY == 0))
2108 return handleGUIButtons(PTB_QUIT);
2109
2110 int32_t guiButton = checkGUIButtons();
2111 if (guiButton == -1)
2112 return false;
2113
2114 return handleGUIButtons(guiButton);
2115 }
2116
2117 void updateMouseCounters(void)
2118 {
2119 if (mouse.buttonWaiting)
2120 {
2121 if (++mouse.buttonWaitCounter > VBLANK_HZ/4) // quarter of a second
2122 {
2123 mouse.buttonWaitCounter = 0;
2124 mouse.buttonWaiting = false;
2125 }
2126 }
2127
2128 if (editor.errorMsgActive)
2129 {
2130 if (++editor.errorMsgCounter >= VBLANK_HZ) // 1 second
2131 {
2132 editor.errorMsgCounter = 0;
2133
2134 // don't reset status text/mouse color during certain modes
2135 if (!ui.askBoxShown && !ui.changingChordNote && !ui.changingDrumPadNote &&
2136 !ui.changingSmpResample && !editor.swapChannelFlag && !ui.changingSamplingNote)
2137 {
2138 pointerSetPreviousMode();
2139 setPrevStatusMessage();
2140 }
2141
2142 editor.errorMsgActive = false;
2143 editor.errorMsgBlock = false;
2144
2145 if (ui.diskOpScreenShown)
2146 diskOpShowSelectText();
2147 }
2148 }
2149 }
2150
2151 static bool handleGUIButtons(int32_t button) // are you prepared to enter the jungle?
2152 {
2153 textEdit.force32BitNumPtr = false;
2154
2155 switch (button)
2156 {
2157 case PTB_DUMMY: return false; // for gaps / empty spaces / dummies
2158
2159 case PTB_PAT2SMP:
2160 {
2161 if (askBox(ASKBOX_PAT2SMP, "PLEASE SELECT"))
2162 pat2SmpRender();
2163 }
2164 break;
2165
2166 // Edit Op. All Screens
2167 case PTB_EO_TITLEBAR:
2168 {
2169 if (ui.editOpScreen == 0) editor.sampleAllFlag ^= 1;
2170 else if (ui.editOpScreen == 1) editor.trackPattFlag = (editor.trackPattFlag + 1) % 3;
2171 else if (ui.editOpScreen == 2) editor.halveSampleFlag ^= 1;
2172 else if (ui.editOpScreen == 3) editor.newOldFlag ^= 1;
2173
2174 renderEditOpMode();
2175 }
2176 break;
2177
2178 case PTB_EO_1:
2179 {
2180 ui.editOpScreen = 0;
2181 renderEditOpScreen();
2182 }
2183 break;
2184
2185 case PTB_EO_2:
2186 {
2187 ui.editOpScreen = 1;
2188 renderEditOpScreen();
2189 }
2190 break;
2191
2192 case PTB_EO_3:
2193 {
2194 ui.editOpScreen = 2;
2195 renderEditOpScreen();
2196 }
2197 break;
2198
2199 case PTB_EO_EXIT:
2200 {
2201 ui.aboutScreenShown = false;
2202 ui.editOpScreenShown = false;
2203 displayMainScreen();
2204 }
2205 break;
2206 // ----------------------------------------------------------
2207
2208 // Edit Op. Screen #1
2209 case PTB_EO_TRACK_NOTE_UP: trackNoteUp(editor.sampleAllFlag, 0, MOD_ROWS - 1); break;
2210 case PTB_EO_TRACK_NOTE_DOWN: trackNoteDown(editor.sampleAllFlag, 0, MOD_ROWS - 1); break;
2211 case PTB_EO_TRACK_OCTA_UP: trackOctaUp(editor.sampleAllFlag, 0, MOD_ROWS - 1); break;
2212 case PTB_EO_TRACK_OCTA_DOWN: trackOctaDown(editor.sampleAllFlag, 0, MOD_ROWS - 1); break;
2213 case PTB_EO_PATT_NOTE_UP: pattNoteUp(editor.sampleAllFlag); break;
2214 case PTB_EO_PATT_NOTE_DOWN: pattNoteDown(editor.sampleAllFlag); break;
2215 case PTB_EO_PATT_OCTA_UP: pattOctaUp(editor.sampleAllFlag); break;
2216 case PTB_EO_PATT_OCTA_DOWN: pattOctaDown(editor.sampleAllFlag); break;
2217 // ----------------------------------------------------------
2218
2219 // Edit Op. Screen #2
2220 case PTB_EO_RECORD:
2221 {
2222 editor.recordMode ^= 1;
2223 ui.updateRecordText = true;
2224 }
2225 break;
2226
2227 case PTB_EO_DELETE: delSampleTrack(); break;
2228 case PTB_EO_EXCHGE: exchSampleTrack(); break;
2229 case PTB_EO_COPY: copySampleTrack(); break;
2230
2231 case PTB_EO_FROM:
2232 {
2233 editor.sampleFrom = editor.currSample + 1;
2234 ui.updateFromText = true;
2235 }
2236 break;
2237
2238 case PTB_EO_TO:
2239 {
2240 editor.sampleTo = editor.currSample + 1;
2241 ui.updateToText = true;
2242 }
2243 break;
2244
2245 case PTB_EO_KILL:
2246 {
2247 if (askBox(ASKBOX_YES_NO, "KILL SAMPLE ?"))
2248 killSample();
2249 }
2250 break;
2251
2252 case PTB_EO_QUANTIZE:
2253 {
2254 textEdit.tmpDisp16 = config.quantizeValue;
2255 editor.quantizeValueDisp = &textEdit.tmpDisp16;
2256 textEdit.numPtr16 = &textEdit.tmpDisp16;
2257 textEdit.numDigits = 2;
2258 textEdit.cursorStartX = 192;
2259 textEdit.cursorStartY = 74;
2260 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_EO_QUANTIZE);
2261 }
2262 break;
2263
2264 case PTB_EO_METRO_1: // metronome speed
2265 {
2266 textEdit.tmpDisp16 = editor.metroSpeed;
2267 editor.metroSpeedDisp = &textEdit.tmpDisp16;
2268 textEdit.numPtr16 = &textEdit.tmpDisp16;
2269 textEdit.numDigits = 2;
2270 textEdit.cursorStartX = 168;
2271 textEdit.cursorStartY = 85;
2272 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_EO_METRO_1);
2273 }
2274 break;
2275
2276 case PTB_EO_METRO_2: // metronome channel
2277 {
2278 textEdit.tmpDisp16 = editor.metroChannel;
2279 editor.metroChannelDisp = &textEdit.tmpDisp16;
2280 textEdit.numPtr16 = &textEdit.tmpDisp16;
2281 textEdit.numDigits = 2;
2282 textEdit.cursorStartX = 192;
2283 textEdit.cursorStartY = 85;
2284 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_EO_METRO_2);
2285 }
2286 break;
2287
2288 case PTB_EO_FROM_NUM:
2289 {
2290 textEdit.tmpDisp8 = editor.sampleFrom;
2291 editor.sampleFromDisp = &textEdit.tmpDisp8;
2292 textEdit.numPtr8 = &textEdit.tmpDisp8;
2293 textEdit.numDigits = 2;
2294 textEdit.numBits = 8;
2295 textEdit.cursorStartX = 264;
2296 textEdit.cursorStartY = 85;
2297 enterNumberEditMode(TEXT_EDIT_HEX, PTB_EO_FROM_NUM);
2298 }
2299 break;
2300
2301 case PTB_EO_TO_NUM:
2302 {
2303 textEdit.tmpDisp8 = editor.sampleTo;
2304 editor.sampleToDisp = &textEdit.tmpDisp8;
2305 textEdit.numPtr8 = &textEdit.tmpDisp8;
2306 textEdit.numDigits = 2;
2307 textEdit.numBits = 8;
2308 textEdit.cursorStartX = 264;
2309 textEdit.cursorStartY = 96;
2310 enterNumberEditMode(TEXT_EDIT_HEX, PTB_EO_TO_NUM);
2311 }
2312 break;
2313
2314 case PTB_EO_FROM_UP:
2315 {
2316 if (editor.sampleFrom < 0x1F)
2317 {
2318 editor.sampleFrom++;
2319 ui.updateFromText = true;
2320 }
2321 }
2322 break;
2323
2324 case PTB_EO_FROM_DOWN:
2325 {
2326 if (editor.sampleFrom > 0x00)
2327 {
2328 editor.sampleFrom--;
2329 ui.updateFromText = true;
2330 }
2331 }
2332 break;
2333
2334 case PTB_EO_TO_UP:
2335 {
2336 if (editor.sampleTo < 0x1F)
2337 {
2338 editor.sampleTo++;
2339 ui.updateToText = true;
2340 }
2341 }
2342 break;
2343
2344 case PTB_EO_TO_DOWN:
2345 {
2346 if (editor.sampleTo > 0x00)
2347 {
2348 editor.sampleTo--;
2349 ui.updateToText = true;
2350 }
2351 }
2352 break;
2353
2354 case PTB_EO_KEYS:
2355 {
2356 editor.multiFlag ^= 1;
2357 ui.updateTrackerFlags = true;
2358 ui.updateKeysText = true;
2359 }
2360 break;
2361 // ----------------------------------------------------------
2362
2363 // Edit Op. Screen #3
2364
2365 case PTB_EO_MIX:
2366 {
2367 if (!mouse.rightButtonPressed)
2368 {
2369 editor.mixFlag = true;
2370 ui.updateMixText = true;
2371
2372 textEdit.textStartPtr = editor.mixText;
2373 textEdit.textEndPtr = editor.mixText + 15;
2374 textEdit.numBlocks = 16;
2375 textEdit.cursorStartX = 128;
2376 textEdit.cursorStartY = 52;
2377 textEdit.scrollable = false;
2378 enterTextEditMode(PTB_EO_MIX);
2379 }
2380 else // right mouse button = special, mix sample with itself (w/ modulation)
2381 {
2382 if (editor.sampleZero)
2383 {
2384 statusNotSampleZero();
2385 break;
2386 }
2387
2388 moduleSample_t *s = &song->samples[editor.currSample];
2389 if (s->length == 0)
2390 {
2391 statusSampleIsEmpty();
2392 break;
2393 }
2394
2395 if (editor.samplePos >= s->length)
2396 {
2397 displayErrorMsg("INVALID POS !");
2398 break;
2399 }
2400
2401 int8_t *sampleData = &song->sampleData[s->offset];
2402
2403 // make copy of sample data
2404 int8_t *sampleCopy = (int8_t *)malloc(s->length);
2405 if (sampleCopy == NULL)
2406 {
2407 statusOutOfMemory();
2408 return true;
2409 }
2410
2411 memcpy(sampleCopy, sampleData, s->length);
2412
2413 int8_t *mixPtr = sampleData + editor.samplePos;
2414 const int32_t mixLength = s->length - editor.samplePos;
2415
2416 if (editor.modulateSpeed == 0) // no modulation
2417 {
2418 for (int32_t j = 0; j < mixLength; j++)
2419 {
2420 int16_t tmp16 = mixPtr[j] + sampleCopy[j];
2421 if (editor.halveSampleFlag)
2422 tmp16 >>= 1;
2423
2424 mixPtr[j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2425 }
2426 }
2427 else // modulation
2428 {
2429 int32_t modTableOffset = 0;
2430 uint32_t modOffset = 0; // 21.11fp
2431
2432 for (int32_t j = 0; j < mixLength; j++)
2433 {
2434 const int32_t sampleReadPos = modOffset >> 11;
2435 int16_t tmp16 = mixPtr[j] + sampleCopy[MIN(sampleReadPos, s->length-1)];
2436 if (editor.halveSampleFlag)
2437 tmp16 >>= 1;
2438
2439 mixPtr[j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2440
2441 modTableOffset += editor.modulateSpeed;
2442 modOffset += modulationTable[(modTableOffset >> 12) & 63];
2443 }
2444 }
2445
2446 free(sampleCopy);
2447
2448 fixSampleBeep(s);
2449 if (ui.samplerScreenShown)
2450 displaySample();
2451
2452 updateWindowTitle(MOD_IS_MODIFIED);
2453 }
2454 }
2455 break;
2456
2457 case PTB_EO_ECHO:
2458 {
2459 if (editor.sampleZero)
2460 {
2461 statusNotSampleZero();
2462 break;
2463 }
2464
2465 moduleSample_t *s = &song->samples[editor.currSample];
2466 if (s->length == 0)
2467 {
2468 statusSampleIsEmpty();
2469 break;
2470 }
2471
2472 if (editor.samplePos == 0)
2473 {
2474 displayErrorMsg("SET SAMPLE POS !");
2475 break;
2476 }
2477
2478 if (editor.samplePos >= s->length)
2479 {
2480 displayErrorMsg("INVALID POS !");
2481 break;
2482 }
2483
2484 int8_t *sampleData = &song->sampleData[s->offset];
2485
2486 if (editor.modulateSpeed == 0) // no modulation
2487 {
2488 for (int32_t j = 0; j < s->length; j++)
2489 {
2490 int16_t tmp16 = (sampleData[editor.samplePos+j] + sampleData[j]) >> 1;
2491 sampleData[editor.samplePos+j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2492 }
2493 }
2494 else // modulation
2495 {
2496 int32_t modTableOffset = 0;
2497 uint32_t modOffset = 0; // 21.11fp
2498
2499 for (int32_t j = 0; j < s->length; j++)
2500 {
2501 const int32_t sampleReadPos = modOffset >> 11;
2502 int16_t tmp16 = (sampleData[editor.samplePos+j] + sampleData[MIN(sampleReadPos, s->length-1)]) >> 1;
2503 sampleData[editor.samplePos+j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2504
2505 modTableOffset += editor.modulateSpeed;
2506 modOffset += modulationTable[(modTableOffset >> 12) & 63];
2507 }
2508 }
2509
2510 if (!editor.halveSampleFlag)
2511 {
2512 for (int32_t j = 0; j < s->length; j++)
2513 {
2514 int16_t tmp16 = (int16_t)sampleData[j] * 2;
2515 sampleData[j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2516 }
2517 }
2518
2519 fixSampleBeep(s);
2520 if (ui.samplerScreenShown)
2521 displaySample();
2522
2523 updateWindowTitle(MOD_IS_MODIFIED);
2524 }
2525 break;
2526
2527 case PTB_EO_POS_NUM:
2528 {
2529 if (config.maxSampleLength == 65534 && mouse.x < 244) // yuck!
2530 break;
2531
2532 if (mouse.rightButtonPressed)
2533 {
2534 editor.samplePos = 0;
2535 ui.updatePosText = true;
2536 }
2537 else
2538 {
2539 textEdit.force32BitNumPtr = true;
2540
2541 textEdit.tmpDisp32 = editor.samplePos;
2542 editor.samplePosDisp = &textEdit.tmpDisp32;
2543 textEdit.numPtr32 = &textEdit.tmpDisp32;
2544
2545 if (config.maxSampleLength == 65534)
2546 {
2547 textEdit.numDigits = 4;
2548 textEdit.numBits = 16;
2549 textEdit.cursorStartX = 248;
2550 textEdit.cursorStartY = 63;
2551 }
2552 else
2553 {
2554 textEdit.numDigits = 5;
2555 textEdit.numBits = 17;
2556 textEdit.cursorStartX = 240;
2557 textEdit.cursorStartY = 63;
2558 }
2559
2560 enterNumberEditMode(TEXT_EDIT_HEX, PTB_EO_POS_NUM);
2561 }
2562 }
2563 break;
2564
2565 case PTB_EO_POS_UP: edPosUpButton(INCREMENT_SLOW); break;
2566 case PTB_EO_POS_DOWN: edPosDownButton(INCREMENT_SLOW); break;
2567
2568 case PTB_EO_BOOST: // this is actually treble increase
2569 {
2570 if (editor.sampleZero)
2571 {
2572 statusNotSampleZero();
2573 break;
2574 }
2575
2576 moduleSample_t *s = &song->samples[editor.currSample];
2577 if (s->length == 0)
2578 {
2579 statusSampleIsEmpty();
2580 break;
2581 }
2582
2583 boostSample(editor.currSample, false);
2584 if (ui.samplerScreenShown)
2585 displaySample();
2586
2587 updateWindowTitle(MOD_IS_MODIFIED);
2588 }
2589 break;
2590
2591 case PTB_EO_FILTER: // this is actually treble decrease
2592 {
2593 if (editor.sampleZero)
2594 {
2595 statusNotSampleZero();
2596 break;
2597 }
2598
2599 moduleSample_t *s = &song->samples[editor.currSample];
2600 if (s->length == 0)
2601 {
2602 statusSampleIsEmpty();
2603 break;
2604 }
2605
2606 filterSample(editor.currSample, false);
2607 if (ui.samplerScreenShown)
2608 displaySample();
2609
2610 updateWindowTitle(MOD_IS_MODIFIED);
2611 }
2612 break;
2613
2614 case PTB_EO_MOD_NUM:
2615 {
2616 if (mouse.rightButtonPressed)
2617 {
2618 editor.modulateSpeed = 0;
2619 ui.updateModText = true;
2620 }
2621 }
2622 break;
2623
2624 case PTB_EO_MOD: // modulate sample data
2625 {
2626 if (editor.sampleZero)
2627 {
2628 statusNotSampleZero();
2629 break;
2630 }
2631
2632 moduleSample_t *s = &song->samples[editor.currSample];
2633 if (s->length == 0)
2634 {
2635 statusSampleIsEmpty();
2636 break;
2637 }
2638
2639 if (editor.modulateSpeed == 0)
2640 {
2641 displayErrorMsg("SET MOD. SPEED !");
2642 break;
2643 }
2644
2645 int8_t *sampleData = &song->sampleData[s->offset];
2646
2647 // make copy of sample data
2648 int8_t *sampleCopy = (int8_t *)malloc(s->length);
2649 if (sampleCopy == NULL)
2650 {
2651 statusOutOfMemory();
2652 return true;
2653 }
2654
2655 memcpy(sampleCopy, sampleData, s->length);
2656
2657 int32_t modTableOffset = 0;
2658 uint32_t modOffset = 0; // 21.11fp
2659
2660 for (int32_t j = 0; j < s->length; j++)
2661 {
2662 const int32_t sampleReadPos = modOffset >> 11;
2663 sampleData[j] = sampleCopy[MIN(sampleReadPos, s->length-1)];
2664
2665 modTableOffset += editor.modulateSpeed;
2666 modOffset += modulationTable[(modTableOffset >> 12) & 63];
2667 }
2668
2669 free(sampleCopy);
2670
2671 fixSampleBeep(s);
2672 if (ui.samplerScreenShown)
2673 displaySample();
2674
2675 updateWindowTitle(MOD_IS_MODIFIED);
2676 }
2677 break;
2678
2679 case PTB_EO_MOD_UP: edModUpButton(); break;
2680 case PTB_EO_MOD_DOWN: edModDownButton(); break;
2681
2682 case PTB_EO_X_FADE:
2683 {
2684 if (editor.sampleZero)
2685 {
2686 statusNotSampleZero();
2687 break;
2688 }
2689
2690 moduleSample_t *s = &song->samples[editor.currSample];
2691
2692 if (s->length == 0)
2693 {
2694 statusSampleIsEmpty();
2695 break;
2696 }
2697
2698 int8_t *sampleData = &song->sampleData[s->offset];
2699 int32_t lastSamplePoint = s->length - 1;
2700
2701 for (int32_t j = 0; j < s->length / 2; j++)
2702 {
2703 int16_t tmp16 = sampleData[j] + sampleData[lastSamplePoint-j];
2704 if (editor.halveSampleFlag)
2705 tmp16 >>= 1;
2706
2707 int8_t tmpSmp = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2708
2709 sampleData[j] = tmpSmp;
2710 sampleData[lastSamplePoint-j] = tmpSmp;
2711 }
2712
2713 fixSampleBeep(s);
2714 if (ui.samplerScreenShown)
2715 displaySample();
2716
2717 updateWindowTitle(MOD_IS_MODIFIED);
2718 }
2719 break;
2720
2721 case PTB_EO_BACKWD:
2722 {
2723 int8_t *ptr8_1, *ptr8_2;
2724
2725 if (editor.sampleZero)
2726 {
2727 statusNotSampleZero();
2728 break;
2729 }
2730
2731 moduleSample_t *s = &song->samples[editor.currSample];
2732 if (s->length == 0)
2733 {
2734 statusSampleIsEmpty();
2735 break;
2736 }
2737
2738 int32_t length;
2739 if (editor.markStartOfs != -1 && editor.markStartOfs != editor.markEndOfs && editor.markEndOfs != 0)
2740 {
2741 ptr8_1 = &song->sampleData[s->offset+editor.markStartOfs];
2742 ptr8_2 = &song->sampleData[s->offset+editor.markEndOfs-1];
2743 length = editor.markEndOfs - editor.markStartOfs;
2744 }
2745 else
2746 {
2747 ptr8_1 = &song->sampleData[s->offset];
2748 ptr8_2 = &song->sampleData[s->offset+s->length-1];
2749 length = s->length;
2750 }
2751
2752 for (int32_t j = 0; j < length / 2; j++)
2753 {
2754 const int8_t tmpSmp = *ptr8_1;
2755 *ptr8_1++ = *ptr8_2;
2756 *ptr8_2-- = tmpSmp;
2757 }
2758
2759 fixSampleBeep(s);
2760 if (ui.samplerScreenShown)
2761 displaySample();
2762
2763 updateWindowTitle(MOD_IS_MODIFIED);
2764 }
2765 break;
2766
2767 case PTB_EO_CB:
2768 {
2769 if (editor.sampleZero)
2770 {
2771 statusNotSampleZero();
2772 break;
2773 }
2774
2775 moduleSample_t *s = &song->samples[editor.currSample];
2776 if (s->length == 0)
2777 {
2778 statusSampleIsEmpty();
2779 break;
2780 }
2781
2782 if (editor.samplePos == 0)
2783 {
2784 displayErrorMsg("SET SAMPLE POS !");
2785 break;
2786 }
2787
2788 if (editor.samplePos >= s->length)
2789 {
2790 displayErrorMsg("INVALID POS !");
2791 break;
2792 }
2793
2794 turnOffVoices();
2795
2796 memcpy(&song->sampleData[s->offset], &song->sampleData[s->offset + editor.samplePos], config.maxSampleLength - editor.samplePos);
2797 memset(&song->sampleData[s->offset + (config.maxSampleLength - editor.samplePos)], 0, editor.samplePos);
2798
2799 if (editor.samplePos > s->loopStart)
2800 {
2801 s->loopStart = 0;
2802 s->loopLength = 2;
2803 }
2804 else
2805 {
2806 s->loopStart = (s->loopStart - editor.samplePos) & config.maxSampleLength;
2807 }
2808
2809 s->length = (s->length - editor.samplePos) & config.maxSampleLength;
2810
2811 editor.samplePos = 0;
2812 fixSampleBeep(s);
2813 updateCurrSample();
2814
2815 updateWindowTitle(MOD_IS_MODIFIED);
2816 }
2817 break;
2818
2819 case PTB_EO_CHORD:
2820 {
2821 ui.editOpScreen = 3;
2822 renderEditOpScreen();
2823 }
2824 break;
2825
2826 // fade up
2827 case PTB_EO_FU:
2828 {
2829 if (editor.sampleZero)
2830 {
2831 statusNotSampleZero();
2832 break;
2833 }
2834
2835 moduleSample_t *s = &song->samples[editor.currSample];
2836 if (s->length == 0)
2837 {
2838 statusSampleIsEmpty();
2839 break;
2840 }
2841
2842 if (editor.samplePos == 0)
2843 {
2844 displayErrorMsg("INVALID POS !");
2845 break;
2846 }
2847
2848 double dDelta = 1.0 / editor.samplePos;
2849 double dPos = 0.0;
2850
2851 int8_t *ptr8 = &song->sampleData[s->offset];
2852 for (int32_t j = 0; j < editor.samplePos; j++)
2853 {
2854 int32_t smp32 = (int32_t)((*ptr8) * dPos);
2855 *ptr8++ = (int8_t)(CLAMP(smp32, INT8_MIN, INT8_MAX));
2856
2857 dPos += dDelta;
2858 }
2859
2860 fixSampleBeep(s);
2861 if (ui.samplerScreenShown)
2862 displaySample();
2863
2864 updateWindowTitle(MOD_IS_MODIFIED);
2865 }
2866 break;
2867
2868 // fade down
2869 case PTB_EO_FD:
2870 {
2871 if (editor.sampleZero)
2872 {
2873 statusNotSampleZero();
2874 break;
2875 }
2876
2877 moduleSample_t *s = &song->samples[editor.currSample];
2878 if (s->length == 0)
2879 {
2880 statusSampleIsEmpty();
2881 break;
2882 }
2883
2884 if (editor.samplePos >= s->length-1)
2885 {
2886 displayErrorMsg("INVALID POS !");
2887 break;
2888 }
2889
2890 int32_t tmp32 = (s->length - 1) - editor.samplePos;
2891 if (tmp32 == 0)
2892 tmp32 = 1;
2893
2894 double dDelta = 1.0 / tmp32;
2895 double dPos = 0.0;
2896
2897 int8_t *ptr8 = &song->sampleData[s->offset+s->length-1];
2898 for (int32_t j = editor.samplePos; j < s->length; j++)
2899 {
2900 int32_t smp32 = (int32_t)((*ptr8) * dPos);
2901 *ptr8-- = (int8_t)(CLAMP(smp32, INT8_MIN, INT8_MAX));
2902
2903 dPos += dDelta;
2904 }
2905
2906 fixSampleBeep(s);
2907 if (ui.samplerScreenShown)
2908 displaySample();
2909
2910 updateWindowTitle(MOD_IS_MODIFIED);
2911 }
2912 break;
2913
2914 case PTB_EO_UPSAMP:
2915 {
2916 moduleSample_t *s = &song->samples[editor.currSample];
2917 if (s->length == 0)
2918 {
2919 statusSampleIsEmpty();
2920 break;
2921 }
2922
2923 if (askBox(ASKBOX_YES_NO, "UPSAMPLE ?"))
2924 upSample();
2925 }
2926 break;
2927
2928 case PTB_EO_DNSAMP:
2929 {
2930 moduleSample_t *s = &song->samples[editor.currSample];
2931 if (s->length == 0)
2932 {
2933 statusSampleIsEmpty();
2934 break;
2935 }
2936
2937 if (askBox(ASKBOX_YES_NO, "DOWNSAMPLE ?"))
2938 downSample();
2939 }
2940 break;
2941
2942 case PTB_EO_VOL_NUM:
2943 {
2944 if (mouse.rightButtonPressed)
2945 {
2946 editor.sampleVol = 100;
2947 ui.updateVolText = true;
2948 }
2949 else
2950 {
2951 textEdit.tmpDisp16 = editor.sampleVol;
2952 editor.sampleVolDisp = &textEdit.tmpDisp16;
2953 textEdit.numPtr16 = &textEdit.tmpDisp16;
2954 textEdit.numDigits = 3;
2955 textEdit.cursorStartX = 248;
2956 textEdit.cursorStartY = 96;
2957 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_EO_VOL_NUM);
2958 }
2959 }
2960 break;
2961
2962 case PTB_EO_VOL:
2963 {
2964 if (editor.sampleZero)
2965 {
2966 statusNotSampleZero();
2967 break;
2968 }
2969
2970 moduleSample_t *s = &song->samples[editor.currSample];
2971 if (s->length == 0)
2972 {
2973 statusSampleIsEmpty();
2974 break;
2975 }
2976
2977 if (editor.sampleVol != 100)
2978 {
2979 int8_t *ptr8 = &song->sampleData[s->offset];
2980 int32_t sampleMul = (((1UL << 19) * editor.sampleVol) + 50) / 100;
2981
2982 for (int32_t j = 0; j < s->length; j++)
2983 {
2984 int16_t tmp16 = (ptr8[j] * sampleMul) >> 19;
2985 ptr8[j] = (int8_t)(CLAMP(tmp16, INT8_MIN, INT8_MAX));
2986 }
2987
2988 fixSampleBeep(s);
2989 if (ui.samplerScreenShown)
2990 displaySample();
2991
2992 updateWindowTitle(MOD_IS_MODIFIED);
2993 }
2994 }
2995 break;
2996
2997 case PTB_EO_VOL_UP: edVolUpButton(); break;
2998 case PTB_EO_VOL_DOWN: edVolDownButton(); break;
2999 // ----------------------------------------------------------
3000
3001 // Edit Op. Screen #4 (chord maker)
3002
3003 case PTB_EO_DOCHORD:
3004 {
3005 if (askBox(ASKBOX_YES_NO, "MAKE CHORD?"))
3006 mixChordSample();
3007 }
3008 break;
3009
3010 case PTB_EO_NOTE1: selectChordNote1(); break;
3011 case PTB_EO_NOTE2: selectChordNote2(); break;
3012 case PTB_EO_NOTE3: selectChordNote3(); break;
3013 case PTB_EO_NOTE4: selectChordNote4(); break;
3014 case PTB_EO_NOTE1_UP: edNote1UpButton(); break;
3015 case PTB_EO_NOTE1_DOWN: edNote1DownButton(); break;
3016 case PTB_EO_NOTE2_UP: edNote2UpButton(); break;
3017 case PTB_EO_NOTE2_DOWN: edNote2DownButton(); break;
3018 case PTB_EO_NOTE3_UP: edNote3UpButton(); break;
3019 case PTB_EO_NOTE3_DOWN: edNote3DownButton(); break;
3020 case PTB_EO_NOTE4_UP: edNote4UpButton(); break;
3021 case PTB_EO_NOTE4_DOWN: edNote4DownButton(); break;
3022 case PTB_EO_RESET: resetChord(); break;
3023 case PTB_EO_UNDO: undoChord(); break;
3024
3025 case PTB_EO_LENGTH:
3026 {
3027 if (config.maxSampleLength != 65534 && mouse.x > 157) // yuck!
3028 break;
3029
3030 toggleChordLength();
3031 }
3032 break;
3033
3034 case PTB_EO_MAJOR: setChordMajor(); break;
3035 case PTB_EO_MINOR: setChordMinor(); break;
3036 case PTB_EO_SUS4: setChordSus4(); break;
3037 case PTB_EO_MAJOR7: setChordMajor7(); break;
3038 case PTB_EO_MINOR7: setChordMinor7(); break;
3039 case PTB_EO_MAJOR6: setChordMajor6(); break;
3040 case PTB_EO_MINOR6: setChordMinor6(); break;
3041 // ----------------------------------------------------------
3042
3043 case PTB_ABOUT:
3044 {
3045 ui.aboutScreenShown ^= 1;
3046
3047 if (ui.aboutScreenShown)
3048 renderAboutScreen();
3049 else if (ui.visualizerMode == VISUAL_QUADRASCOPE)
3050 renderQuadrascopeBg();
3051 else if (ui.visualizerMode == VISUAL_SPECTRUM)
3052 renderSpectrumAnalyzerBg();
3053 }
3054 break;
3055
3056 case PTB_PE_PATT:
3057 {
3058 if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)
3059 {
3060 textEdit.tmpDisp16 = song->currPos;
3061 if (textEdit.tmpDisp16 > song->header.songLength-1)
3062 textEdit.tmpDisp16 = song->header.songLength-1;
3063
3064 textEdit.tmpDisp16 = song->header.patternTable[textEdit.tmpDisp16];
3065 editor.currPosEdPattDisp = &textEdit.tmpDisp16;
3066 textEdit.numPtr16 = &textEdit.tmpDisp16;
3067 textEdit.numDigits = 2;
3068 textEdit.cursorStartX = 160;
3069 textEdit.cursorStartY = 58;
3070 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_PE_PATT);
3071 }
3072 }
3073 break;
3074
3075 case PTB_PE_PATTNAME: posEdEditName(); break;
3076 case PTB_PE_SCROLLTOP: posEdScrollToTop(); break;
3077 case PTB_PE_SCROLLUP: posEdScrollUp(); break;
3078 case PTB_PE_SCROLLDOWN: posEdScrollDown(); break;
3079 case PTB_PE_SCROLLBOT: posEdScrollToBottom(); break;
3080
3081 case PTB_PE_EXIT:
3082 {
3083 ui.aboutScreenShown = false;
3084 ui.posEdScreenShown = false;
3085 displayMainScreen();
3086 }
3087 break;
3088
3089 case PTB_POS:
3090 case PTB_POSED:
3091 posEdToggle();
3092 break;
3093
3094 case PTB_POSS:
3095 {
3096 if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)
3097 {
3098 if (mouse.rightButtonPressed)
3099 {
3100 song->currPos = 0;
3101 editor.currPatternDisp = &song->header.patternTable[song->currPos];
3102
3103 if (ui.posEdScreenShown)
3104 ui.updatePosEd = true;
3105 }
3106 else
3107 {
3108 textEdit.tmpDisp16 = song->currPos;
3109 editor.currPosDisp = &textEdit.tmpDisp16;
3110 textEdit.numPtr16 = &textEdit.tmpDisp16;
3111 textEdit.numDigits = 3;
3112 textEdit.cursorStartX = 72;
3113 textEdit.cursorStartY = 8;
3114 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_POSS);
3115 }
3116 }
3117 }
3118 break;
3119
3120 case PTB_PATTERNS:
3121 {
3122 if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)
3123 {
3124 if (mouse.rightButtonPressed)
3125 {
3126 song->header.patternTable[song->currPos] = 0;
3127
3128 ui.updateSongSize = true;
3129 updateWindowTitle(MOD_IS_MODIFIED);
3130
3131 if (ui.posEdScreenShown)
3132 ui.updatePosEd = true;
3133 }
3134 else
3135 {
3136 textEdit.tmpDisp16 = song->header.patternTable[song->currPos];
3137 editor.currPatternDisp = &textEdit.tmpDisp16;
3138 textEdit.numPtr16 = &textEdit.tmpDisp16;
3139 textEdit.numDigits = 2;
3140 textEdit.cursorStartX = 80;
3141 textEdit.cursorStartY = 19;
3142 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_PATTERNS);
3143 }
3144 }
3145 }
3146 break;
3147
3148 case PTB_LENGTHS:
3149 {
3150 if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)
3151 {
3152 if (mouse.rightButtonPressed)
3153 {
3154 song->header.songLength = 1;
3155
3156 int16_t tmp16 = song->currPos;
3157 if (tmp16 > song->header.songLength-1)
3158 tmp16 = song->header.songLength-1;
3159
3160 editor.currPosEdPattDisp = &song->header.patternTable[tmp16];
3161
3162 ui.updateSongSize = true;
3163 updateWindowTitle(MOD_IS_MODIFIED);
3164
3165 if (ui.posEdScreenShown)
3166 ui.updatePosEd = true;
3167 }
3168 else
3169 {
3170 textEdit.tmpDisp16 = song->header.songLength;
3171 editor.currLengthDisp = &textEdit.tmpDisp16;
3172 textEdit.numPtr16 = &textEdit.tmpDisp16;
3173 textEdit.numDigits = 3;
3174 textEdit.cursorStartX = 72;
3175 textEdit.cursorStartY = 30;
3176 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_LENGTHS);
3177 }
3178 }
3179 }
3180 break;
3181
3182 // pattern editor main area (sets current pattern number to edit)
3183 case PTB_PATTBOX:
3184 case PTB_PATTDATA:
3185 {
3186 if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT || editor.playMode != PLAY_MODE_NORMAL)
3187 {
3188 // limit click coordinate to pattern edit number box if needed
3189 if (config.restrictedPattEditClick && (mouse.x < 6 || mouse.y < 124 || mouse.x >= 26 || mouse.y >= 135))
3190 break;
3191
3192 textEdit.tmpDisp16 = song->currPattern;
3193 editor.currEditPatternDisp = &textEdit.tmpDisp16;
3194 textEdit.numPtr16 = &textEdit.tmpDisp16;
3195 textEdit.numDigits = 2;
3196 textEdit.cursorStartX = 8;
3197 textEdit.cursorStartY = 132;
3198 enterNumberEditMode(TEXT_EDIT_DECIMAL, PTB_PATTDATA);
3199 }
3200 }
3201 break;
3202
3203 case PTB_SAMPLES:
3204 {
3205 if (editor.sampleZero)
3206 {
3207 editor.sampleZero = false;
3208 ui.updateCurrSampleNum = true;
3209 }
3210
3211 textEdit.tmpDisp8 = editor.currSample;
3212 editor.currSampleDisp = &textEdit.tmpDisp8;
3213 textEdit.numPtr8 = &textEdit.tmpDisp8;
3214 textEdit.numDigits = 2;
3215 textEdit.numBits = 8;
3216 textEdit.cursorStartX = 80;
3217 textEdit.cursorStartY = 52;
3218 enterNumberEditMode(TEXT_EDIT_HEX, PTB_SAMPLES);
3219 }
3220 break;
3221
3222 case PTB_SVOLUMES:
3223 {
3224 if (editor.sampleZero)
3225 {
3226 statusNotSampleZero();
3227 break;
3228 }
3229
3230 if (mouse.rightButtonPressed)
3231 {
3232 song->samples[editor.currSample].volume = 0;
3233 }
3234 else
3235 {
3236 textEdit.tmpDisp8 = song->samples[editor.currSample].volume;
3237 song->samples[editor.currSample].volumeDisp = &textEdit.tmpDisp8;
3238 textEdit.numPtr8 = &textEdit.tmpDisp8;
3239 textEdit.numDigits = 2;
3240 textEdit.numBits = 8;
3241 textEdit.cursorStartX = 80;
3242 textEdit.cursorStartY = 63;
3243 enterNumberEditMode(TEXT_EDIT_HEX, PTB_SVOLUMES);
3244 }
3245 }
3246 break;
3247
3248 case PTB_SLENGTHS:
3249 {
3250 if (config.maxSampleLength == 65534 && mouse.x < 62) // yuck!
3251 break;
3252
3253 if (editor.sampleZero)
3254 {
3255 statusNotSampleZero();
3256 break;
3257 }
3258
3259 if (mouse.rightButtonPressed)
3260 {
3261 moduleSample_t *s = &song->samples[editor.currSample];
3262
3263 turnOffVoices();
3264
3265 s->length = 0;
3266 if (s->loopStart+s->loopLength > 2)
3267 {
3268 if (s->length < s->loopStart+s->loopLength)
3269 s->length = s->loopStart+s->loopLength;
3270 }
3271
3272 ui.updateSongSize = true;
3273 ui.updateCurrSampleLength = true;
3274
3275 if (ui.samplerScreenShown)
3276 redrawSample();
3277
3278 recalcChordLength();
3279 updateWindowTitle(MOD_IS_MODIFIED);
3280 }
3281 else
3282 {
3283 textEdit.force32BitNumPtr = true;
3284
3285 textEdit.tmpDisp32 = song->samples[editor.currSample].length;
3286 song->samples[editor.currSample].lengthDisp = &textEdit.tmpDisp32;
3287 textEdit.numPtr32 = &textEdit.tmpDisp32;
3288
3289 if (config.maxSampleLength == 65534)
3290 {
3291 textEdit.numDigits = 4;
3292 textEdit.numBits = 16;
3293 textEdit.cursorStartX = 64;
3294 textEdit.cursorStartY = 74;
3295 }
3296 else
3297 {
3298 textEdit.numDigits = 5;
3299 textEdit.numBits = 17;
3300 textEdit.cursorStartX = 56;
3301 textEdit.cursorStartY = 74;
3302 }
3303
3304 enterNumberEditMode(TEXT_EDIT_HEX, PTB_SLENGTHS);
3305 }
3306 }
3307 break;
3308
3309 case PTB_SREPEATS:
3310 {
3311 if (config.maxSampleLength == 65534 && mouse.x < 62) // yuck!
3312 break;
3313
3314 if (editor.sampleZero)
3315 {
3316 statusNotSampleZero();
3317 break;
3318 }
3319
3320 if (mouse.rightButtonPressed)
3321 {
3322 moduleSample_t *s = &song->samples[editor.currSample];
3323
3324 s->loopStart = 0;
3325 if (s->length >= s->loopLength)
3326 {
3327 if (s->loopStart+s->loopLength > s->length)
3328 s->loopStart = s->length-s->loopLength;
3329 }
3330 else
3331 {
3332 s->loopStart = 0;
3333 }
3334
3335 ui.updateCurrSampleRepeat = true;
3336 if (ui.editOpScreenShown && ui.editOpScreen == 3)
3337 ui.updateChordLengthText = true;
3338
3339 if (ui.samplerScreenShown)
3340 setLoopSprites();
3341
3342 updatePaulaLoops();
3343 updateWindowTitle(MOD_IS_MODIFIED);
3344 }
3345 else
3346 {
3347 textEdit.force32BitNumPtr = true;
3348
3349 textEdit.tmpDisp32 = song->samples[editor.currSample].loopStart;
3350 song->samples[editor.currSample].loopStartDisp = &textEdit.tmpDisp32;
3351 textEdit.numPtr32 = &textEdit.tmpDisp32;
3352
3353 if (config.maxSampleLength == 65534)
3354 {
3355 textEdit.numDigits = 4;
3356 textEdit.numBits = 16;
3357 textEdit.cursorStartX = 64;
3358 textEdit.cursorStartY = 85;
3359 }
3360 else
3361 {
3362 textEdit.numDigits = 5;
3363 textEdit.numBits = 17;
3364 textEdit.cursorStartX = 56;
3365 textEdit.cursorStartY = 85;
3366 }
3367
3368 enterNumberEditMode(TEXT_EDIT_HEX, PTB_SREPEATS);
3369 }
3370 }
3371 break;
3372
3373 case PTB_SREPLENS:
3374 {
3375 if (config.maxSampleLength == 65534 && mouse.x < 62) // yuck!
3376 break;
3377
3378 if (editor.sampleZero)
3379 {
3380 statusNotSampleZero();
3381 break;
3382 }
3383
3384 if (mouse.rightButtonPressed)
3385 {
3386 moduleSample_t *s = &song->samples[editor.currSample];
3387
3388 s->loopLength = 0;
3389 if (s->length >= s->loopStart)
3390 {
3391 if (s->loopStart+s->loopLength > s->length)
3392 s->loopLength = s->length-s->loopStart;
3393 }
3394 else
3395 {
3396 s->loopLength = 2;
3397 }
3398
3399 if (s->loopLength < 2)
3400 s->loopLength = 2;
3401
3402 ui.updateCurrSampleReplen = true;
3403 if (ui.editOpScreenShown && ui.editOpScreen == 3)
3404 ui.updateChordLengthText = true;
3405
3406 if (ui.samplerScreenShown)
3407 setLoopSprites();
3408
3409 updatePaulaLoops();
3410 updateWindowTitle(MOD_IS_MODIFIED);
3411 }
3412 else
3413 {
3414 textEdit.force32BitNumPtr = true;
3415
3416 textEdit.tmpDisp32 = song->samples[editor.currSample].loopLength;
3417 song->samples[editor.currSample].loopLengthDisp = &textEdit.tmpDisp32;
3418 textEdit.numPtr32 = &textEdit.tmpDisp32;
3419
3420 if (config.maxSampleLength == 0xFFFE)
3421 {
3422 textEdit.numDigits = 4;
3423 textEdit.numBits = 16;
3424 textEdit.cursorStartX = 64;
3425 textEdit.cursorStartY = 96;
3426 }
3427 else
3428 {
3429 textEdit.numDigits = 5;
3430 textEdit.numBits = 17;
3431 textEdit.cursorStartX = 56;
3432 textEdit.cursorStartY = 96;
3433 }
3434
3435 enterNumberEditMode(TEXT_EDIT_HEX, PTB_SREPLENS);
3436 }
3437 }
3438 break;
3439
3440 case PTB_EDITOP:
3441 {
3442 if (ui.editOpScreenShown)
3443 {
3444 if (ui.editOpScreen == 3)
3445 ui.editOpScreen = 0;
3446 else
3447 ui.editOpScreen = (ui.editOpScreen + 1) % 3;
3448 }
3449 else
3450 {
3451 ui.editOpScreenShown = true;
3452 }
3453
3454 renderEditOpScreen();
3455 }
3456 break;
3457
3458 case PTB_DO_LOADMODULE:
3459 {
3460 diskop.mode = DISKOP_MODE_MOD;
3461 setPathFromDiskOpMode();
3462 diskop.scrollOffset = 0;
3463 diskop.cached = false;
3464 ui.updateDiskOpFileList = true;
3465 ui.updateLoadMode = true;
3466 }
3467 break;
3468
3469 case PTB_DO_LOADSAMPLE:
3470 {
3471 diskop.mode = DISKOP_MODE_SMP;
3472 setPathFromDiskOpMode();
3473 diskop.scrollOffset = 0;
3474 diskop.cached = false;
3475 ui.updateDiskOpFileList = true;
3476 ui.updateLoadMode = true;
3477 }
3478 break;
3479
3480 case PTB_LOADSAMPLE: // "LOAD" button next to sample name
3481 {
3482 ui.posEdScreenShown = false;
3483 diskop.mode = DISKOP_MODE_SMP;
3484 setPathFromDiskOpMode();
3485 diskop.scrollOffset = 0;
3486 diskop.cached = false;
3487
3488 if (!ui.diskOpScreenShown)
3489 {
3490 ui.diskOpScreenShown = true;
3491 renderDiskOpScreen();
3492 }
3493 else
3494 {
3495 ui.updateDiskOpFileList = true;
3496 ui.updateLoadMode = true;
3497 }
3498 }
3499 break;
3500
3501 case PTB_DO_SAVESAMPLE:
3502 {
3503 bool changeLoadMode = (diskop.mode != DISKOP_MODE_SMP);
3504 if (changeLoadMode)
3505 {
3506 diskop.mode = DISKOP_MODE_SMP;
3507 setPathFromDiskOpMode();
3508 ui.updateLoadMode = true; // redraw load mode cursor
3509 updateDiskOp();
3510 }
3511
3512 if (askBox(ASKBOX_YES_NO, "SAVE SAMPLE ?"))
3513 saveSample(CHECK_IF_FILE_EXIST, DONT_GIVE_NEW_FILENAME); // also updates file list
3514
3515 // if we changed mode, re-read directory in Disk Op.
3516 if (changeLoadMode)
3517 {
3518 diskop.scrollOffset = 0;
3519 diskop.cached = false; // read new directory
3520 ui.updateDiskOpFileList = true;
3521 updateDiskOp();
3522 }
3523 }
3524 break;
3525
3526 case PTB_MOD2WAV:
3527 {
3528 if (askBox(ASKBOX_MOD2WAV, "PLEASE SELECT"))
3529 {
3530 char fileName[20 + 4 + 1];
3531
3532 memset(fileName, 0, sizeof (fileName));
3533
3534 if (song->header.name[0] != '\0')
3535 {
3536 for (int32_t i = 0; i < 20; i++)
3537 {
3538 fileName[i] = (char)tolower(song->header.name[i]);
3539 if (fileName[i] == '\0') break;
3540 sanitizeFilenameChar(&fileName[i]);
3541 }
3542
3543 strcat(fileName, ".wav");
3544 }
3545 else
3546 {
3547 strcpy(fileName, "untitled.wav");
3548 }
3549
3550 mod2WavRender(fileName);
3551 }
3552 }
3553 break;
3554
3555 case PTB_SA_SAMPLE:
3556 {
3557 ui.samplingBoxShown = true;
3558 renderSamplingBox();
3559 }
3560 break;
3561
3562 case PTB_SA_RESAMPLE:
3563 {
3564 if (askBox(ASKBOX_YES_NO, "RESAMPLE?"))
3565 samplerResample();
3566 }
3567 break;
3568
3569 case PTB_SA_RESAMPLENOTE:
3570 {
3571 ui.changingSmpResample = true;
3572 ui.updateResampleNote = true;
3573 setStatusMessage("SELECT NOTE", NO_CARRY);
3574 pointerSetMode(POINTER_MODE_MSG1, NO_CARRY);
3575 }
3576 break;
3577
3578 case PTB_SA_SAMPLEAREA:
3579 {
3580 if (ui.sampleMarkingPos == -1)
3581 {
3582 samplerSamplePressed(MOUSE_BUTTON_NOT_HELD);
3583 return true;
3584 }
3585 }
3586 break;
3587
3588 case PTB_SA_ZOOMBARAREA:
3589 {
3590 mouse.lastGUIButton = button;
3591 if (!ui.forceSampleDrag)
3592 {
3593 samplerBarPressed(MOUSE_BUTTON_NOT_HELD);
3594 return true;
3595 }
3596 }
3597 break;
3598
3599 case PTB_SA_FIXDC: samplerRemoveDcOffset(); break;
3600 case PTB_SA_CUT: samplerSamDelete(SAMPLE_CUT); break;
3601 case PTB_SA_PASTE: samplerSamPaste(); break;
3602 case PTB_SA_COPY: samplerSamCopy(); break;
3603 case PTB_SA_LOOP: samplerLoopToggle(); break;
3604 case PTB_SA_PLAYWAVE: samplerPlayWaveform(); break;
3605 case PTB_SA_PLAYDISPLAY: samplerPlayDisplay(); break;
3606 case PTB_SA_PLAYRANGE: samplerPlayRange(); break;
3607 case PTB_SA_RANGEALL: samplerRangeAll(); break;
3608 case PTB_SA_SHOWALL: samplerShowAll(); break;
3609 case PTB_SA_SHOWRANGE: samplerShowRange(); break;
3610 case PTB_SA_RANGECENTER: sampleMarkerToCenter(); break;
3611 case PTB_SA_RANGEBEG: sampleMarkerToBeg(); break;
3612 case PTB_SA_RANGEEND: sampleMarkerToEnd(); break;
3613 case PTB_SA_ZOOMOUT: samplerZoomOut2x(); break;
3614
3615 case PTB_SA_VOLUME:
3616 {
3617 ui.samplerVolBoxShown = true;
3618 renderSamplerVolBox();
3619 }
3620 break;
3621
3622 case PTB_SA_FILTERS:
3623 {
3624 ui.samplerFiltersBoxShown = true;
3625 renderSamplerFiltersBox();
3626 fillSampleFilterUndoBuffer();
3627 }
3628 break;
3629
3630 case PTB_SA_STOP: turnOffVoices(); break;
3631
3632 case PTB_DO_REFRESH:
3633 {
3634 diskop.scrollOffset = 0;
3635 diskop.cached = false;
3636 ui.updateDiskOpFileList = true;
3637 }
3638 break;
3639
3640 // TODO: Find a PowerPacker packer and enable this
3641 // case PTB_DO_PACKMOD: diskop.modPackFlg ^= 1; break;
3642
3643 case PTB_DO_SAMPLEFORMAT:
3644 {
3645 diskop.smpSaveType = (diskop.smpSaveType + 1) % 3;
3646 ui.updateSaveFormatText = true;
3647 }
3648 break;
3649
3650 case PTB_DO_MODARROW:
3651 {
3652 diskop.mode = DISKOP_MODE_MOD;
3653 diskop.scrollOffset = 0;
3654 diskop.cached = false;
3655 ui.updateDiskOpFileList = true;
3656 ui.updateLoadMode = true;
3657 }
3658 break;
3659
3660 case PTB_DO_SAMPLEARROW:
3661 {
3662 diskop.mode = DISKOP_MODE_SMP;
3663 diskop.scrollOffset = 0;
3664 diskop.cached = false;
3665 ui.updateDiskOpFileList = true;
3666 ui.updateLoadMode = true;
3667 }
3668 break;
3669
3670 case PTB_SA_TUNETONE: toggleTuningTone(); break;
3671
3672 case PTB_POSINS: posEdInsert(); break;
3673 case PTB_POSDEL: posEdDelete(); break;
3674
3675 case PTB_DO_SAVEMODULE:
3676 {
3677 bool changeLoadMode = (diskop.mode != DISKOP_MODE_MOD);
3678 if (changeLoadMode)
3679 {
3680 diskop.mode = DISKOP_MODE_MOD;
3681 setPathFromDiskOpMode();
3682 ui.updateLoadMode = true; // redraw load mode cursor
3683 updateDiskOp();
3684 }
3685
3686 if (askBox(ASKBOX_YES_NO, "SAVE MODULE ?"))
3687 saveModule(CHECK_IF_FILE_EXIST, DONT_GIVE_NEW_FILENAME); // also updates file list
3688
3689 // if we changed mode, re-read directory in Disk Op.
3690 if (changeLoadMode)
3691 {
3692 diskop.scrollOffset = 0;
3693 diskop.cached = false; // read new directory
3694 ui.updateDiskOpFileList = true;
3695 updateDiskOp();
3696 }
3697 }
3698 break;
3699
3700 case PTB_DO_DATAPATH:
3701 {
3702 if (mouse.rightButtonPressed)
3703 {
3704 memset(editor.currPath, 0, PATH_MAX + 1);
3705 ui.updateDiskOpPathText = true;
3706 }
3707
3708 textEdit.textStartPtr = editor.currPath;
3709 textEdit.textEndPtr = &editor.currPath[PATH_MAX - 1];
3710 textEdit.numBlocks = 26;
3711 textEdit.cursorStartX = 24;
3712 textEdit.cursorStartY = 30;
3713 textEdit.scrollable = true;
3714 enterTextEditMode(PTB_DO_DATAPATH);
3715 }
3716 break;
3717
3718 case PTB_SONGNAME:
3719 {
3720 if (mouse.rightButtonPressed)
3721 {
3722 memset(song->header.name, 0, sizeof (song->header.name));
3723 ui.updateSongName = true;
3724 updateWindowTitle(MOD_IS_MODIFIED);
3725 }
3726 else
3727 {
3728 textEdit.textStartPtr = song->header.name;
3729 textEdit.textEndPtr = song->header.name + 19;
3730 textEdit.numBlocks = 20;
3731 textEdit.cursorStartX = 104;
3732 textEdit.cursorStartY = 107;
3733 textEdit.scrollable = false;
3734 enterTextEditMode(PTB_SONGNAME);
3735 }
3736 }
3737 break;
3738
3739 case PTB_SAMPLENAME:
3740 {
3741 if (mouse.rightButtonPressed)
3742 {
3743 memset(song->samples[editor.currSample].text, 0, sizeof (song->samples[editor.currSample].text));
3744 ui.updateCurrSampleName = true;
3745 updateWindowTitle(MOD_IS_MODIFIED);
3746 }
3747 else
3748 {
3749 textEdit.textStartPtr = song->samples[editor.currSample].text;
3750 textEdit.textEndPtr = song->samples[editor.currSample].text + 21;
3751 textEdit.numBlocks = 22;
3752 textEdit.cursorStartX = 104;
3753 textEdit.cursorStartY = 118;
3754 textEdit.scrollable = false;
3755 enterTextEditMode(PTB_SAMPLENAME);
3756 }
3757 }
3758 break;
3759
3760 case PTB_VISUALS:
3761 {
3762 if (ui.aboutScreenShown)
3763 {
3764 ui.aboutScreenShown = false;
3765 }
3766 else if (!mouse.rightButtonPressed)
3767 {
3768 ui.visualizerMode = (ui.visualizerMode + 1) % 2;
3769 if (ui.visualizerMode == VISUAL_SPECTRUM)
3770 memset((int8_t *)editor.spectrumVolumes, 0, sizeof (editor.spectrumVolumes));
3771 }
3772
3773 if (ui.visualizerMode == VISUAL_QUADRASCOPE)
3774 renderQuadrascopeBg();
3775 else if (ui.visualizerMode == VISUAL_SPECTRUM)
3776 renderSpectrumAnalyzerBg();
3777 }
3778 break;
3779
3780 case PTB_QUIT:
3781 {
3782 if (askBox(ASKBOX_YES_NO, "REALLY QUIT ?"))
3783 ui.throwExit = true;
3784 }
3785 break;
3786
3787 case PTB_CHAN1:
3788 {
3789 if (mouse.rightButtonPressed)
3790 {
3791 editor.muted[0] = false;
3792 editor.muted[1] = true;
3793 editor.muted[2] = true;
3794 editor.muted[3] = true;
3795 }
3796 else
3797 {
3798 editor.muted[0] ^= 1;
3799 }
3800
3801 renderMuteButtons();
3802 }
3803 break;
3804
3805 case PTB_CHAN2:
3806 {
3807 if (mouse.rightButtonPressed)
3808 {
3809 editor.muted[0] = true;
3810 editor.muted[1] = false;
3811 editor.muted[2] = true;
3812 editor.muted[3] = true;
3813 }
3814 else
3815 {
3816 editor.muted[1] ^= 1;
3817 }
3818
3819 renderMuteButtons();
3820 }
3821 break;
3822
3823 case PTB_CHAN3:
3824 {
3825 if (mouse.rightButtonPressed)
3826 {
3827 editor.muted[0] = true;
3828 editor.muted[1] = true;
3829 editor.muted[2] = false;
3830 editor.muted[3] = true;
3831 }
3832 else
3833 {
3834 editor.muted[2] ^= 1;
3835 }
3836
3837 renderMuteButtons();
3838 }
3839 break;
3840
3841 case PTB_CHAN4:
3842 {
3843 if (mouse.rightButtonPressed)
3844 {
3845 editor.muted[0] = true;
3846 editor.muted[1] = true;
3847 editor.muted[2] = true;
3848 editor.muted[3] = false;
3849 }
3850 else
3851 {
3852 editor.muted[3] ^= 1;
3853 }
3854
3855 renderMuteButtons();
3856 }
3857 break;
3858
3859 case PTB_SAMPLER: samplerScreen(); break;
3860 case PTB_SA_EXIT: exitFromSam(); break;
3861
3862 case PTB_DO_FILEAREA: diskOpLoadFile((mouse.y - 34) / 6, true); break;
3863 case PTB_DO_PARENT:
3864 {
3865 #ifdef _WIN32
3866 diskOpSetPath(L"..", DISKOP_CACHE);
3867 #else
3868 diskOpSetPath("..", DISKOP_CACHE);
3869 #endif
3870 }
3871 break;
3872
3873 case PTB_DISKOP:
3874 {
3875 editor.blockMarkFlag = false;
3876 ui.diskOpScreenShown = true;
3877 renderDiskOpScreen();
3878 }
3879 break;
3880
3881 case PTB_DO_EXIT:
3882 {
3883 ui.aboutScreenShown = false;
3884 ui.diskOpScreenShown = false;
3885 editor.blockMarkFlag = false;
3886 pointerSetPreviousMode();
3887 setPrevStatusMessage();
3888 displayMainScreen();
3889 }
3890 break;
3891
3892 case PTB_DO_SCROLLUP:
3893 {
3894 if (diskop.scrollOffset > 0)
3895 {
3896 diskop.scrollOffset--;
3897 ui.updateDiskOpFileList = true;
3898 }
3899 }
3900 break;
3901
3902 case PTB_DO_SCROLLTOP:
3903 {
3904 diskop.scrollOffset = 0;
3905 ui.updateDiskOpFileList = true;
3906 }
3907 break;
3908
3909 case PTB_DO_SCROLLDOWN:
3910 {
3911 if (diskop.numEntries > DISKOP_LINES && diskop.scrollOffset < diskop.numEntries-DISKOP_LINES)
3912 {
3913 diskop.scrollOffset++;
3914 ui.updateDiskOpFileList = true;
3915 }
3916 }
3917 break;
3918
3919 case PTB_DO_SCROLLBOT:
3920 {
3921 if (diskop.numEntries > DISKOP_LINES)
3922 {
3923 diskop.scrollOffset = diskop.numEntries - DISKOP_LINES;
3924 ui.updateDiskOpFileList = true;
3925 }
3926 }
3927 break;
3928
3929 case PTB_STOP:
3930 {
3931 editor.playMode = PLAY_MODE_NORMAL;
3932 modStop();
3933 editor.currMode = MODE_IDLE;
3934 pointerSetMode(POINTER_MODE_IDLE, DO_CARRY);
3935 statusAllRight();
3936 }
3937 break;
3938
3939 case PTB_PLAY:
3940 {
3941 editor.playMode = PLAY_MODE_NORMAL;
3942
3943 if (mouse.rightButtonPressed)
3944 modPlay(DONT_SET_PATTERN, song->currPos, song->currRow);
3945 else
3946 modPlay(DONT_SET_PATTERN, song->currPos, DONT_SET_ROW);
3947
3948 editor.currMode = MODE_PLAY;
3949 pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);
3950 statusAllRight();
3951 }
3952 break;
3953
3954 case PTB_PATTERN:
3955 {
3956 editor.playMode = PLAY_MODE_PATTERN;
3957
3958 if (mouse.rightButtonPressed)
3959 modPlay(song->currPattern, DONT_SET_ORDER, song->currRow);
3960 else
3961 modPlay(song->currPattern, DONT_SET_ORDER, DONT_SET_ROW);
3962
3963 editor.currMode = MODE_PLAY;
3964 pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);
3965 statusAllRight();
3966 }
3967 break;
3968
3969 case PTB_EDIT:
3970 {
3971 if (!ui.samplerScreenShown)
3972 {
3973 editor.playMode = PLAY_MODE_NORMAL;
3974 modStop();
3975 editor.currMode = MODE_EDIT;
3976 pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);
3977 statusAllRight();
3978 }
3979 }
3980 break;
3981
3982 case PTB_RECORD:
3983 {
3984 if (!ui.samplerScreenShown)
3985 {
3986 editor.playMode = PLAY_MODE_PATTERN;
3987
3988 if (mouse.rightButtonPressed)
3989 modPlay(song->currPattern, DONT_SET_ORDER, song->currRow);
3990 else
3991 modPlay(song->currPattern, DONT_SET_ORDER, DONT_SET_ROW);
3992
3993 editor.currMode = MODE_RECORD;
3994 pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);
3995 statusAllRight();
3996 }
3997 }
3998 break;
3999
4000 case PTB_CLEAR:
4001 {
4002 int32_t result = askBox(ASKBOX_CLEAR, "PLEASE SELECT");
4003 if (result == ASKBOX_CLEAR_CANCEL)
4004 break;
4005
4006 editor.playMode = PLAY_MODE_NORMAL;
4007 modStop();
4008
4009 if (result == ASKBOX_CLEAR_SONG)
4010 {
4011 clearSong();
4012 }
4013 else if (result == ASKBOX_CLEAR_SAMPLES)
4014 {
4015 clearSamples();
4016 }
4017 else if (result == ASKBOX_CLEAR_ALL)
4018 {
4019 clearSong();
4020 clearSamples();
4021 }
4022
4023 editor.currMode = MODE_IDLE;
4024 pointerSetMode(POINTER_MODE_IDLE, DO_CARRY);
4025 statusAllRight();
4026
4027 // hide edit op. and about screen, and redraw visualizer
4028
4029 ui.editOpScreenShown = false;
4030 ui.aboutScreenShown = false;
4031
4032 if (ui.visualizerMode == VISUAL_QUADRASCOPE) renderQuadrascopeBg();
4033 else if (ui.visualizerMode == VISUAL_SPECTRUM) renderSpectrumAnalyzerBg();
4034
4035 updateWindowTitle(MOD_IS_MODIFIED);
4036 }
4037 break;
4038
4039 case PTB_SAMPLEU:
4040 {
4041 if (mouse.rightButtonPressed)
4042 {
4043 editor.sampleZero = true;
4044 ui.updateCurrSampleNum = true;
4045 }
4046 else
4047 {
4048 sampleUpButton();
4049 }
4050 }
4051 break;
4052
4053 case PTB_SAMPLED:
4054 {
4055 if (mouse.rightButtonPressed)
4056 {
4057 editor.sampleZero = true;
4058 ui.updateCurrSampleNum = true;
4059 }
4060 else
4061 {
4062 sampleDownButton();
4063 }
4064 }
4065 break;
4066
4067 case PTB_FTUNEU:
4068 {
4069 if (!editor.sampleZero && (song->samples[editor.currSample].fineTune & 0xF) != 7)
4070 {
4071 sampleFineTuneUpButton();
4072 updateWindowTitle(MOD_IS_MODIFIED);
4073 }
4074 }
4075 break;
4076
4077 case PTB_FTUNED:
4078 {
4079 if (!editor.sampleZero && (song->samples[editor.currSample].fineTune & 0xF) != 8)
4080 {
4081 sampleFineTuneDownButton();
4082 updateWindowTitle(MOD_IS_MODIFIED);
4083 }
4084 }
4085 break;
4086
4087 case PTB_SVOLUMEU:
4088 {
4089 if (!editor.sampleZero && song->samples[editor.currSample].volume < 64)
4090 {
4091 sampleVolumeUpButton();
4092 updateWindowTitle(MOD_IS_MODIFIED);
4093 }
4094 }
4095 break;
4096
4097 case PTB_SVOLUMED:
4098 {
4099 if (!editor.sampleZero && song->samples[editor.currSample].volume > 0)
4100 {
4101 sampleVolumeDownButton();
4102 updateWindowTitle(MOD_IS_MODIFIED);
4103 }
4104 }
4105 break;
4106
4107 case PTB_SLENGTHU:
4108 {
4109 if (!editor.sampleZero && song->samples[editor.currSample].length < config.maxSampleLength)
4110 {
4111 sampleLengthUpButton(INCREMENT_SLOW);
4112 updateWindowTitle(MOD_IS_MODIFIED);
4113 }
4114 }
4115 break;
4116
4117 case PTB_SLENGTHD:
4118 {
4119 if (!editor.sampleZero && song->samples[editor.currSample].length > 0)
4120 {
4121 sampleLengthDownButton(INCREMENT_SLOW);
4122 updateWindowTitle(MOD_IS_MODIFIED);
4123 }
4124 }
4125 break;
4126
4127 case PTB_SREPEATU:
4128 {
4129 if (!editor.sampleZero)
4130 {
4131 int32_t oldVal = song->samples[editor.currSample].loopStart;
4132 sampleRepeatUpButton(INCREMENT_SLOW);
4133 if (song->samples[editor.currSample].loopStart != oldVal)
4134 updateWindowTitle(MOD_IS_MODIFIED);
4135 }
4136 }
4137 break;
4138
4139 case PTB_SREPEATD:
4140 {
4141 if (!editor.sampleZero)
4142 {
4143 int32_t oldVal = song->samples[editor.currSample].loopStart;
4144 sampleRepeatDownButton(INCREMENT_SLOW);
4145 if (song->samples[editor.currSample].loopStart != oldVal)
4146 updateWindowTitle(MOD_IS_MODIFIED);
4147 }
4148 }
4149 break;
4150
4151 case PTB_SREPLENU:
4152 {
4153 if (!editor.sampleZero)
4154 {
4155 int32_t oldVal = song->samples[editor.currSample].loopLength;
4156 sampleRepeatLengthUpButton(INCREMENT_SLOW);
4157 if (song->samples[editor.currSample].loopLength != oldVal)
4158 updateWindowTitle(MOD_IS_MODIFIED);
4159 }
4160 }
4161 break;
4162
4163 case PTB_SREPLEND:
4164 {
4165 if (!editor.sampleZero)
4166 {
4167 int32_t oldVal = song->samples[editor.currSample].loopLength;
4168 sampleRepeatLengthDownButton(INCREMENT_SLOW);
4169 if (song->samples[editor.currSample].loopLength != oldVal)
4170 updateWindowTitle(MOD_IS_MODIFIED);
4171 }
4172 }
4173 break;
4174
4175 case PTB_TEMPOU: tempoUpButton(); break;
4176 case PTB_TEMPOD: tempoDownButton(); break;
4177
4178 case PTB_LENGTHU:
4179 {
4180 if (song->header.songLength < 128)
4181 {
4182 songLengthUpButton();
4183 updateWindowTitle(MOD_IS_MODIFIED);
4184 }
4185 }
4186 break;
4187
4188 case PTB_LENGTHD:
4189 {
4190 if (song->header.songLength > 1)
4191 {
4192 songLengthDownButton();
4193 updateWindowTitle(MOD_IS_MODIFIED);
4194 }
4195 }
4196 break;
4197
4198 case PTB_PATTERNU:
4199 {
4200 if (song->header.patternTable[song->currPos] < 99)
4201 {
4202 patternUpButton();
4203 updateWindowTitle(MOD_IS_MODIFIED);
4204 }
4205 }
4206 break;
4207
4208 case PTB_PATTERND:
4209 {
4210 if (song->header.patternTable[song->currPos] > 0)
4211 {
4212 patternDownButton();
4213 updateWindowTitle(MOD_IS_MODIFIED);
4214 }
4215 }
4216 break;
4217
4218 case PTB_POSU: positionUpButton(); break;
4219 case PTB_POSD: positionDownButton(); break;
4220
4221 default: displayErrorMsg("NOT IMPLEMENTED"); return false; // button not mapped
4222 }
4223
4224 mouse.lastGUIButton = button;
4225 return false;
4226 }
4227
4228 static void handleRepeatedGUIButtons(void)
4229 {
4230 // repeat button
4231 switch (mouse.lastGUIButton)
4232 {
4233 case PTB_EO_NOTE1_UP:
4234 {
4235 if (mouse.repeatCounter >= 4)
4236 {
4237 mouse.repeatCounter = 0;
4238 edNote1UpButton();
4239 }
4240 }
4241 break;
4242
4243 case PTB_EO_NOTE1_DOWN:
4244 {
4245 if (mouse.repeatCounter >= 4)
4246 {
4247 mouse.repeatCounter = 0;
4248 edNote1DownButton();
4249 }
4250 }
4251 break;
4252
4253 case PTB_EO_NOTE2_UP:
4254 {
4255 if (mouse.repeatCounter >= 4)
4256 {
4257 mouse.repeatCounter = 0;
4258 edNote2UpButton();
4259 }
4260 }
4261 break;
4262
4263 case PTB_EO_NOTE2_DOWN:
4264 {
4265 if (mouse.repeatCounter >= 4)
4266 {
4267 mouse.repeatCounter = 0;
4268 edNote2DownButton();
4269 }
4270 }
4271 break;
4272
4273 case PTB_EO_NOTE3_UP:
4274 {
4275 if (mouse.repeatCounter >= 4)
4276 {
4277 mouse.repeatCounter = 0;
4278 edNote3UpButton();
4279 }
4280 }
4281 break;
4282
4283 case PTB_EO_NOTE3_DOWN:
4284 {
4285 if (mouse.repeatCounter >= 4)
4286 {
4287 mouse.repeatCounter = 0;
4288 edNote3DownButton();
4289 }
4290 }
4291 break;
4292
4293 case PTB_EO_NOTE4_UP:
4294 {
4295 if (mouse.repeatCounter >= 4)
4296 {
4297 mouse.repeatCounter = 0;
4298 edNote4UpButton();
4299 }
4300 }
4301 break;
4302
4303 case PTB_EO_NOTE4_DOWN:
4304 {
4305 if (mouse.repeatCounter >= 4)
4306 {
4307 mouse.repeatCounter = 0;
4308 edNote4DownButton();
4309 }
4310 }
4311 break;
4312
4313 case PTB_EO_VOL_UP:
4314 {
4315 if (mouse.repeatCounter >= 2)
4316 {
4317 mouse.repeatCounter = 0;
4318 edVolUpButton();
4319 }
4320 }
4321 break;
4322
4323 case PTB_EO_VOL_DOWN:
4324 {
4325 if (mouse.repeatCounter >= 2)
4326 {
4327 mouse.repeatCounter = 0;
4328 edVolDownButton();
4329 }
4330 }
4331 break;
4332
4333 case PTB_EO_MOD_UP:
4334 {
4335 if (mouse.repeatCounter >= 2)
4336 {
4337 mouse.repeatCounter = 0;
4338 edModUpButton();
4339 }
4340 }
4341 break;
4342
4343 case PTB_EO_MOD_DOWN:
4344 {
4345 if (mouse.repeatCounter >= 2)
4346 {
4347 mouse.repeatCounter = 0;
4348 edModDownButton();
4349 }
4350 }
4351 break;
4352
4353 case PTB_EO_POS_UP:
4354 {
4355 if (mouse.repeatCounter >= 1)
4356 {
4357 mouse.repeatCounter = 0;
4358 edPosUpButton(INCREMENT_FAST);
4359 }
4360 }
4361 break;
4362
4363 case PTB_EO_POS_DOWN:
4364 {
4365 if (mouse.repeatCounter >= 1)
4366 {
4367 mouse.repeatCounter = 0;
4368 edPosDownButton(INCREMENT_FAST);
4369 }
4370 }
4371 break;
4372
4373 case PTB_EO_FROM_UP:
4374 {
4375 if (mouse.repeatCounter >= 2)
4376 {
4377 mouse.repeatCounter = 0;
4378 if (editor.sampleFrom < 0x1F)
4379 {
4380 editor.sampleFrom++;
4381 ui.updateFromText = true;
4382 }
4383 }
4384 }
4385 break;
4386
4387 case PTB_EO_FROM_DOWN:
4388 {
4389 if (mouse.repeatCounter >= 2)
4390 {
4391 mouse.repeatCounter = 0;
4392 if (editor.sampleFrom > 0x00)
4393 {
4394 editor.sampleFrom--;
4395 ui.updateFromText = true;
4396 }
4397 }
4398 }
4399 break;
4400
4401 case PTB_EO_TO_UP:
4402 {
4403 if (mouse.repeatCounter >= 2)
4404 {
4405 mouse.repeatCounter = 0;
4406 if (editor.sampleTo < 0x1F)
4407 {
4408 editor.sampleTo++;
4409 ui.updateToText = true;
4410 }
4411 }
4412 }
4413 break;
4414
4415 case PTB_EO_TO_DOWN:
4416 {
4417 if (mouse.repeatCounter >= 2)
4418 {
4419 mouse.repeatCounter = 0;
4420 if (editor.sampleTo > 0x00)
4421 {
4422 editor.sampleTo--;
4423 ui.updateToText = true;
4424 }
4425 }
4426 }
4427 break;
4428
4429 case PTB_SAMPLEU:
4430 {
4431 if (mouse.repeatCounter >= 5)
4432 {
4433 mouse.repeatCounter = 0;
4434 if (!mouse.rightButtonPressed)
4435 sampleUpButton();
4436 else
4437 ui.updateCurrSampleNum = true;
4438 }
4439 }
4440 break;
4441
4442 case PTB_SAMPLED:
4443 {
4444 if (mouse.repeatCounter >= 5)
4445 {
4446 mouse.repeatCounter = 0;
4447 if (!mouse.rightButtonPressed)
4448 sampleDownButton();
4449 else
4450 ui.updateCurrSampleNum = true;
4451 }
4452 }
4453 break;
4454
4455 case PTB_FTUNEU:
4456 {
4457 if (mouse.repeatCounter >= 5)
4458 {
4459 mouse.repeatCounter = 0;
4460 sampleFineTuneUpButton();
4461 }
4462 }
4463 break;
4464
4465 case PTB_FTUNED:
4466 {
4467 if (mouse.repeatCounter >= 5)
4468 {
4469 mouse.repeatCounter = 0;
4470 sampleFineTuneDownButton();
4471 }
4472 }
4473 break;
4474
4475 case PTB_SVOLUMEU:
4476 {
4477 if (mouse.repeatCounter >= 5)
4478 {
4479 mouse.repeatCounter = 0;
4480 sampleVolumeUpButton();
4481 }
4482 }
4483 break;
4484
4485 case PTB_SVOLUMED:
4486 {
4487 if (mouse.repeatCounter >= 5)
4488 {
4489 mouse.repeatCounter = 0;
4490 sampleVolumeDownButton();
4491 }
4492 }
4493 break;
4494
4495 case PTB_SLENGTHU:
4496 {
4497 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4498 {
4499 mouse.repeatCounter = 0;
4500 sampleLengthUpButton(INCREMENT_FAST);
4501 }
4502 }
4503 break;
4504
4505 case PTB_SLENGTHD:
4506 {
4507 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4508 {
4509 mouse.repeatCounter = 0;
4510 sampleLengthDownButton(INCREMENT_FAST);
4511 }
4512 }
4513 break;
4514
4515 case PTB_SREPEATU:
4516 {
4517 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4518 {
4519 mouse.repeatCounter = 0;
4520 sampleRepeatUpButton(INCREMENT_FAST);
4521 }
4522 }
4523 break;
4524
4525 case PTB_SREPEATD:
4526 {
4527 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4528 {
4529 mouse.repeatCounter = 0;
4530 sampleRepeatDownButton(INCREMENT_FAST);
4531 }
4532 }
4533 break;
4534
4535 case PTB_SREPLENU:
4536 {
4537 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4538 {
4539 mouse.repeatCounter = 0;
4540 sampleRepeatLengthUpButton(INCREMENT_FAST);
4541 }
4542 }
4543 break;
4544
4545 case PTB_SREPLEND:
4546 {
4547 if (mouse.rightButtonPressed || mouse.repeatCounter >= 1)
4548 {
4549 mouse.repeatCounter = 0;
4550 sampleRepeatLengthDownButton(INCREMENT_FAST);
4551 }
4552 }
4553 break;
4554
4555 case PTB_TEMPOU:
4556 {
4557 if (mouse.repeatCounter >= 3)
4558 {
4559 mouse.repeatCounter = 0;
4560 tempoUpButton();
4561 }
4562 }
4563 break;
4564
4565 case PTB_TEMPOD:
4566 {
4567 if (mouse.repeatCounter >= 3)
4568 {
4569 mouse.repeatCounter = 0;
4570 tempoDownButton();
4571 }
4572 }
4573 break;
4574
4575 case PTB_LENGTHU:
4576 {
4577 if (mouse.repeatCounter >= 7)
4578 {
4579 mouse.repeatCounter = 0;
4580 songLengthUpButton();
4581 }
4582 }
4583 break;
4584
4585 case PTB_LENGTHD:
4586 {
4587 if (mouse.repeatCounter >= 7)
4588 {
4589 mouse.repeatCounter = 0;
4590 songLengthDownButton();
4591 }
4592 }
4593 break;
4594
4595 case PTB_PATTERNU:
4596 {
4597 if (mouse.repeatCounter >= 7)
4598 {
4599 mouse.repeatCounter = 0;
4600 patternUpButton();
4601 }
4602 }
4603 break;
4604
4605 case PTB_PATTERND:
4606 {
4607 if (mouse.repeatCounter >= 7)
4608 {
4609 mouse.repeatCounter = 0;
4610 patternDownButton();
4611 }
4612 }
4613 break;
4614
4615 case PTB_POSU:
4616 {
4617 if (mouse.repeatCounter >= 7)
4618 {
4619 mouse.repeatCounter = 0;
4620 positionUpButton();
4621 }
4622 }
4623 break;
4624
4625 case PTB_POSD:
4626 {
4627 if (mouse.repeatCounter >= 7)
4628 {
4629 mouse.repeatCounter = 0;
4630 positionDownButton();
4631 }
4632 }
4633 break;
4634
4635 case PTB_PE_SCROLLUP:
4636 {
4637 if (mouse.repeatCounter >= 2)
4638 {
4639 mouse.repeatCounter = 0;
4640 posEdScrollUp();
4641 }
4642 }
4643 break;
4644
4645 case PTB_PE_SCROLLDOWN:
4646 {
4647 if (mouse.repeatCounter >= 2)
4648 {
4649 mouse.repeatCounter = 0;
4650 posEdScrollDown();
4651 }
4652 }
4653 break;
4654
4655 case PTB_DO_SCROLLUP:
4656 {
4657 if (mouse.repeatCounter >= 1)
4658 {
4659 mouse.repeatCounter = 0;
4660
4661 diskop.scrollOffset--;
4662 if (mouse.rightButtonPressed)
4663 diskop.scrollOffset -= 3;
4664
4665 if (diskop.scrollOffset < 0)
4666 diskop.scrollOffset = 0;
4667
4668 ui.updateDiskOpFileList = true;
4669 }
4670 }
4671 break;
4672
4673 case PTB_DO_SCROLLDOWN:
4674 {
4675 if (mouse.repeatCounter >= 1)
4676 {
4677 mouse.repeatCounter = 0;
4678
4679 if (diskop.numEntries > DISKOP_LINES)
4680 {
4681 diskop.scrollOffset++;
4682 if (mouse.rightButtonPressed)
4683 diskop.scrollOffset += 3;
4684
4685 if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)
4686 diskop.scrollOffset = diskop.numEntries-DISKOP_LINES;
4687
4688 ui.updateDiskOpFileList = true;
4689 }
4690 }
4691 }
4692 break;
4693
4694 case PTB_SA_ZOOMBARAREA:
4695 {
4696 if (mouse.repeatCounter >= 4)
4697 {
4698 mouse.repeatCounter = 0;
4699 if (!ui.forceSampleDrag)
4700 samplerBarPressed(MOUSE_BUTTON_NOT_HELD);
4701 }
4702 }
4703 break;
4704
4705 default: break;
4706 }
4707 }