-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathrotate-mode.ts
More file actions
209 lines (180 loc) · 6.68 KB
/
rotate-mode.ts
File metadata and controls
209 lines (180 loc) · 6.68 KB
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
/* eslint-disable prettier/prettier */
import bbox from '@turf/bbox';
import turfCentroid from '@turf/centroid';
import turfBearing from '@turf/bearing';
import bboxPolygon from '@turf/bbox-polygon';
import turfDistance from '@turf/distance';
import { coordEach } from '@turf/meta';
import { getGeom } from '@turf/invariant';
import { point, featureCollection, lineString } from '@turf/helpers';
import turfTransformRotate from '@turf/transform-rotate';
import polygonToLine from '@turf/polygon-to-line';
import {
PointerMoveEvent,
StartDraggingEvent,
StopDraggingEvent,
DraggingEvent,
ModeProps,
EditHandleFeature,
GuideFeatureCollection,
} from '../types';
import { getPickedEditHandle } from '../utils';
import { FeatureCollection, Position } from '../geojson-types';
import { GeoJsonEditMode, GeoJsonEditAction, getIntermediatePosition } from './geojson-edit-mode';
import { ImmutableFeatureCollection } from './immutable-feature-collection';
export class RotateMode extends GeoJsonEditMode {
_selectedEditHandle: EditHandleFeature | null | undefined;
_geometryBeingRotated: FeatureCollection | null | undefined;
_isRotating = false;
_isSinglePointGeometrySelected = (geometry: FeatureCollection | null | undefined): boolean => {
const { features } = geometry || {};
if (Array.isArray(features) && features.length === 1) {
// @ts-expect-error turf type diff
const { type }: { type: string } = getGeom(features[0]);
return type === 'Point';
}
return false;
};
getIsRotating = () => this._isRotating;
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const selectedGeometry =
this._geometryBeingRotated || this.getSelectedFeaturesAsFeatureCollection(props);
if (this._isSinglePointGeometrySelected(selectedGeometry)) {
return { type: 'FeatureCollection', features: [] };
}
if (this._isRotating) {
// Display rotate pivot
// @ts-expect-error turf types diff
return featureCollection([turfCentroid(selectedGeometry)]) as GuideFeatureCollection;
}
const boundingBox = bboxPolygon(bbox(selectedGeometry));
let previousCoord = null;
let topEdgeMidpointCoords = null;
let longestEdgeLength = 0;
coordEach(boundingBox, (coord: Position) => {
if (previousCoord) {
const edgeMidpoint = getIntermediatePosition(coord, previousCoord);
if (!topEdgeMidpointCoords || edgeMidpoint[1] > topEdgeMidpointCoords[1]) {
// Get the top edge midpoint of the enveloping box
topEdgeMidpointCoords = edgeMidpoint;
}
// Get the length of the longest edge of the enveloping box
const edgeDistance = turfDistance(coord, previousCoord);
longestEdgeLength = Math.max(longestEdgeLength, edgeDistance);
}
previousCoord = coord;
});
// Scale the length of the line between the rotate handler and the enveloping box
// relative to the length of the longest edge of the enveloping box
const rotateHandleCoords = topEdgeMidpointCoords && [
topEdgeMidpointCoords[0],
topEdgeMidpointCoords[1] + longestEdgeLength / 1000,
];
const lineFromEnvelopeToRotateHandle = lineString([topEdgeMidpointCoords, rotateHandleCoords]);
const rotateHandle = point(rotateHandleCoords, {
guideType: 'editHandle',
editHandleType: 'rotate',
});
const outFeatures = [polygonToLine(boundingBox), rotateHandle, lineFromEnvelopeToRotateHandle];
// @ts-expect-error turf type diff
return featureCollection(outFeatures);
}
handleDragging(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
if (!this._isRotating) {
return;
}
const rotateAction = this.getRotateAction(
event.pointerDownMapCoords,
event.mapCoords,
'rotating',
props
);
if (rotateAction) {
props.onEdit(rotateAction);
}
event.cancelPan();
}
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
if (!this._isRotating) {
const selectedEditHandle = getPickedEditHandle(event.picks);
this._selectedEditHandle =
selectedEditHandle && selectedEditHandle.properties.editHandleType === 'rotate'
? selectedEditHandle
: null;
}
this.updateCursor(props);
}
handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
this._isRotating = true;
this._geometryBeingRotated = this.getSelectedFeaturesAsFeatureCollection(props);
}
}
handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._isRotating) {
// Rotate the geometry
const rotateAction = this.getRotateAction(
event.pointerDownMapCoords,
event.mapCoords,
'rotated',
props
);
if (rotateAction) {
props.onEdit(rotateAction);
}
this._geometryBeingRotated = null;
this._selectedEditHandle = null;
this._isRotating = false;
}
}
updateCursor(props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
// TODO: look at doing SVG cursors to get a better "rotate" cursor
props.onUpdateCursor('crosshair');
} else {
props.onUpdateCursor(null);
}
}
getRotateAction(
startDragPoint: Position,
currentPoint: Position,
editType: string,
props: ModeProps<FeatureCollection>
): GeoJsonEditAction | null | undefined {
if (!this._geometryBeingRotated) {
return null;
}
// @ts-expect-error turf types diff
const centroid = turfCentroid(this._geometryBeingRotated);
// @ts-expect-error turf types diff
const angle = getRotationAngle(centroid, startDragPoint, currentPoint);
// @ts-expect-error turf types too wide
const rotatedFeatures: FeatureCollection = turfTransformRotate(
// @ts-expect-error turf types too wide
this._geometryBeingRotated,
angle,
{
pivot: centroid,
}
);
let updatedData = new ImmutableFeatureCollection(props.data);
const selectedIndexes = props.selectedIndexes;
for (let i = 0; i < selectedIndexes.length; i++) {
const selectedIndex = selectedIndexes[i];
const movedFeature = rotatedFeatures.features[i];
updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
}
return {
updatedData: updatedData.getObject(),
editType,
editContext: {
featureIndexes: selectedIndexes,
},
};
}
}
function getRotationAngle(centroid: Position, startDragPoint: Position, currentPoint: Position) {
const bearing1 = turfBearing(centroid, startDragPoint);
const bearing2 = turfBearing(centroid, currentPoint);
return bearing2 - bearing1;
}