forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer-lines-feature-manager.ts
127 lines (119 loc) · 4.58 KB
/
transfer-lines-feature-manager.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
import type { Store } from '@ngrx/store';
// eslint-disable-next-line @typescript-eslint/no-shadow
import type { Element } from 'digital-fuesim-manv-shared';
import type { MapBrowserEvent } from 'ol';
import { Feature } from 'ol';
import LineString from 'ol/geom/LineString';
import type { TranslateEvent } from 'ol/interaction/Translate';
import type VectorLayer from 'ol/layer/Vector';
import type OlMap from 'ol/Map';
import type VectorSource from 'ol/source/Vector';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import type { Subject } from 'rxjs';
import type { TransferLine } from 'src/app/shared/types/transfer-line';
import type { AppState } from 'src/app/state/app.state';
import { selectTransferLines } from 'src/app/state/application/selectors/exercise.selectors';
import { selectCurrentRole } from 'src/app/state/application/selectors/shared.selectors';
import { selectStateSnapshot } from 'src/app/state/get-state-snapshot';
import type { TransferLinesService } from '../../core/transfer-lines.service';
import type { FeatureManager } from '../utility/feature-manager';
import type { OlMapInteractionsManager } from '../utility/ol-map-interactions-manager';
import { ElementManager } from './element-manager';
export class TransferLinesFeatureManager
extends ElementManager<TransferLine, LineString>
implements FeatureManager<LineString>
{
public readonly layer: VectorLayer<VectorSource<LineString>>;
constructor(
private readonly store: Store<AppState>,
private readonly transferLinesService: TransferLinesService,
private readonly olMap: OlMap
) {
super();
this.layer = this.createElementLayer<LineString>();
this.layer.setStyle(
new Style({
stroke: new Stroke({
color: '#fd7e14',
// We don't want to scale with the zoom to be better seen when zoomed out
width: 3,
}),
})
);
}
register(
destroy$: Subject<void>,
mapInteractionsManager: OlMapInteractionsManager
) {
this.olMap.addLayer(this.layer);
mapInteractionsManager.addFeatureLayer(this.layer);
if (selectStateSnapshot(selectCurrentRole, this.store) === 'trainer') {
this.registerChangeHandlers(
this.store.select(selectTransferLines),
destroy$,
(element) => this.onElementCreated(element),
(element) => this.onElementDeleted(element),
(oldElement, newElement) =>
this.onElementChanged(oldElement, newElement)
);
this.transferLinesService.displayTransferLines$.subscribe(
(display) => {
this.layer.setVisible(display);
}
);
}
}
createFeature(element: TransferLine): Feature<LineString> {
const feature = new Feature(
new LineString([
[element.startPosition.x, element.startPosition.y],
[element.endPosition.x, element.endPosition.y],
])
);
feature.setId(element.id);
this.layer.getSource()!.addFeature(feature);
return feature;
}
deleteFeature(
element: TransferLine,
elementFeature: Feature<LineString>
): void {
this.layer.getSource()!.removeFeature(elementFeature);
}
changeFeature(
oldElement: TransferLine,
newElement: TransferLine,
changedProperties: ReadonlySet<keyof TransferLine>,
elementFeature: Feature<LineString>
): void {
// Rendering the line again is expensive, so we only do it if we must
if (
changedProperties.has('startPosition') ||
changedProperties.has('endPosition')
) {
elementFeature.getGeometry()!.setCoordinates([
[newElement.startPosition.x, newElement.startPosition.y],
[newElement.endPosition.x, newElement.endPosition.y],
]);
}
}
onFeatureClicked(
event: MapBrowserEvent<any>,
feature: Feature<LineString>
// eslint-disable-next-line @typescript-eslint/no-empty-function
) {}
onFeatureDrop(
droppedElement: Element,
droppedOnFeature: Feature<LineString>,
dropEvent?: TranslateEvent
) {
return false;
}
getFeatureFromElement(element: TransferLine) {
return this.layer.getSource()!.getFeatureById(element.id) ?? undefined;
}
isFeatureTranslatable(feature: Feature<LineString>) {
return false;
}
}