/
BoundingBox.ts
647 lines (578 loc) · 26.9 KB
/
BoundingBox.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import {ArgumentOutOfRangeException} from "../Exceptions";
import {PointF2D} from "../../Common/DataObjects/PointF2D";
import {SizeF2D} from "../../Common/DataObjects/SizeF2D";
import {RectangleF2D} from "../../Common/DataObjects/RectangleF2D";
/**
* A bounding box delimits an area on the 2D plane.
*/
export class BoundingBox {
protected isSymbol: boolean = false;
protected relativePositionHasBeenSet: boolean = false;
protected xBordersHaveBeenSet: boolean = false;
protected yBordersHaveBeenSet: boolean = false;
protected absolutePosition: PointF2D = new PointF2D();
protected relativePosition: PointF2D = new PointF2D();
protected size: SizeF2D = new SizeF2D();
protected marginSize: SizeF2D;
protected upperLeftCorner: PointF2D;
protected upperLeftMarginCorner: PointF2D;
protected borderLeft: number = 0;
protected borderRight: number = 0;
protected borderTop: number = 0;
protected borderBottom: number = 0;
protected borderMarginLeft: number = 0;
protected borderMarginRight: number = 0;
protected borderMarginTop: number = 0;
protected borderMarginBottom: number = 0;
protected boundingRectangle: RectangleF2D;
protected boundingMarginRectangle: RectangleF2D;
protected childElements: BoundingBox[] = [];
protected parent: BoundingBox;
protected dataObject: Object;
constructor(dataObject: Object = undefined, parent: BoundingBox = undefined) {
this.parent = parent;
this.dataObject = dataObject;
this.xBordersHaveBeenSet = false;
this.yBordersHaveBeenSet = false;
}
public get RelativePositionHasBeenSet(): boolean {
return this.relativePositionHasBeenSet;
}
public get XBordersHaveBeenSet(): boolean {
return this.xBordersHaveBeenSet;
}
public set XBordersHaveBeenSet(value: boolean) {
this.xBordersHaveBeenSet = value;
}
public get YBordersHaveBeenSet(): boolean {
return this.yBordersHaveBeenSet;
}
public set YBordersHaveBeenSet(value: boolean) {
this.yBordersHaveBeenSet = value;
}
public get AbsolutePosition(): PointF2D {
return this.absolutePosition;
}
public set AbsolutePosition(value: PointF2D) {
this.absolutePosition = value;
}
public get RelativePosition(): PointF2D {
return this.relativePosition;
}
public set RelativePosition(value: PointF2D) {
this.relativePosition = value;
this.relativePositionHasBeenSet = true;
}
public get Size(): SizeF2D {
return this.size;
}
public set Size(value: SizeF2D) {
this.size = value;
}
public get MarginSize(): SizeF2D {
return this.marginSize;
}
public get UpperLeftCorner(): PointF2D {
return this.upperLeftCorner;
}
public get UpperLeftMarginCorner(): PointF2D {
return this.upperLeftMarginCorner;
}
public get BorderLeft(): number {
return this.borderLeft;
}
public set BorderLeft(value: number) {
this.borderLeft = value;
this.calculateRectangle();
}
public get BorderRight(): number {
return this.borderRight;
}
public set BorderRight(value: number) {
this.borderRight = value;
this.calculateRectangle();
}
public get BorderTop(): number {
return this.borderTop;
}
public set BorderTop(value: number) {
this.borderTop = value;
this.calculateRectangle();
}
public get BorderBottom(): number {
return this.borderBottom;
}
public set BorderBottom(value: number) {
this.borderBottom = value;
this.calculateRectangle();
}
public get BorderMarginLeft(): number {
return this.borderMarginLeft;
}
public set BorderMarginLeft(value: number) {
this.borderMarginLeft = value;
this.calculateMarginRectangle();
}
public get BorderMarginRight(): number {
return this.borderMarginRight;
}
public set BorderMarginRight(value: number) {
this.borderMarginRight = value;
this.calculateMarginRectangle();
}
public get BorderMarginTop(): number {
return this.borderMarginTop;
}
public set BorderMarginTop(value: number) {
this.borderMarginTop = value;
this.calculateMarginRectangle();
}
public get BorderMarginBottom(): number {
return this.borderMarginBottom;
}
public set BorderMarginBottom(value: number) {
this.borderMarginBottom = value;
this.calculateMarginRectangle();
}
public get BoundingRectangle(): RectangleF2D {
return this.boundingRectangle;
}
public get BoundingMarginRectangle(): RectangleF2D {
return this.boundingMarginRectangle;
}
public get ChildElements(): BoundingBox[] {
return this.childElements;
}
public set ChildElements(value: BoundingBox[]) {
this.childElements = value;
}
public get Parent(): BoundingBox {
return this.parent;
}
public set Parent(value: BoundingBox) {
this.parent = value;
}
public get DataObject(): Object {
return this.dataObject;
}
public setAbsolutePositionFromParent(): void {
if (this.parent !== undefined) {
this.absolutePosition.x = this.parent.AbsolutePosition.x + this.relativePosition.x;
this.absolutePosition.y = this.parent.AbsolutePosition.y + this.relativePosition.y;
} else {
this.absolutePosition = this.relativePosition;
}
}
/**
* This method calculates the Absolute Positions recursively
*/
public calculateAbsolutePositionsRecursiveWithoutTopelement(): void {
this.absolutePosition.x = 0.0;
this.absolutePosition.y = 0.0;
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let child: BoundingBox = this.ChildElements[idx];
child.calculateAbsolutePositionsRecursive(this.absolutePosition.x, this.absolutePosition.y);
}
}
/**
* This method calculates the Absolute Positions recursively
* @param x
* @param y
*/
public calculateAbsolutePositionsRecursive(x: number, y: number): void {
this.absolutePosition.x = this.relativePosition.x + x;
this.absolutePosition.y = this.relativePosition.y + y;
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let child: BoundingBox = this.ChildElements[idx];
child.calculateAbsolutePositionsRecursive(this.absolutePosition.x, this.absolutePosition.y);
}
}
/**
* This method calculates the BoundingBoxes
*/
public calculateBoundingBox(): void {
if (this.childElements.length === 0) {
return;
}
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
childElement.calculateBoundingBox();
}
// initialize with max/min values
let minLeft: number = Number.MAX_VALUE;
let maxRight: number = Number.MIN_VALUE;
let minTop: number = Number.MAX_VALUE;
let maxBottom: number = Number.MIN_VALUE;
let minMarginLeft: number = Number.MAX_VALUE;
let maxMarginRight: number = Number.MIN_VALUE;
let minMarginTop: number = Number.MAX_VALUE;
let maxMarginBottom: number = Number.MIN_VALUE;
// apart from symbol elements, where we initialize with the symbol's borders
if (this.isSymbol) {
minLeft = this.borderLeft;
maxRight = this.borderRight;
minTop = this.borderTop;
maxBottom = this.borderBottom;
minMarginLeft = this.borderMarginLeft;
maxMarginRight = this.borderMarginRight;
minMarginTop = this.borderMarginTop;
maxMarginBottom = this.borderMarginBottom;
}
// ChildElements will have their borders calculated, so calculate current borders
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
minLeft = Math.min(minLeft, childElement.relativePosition.x + childElement.borderLeft);
maxRight = Math.max(maxRight, childElement.relativePosition.x + childElement.borderRight);
minTop = Math.min(minTop, childElement.relativePosition.y + childElement.borderTop);
maxBottom = Math.max(maxBottom, childElement.relativePosition.y + childElement.borderBottom);
minMarginLeft = Math.min(minMarginLeft, childElement.relativePosition.x + childElement.borderMarginLeft);
maxMarginRight = Math.max(maxMarginRight, childElement.relativePosition.x + childElement.borderMarginRight);
minMarginTop = Math.min(minMarginTop, childElement.relativePosition.y + childElement.borderMarginTop);
maxMarginBottom = Math.max(maxMarginBottom, childElement.relativePosition.y + childElement.borderMarginBottom);
}
// ChildElements will have their borders calculated, so calculate current borders
this.borderLeft = minLeft;
this.borderRight = maxRight;
this.borderTop = minTop;
this.borderBottom = maxBottom;
this.borderMarginLeft = minMarginLeft;
this.borderMarginRight = maxMarginRight;
this.borderMarginTop = minMarginTop;
this.borderMarginBottom = maxMarginBottom;
this.calculateRectangle();
this.calculateMarginRectangle();
this.xBordersHaveBeenSet = true;
this.yBordersHaveBeenSet = true;
}
public calculateTopBottomBorders(): void {
if (this.childElements.length === 0) {
return;
}
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
childElement.calculateTopBottomBorders();
}
let minTop: number = Number.MAX_VALUE;
let maxBottom: number = Number.MIN_VALUE;
let minMarginTop: number = Number.MAX_VALUE;
let maxMarginBottom: number = Number.MIN_VALUE;
if (this.yBordersHaveBeenSet) {
minTop = this.borderTop;
maxBottom = this.borderBottom;
minMarginTop = this.borderMarginTop;
maxMarginBottom = this.borderMarginBottom;
}
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
minTop = Math.min(minTop, childElement.relativePosition.y + childElement.borderTop);
maxBottom = Math.max(maxBottom, childElement.relativePosition.y + childElement.borderBottom);
minMarginTop = Math.min(minMarginTop, childElement.relativePosition.y + childElement.borderMarginTop);
maxMarginBottom = Math.max(maxMarginBottom, childElement.relativePosition.y + childElement.borderMarginBottom);
}
this.borderTop = minTop;
this.borderBottom = maxBottom;
this.borderMarginTop = minMarginTop;
this.borderMarginBottom = maxMarginBottom;
this.calculateRectangle();
this.calculateMarginRectangle();
}
/**
* This method computes the first non-overlapping position in the placementPsi Element for the current (this) positionAndShapeInfo
* @param placementPsi
* @param direction
* @param position
*/
public computeNonOverlappingPositionWithMargin(placementPsi: BoundingBox, direction: ColDirEnum, position: PointF2D): void {
this.RelativePosition = new PointF2D(position.x, position.y);
this.setAbsolutePositionFromParent();
let currentPosition: number = 0.0;
let hasBeenMoved: boolean = false;
do {
switch (direction) {
case ColDirEnum.Left:
case ColDirEnum.Right:
currentPosition = this.relativePosition.x;
placementPsi.calculateMarginPositionAlongDirection(this, direction);
hasBeenMoved = Math.abs(currentPosition - this.relativePosition.x) > 0.001;
break;
case ColDirEnum.Up:
case ColDirEnum.Down:
currentPosition = this.relativePosition.y;
placementPsi.calculateMarginPositionAlongDirection(this, direction);
hasBeenMoved = Math.abs(currentPosition - this.relativePosition.y) > 0.001;
break;
default:
throw new ArgumentOutOfRangeException("direction");
}
}
while (hasBeenMoved);
}
/**
* This method detects a collision (without margins)
* @param psi
* @returns {boolean}
*/
public collisionDetection(psi: BoundingBox): boolean {
let overlapWidth: number = Math.min(this.AbsolutePosition.x + this.borderRight, psi.absolutePosition.x + psi.borderRight)
- Math.max(this.AbsolutePosition.x + this.borderLeft, psi.absolutePosition.x + psi.borderLeft);
let overlapHeight: number = Math.min(this.AbsolutePosition.y + this.borderBottom, psi.absolutePosition.y + psi.borderBottom)
- Math.max(this.AbsolutePosition.y + this.borderTop, psi.absolutePosition.y + psi.borderTop);
if (overlapWidth > 0 && overlapHeight > 0) {
return true;
}
return false;
}
/**
* This method checks if the given Psi's Margins lie inside the current Psi's Margins.
* @param psi
* @returns {boolean}
*/
public liesInsideBorders(psi: BoundingBox): boolean {
let leftBorderInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= (psi.absolutePosition.x + psi.borderLeft)
&& (psi.absolutePosition.x + psi.borderLeft) <= (this.AbsolutePosition.x + this.borderRight);
let rightBorderInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= (psi.absolutePosition.x + psi.borderRight)
&& (psi.absolutePosition.x + psi.borderRight) <= (this.AbsolutePosition.x + this.borderRight);
if (leftBorderInside && rightBorderInside) {
let topBorderInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= (psi.absolutePosition.y + psi.borderTop)
&& (psi.absolutePosition.y + psi.borderTop) <= (this.AbsolutePosition.y + this.borderBottom);
let bottomBorderInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= (psi.absolutePosition.y + psi.borderBottom)
&& (psi.absolutePosition.y + psi.borderBottom) <= (this.AbsolutePosition.y + this.borderBottom);
if (topBorderInside && bottomBorderInside) {
return true;
}
}
return false;
}
public pointLiesInsideBorders(position: PointF2D): boolean {
let xInside: boolean = (this.AbsolutePosition.x + this.borderLeft) <= position.x && position.x <= (this.AbsolutePosition.x + this.borderRight);
if (xInside) {
let yInside: boolean = (this.AbsolutePosition.y + this.borderTop) <= position.y && position.y <= (this.AbsolutePosition.y + this.borderBottom);
if (yInside) {
return true;
}
}
return false;
}
/**
* This method detects a collision (margin-wide)
* @param psi
* @returns {boolean}
*/
public marginCollisionDetection(psi: BoundingBox): boolean {
let overlapWidth: number = Math.min(this.AbsolutePosition.x + this.borderMarginRight, psi.absolutePosition.x + psi.borderMarginRight)
- Math.max(this.AbsolutePosition.x + this.borderMarginLeft, psi.absolutePosition.x + psi.borderMarginLeft);
let overlapHeight: number = Math.min(this.AbsolutePosition.y + this.borderMarginBottom, psi.absolutePosition.y + psi.borderMarginBottom)
- Math.max(this.AbsolutePosition.y + this.borderMarginTop, psi.absolutePosition.y + psi.borderMarginTop);
if (overlapWidth > 0 && overlapHeight > 0) {
return true;
}
return false;
}
/**
* This method checks if the given Psi's Margins lie inside the current Psi's Margins
* @param psi
* @returns {boolean}
*/
public liesInsideMargins(psi: BoundingBox): boolean {
let leftMarginInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= (psi.absolutePosition.x + psi.borderMarginLeft)
&& (psi.absolutePosition.x + psi.borderMarginLeft) <= (this.AbsolutePosition.x + this.borderMarginRight);
let rightMarginInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= (psi.absolutePosition.x + psi.borderMarginRight)
&& (psi.absolutePosition.x + psi.borderMarginRight) <= (this.AbsolutePosition.x + this.borderMarginRight);
if (leftMarginInside && rightMarginInside) {
let topMarginInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= (psi.absolutePosition.y + psi.borderMarginTop)
&& (psi.absolutePosition.y + psi.borderMarginTop) <= (this.AbsolutePosition.y + this.borderMarginBottom);
let bottomMarginInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= (psi.absolutePosition.y + psi.borderMarginBottom)
&& (psi.absolutePosition.y + psi.borderMarginBottom) <= (this.AbsolutePosition.y + this.borderMarginBottom);
if (topMarginInside && bottomMarginInside) {
return true;
}
}
return false;
}
public pointLiesInsideMargins(position: PointF2D): boolean {
let xInside: boolean = (this.AbsolutePosition.x + this.borderMarginLeft) <= position.x
&& position.x <= (this.AbsolutePosition.x + this.borderMarginRight);
if (xInside) {
let yInside: boolean = (this.AbsolutePosition.y + this.borderMarginTop) <= position.y
&& position.y <= (this.AbsolutePosition.y + this.borderMarginBottom);
if (yInside) {
return true;
}
}
return false;
}
/**
* This method computes the first non-overlapping position in the placementPsi Element for the current (this) positionAndShapeInfo
* @param placementPsi
* @param direction
* @param position
*/
public computeNonOverlappingPosition(placementPsi: BoundingBox, direction: ColDirEnum, position: PointF2D): void {
this.RelativePosition = new PointF2D(position.x, position.y);
this.setAbsolutePositionFromParent();
let currentPosition: number = 0.0;
let hasBeenMoved: boolean = false;
do {
switch (direction) {
case ColDirEnum.Left:
case ColDirEnum.Right:
currentPosition = this.relativePosition.x;
placementPsi.calculatePositionAlongDirection(this, direction);
hasBeenMoved = Math.abs(currentPosition - this.relativePosition.x) > 0.0001;
break;
case ColDirEnum.Up:
case ColDirEnum.Down:
currentPosition = this.relativePosition.y;
placementPsi.calculatePositionAlongDirection(this, direction);
hasBeenMoved = Math.abs(currentPosition - this.relativePosition.y) > 0.0001;
break;
default:
throw new ArgumentOutOfRangeException("direction");
}
} while (hasBeenMoved); // as long as the element is moved
}
public getClickedObjectOfType<T>(clickPosition: PointF2D): T {
let obj: Object = this.dataObject;
if (this.pointLiesInsideBorders(clickPosition) && (<T>obj !== undefined)) {
return (obj as T);
}
for (let idx: number = 0, len: number = this.childElements.length; idx < len; ++idx) {
let psi: BoundingBox = this.childElements[idx];
let innerObject: Object = psi.getClickedObjectOfType<T>(clickPosition);
if (innerObject !== undefined) {
return (innerObject as T);
}
}
return undefined;
}
public getObjectsInRegion<T>(region: BoundingBox, liesInside: boolean = true): T[] {
if (<T>this.dataObject !== undefined) {
if (liesInside) {
if (region.liesInsideBorders(this)) {
return [this.dataObject as T];
}
} else {
if (region.collisionDetection(this)) {
return [this.dataObject as T];
}
}
// FIXME Andrea: add here "return []"?
}
let result: T[] = [];
for (let child of this.childElements) {
result.concat(child.getObjectsInRegion<T>(region, liesInside));
}
return result;
//return this.childElements.SelectMany(psi => psi.getObjectsInRegion<T>(region, liesInside));
}
protected calculateRectangle(): void {
this.upperLeftCorner = new PointF2D(this.borderLeft, this.borderTop);
this.size = new SizeF2D(this.borderRight - this.borderLeft, this.borderBottom - this.borderTop);
this.boundingRectangle = RectangleF2D.createFromLocationAndSize(this.upperLeftCorner, this.size);
}
protected calculateMarginRectangle(): void {
this.upperLeftMarginCorner = new PointF2D(this.borderMarginLeft, this.borderMarginTop);
this.marginSize = new SizeF2D(this.borderMarginRight - this.borderMarginLeft, this.borderMarginBottom - this.borderMarginTop);
this.boundingMarginRectangle = RectangleF2D.createFromLocationAndSize(this.upperLeftMarginCorner, this.marginSize);
}
/**
* This method calculates the margin border along the given direction so that no collision takes place along this direction
* @param toBePlaced
* @param direction
*/
private calculateMarginPositionAlongDirection(toBePlaced: BoundingBox, direction: ColDirEnum): void {
// now this will be the "known" Element, about to get bigger with the toBePlaced
// eg toBePlaced will always be in the PositionAndShape hierarchy a Child of this
// example: this = StaffEntry, toBePlaced = Accidental
// logical return
if (this === toBePlaced) {
return;
}
// check for collision only at symbols and return border
if (this.isSymbol && this.marginCollisionDetection(toBePlaced)) {
let shiftDistance: number = 0;
switch (direction) {
case ColDirEnum.Left:
shiftDistance = (this.absolutePosition.x + this.borderMarginLeft) - (toBePlaced.absolutePosition.x + toBePlaced.borderMarginRight);
toBePlaced.relativePosition.x += shiftDistance;
toBePlaced.absolutePosition.x += shiftDistance;
return;
case ColDirEnum.Right:
shiftDistance = (this.absolutePosition.x + this.borderMarginRight) - (toBePlaced.absolutePosition.x + toBePlaced.borderMarginLeft);
toBePlaced.relativePosition.x += shiftDistance;
toBePlaced.absolutePosition.x += shiftDistance;
return;
case ColDirEnum.Up:
shiftDistance = (this.absolutePosition.y + this.borderMarginTop) - (toBePlaced.absolutePosition.y + toBePlaced.borderMarginBottom);
toBePlaced.relativePosition.y += shiftDistance;
toBePlaced.absolutePosition.y += shiftDistance;
return;
case ColDirEnum.Down:
shiftDistance = (this.absolutePosition.y + this.borderMarginBottom) - (toBePlaced.absolutePosition.y + toBePlaced.borderMarginTop);
toBePlaced.relativePosition.y += shiftDistance;
toBePlaced.absolutePosition.y += shiftDistance;
return;
default:
throw new ArgumentOutOfRangeException("direction");
}
}
// perform check for all children iteratively and return border from children symbols
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
childElement.calculateMarginPositionAlongDirection(toBePlaced, direction);
}
}
/**
* This method calculates the border along the given direction so that no collision takes place along this direction
* @param toBePlaced
* @param direction
*/
private calculatePositionAlongDirection(toBePlaced: BoundingBox, direction: ColDirEnum): void {
// now this will be the "known" Element, about to get bigger with the toBePlaced
// eg toBePlaced will always be in the PositionAndShape hierarchy a Child of this
// example: this = StaffEntry, toBePlaced = Accidental
// logical return
if (this === toBePlaced) {
return;
}
// check for collision only at symbols and return border
if (this.isSymbol && this.collisionDetection(toBePlaced)) {
let shiftDistance: number;
switch (direction) {
case ColDirEnum.Left:
shiftDistance = (this.absolutePosition.x + this.borderLeft) - (toBePlaced.absolutePosition.x + toBePlaced.borderRight);
toBePlaced.relativePosition.x += shiftDistance;
toBePlaced.absolutePosition.x += shiftDistance;
return;
case ColDirEnum.Right:
shiftDistance = (this.absolutePosition.x + this.borderRight) - (toBePlaced.absolutePosition.x + toBePlaced.borderLeft);
toBePlaced.relativePosition.x += shiftDistance;
toBePlaced.absolutePosition.x += shiftDistance;
return;
case ColDirEnum.Up:
shiftDistance = (this.absolutePosition.y + this.borderTop) - (toBePlaced.absolutePosition.y + toBePlaced.borderBottom);
toBePlaced.relativePosition.y += shiftDistance;
toBePlaced.absolutePosition.y += shiftDistance;
return;
case ColDirEnum.Down:
shiftDistance = (this.absolutePosition.y + this.borderBottom) - (toBePlaced.absolutePosition.y + toBePlaced.borderTop);
toBePlaced.relativePosition.y += shiftDistance;
toBePlaced.absolutePosition.y += shiftDistance;
return;
default:
throw new ArgumentOutOfRangeException("direction");
}
}
// perform check for all children iteratively and return border from children symbols
for (let idx: number = 0, len: number = this.ChildElements.length; idx < len; ++idx) {
let childElement: BoundingBox = this.ChildElements[idx];
childElement.calculatePositionAlongDirection(toBePlaced, direction);
}
}
}
export enum ColDirEnum {
Left = 0,
Right = 1,
Up = 2,
Down = 3
}