Skip to content

Commit 2279e88

Browse files
committed
refactor: merge duplicate text positioning functions
1 parent ed4f922 commit 2279e88

File tree

1 file changed

+20
-60
lines changed

1 file changed

+20
-60
lines changed

src/app/view/editor-main-view/data-views/trainrunsections.view.ts

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ export class TrainrunSectionsView {
5454
static translateAndRotateText(
5555
trainrunSection: TrainrunSection,
5656
trainrunSectionText: TrainrunSectionText,
57+
viewObject?: TrainrunSectionViewObject,
5758
) {
5859
const x = trainrunSection.getTextPositionX(trainrunSectionText);
5960
const y = trainrunSection.getTextPositionY(trainrunSectionText);
6061

61-
const pathVec2D: Vec2D[] = trainrunSection.getPath();
62+
// Use viewObject path if provided, otherwise use trainrunSection path
63+
const pathVec2D: Vec2D[] = viewObject
64+
? viewObject.trainrunSections[0].getPath()
65+
: trainrunSection.getPath();
66+
67+
// Check if path has enough points
68+
if (pathVec2D.length < 4) {
69+
return "translate(" + x + "," + y + ") rotate(0, 0,0) ";
70+
}
71+
6272
const s1: Vec2D = pathVec2D[1];
6373
const t1: Vec2D = pathVec2D[2];
6474
const diff: Vec2D = Vec2D.sub(t1, s1);
@@ -76,41 +86,6 @@ export class TrainrunSectionsView {
7686
return "translate(" + x + "," + y + ") rotate(" + a + ", 0,0) ";
7787
}
7888

79-
/**
80-
* Version that works with TrainrunSectionViewObject to handle custom paths for collapsed chains
81-
* This method needs access to the view instance to get the collapsed path
82-
*/
83-
static translateAndRotateTextForViewObject(
84-
viewObject: TrainrunSectionViewObject,
85-
trainrunSectionText: TrainrunSectionText,
86-
) {
87-
const trainrunSection = viewObject.trainrunSections[0];
88-
89-
const x = trainrunSection.getTextPositionX(trainrunSectionText);
90-
const y = trainrunSection.getTextPositionY(trainrunSectionText);
91-
const originalPath = trainrunSection.getPath();
92-
93-
if (originalPath.length >= 4) {
94-
const s1: Vec2D = originalPath[1];
95-
const t1: Vec2D = originalPath[2];
96-
const diff: Vec2D = Vec2D.sub(t1, s1);
97-
let a: number = (Math.atan2(diff.getY(), diff.getX()) / Math.PI) * 180.0;
98-
if (Math.abs(a) > ANGLE_UPSIDE_DOWN_THRESHOLD) {
99-
a = a + 180;
100-
}
101-
// Math.atan2 -> edge cases -> correct manually
102-
if (Math.abs(diff.getX()) < EDGE_CASE_THRESHOLD) {
103-
a = DEFAULT_ANGLE_VERTICAL;
104-
}
105-
if (Math.abs(diff.getY()) < EDGE_CASE_THRESHOLD) {
106-
a = DEFAULT_ANGLE_HORIZONTAL;
107-
}
108-
return "translate(" + x + "," + y + ") rotate(" + a + ", 0,0) ";
109-
}
110-
111-
return "translate(" + x + "," + y + ") rotate(0, 0,0) ";
112-
}
113-
11489
private getTextPositionsForCollapsedChain(
11590
collapsedChainPath: Vec2D[],
11691
trainrunSection: TrainrunSection,
@@ -189,9 +164,10 @@ export class TrainrunSectionsView {
189164
return `translate(${x},${y}) rotate(${angle}, 0,0) `;
190165
}
191166

192-
return TrainrunSectionsView.translateAndRotateTextForViewObject(
193-
viewObject,
167+
return TrainrunSectionsView.translateAndRotateText(
168+
trainrunSection,
194169
trainrunSectionText,
170+
viewObject,
195171
);
196172
}
197173

@@ -497,6 +473,7 @@ export class TrainrunSectionsView {
497473
static getAdditionPositioningValue(
498474
trainrunSection: TrainrunSection,
499475
textElement: TrainrunSectionText,
476+
viewObject?: TrainrunSectionViewObject,
500477
) {
501478
switch (textElement) {
502479
case TrainrunSectionText.SourceDeparture:
@@ -506,28 +483,11 @@ export class TrainrunSectionsView {
506483
return 1.5;
507484
case TrainrunSectionText.TrainrunSectionTravelTime:
508485
case TrainrunSectionText.TrainrunSectionName:
509-
return TrainrunSectionsView.translateAndRotateText(trainrunSection, textElement);
510-
default:
511-
return 0;
512-
}
513-
}
514-
515-
/**
516-
* Version that works with TrainrunSectionViewObject for collapsed chains
517-
*/
518-
static getAdditionPositioningValueForViewObject(
519-
viewObject: TrainrunSectionViewObject,
520-
textElement: TrainrunSectionText,
521-
) {
522-
switch (textElement) {
523-
case TrainrunSectionText.SourceDeparture:
524-
case TrainrunSectionText.SourceArrival:
525-
case TrainrunSectionText.TargetDeparture:
526-
case TrainrunSectionText.TargetArrival:
527-
return 1.5;
528-
case TrainrunSectionText.TrainrunSectionTravelTime:
529-
case TrainrunSectionText.TrainrunSectionName:
530-
return TrainrunSectionsView.translateAndRotateTextForViewObject(viewObject, textElement);
486+
return TrainrunSectionsView.translateAndRotateText(
487+
trainrunSection,
488+
textElement,
489+
viewObject,
490+
);
531491
default:
532492
return 0;
533493
}

0 commit comments

Comments
 (0)