-
Notifications
You must be signed in to change notification settings - Fork 1
/
noteHandlerInput.js
250 lines (230 loc) · 14.4 KB
/
noteHandlerInput.js
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
// Setup namespace
var Set_Note_Handler_Input = new function() {
this.init = function(nh){
// processes keyboard and mouse inputs
nh.prototype.generalInput = function(e){
// if the mouse is down
if(e.detail.mouseDown){
// possible new double click
this.possibleDoubleClick = true;
// this variable tells later if statments if they should execute
var exit = false;
// if not doing drag select
if(!this.multiSelect.isActive){
// if the selected array is empty
if(this.selected.length == 0){
// go through visible notes in reverse order because the ones on top were likely added later
for(var note = this.visibleNotes.length-1; note >= 0; note--)
/* if the mouse is down on a visible note, add that note to the selected array
* set the note to selected, set exit to true so that a new note isn't created
* set selectedMouseUp to false to indicate that user is dragging
*/
if(this.visibleNotes[note].mouseOver(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
this.visibleNotes[note].selected = true;
this.selected.push(this.visibleNotes[note]);
this.selectedMouseUp = false;
// if note was just selected, dragging it no matter where because
// the user doesn't know what part they are clicking on
this.resizing = 0;
exit = true;
break;
}
}
// the selected array is not empty
else{
// if what is selected is being dragged
if(!this.selectedMouseUp){
// if user hasn't decided if they are dragging or not
if(this.resizing == -1)
// check if user wants to resize
for(var note in this.selected){
if(this.selected[note].overLeftEdge(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
this.resizing = 1;
break
}
else if(this.selected[note].overRightEdge(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
this.resizing = 2;
break;
}
}
// if not resizing, that means user is dragging
if(this.resizing == -1)
this.resizing = 0;
// update all notes in selected by either moving them or resizing them
for(var note in this.selected){
if(this.resizing == 1)
this.selected[note].leftResize(e.detail.deltaX)
else if(this.resizing == 2)
this.selected[note].rightResize(e.detail.deltaX)
else
this.selected[note].moveNote(e.detail.deltaX, e.detail.deltaY);
}
// notes have been modified, set reminder to make them snap into place when user is done modifying notes
this.selectedSet = false;
// prevent the creation of a new note
exit = true;
}
// the mouse was just pressed down, make sure user is still selecting an object in the selected array
else{
for(var note in this.selected)
if(this.selected[note].mouseOver(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
this.selectedMouseUp = false;
break;
}
}
}
}
// doing drag select
else{
this.updateMulitSelect(e.detail.mouseX, e.detail.mouseY);
// not new note creation
exit = true;
}
// if not single selecting or dragg selecting and clicked on empty space
// with something selected, empty the selected aray
if(!exit && this.selectedMouseUp && this.selected.length != 0){
for(var note in this.selected)
this.selected[note].selected = false;
this.selected = [];
exit = true;
}
// if nothing is selected, create a potential new note, only confirmed if user doesn't drag (indicating a drag select)
if(!exit && this.selectedMouseUp && e.detail.mouseX > this.x && e.detail.mouseX < this.x+this.width && e.detail.mouseY > this.y && e.detail.mouseY < this.y+this.height)
// get mouse pixel position in terms of input handler window, convert to notes and beats, pass a few other parameters to help set up note
this.possibleNewNote = Note_Space.createNote(Math.floor((e.detail.mouseY-this.y-this.verticalOffset)/this.PixelsPerNote), Math.floor((e.detail.mouseX-this.x-this.horizontalOffset)/this.PixelsPerSection)*this.resolution, this.resolution, this.PixelsPerNote, this.PixelsPerBeat, this.x, this.y, this.maxKeys);
// this version takes into account mouse offsets but you can place a note over another one without knowing it in edge cases
// this.possibleNewNote = Note_Space.createNote(Math.floor((e.detail.mouseY-this.y-this.verticalOffset-5)/this.PixelsPerNote), Math.floor((e.detail.mouseX-this.x-this.horizontalOffset-4)/this.PixelsPerSection)*this.resolution, this.resolution, this.PixelsPerNote, this.PixelsPerBeat, this.x, this.y);
}
// if the mouse is not down
else{
// if the possible new note is still around, create it, add it to the arrays
if(this.possibleNewNote != null){
this.addNewNote(this.possibleNewNote);
this.possibleNewNote = null;
}
// if user clicks on empty space next time, this flag will empty the selected array
this.selectedMouseUp = true;
// hide dragging selector highlighter
this.multiSelect.isActive = false;
// reset selected action
this.resizing = -1;
// check if cursor should change to let the user know that they can resize the selected notes
// only makes sense if something is selected
if(this.selected.length > 0){
var changeCursor = false;
for(var i = this.selected.length-1; i >= 0; i--)
if(this.selected[i].mouseOver(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
if(this.selected[i].overLeftEdge(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset) || this.selected[i].overRightEdge(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
$(this.editor).css("cursor","col-resize");
changeCursor = true;
}
break;
}
if(!changeCursor)
$(this.editor).css("cursor","default");
}
}
// if escape key pressed, deselect everything
if(e.detail.keyDown == 27){
for(var note in this.selected)
this.selected[note].selected = false;
this.selected = [];
}
// if delete key pressed, remove all selected notes from workspace
else if(e.detail.keyDown == 8){
for(var note in this.selected){
this.notes.splice(this.notes.indexOf(this.selected[note]),1);
var visInd = this.visibleNotes.indexOf(this.selected[note]);
if(visInd != -1)
this.visibleNotes.splice(visInd,1);
}
this.selected = [];
}
else if(e.detail.keyDown == 32){
this.midiEditor.spaceKey();
}
else if(e.detail.ctrlKey){
// c, copy
if(e.detail.keyDown == 67 && this.selected.length > 0){
// last note
this.clipboardStart = this.notes[this.notes.length-1].beat;
this.clipboard = [];
for(var n in this.selected){
this.clipboard.push(this.selected[n]);
this.clipboardStart = Math.min(this.clipboardStart, this.selected[n].beat)
}
}
// v, paste
else if(e.detail.keyDown == 86 && this.clipboard.length > 0){
var startCopying = this.midiEditor.getScrubBarAt();
var endBeat = 0;
for(var note in this.selected)
this.selected[note].selected = false;
this.selected = [];
// create copies of notes on clipboard shifted forward so that the first note on the clipboard
// starts where the scrubbing bar is at
for(var n in this.clipboard){
var addNote = Note_Space.createNote(this.clipboard[n].note, startCopying+this.clipboard[n].beat-this.clipboardStart, this.clipboard[n].length, this.PixelsPerNote, this.PixelsPerBeat, this.x, this.y, this.maxKeys);
this.insertNote(addNote);
this.selected.push(addNote);
addNote.selected = true;
endBeat = Math.max(endBeat, addNote.beat+addNote.length);
}
// set srubbing bar to the end of the last note so that several copies
// can be appended right after each other
this.midiEditor.setScrubBarAt(endBeat);
this.scroll(0,0,this.horizontalOffset,this.verticalOffset);
}
}
// if mouse dragged, remove possible new note
if(e.detail.mouseDrag){
this.possibleNewNote = null;
// if nothing is selected, start a new dragging selector if one is not yet created
if(this.selected.length == 0 && !this.multiSelect.isActive && e.detail.mouseY > this.y){
this.multiSelect.startNew(e.detail.mouseX, e.detail.mouseY, this.horizontalOffset, this.verticalOffset);
}
// no longer a double click possibility, prevents double click deleting on drag
else if(this.selected.length > 0)
this.possibleDoubleClick = false;
}
// if the mouse is up or the mouse goes out of bounds of the note handler window, place all currently selected notes
if(!e.detail.mouseDown || e.detail.mouseX < this.x || e.detail.mouseY < this.y || e.detail.mouseY > this.y+this.height){
// check so that this isn't done every single time there is a mouse movement
if(!this.selectedSet){
// remove the selected notes from the notes array because they could have changed position
// and the notes array needs to remain ordered
for(var note in this.selected)
this.notes.splice(this.findNote(this.notes, this.selected[note], false),1);
for(var note in this.selected){
this.selected[note].mouseUp(this.resolution, this.PixelsPerNote, this.PixelsPerSection, this.PixelsPerBeat, this.maxKeys, this.selected.length == 1);
// reinsert selected notes into notes array in their correct place
this.insertNote(this.selected[note]);
}
this.selectedSet = true;
}
}
// if there was a double click, go through notes
// if the double click was on that note, delete it
if(e.detail.doubleClickConsumes > 0){
if(this.possibleDoubleClick){
for(var note = this.visibleNotes.length-1; note >= 0; note--)
if(this.visibleNotes[note].mouseOver(e.detail.mouseX-this.horizontalOffset, e.detail.mouseY-this.verticalOffset)){
this.notes.splice(this.notes.indexOf(this.visibleNotes[note]),1);
this.visibleNotes.splice(note,1);
this.selected.splice(this.selected.indexOf(this.visibleNotes[note]),1);
break;
}
//console.log(e.detail.doubleClickConsumes);
}
this.possibleDoubleClick = false;
e.detail.doubleClickConsumes--;
}
// use this to make sure that the notes array is in order
// var arrStr = "";
// for(var note in this.notes)
// arrStr+=this.notes[note].beat+",";
// console.log(arrStr);
}
console.log("Added input function to Note Handler object")
}
}