forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveable-feature-manager.ts
174 lines (162 loc) · 5.98 KB
/
moveable-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
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
import type { Feature, MapBrowserEvent } from 'ol';
import type Point from 'ol/geom/Point';
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 type { Observable, Subject } from 'rxjs';
// eslint-disable-next-line @typescript-eslint/no-shadow
import type { Element, UUID } from 'digital-fuesim-manv-shared';
import type { FeatureLike } from 'ol/Feature';
import { selectStateSnapshot } from 'src/app/state/get-state-snapshot';
import { selectCurrentRole } from 'src/app/state/application/selectors/shared.selectors';
import type Style from 'ol/style/Style';
import type { FeatureManager } from '../utility/feature-manager';
import type {
GeometryHelper,
GeometryWithCoordinates,
PositionableElement,
Positions,
} from '../utility/geometry-helper';
import { MovementAnimator } from '../utility/movement-animator';
import type { OlMapInteractionsManager } from '../utility/ol-map-interactions-manager';
import { TranslateInteraction } from '../utility/translate-interaction';
import { ElementManager } from './element-manager';
/**
* Manages the position of the element.
* Manages the default interactions of the element.
* Automatically redraws a feature (= reevaluates its style function) when an element property has changed.
*/
export abstract class MoveableFeatureManager<
ManagedElement extends PositionableElement,
FeatureType extends GeometryWithCoordinates = Point
>
extends ElementManager<ManagedElement, FeatureType>
implements FeatureManager<FeatureType>
{
protected movementAnimator: MovementAnimator<FeatureType>;
public layer: VectorLayer<VectorSource<FeatureType>>;
constructor(
protected readonly olMap: OlMap,
private readonly proposeMovementAction: (
newPosition: Positions<FeatureType>,
element: ManagedElement
) => void,
protected readonly geometryHelper: GeometryHelper<
FeatureType,
ManagedElement
>,
renderBuffer?: number
) {
super();
this.layer = super.createElementLayer<FeatureType>(renderBuffer);
this.movementAnimator = this.createMovementAnimator();
}
createMovementAnimator() {
return new MovementAnimator<FeatureType>(
this.olMap,
this.layer,
this.geometryHelper.interpolateCoordinates,
this.geometryHelper.getFeatureCoordinates
);
}
createFeature(element: ManagedElement): Feature<FeatureType> {
const elementFeature = this.geometryHelper.create(element);
elementFeature.setId(element.id);
this.layer.getSource()!.addFeature(elementFeature);
TranslateInteraction.onTranslateEnd<FeatureType>(
elementFeature,
(newPosition) => {
this.proposeMovementAction(newPosition, element);
},
this.geometryHelper.getFeaturePosition
);
return elementFeature;
}
isFeatureTranslatable(feature: Feature<FeatureType>) {
return true;
}
deleteFeature(
element: ManagedElement,
elementFeature: Feature<FeatureType>
): void {
this.layer.getSource()!.removeFeature(elementFeature);
elementFeature.dispose();
this.movementAnimator.stopMovementAnimation(elementFeature);
}
changeFeature(
oldElement: ManagedElement,
newElement: ManagedElement,
changedProperties: ReadonlySet<keyof ManagedElement>,
elementFeature: Feature<FeatureType>
): void {
if (changedProperties.has('position')) {
this.movementAnimator.animateFeatureMovement(
elementFeature,
this.geometryHelper.getElementCoordinates(newElement)
);
}
// Redraw the feature to reevaluate its style function
elementFeature.changed();
}
getFeatureFromElement(
element: ManagedElement
): Feature<FeatureType> | undefined {
return this.layer.getSource()!.getFeatureById(element.id) ?? undefined;
}
protected addMarking(
feature: FeatureLike,
styles: Style[],
popupService: any,
store: any,
markingStyle: any
) {
if (
(popupService.currentPopup?.markedForTrainerUUIDs.includes(
feature.getId() as UUID
) &&
selectStateSnapshot(selectCurrentRole, store) === 'trainer') ||
(popupService.currentPopup?.markedForParticipantUUIDs.includes(
feature.getId() as UUID
) &&
selectStateSnapshot(selectCurrentRole, store) === 'participant')
) {
styles.push(markingStyle);
}
}
public onFeatureClicked(
event: MapBrowserEvent<any>,
feature: Feature<FeatureType>
// eslint-disable-next-line @typescript-eslint/no-empty-function
): void {}
/**
* The standard implementation is to ignore these events.
*/
public onFeatureDrop(
droppedElement: Element,
droppedOnFeature: Feature<FeatureType>,
dropEvent?: TranslateEvent
): boolean {
return false;
}
public abstract register(
destroy$: Subject<void>,
mapInteractionsManager: OlMapInteractionsManager
): void;
protected registerFeatureElementManager(
elementDictionary$: Observable<{ [id: UUID]: ManagedElement }>,
destroy$: Subject<void>,
mapInteractionsManager: OlMapInteractionsManager
) {
this.olMap.addLayer(this.layer);
mapInteractionsManager.addFeatureLayer(this.layer);
this.registerChangeHandlers(
elementDictionary$,
destroy$,
(element) => this.onElementCreated(element),
(element) => this.onElementDeleted(element),
(oldElement, newElement) =>
this.onElementChanged(oldElement, newElement)
);
}
}