-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouseandKeyActions.pde
More file actions
536 lines (441 loc) · 12.3 KB
/
Copy pathmouseandKeyActions.pde
File metadata and controls
536 lines (441 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//Global Settings
int scrollbarW = 16;
int scrollbarH;
int scrollbarX;
int scrollbarY = 0;
int scrollbarMarginRight = 10;
float imageMoveAmount = 1.5;
VScrollbar featureScrollbar;
VScrollbar libraryScrollbar;
PImage featuresImg, FAQImg;
float libraryAddX = 150;
float libraryAddY = 100;
float libraryAddW = 220;
float libraryAddH = 45;
float libraryRowX = 150;
float libraryRowY = 165;
float libraryRowW = 700;
float libraryRowH = 60;
float libraryRowGap = 90;
float libraryDeleteRadius = 14;
boolean showDeleteConfirm = false;
String pendingDeleteProject = "";
boolean isNavHovered(int i) {
//Checks if the mouse is inside the hover area of a navigation item
return mouseX >= navX[i] - navPad && mouseX <= navX[i] + navW[i] + navPad && mouseY >= navY - 45 && mouseY <= navY + 45;
}
void mousePressed() {
if (showSavePopup){
return;
}
if (showDeleteConfirm){
handleDeleteConfirmationClick();
return;
}
if (handleDemoCardClick()) { //Checks if a demo card was clicked
return;
}
if (currentScreen == libraryPage){ //If user is on library page display scroll bar
libraryScrollbar.startDrag();
if (libraryScrollbar.dragging){
return;
}
}
if (handleLibraryProjectClick()) {
return;
}
if (currentScreen == composePage && handleComposeClick()){
return;
}
if (currentScreen == featuresPage || currentScreen == FAQPage){
featureScrollbar.startDrag();
if (featureScrollbar.dragging){
return;
}
}
handleNavClick();
}
void mouseReleased() {
if (currentScreen == featuresPage || currentScreen == FAQPage){
featureScrollbar.stopDrag();
}
if (currentScreen == libraryPage){
libraryScrollbar.stopDrag();
}
}
void keyPressed(){
if (currentScreen != composePage || showSavePopup){
return;
}
if (key == CODED){
if (keyCode == UP){
editManager.changeSelectedPitch(1);
}else if (keyCode == DOWN){
editManager.changeSelectedPitch(-1);
}else if (keyCode == LEFT){
editManager.selectNextEvent(-1);
}else if (keyCode == RIGHT){
editManager.selectNextEvent(1);
}
return;
}
if (key == DELETE || key == BACKSPACE){
editManager.deleteSelected();
}else if (key == 'p' || key == 'P'){
editManager.togglePlaceMode();
}else if (key == 'r' || key == 'R'){
editManager.toggleRestMode();
}else if (key == 's' || key == 'S'){
editManager.setSelectedAccidental(1);
}else if (key == 'f' || key == 'F'){
editManager.setSelectedAccidental(-1);
}else if (key == 'n' || key == 'N'){
editManager.setSelectedAccidental(0);
}else if (key == 'c' || key == 'C'){
editManager.clearSelectedAccidental();
}
}
void handleNavClick() {
if (mouseX > 8 && mouseX < 72 && mouseY > 16 && mouseY < 68) {
stopActiveDemo();
currentScreen = homePage;
updateGUIVisibility();
return;
}
if (currentScreen != composePage){
if (mouseY > 80) return;
for (int i = 0; i < navLabels.length; i++) {
if (isNavHovered(i)) {
stopActiveDemo();
currentScreen = navScreens[i];
updateGUIVisibility();
return;
}
}
}
}
boolean handleDemoCardClick() {
if (currentScreen != demosPage){
return false;
}
for (int i = 0; i < 3; i++) {
float x = 120 + i * 290;
float y = 320;
float w = 200;
float h = 150;
if (mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h) {
loadDemoIntoComposer(i);
return true;
}
}
return false;
}
boolean handleLibraryProjectClick() {
if (currentScreen != libraryPage){
return false;
}
if (mouseX >= libraryAddX && mouseX <= libraryAddX + libraryAddW && mouseY >= libraryAddY && mouseY <= libraryAddY + libraryAddH){
stopActiveDemo();
makeBlankUserPiece("My Piece");
currentScreen = composePage;
updateGUIVisibility();
return true;
}
if (mouseY < libraryRowY){
return false;
}
float scrollAmount = libraryScrollAmount();
for (int i=0;i<savedProjects.size();i++){
float y1 = libraryRowY + i*libraryRowGap - scrollAmount;
float y2 = y1 + libraryRowH;
if (y2 < libraryRowY || y1 > height){
continue;
}
float deleteX = libraryRowX + libraryRowW - 32;
float deleteY = y1 + libraryRowH/2;
if (dist(mouseX, mouseY, deleteX, deleteY) <= libraryDeleteRadius){
pendingDeleteProject = savedProjects.get(i);
showDeleteConfirm = true;
return true;
}
if (mouseX >= libraryRowX && mouseX <= libraryRowX + libraryRowW && mouseY >= y1 && mouseY <= y2) {
stopActiveDemo();
loadProject(savedProjects.get(i));
currentScreen = composePage;
updateGUIVisibility();
return true;
}
}
return false;
}
void handleDeleteConfirmationClick(){
float panelX = width/2 - 220;
float panelY = height/2 - 110;
float cancelX = panelX + 70;
float buttonY = panelY + 145;
float deleteX = panelX + 245;
float buttonW = 125;
float buttonH = 38;
if (mouseX >= cancelX && mouseX <= cancelX + buttonW && mouseY >= buttonY && mouseY <= buttonY + buttonH){
showDeleteConfirm = false;
pendingDeleteProject = "";
return;
}
if (mouseX >= deleteX && mouseX <= deleteX + buttonW && mouseY >= buttonY && mouseY <= buttonY + buttonH){
deleteSavedProject(pendingDeleteProject);
showDeleteConfirm = false;
pendingDeleteProject = "";
}
}
boolean handleComposeClick(){
if (userPiece == null || userPiece.tracks.size() == 0){
return false;
}
if (mouseY >= yInitialValueButtons - 10){
return false;
}
if (mouseX >= xInitialValueButtons + 530 && mouseX <= xInitialValueButtons + 890 && mouseY >= yInitialValueButtons - 55 && mouseY <= yInitialValueButtons - 5){
return false;
}
editManager.keepTrackInRange();
int measureID = measureFromMouse();
if (measureID < 0){
return false;
}
if (measureID >= userPiece.measureCount()){
editManager.setStatus("Add more measures");
return true;
}
Measure measure = userPiece.getMeasure(editManager.activeTrack, measureID);
float rawBeat = ((mouseX - measureStartX(measureID)) / measureWidth()) * userPiece.timeSig.measureDuration();
rawBeat = constrain(rawBeat, 0, userPiece.timeSig.measureDuration() - MUSIC_EPSILON);
int eventID = eventIDAtBeat(measure, rawBeat);
if (!editManager.placeMode){
editManager.selectEvent(measureID, eventID);
return true;
}
if (eventID < 0){
return true;
}
if (measure.events.get(eventID) instanceof Note){
editManager.selectEvent(measureID, eventID);
return true;
}
MusicEvent event;
if (editManager.placingRest){
event = new Rest(selectedDuration);
}else{
int midiNote = midiFromMouseY(measureID);
Instrument instrument = userPiece.instruments.get(editManager.activeTrack);
event = new Note(selectedDuration, midiNote, instrument);
}
boolean placed = userPiece.placeEvent(editManager.activeTrack, measureID, eventID,event);
if (placed){
editManager.selectEvent(measureID, eventID);
editManager.setStatus("Placed event");
}
else{
editManager.setStatus("Could not place event");
}
return true;
}
MusicalPiece createDemoPiece(int demoID){
boolean oldLoading = loadingHistoryState;
loadingHistoryState = true;
MusicalPiece piece;
if (demoID == 0){
piece = createDemo1(this);
}else if (demoID == 1){
piece = createDemo2(this);
}else{
piece = createDemo3(this);
}
loadingHistoryState = oldLoading;
return piece;
}
void loadDemoIntoComposer(int demoID){
stopActiveDemo();
if (userPiece != null){
userPiece.stopPlayback();
}
userPiece = createDemoPiece(demoID);
if (userPiece.instruments.size() > 0){
composeInstrument = userPiece.instruments.get(0);
}
editManager.resetEditor();
applyMasterVolumeToUserPiece();
syncInstrumentDropdown();
saveHistoryState();
currentScreen = composePage;
updateGUIVisibility();
editManager.setStatus("Loaded " + userPiece.title);
}
void startUserPieceVisual(){
if (userPiece == null){
return;
}
userPiece.stopPlayback();
startDemoVisual(userPiece);
}
void startDemoVisual(MusicalPiece piece) {
stopActiveDemo();
visualReturnScreen = currentScreen;
demoEqualizer.start(piece);
currentScreen = demoVisualPage;
updateGUIVisibility();
}
void stopActiveDemo() {
if (demoEqualizer != null) {
demoEqualizer.stop();
}
}
void updateGUIVisibility() {
boolean show = (currentScreen == composePage);
BPM.setVisible(show);
VolumeSlider.setVisible(show);
UndoButton.setVisible(show);
RedoButton.setVisible(show);
NoteKey.setVisible(show);
InstrumentKey.setVisible(show);
PlayButton.setVisible(show);
button1.setVisible(show);
button2.setVisible(show);
PrevTrackButton.setVisible(show);
NextTrackButton.setVisible(show);
AddTrackButton.setVisible(show);
DeleteTrackButton.setVisible(show);
AddMeasureButton.setVisible(show);
DeleteMeasureButton.setVisible(show);
VisualizeButton.setVisible(show);
KeySignatureButton.setVisible(show);
TimeSignatureButton.setVisible(show);
if (!show){
showSavePopup = false;
}
if (currentScreen != libraryPage){
showDeleteConfirm = false;
pendingDeleteProject = "";
}
SaveNameBox.setVisible(show && showSavePopup);
SaveCheckbox.setVisible(show && showSavePopup);
ConfirmSaveButton.setVisible(show && showSavePopup);
CloseSaveButton.setVisible(show && showSavePopup);
}
void setupFeatureScrollImage() {
scrollbarY = toolbarH;
scrollbarH = height - toolbarH;
scrollbarX = width - scrollbarW - scrollbarMarginRight;
featureScrollbar = new VScrollbar(scrollbarX, scrollbarY, scrollbarW, scrollbarH);
libraryScrollbar = new VScrollbar(scrollbarX, scrollbarY, scrollbarW, scrollbarH);
featuresImg = loadImage("features.png");
FAQImg = loadImage("FAQPage.png");
}
void drawNavbarOnTop() {
fill(119, 61, 255);
noStroke();
rect(-10, -10, 1010, 80);
fill(255);
rect(20, 40, 30, 20);
triangle(10, 40, 35, 10, 60, 40);
stroke(5);
fill(0);
line(15, 33, 35, 10);
line(15, 43, 35, 20);
line(15, 33, 15, 55);
line(35, 10, 35, 50);
ellipse(30, 50, 10, 5);
ellipse(10, 55, 10, 5);
strokeWeight(2);
textSize(25);
fill(255);
textAlign(LEFT);
naviBarText();
}
void drawFeatureScrollImage() {
background(0);
// Pick which image to show
PImage currentImg;
if (currentScreen == FAQPage) {
currentImg = FAQImg;
} else {
currentImg = featuresImg;
}
// Calculate scroll amount based on the current image
float maxScroll = max(0, currentImg.height - (height - toolbarH));
float scrollAmount = featureScrollbar.getScrollAmount(maxScroll);
// Center the current image
float imgX = width/2 - currentImg.width/2;
image(currentImg, imgX, toolbarH - scrollAmount);
//Cover the tiny white edge that shows up on this image
fill(0);
noStroke();
rect(imgX + currentImg.width - 2, toolbarH, 4, height - toolbarH);
featureScrollbar.update();
featureScrollbar.display();
drawNavbarOnTop();
}
float libraryMaxScroll(){
float visibleH = height - libraryRowY - 20;
float contentH = savedProjects.size() * libraryRowGap;
return max(0, contentH - visibleH);
}
float libraryScrollAmount(){
if (libraryScrollbar == null){
return 0;
}
return libraryScrollbar.getScrollAmount(libraryMaxScroll());
}
void mousePressedFeatureScrollImage() {
featureScrollbar.startDrag();
}
void mouseReleasedFeatureScrollImage() {
featureScrollbar.stopDrag();
}
//Scrollbar Class
class VScrollbar {
//Methods
float x, y;
int w, h;
float sliderY;
int sliderH = 30;
boolean dragging = false;
//Constructor
VScrollbar(float tempX, float tempY, int tempW, int tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
sliderY = y + h/2 - sliderH/2;
}
//Fields
void update() {
if (dragging) {
sliderY = mouseY - sliderH/2;
sliderY = constrain(sliderY, y, y + h - sliderH);
}
}
void display() {
noStroke();
fill(204);
rect(x, y, w, h);
fill(122);
rect(x, sliderY, w, sliderH);
}
void startDrag() {
if (mouseOverSlider()) {
dragging = true;
}
}
void stopDrag() {
dragging = false;
}
boolean mouseOverSlider() {
return mouseX > x && mouseX < x + w && mouseY > sliderY && mouseY < sliderY + sliderH;
}
float getPos() {
return map(sliderY, y, y + h - sliderH, 0, height);
}
float getScrollAmount(float maxScroll) {
return map(sliderY, y, y + h - sliderH, 0, maxScroll);
}
}