Repository navigation
Expand file tree
/
Copy pathScoreCursor.js
More file actions
311 lines (275 loc) · 10.5 KB
/
Copy pathScoreCursor.js
File metadata and controls
311 lines (275 loc) · 10.5 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
var ScoreCursor = function(svg,scale,xPadding,yPadding) {
this.navigator = new ScoreNavigator();
this.xPadding = xPadding;
this.yPadding = yPadding;
this.scale = scale;
this.endOfMeasure = false;
this.svg = svg;
//Have to do it in a very stupid way because there is a bug in WebKit when
//dynamically adding animations to an SVG object. The preferred way, of course,
//would be to create <line>, <animate>, and <rect> programmatically and add them.
this.svg.innerHTML = "<rect id='selectionBox' x='-100' y='-100' width='0' height='0' style='fill:blue;fill-opacity:0.1'/><line id='cursorLine' visibility='hidden' style='stroke:rgb(0,0,0);stroke-width:2' x1='-100' x2='-100' y1='-100' y2='-100'><animate attributename='visibility' from='hidden' to='visible' dur='1.3' repeatcount='indefinite'></animate></line>";
this.line = document.getElementById('cursorLine');
this.box = document.getElementById('selectionBox');
this.mode = 'insertion';
};
ScoreCursor.prototype = {
first: function() {
var cur = this.navigator.getObjectAtCurrentPosition();
console.log("In first. Cur is "+cur);
var bool; if (cur) {bool=true;} else {bool=false;}
console.log("In first. "+bool);
if (cur && !cur.duration) {
this.next();
} else if (!cur) {
console.log("In previous");
this.navigator.previous();
this._handleEndOfMeasure();
if (this.mode == 'selection') {
this.switchModes();
}
}
console.log("first takes us to ");
console.log(this.navigator.getCurrentPosition());
},
next: function() {
this._handleEndOfMeasure();
if (this.endOfMeasure) {
this._updateCursor();
return;
}
console.log("Saving position.");
this.navigator.save();
console.log("hasNext is "+this.navigator.hasNext());
while(this.navigator.hasNext()) {
console.log("After hasnext. measure and contents are "+this.navigator._measuresIndex+","+this.navigator._measureContentsIndex);
var cur = this.navigator.next();
if (!cur || cur.duration) {
this._updateCursor();
return;
}
}
this.navigator.restore();
},
previous: function() {
if (this.endOfMeasure) {
this._handleEndOfMeasure();
this._updateCursor();
return;
}
this.navigator.save();
while(this.navigator.hasPrevious()) {
var cur = this.navigator.previous();
if (cur.duration) {
this._handleEndOfMeasure();
this._updateCursor();
return;
}
}
this.navigator.restore();
},
_handleEndOfMeasure: function() {
console.log("In _handleEndOfMeasure");
console.log("navigator's endofmeasure is "+this.navigator.isAtEndOfMeasure());
if (this.navigator.isAtEndOfMeasure() && !this.endOfMeasure) {
console.log("Setting endOfMeasure to true");
this.endOfMeasure = true;
return;
}
if (this.endOfMeasure) {
console.log("Setting endOfMeasure to false");
this.endOfMeasure = false;
}
},
nextPart: function() {
if (this.navigator.hasNextPart()) {
this.navigator.nextPart();
this._updateCurVoiceCoords();
this._updateCursor();
}
},
previousPart: function() {
if (this.navigator.hasPreviousPart()) {
this.navigator.previousPart();
this._updateCurVoiceCoords();
this._updateCursor();
}
},
_updateCurVoiceCoords: function() {
var start = {
x: this.xPadding*this.scale*Renderer.HORIZ_MULTIPLIER,
y: this.yPadding*this.scale*Renderer.VERT_MULTIPLIER
};
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
var pos = this.navigator.getCurrentPosition();
var part = this.score.getChild(pos.part);
var voice = part.voiceGroup.getChild(pos.voice);
this.curVoiceCoords = this._getRealCoords(voice,
this._getRealCoords(part,
this._getRealCoords(this.score,start)));
},
_getRealCoords: function(component,parentCoords) {
var realRender = component.displayInfo.getRealRender();
console.log(component);
console.log(parentCoords);
parentCoords.x += realRender.x*this.scale;
parentCoords.y += realRender.y*this.scale;
return parentCoords;
},
_updateDisplay: function() {
if (this.mode == 'insertion') {
this._updateCursor();
} else {
this._updateBox();
}
},
_updateCursor: function() {
console.log("In _updateCursor. Getting coords");
var cursorPos = this._getCursorCoords();
console.log("cursorPos is ");
console.log(cursorPos);
this.line.setAttribute('x1',cursorPos.x);
this.line.setAttribute('x2',cursorPos.x);
this.line.setAttribute('y1',cursorPos.y-2.5*this.scale);
this.line.setAttribute('y2',cursorPos.y+2.5*this.scale);
},
_hideCursor: function() {
this.line.setAttribute('x1',-100);
this.line.setAttribute('x2',-100);
this.line.setAttribute('y1',-100);
this.line.setAttribute('y2',-100);
},
_updateBox: function() {
console.log("In _updateBox.");
var pos = this._getBoxCoords();
console.log("Box coords are ");
console.log(pos);
this.box.setAttribute('x',pos.x-0.5*this.scale);
this.box.setAttribute('y',pos.y-0.5*this.scale);
this.box.setAttribute('width',pos.width+1*this.scale);
this.box.setAttribute('height',pos.height+1*this.scale);
},
_hideBox: function() {
this.box.setAttribute('width',0);
this.box.setAttribute('height',0);
},
_getCursorCoords: function() {
console.log("In _getCursorCoords. pos is ");
var pos = this.navigator.getCurrentPosition();
console.log(pos);
var measure = this.score.getChild(pos.part).voiceGroup.getChild(pos.voice).getChild(pos.measure);
console.log("Measure is");
console.log(measure);
this._updateCurVoiceCoords();
console.log("Voice coords are");
console.log(this.curVoiceCoords);
var measureCoords = this._getRealCoords(measure,this.curVoiceCoords);
console.log("Meausre coords are:");
console.log(measureCoords);
if (measure.children.length > 0) {
var extrema = measure.getChild(pos.measureContents).displayInfo.getRenderExtrema();
console.log("Extrema are:");
console.log(extrema);
var curChordX = measureCoords.x+extrema.minX*this.scale;
console.log("curChordX is ");
console.log(curChordX);
var prevChordX;
console.log("In _getCursorCoords. endOfMeasure is "+this.endOfMeasure);
if (this.endOfMeasure) {
curChordX = measureCoords.x+extrema.maxX*this.scale;
return {
x:curChordX+0.5,
y:this.curVoiceCoords.y
};
}
if (this.navigator.isAtStartOfMeasure()) {
prevChordX = curChordX-1;
} else {
console.log("In _getCursorCoords. hasPrevious is true");
prevChordX = measureCoords.x+measure.getChild(pos.measureContents-1).displayInfo.getRenderExtrema().maxX*this.scale;
}
console.log("nextChordX is ");
console.log(prevChordX);
return {
x:(prevChordX+curChordX)/2,
y:this.curVoiceCoords.y
};
} else {
return {
x:measureCoords.x+0.5,
y:this.curVoiceCoords.y
};
}
},
_getBoxCoords: function() {
var pos = this.navigator.getCurrentPosition();
var measure = this.score.getChild(pos.part).voiceGroup.getChild(pos.voice).getChild(pos.measure);
this._updateCurVoiceCoords();
var measureCoords = this._getRealCoords(measure,this.curVoiceCoords);
if (measure.children.length > 0) {
var extrema = measure.getChild(pos.measureContents).displayInfo.getRenderExtrema();
return {
x: measureCoords.x+extrema.minX*this.scale,
y: measureCoords.y+extrema.minY*this.scale,
width: extrema.maxX*this.scale-extrema.minX*this.scale,
height: extrema.maxY*this.scale-extrema.minY*this.scale
};
}
},
curMeasureSplice: function() {
var pos = this.navigator.getCurrentPosition();
var measure = this.score.getChild(pos.part).voiceGroup.getChild(pos.voice).getChild(pos.measure);
var index;
if (this.endOfMeasure) {
index = measure.children.length;
} else {
index = pos.measureContents;
}
return {
id: measure.id,
index: index
};
},
curVoiceSplice: function() {
var pos = this.navigator.getCurrentPosition();
var voice = this.score.getChild(pos.part).voiceGroup.getChild(pos.voice);
return {
id: voice.id,
index: voice.children.length
};
},
getCurrentPosition: function() {
return this.navigator.getCurrentPosition();
},
getObjectAtCurrentPosition: function() {
return this.navigator.getObjectAtCurrentPosition();
},
update: function() {
console.log("In cursor's update. Score is ");
console.log(this._score);
this._updateCurVoiceCoords();
this.first();
this._updateDisplay();
},
switchModes: function() {
if (this.mode == 'insertion') {
this.mode = 'selection';
this._hideCursor();
} else {
this.mode = 'insertion';
this._hideBox();
}
this._updateDisplay();
}
};
Object.defineProperty(ScoreCursor.prototype,"score", {
get: function() {
return this._score;
},
set: function(score) {
console.log("Setting score in cursor:");
console.log(score);
this.navigator.score = score;
this._score = score;
}
});