/
GraphicalContinuousDynamicExpression.ts
428 lines (395 loc) · 21.8 KB
/
GraphicalContinuousDynamicExpression.ts
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
import { GraphicalLine } from "./GraphicalLine";
import { StaffLine } from "./StaffLine";
import { GraphicalMeasure } from "./GraphicalMeasure";
import { ContDynamicEnum, ContinuousDynamicExpression } from "../VoiceData/Expressions/ContinuousExpressions/ContinuousDynamicExpression";
import { PointF2D } from "../../Common/DataObjects/PointF2D";
import { AbstractGraphicalExpression } from "./AbstractGraphicalExpression";
import { PlacementEnum } from "../VoiceData/Expressions/AbstractExpression";
import { SkyBottomLineCalculator } from "./SkyBottomLineCalculator";
import { ISqueezable } from "./ISqueezable";
import log from "loglevel";
import { SourceMeasure } from "../VoiceData/SourceMeasure";
/**
* This class prepares the graphical elements for a continuous expression. It calculates the wedges and
* wrappings if they are split over system breaks.
*/
export class GraphicalContinuousDynamicExpression extends AbstractGraphicalExpression implements ISqueezable {
/** True if expression is split over system borders */
private isSplittedPart: boolean;
/** True if this expression should not be removed if re-rendered */
private notToBeRemoved: boolean;
/** Holds the line objects that can be drawn via implementation */
private lines: GraphicalLine[] = [];
private startMeasure: GraphicalMeasure;
private endMeasure: GraphicalMeasure;
//public StartIsEnd: boolean;
public IsSoftAccent: boolean;
/**
* Create a new instance of the GraphicalContinuousDynamicExpression
* @param continuousDynamic The continuous dynamic instruction read via ExpressionReader
* @param staffLine The staffline where the expression is attached
*/
constructor(continuousDynamic: ContinuousDynamicExpression, staffLine: StaffLine, measure: SourceMeasure) {
super(staffLine, continuousDynamic, measure);
this.isSplittedPart = false;
this.notToBeRemoved = false;
}
//#region Getter / Setter
/** The graphical measure where the parent continuous dynamic expression starts */
public get StartMeasure(): GraphicalMeasure { return this.startMeasure; }
public set StartMeasure(value: GraphicalMeasure) { this.startMeasure = value; }
/** The graphical measure where the parent continuous dynamic expression ends */
public get EndMeasure(): GraphicalMeasure { return this.endMeasure; }
public set EndMeasure(value: GraphicalMeasure) { this.endMeasure = value; }
/** The staff lin where the graphical dynamic expressions ends */
public get EndStaffLine(): StaffLine { return this.endMeasure ? this.endMeasure.ParentStaffLine : undefined; }
/** Is true if this continuous expression is a wedge, that reaches over a system border and needs to be split into two. */
public get IsSplittedPart(): boolean { return this.isSplittedPart; }
public set IsSplittedPart(value: boolean) { this.isSplittedPart = value; }
/** Is true if the dynamic is not a symbol but a text instruction. E.g. "decrescendo" */
public get IsVerbal(): boolean { return this.ContinuousDynamic.Label && this.ContinuousDynamic.Label.length > 0; }
/** True if this expression should not be removed if re-rendered */
public get NotToBeRemoved(): boolean { return this.notToBeRemoved; }
public set NotToBeRemoved(value: boolean) { this.notToBeRemoved = value; }
/** Holds the line objects that can be drawn via implementation */
public get Lines(): GraphicalLine[] { return this.lines; }
public get ContinuousDynamic(): ContinuousDynamicExpression { return this.SourceExpression as ContinuousDynamicExpression; }
//#endregion
//#region Public methods
public updateSkyBottomLine(): void {
// update Sky-BottomLine
const skyBottomLineCalculator: SkyBottomLineCalculator = this.parentStaffLine.SkyBottomLineCalculator;
const left: number = this.IsVerbal ? this.label.PositionAndShape.RelativePosition.x + this.label.PositionAndShape.BorderMarginLeft : 0;
const right: number = this.IsVerbal ? this.label.PositionAndShape.RelativePosition.x + this.label.PositionAndShape.BorderMarginRight : 0;
if (!this.IsSoftAccent && !this.IsVerbal && this.lines.length < 2) {
log.warn("Not enough lines for SkyBottomLine calculation");
}
if (!this.IsVerbal) {
if (this.ContinuousDynamic.DynamicType !== ContDynamicEnum.crescendo &&
this.ContinuousDynamic.DynamicType !== ContDynamicEnum.diminuendo) {
// for now there is only crescendo or decrescendo anyways, but this will catch errors when we add new types in the future
log.warn("GraphicalContinuousDynamicExpression.updateSkyBottomLine(): " +
"unhandled continuous dynamic type. start measure: " + this.startMeasure?.MeasureNumber);
}
}
switch (this.Placement) {
case PlacementEnum.Above:
if (this.IsSoftAccent) {
skyBottomLineCalculator.updateSkyLineWithWedge(this.lines[0].Start, this.lines[0].End);
skyBottomLineCalculator.updateSkyLineWithWedge(this.lines[2].End, this.lines[2].Start);
skyBottomLineCalculator.updateSkyLineWithLine(this.lines[0].End, this.lines[2].End, this.lines[0].End.y);
} else if (!this.IsVerbal) {
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
skyBottomLineCalculator.updateSkyLineWithWedge(this.lines[0].Start, this.lines[0].End);
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
skyBottomLineCalculator.updateSkyLineWithWedge(this.lines[0].End, this.lines[0].Start);
} // else covered with the log.warn above
} else {
const yValue: number = this.label.PositionAndShape.BorderMarginTop + this.label.PositionAndShape.RelativePosition.y;
skyBottomLineCalculator.updateSkyLineInRange(left, right, yValue);
}
break;
case PlacementEnum.Below:
if (!this.IsVerbal) {
// console.log(`id: ${this.parentStaffLine.ParentStaff.Id}`);
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
skyBottomLineCalculator.updateBottomLineWithWedge(this.lines[1].Start, this.lines[1].End);
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
skyBottomLineCalculator.updateBottomLineWithWedge(this.lines[1].End, this.lines[1].Start);
} // else covered with the log.warn above
} else {
const yValue: number = this.label.PositionAndShape.BorderMarginBottom + this.label.PositionAndShape.RelativePosition.y;
skyBottomLineCalculator.updateBottomLineInRange(left, right, yValue);
}
break;
default:
log.error("Placement for GraphicalContinuousDynamicExpression is unknown");
}
}
/**
* Calculate crescendo lines for (full).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeOpeningLength length of the opening
* @param wedgeLineWidth line width of the wedge
*/
public createCrescendoLines(startX: number, endX: number, y: number,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength, wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const lineStart: PointF2D = new PointF2D(startX, y);
const upperLineEnd: PointF2D = new PointF2D(endX, y - wedgeOpeningLength / 2);
const lowerLineEnd: PointF2D = new PointF2D(endX, y + wedgeOpeningLength / 2);
this.addWedgeLines(lineStart, upperLineEnd, lowerLineEnd, wedgeLineWidth);
}
/**
* Calculate crescendo lines for system break (first part).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeMeasureEndOpeningLength length of opening at measure end
* @param wedgeOpeningLength length of the opening
* @param wedgeLineWidth line width of the wedge
*/
public createFirstHalfCrescendoLines(startX: number, endX: number, y: number,
wedgeMeasureEndOpeningLength: number = this.rules.WedgeMeasureEndOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const lineStart: PointF2D = new PointF2D(startX, y);
const upperLineEnd: PointF2D = new PointF2D(endX, y - wedgeMeasureEndOpeningLength / 2);
const lowerLineEnd: PointF2D = new PointF2D(endX, y + wedgeMeasureEndOpeningLength / 2);
this.addWedgeLines(lineStart, upperLineEnd, lowerLineEnd, wedgeLineWidth);
}
/**
* Calculate crescendo lines for system break (second part).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeMeasureBeginOpeningLength length of opening at measure start
* @param wedgeOpeningLength length of the opening
* @param wedgeLineWidth line width of the wedge
*/
public createSecondHalfCrescendoLines(startX: number, endX: number, y: number,
wedgeMeasureBeginOpeningLength: number = this.rules.WedgeMeasureBeginOpeningLength,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const upperLineStart: PointF2D = new PointF2D(startX, y - wedgeMeasureBeginOpeningLength / 2);
const lowerLineStart: PointF2D = new PointF2D(startX, y + wedgeMeasureBeginOpeningLength / 2);
const upperLineEnd: PointF2D = new PointF2D(endX, y - wedgeOpeningLength / 2);
const lowerLineEnd: PointF2D = new PointF2D(endX, y + wedgeOpeningLength / 2);
this.addDoubleLines(upperLineStart, upperLineEnd, lowerLineStart, lowerLineEnd, wedgeLineWidth);
}
/**
* This method recalculates the Crescendo Lines (for all cases).
* @param startX left most starting point
* @param endX right most ending point
* @param y y placement
*/
public recalculateCrescendoLines(startX: number, endX: number, y: number): void {
const isSecondHalfSplit: boolean = Math.abs(this.lines[0].Start.y - this.lines[1].Start.y) > 0.0001;
this.lines.clear();
if (isSecondHalfSplit) {
this.createSecondHalfCrescendoLines(startX, endX, y);
} else if (this.isSplittedPart) {
this.createFirstHalfCrescendoLines(startX, endX, y);
} else {
this.createCrescendoLines(startX, endX, y);
}
}
/**
* Calculate diminuendo lines for system break (full).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeOpeningLength length of the opening
* @param wedgeLineWidth line width of the wedge
*/
public createDiminuendoLines(startX: number, endX: number, y: number,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength, wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const upperWedgeStart: PointF2D = new PointF2D(startX, y - wedgeOpeningLength / 2);
const lowerWedgeStart: PointF2D = new PointF2D(startX, y + wedgeOpeningLength / 2);
const wedgeEnd: PointF2D = new PointF2D(endX, y);
this.addWedgeLines(wedgeEnd, upperWedgeStart, lowerWedgeStart, wedgeLineWidth);
}
/**
* Calculate diminuendo lines for system break (first part).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeOpeningLength length of the opening
* @param wedgeMeasureEndOpeningLength length of opening at measure end
* @param wedgeLineWidth line width of the wedge
*/
public createFirstHalfDiminuendoLines(startX: number, endX: number, y: number,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength,
wedgeMeasureEndOpeningLength: number = this.rules.WedgeMeasureEndOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const upperLineStart: PointF2D = new PointF2D(startX, y - wedgeOpeningLength / 2);
const lowerLineStart: PointF2D = new PointF2D(startX, y + wedgeOpeningLength / 2);
const upperLineEnd: PointF2D = new PointF2D(endX, y - wedgeMeasureEndOpeningLength / 2);
const lowerLineEnd: PointF2D = new PointF2D(endX, y + wedgeMeasureEndOpeningLength / 2);
this.addDoubleLines(upperLineStart, upperLineEnd, lowerLineStart, lowerLineEnd, wedgeLineWidth);
}
/**
* Calculate diminuendo lines for system break (second part).
* @param startX left most starting point
* @param endX right mist ending point
* @param y y placement
* @param wedgeMeasureBeginOpeningLength length of opening at measure start
* @param wedgeLineWidth line width of the wedge
*/
public createSecondHalfDiminuendoLines(startX: number, endX: number, y: number,
wedgeMeasureBeginOpeningLength: number = this.rules.WedgeMeasureBeginOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth): void {
const upperLineStart: PointF2D = new PointF2D(startX, y - wedgeMeasureBeginOpeningLength / 2);
const lowerLineStart: PointF2D = new PointF2D(startX, y + wedgeMeasureBeginOpeningLength / 2);
const lineEnd: PointF2D = new PointF2D(endX, y);
this.addWedgeLines(lineEnd, upperLineStart, lowerLineStart, wedgeLineWidth);
}
/**
* This method recalculates the diminuendo lines (for all cases).
* @param startX left most starting point
* @param endX right most ending point
* @param y y placement
*/
public recalculateDiminuendoLines(startX: number, endX: number, yPosition: number): void {
const isFirstHalfSplit: boolean = Math.abs(this.lines[0].End.y - this.lines[1].End.y) > 0.0001;
this.lines.clear();
if (isFirstHalfSplit) {
this.createFirstHalfDiminuendoLines(startX, endX, yPosition);
} else if (this.isSplittedPart) {
this.createSecondHalfDiminuendoLines(startX, endX, yPosition);
} else {
this.createDiminuendoLines(startX, endX, yPosition);
}
}
/** Wrapper for createFirstHalfCrescendoLines and createFirstHalfDiminuendoLines.
* Checks whether `this` is crescendo or diminuendo, helps avoid code duplication.
*/
public createFirstHalfLines(startX: number, endX: number, y: number,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength,
wedgeMeasureEndOpeningLength: number = this.rules.WedgeMeasureEndOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth
): void {
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
this.createFirstHalfCrescendoLines(startX, endX, y,
wedgeMeasureEndOpeningLength, wedgeLineWidth);
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
this.createFirstHalfDiminuendoLines(startX, endX, y,
wedgeOpeningLength, wedgeMeasureEndOpeningLength, wedgeLineWidth);
}
}
/** Wrapper for createSecondHalfCrescendoLines and createSecondHalfDiminuendoLines, see createFirstHalfLines. */
public createSecondHalfLines(startX: number, endX: number, y: number,
wedgeMeasureBeginOpeningLength: number = this.rules.WedgeMeasureBeginOpeningLength,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength,
wedgeLineWidth: number = this.rules.WedgeLineWidth
): void {
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
this.createSecondHalfCrescendoLines(startX, endX, y,
wedgeMeasureBeginOpeningLength, wedgeOpeningLength, wedgeLineWidth);
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
this.createSecondHalfDiminuendoLines(startX, endX, y,
wedgeMeasureBeginOpeningLength, wedgeLineWidth);
}
}
/** Wrapper for createCrescendoLines and createDiminuendoLines, see createFirstHalfLines. */
public createLines(startX: number, endX: number, y: number,
wedgeOpeningLength: number = this.rules.WedgeOpeningLength, wedgeLineWidth: number = this.rules.WedgeLineWidth
): void {
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
this.createCrescendoLines(startX, endX, y,
wedgeOpeningLength, wedgeLineWidth);
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.diminuendo) {
this.createDiminuendoLines(startX, endX, y,
wedgeOpeningLength, wedgeLineWidth);
}
}
/**
* Calculate the BoundingBox (as a box around the Wedge).
*/
public calcPsi(): void {
if (this.IsVerbal) {
this.PositionAndShape.calculateBoundingBox();
return;
}
this.PositionAndShape.RelativePosition = this.lines[0].Start;
this.PositionAndShape.BorderMarginTop = this.lines[0].End.y - this.lines[0].Start.y;
this.PositionAndShape.BorderMarginBottom = this.lines[1].End.y - this.lines[1].Start.y;
this.PositionAndShape.Center.y = (this.PositionAndShape.BorderMarginTop + this.PositionAndShape.BorderMarginBottom) / 2;
// TODO is the center position correct? it wasn't set before, important for AlignmentManager.alignDynamicExpressions()
// console.log(`relative y, center y: ${this.PositionAndShape.RelativePosition.y},${this.PositionAndShape.Center.y})`);
if (this.IsSoftAccent) {
this.PositionAndShape.BorderMarginLeft = 0;
this.PositionAndShape.BorderMarginRight = this.lines[3].Start.x - this.lines[0].Start.x;
} else if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
this.PositionAndShape.BorderMarginLeft = 0;
this.PositionAndShape.BorderMarginRight = this.lines[0].End.x - this.lines[0].Start.x;
} else {
this.PositionAndShape.BorderMarginLeft = this.lines[0].End.x - this.lines[0].Start.x;
this.PositionAndShape.BorderMarginRight = 0;
}
}
/**
* Clear Lines
*/
public cleanUp(): void {
this.lines.clear();
}
/**
* Shift wedge in y position
* @param shift Number to shift
*/
public shiftYPosition(shift: number): void {
if (this.IsVerbal) {
this.PositionAndShape.RelativePosition.y += shift;
this.PositionAndShape.calculateBoundingBox();
} else {
this.lines[0].Start.y += shift;
this.lines[0].End.y += shift;
this.lines[1].End.y += shift;
}
}
public squeeze(value: number): void {
// Verbal expressions are not squeezable and squeezing below the width is also not possible
if (this.IsVerbal) {
return;
}
const width: number = Math.abs(this.lines[0].End.x - this.lines[0].Start.x);
if (width < Math.abs(value)) {
return;
}
if (this.ContinuousDynamic.DynamicType === ContDynamicEnum.crescendo) {
if (value > 0) {
this.lines[0].Start.x += value;
} else {
this.lines[0].End.x += value;
this.lines[1].End.x += value;
}
} else {
if (value < 0) {
this.lines[0].Start.x += value;
} else {
this.lines[0].End.x += value;
this.lines[1].End.x += value;
}
}
this.calcPsi();
}
//#endregion
//#region Private methods
/**
* Create lines from points and add them to the memory
* @param wedgePoint start of the expression
* @param upperWedgeEnd end of the upper line
* @param lowerWedgeEnd end of lower line
* @param wedgeLineWidth line width
*/
private addWedgeLines(wedgePoint: PointF2D, upperWedgeEnd: PointF2D, lowerWedgeEnd: PointF2D, wedgeLineWidth: number): void {
const upperLine: GraphicalLine = new GraphicalLine(wedgePoint, upperWedgeEnd, wedgeLineWidth);
const lowerLine: GraphicalLine = new GraphicalLine(wedgePoint, lowerWedgeEnd, wedgeLineWidth);
if (this.rules.DefaultColorMusic) {
upperLine.colorHex = this.rules.DefaultColorMusic;
lowerLine.colorHex = this.rules.DefaultColorMusic;
}
this.lines.push(upperLine);
this.lines.push(lowerLine);
}
/**
* Create top and bottom lines for continuing wedges
* @param upperLineStart start of the upper line
* @param upperLineEnd end of the upper line
* @param lowerLineStart start of the lower line
* @param lowerLineEnd end of lower line
* @param wedgeLineWidth line width
*/
private addDoubleLines(upperLineStart: PointF2D, upperLineEnd: PointF2D, lowerLineStart: PointF2D, lowerLineEnd: PointF2D, wedgeLineWidth: number): void {
const upperLine: GraphicalLine = new GraphicalLine(upperLineStart, upperLineEnd, wedgeLineWidth);
const lowerLine: GraphicalLine = new GraphicalLine(lowerLineStart, lowerLineEnd, wedgeLineWidth);
if (this.rules.DefaultColorMusic) {
upperLine.colorHex = this.rules.DefaultColorMusic;
lowerLine.colorHex = this.rules.DefaultColorMusic;
}
this.lines.push(upperLine);
this.lines.push(lowerLine);
}
//#endregion
}