forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulated-region-feature-manager.ts
220 lines (211 loc) · 7.94 KB
/
simulated-region-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
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
import type { Store } from '@ngrx/store';
import type {
UUID,
SimulatedRegion,
// eslint-disable-next-line @typescript-eslint/no-shadow
Element,
} from 'digital-fuesim-manv-shared';
import { MapCoordinates, Size } from 'digital-fuesim-manv-shared';
import type { Feature, MapBrowserEvent } from 'ol';
import type { Polygon } from 'ol/geom';
import type { TranslateEvent } from 'ol/interaction/Translate';
import type OlMap from 'ol/Map';
import { Fill } from 'ol/style';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';
import type { Subject } from 'rxjs';
import type { ExerciseService } from 'src/app/core/exercise.service';
import type { AppState } from 'src/app/state/app.state';
import {
selectCurrentRole,
selectVisibleSimulatedRegions,
} from 'src/app/state/application/selectors/shared.selectors';
import { selectStateSnapshot } from 'src/app/state/get-state-snapshot';
import { SimulatedRegionPopupComponent } from '../shared/simulated-region-popup/simulated-region-popup.component';
import { calculatePopupPositioning } from '../utility/calculate-popup-positioning';
import type { FeatureManager } from '../utility/feature-manager';
import type { OlMapInteractionsManager } from '../utility/ol-map-interactions-manager';
import { PolygonGeometryHelper } from '../utility/polygon-geometry-helper';
import { ResizeRectangleInteraction } from '../utility/resize-rectangle-interaction';
import { NameStyleHelper } from '../utility/style-helper/name-style-helper';
import type { PopupService } from '../utility/popup.service';
import { MoveableFeatureManager } from './moveable-feature-manager';
export class SimulatedRegionFeatureManager
extends MoveableFeatureManager<SimulatedRegion, Polygon>
implements FeatureManager<Polygon>
{
public register(
destroy$: Subject<void>,
mapInteractionsManager: OlMapInteractionsManager
): void {
super.registerFeatureElementManager(
this.store.select(selectVisibleSimulatedRegions),
destroy$,
mapInteractionsManager
);
mapInteractionsManager.addTrainerInteraction(
new ResizeRectangleInteraction(this.layer.getSource()!)
);
}
constructor(
olMap: OlMap,
private readonly exerciseService: ExerciseService,
private readonly store: Store<AppState>,
private readonly popupService: PopupService
) {
super(
olMap,
(targetPositions, simulatedRegion) => {
exerciseService.proposeAction({
type: '[SimulatedRegion] Move simulated region',
simulatedRegionId: simulatedRegion.id,
targetPosition: targetPositions[0]![0]!,
});
},
new PolygonGeometryHelper()
);
this.layer.setStyle((feature, resolution) => [
new Style({
fill: this.fill,
stroke: new Stroke({
color: (
this.getElementFromFeature(
feature as Feature
) as SimulatedRegion
).borderColor,
width: this.strokeWidth,
}),
}),
this.nameStyleHelper.getStyle(feature as Feature, resolution),
]);
}
private readonly fill = new Fill({ color: '#808080cc' });
private readonly strokeWidth = 2;
private readonly nameStyleHelper = new NameStyleHelper(
(feature) => {
const region = this.getElementFromFeature(
feature
) as SimulatedRegion;
return {
name: region.name,
// The offset ist based on the center of the position, not the regions position (which refers to a corner)
offsetY: 0,
};
},
0.75,
'middle'
);
override createFeature(element: SimulatedRegion): Feature<Polygon> {
const feature = super.createFeature(element);
ResizeRectangleInteraction.onResize(
feature,
({ topLeftCoordinate, scale }) => {
const currentElement = this.getElementFromFeature(
feature
) as SimulatedRegion;
this.exerciseService.proposeAction(
{
type: '[SimulatedRegion] Resize simulated region',
simulatedRegionId: element.id,
targetPosition: MapCoordinates.create(
topLeftCoordinate[0]!,
topLeftCoordinate[1]!
),
newSize: Size.create(
currentElement.size.width * scale.x,
currentElement.size.height * scale.y
),
},
true
);
}
);
return feature;
}
override changeFeature(
oldElement: SimulatedRegion,
newElement: SimulatedRegion,
changedProperties: ReadonlySet<keyof SimulatedRegion>,
elementFeature: Feature<Polygon>
): void {
if (
changedProperties.has('position') ||
changedProperties.has('size')
) {
this.movementAnimator.animateFeatureMovement(
elementFeature,
this.geometryHelper.getElementCoordinates(newElement)
);
}
// If the style has updated, we need to redraw the feature
elementFeature.changed();
}
public override onFeatureDrop(
droppedElement: Element,
droppedOnFeature: Feature<any>,
dropEvent?: TranslateEvent
) {
const droppedOnSimulatedRegion = this.getElementFromFeature(
droppedOnFeature
) as SimulatedRegion;
if (!droppedElement || !droppedOnSimulatedRegion) {
console.error('Could not find element for the features');
return false;
}
if (
['vehicle', 'personnel', 'material', 'patient'].includes(
droppedElement.type
)
) {
this.exerciseService.proposeAction(
{
type: '[SimulatedRegion] Add Element',
simulatedRegionId: droppedOnSimulatedRegion.id,
elementToBeAddedType: droppedElement.type as
| 'material'
| 'patient'
| 'personnel'
| 'vehicle',
elementToBeAddedId: droppedElement.id,
},
true
);
return true;
}
return false;
}
public override onFeatureClicked(
event: MapBrowserEvent<any>,
feature: Feature<any>
): void {
super.onFeatureClicked(event, feature);
if (selectStateSnapshot(selectCurrentRole, this.store) !== 'trainer') {
return;
}
const zoom = this.olMap.getView().getZoom()!;
const margin = 10 / zoom;
this.popupService.openPopup({
elementUUID: feature.getId()?.toString(),
component: SimulatedRegionPopupComponent,
closingUUIDs: [feature.getId() as UUID],
markedForParticipantUUIDs: [],
markedForTrainerUUIDs: [],
changedLayers: [],
context: {
simulatedRegionId: feature.getId() as UUID,
},
// We want the popup to be centered on the mouse position
...calculatePopupPositioning(
event.coordinate,
{
height: margin,
width: margin,
},
this.olMap.getView().getCenter()!
),
});
}
public override isFeatureTranslatable(feature: Feature<Polygon>): boolean {
return selectStateSnapshot(selectCurrentRole, this.store) === 'trainer';
}
}