-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathscale-mode.ts
232 lines (203 loc) · 7.45 KB
/
scale-mode.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
221
222
223
224
225
226
227
228
229
230
231
232
/* 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 { point, featureCollection } from '@turf/helpers';
import polygonToLine from '@turf/polygon-to-line';
import { coordEach } from '@turf/meta';
import turfDistance from '@turf/distance';
import turfTransformScale from '@turf/transform-scale';
import { getCoord, getGeom } from '@turf/invariant';
import { FeatureCollection, Position } from '../geojson-types';
import {
ModeProps,
PointerMoveEvent,
StartDraggingEvent,
StopDraggingEvent,
DraggingEvent,
EditHandleFeature,
GuideFeatureCollection,
} from '../types';
import { getPickedEditHandle } from '../utils';
import { GeoJsonEditMode } from './geojson-edit-mode';
import { ImmutableFeatureCollection } from './immutable-feature-collection';
export class ScaleMode extends GeoJsonEditMode {
_geometryBeingScaled: FeatureCollection | null | undefined;
_selectedEditHandle: EditHandleFeature | null | undefined;
_cornerGuidePoints: Array<EditHandleFeature>;
_cursor: string | null | undefined;
_isScaling = false;
_isSinglePointGeometrySelected = (geometry: FeatureCollection | null | undefined): boolean => {
const { features } = geometry || {};
if (Array.isArray(features) && features.length === 1) {
// @ts-expect-error turf types diff
const { type }: { type: string } = getGeom(features[0]);
return type === 'Point';
}
return false;
};
_getOppositeScaleHandle = (selectedHandle: EditHandleFeature) => {
const selectedHandleIndex =
selectedHandle &&
selectedHandle.properties &&
Array.isArray(selectedHandle.properties.positionIndexes) &&
selectedHandle.properties.positionIndexes[0];
if (typeof selectedHandleIndex !== 'number') {
return null;
}
const guidePointCount = this._cornerGuidePoints.length;
const oppositeIndex = (selectedHandleIndex + guidePointCount / 2) % guidePointCount;
return this._cornerGuidePoints.find((p) => {
if (!Array.isArray(p.properties.positionIndexes)) {
return false;
}
return p.properties.positionIndexes[0] === oppositeIndex;
});
};
_getUpdatedData = (props: ModeProps<FeatureCollection>, editedData: FeatureCollection) => {
let updatedData = new ImmutableFeatureCollection(props.data);
const selectedIndexes = props.selectedIndexes;
for (let i = 0; i < selectedIndexes.length; i++) {
const selectedIndex = selectedIndexes[i];
const movedFeature = editedData.features[i];
updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry);
}
return updatedData.getObject();
};
isEditHandleSelected = (): boolean => Boolean(this._selectedEditHandle);
getScaleAction = (
startDragPoint: Position,
currentPoint: Position,
editType: string,
props: ModeProps<FeatureCollection>
) => {
if (!this._selectedEditHandle) {
return null;
}
const oppositeHandle = this._getOppositeScaleHandle(this._selectedEditHandle);
const origin = getCoord(oppositeHandle) as Position;
const scaleFactor = getScaleFactor(origin, startDragPoint, currentPoint);
// @ts-expect-error turf types diff
const scaledFeatures: FeatureCollection = turfTransformScale(
// @ts-expect-error turf types diff
this._geometryBeingScaled,
scaleFactor,
{ origin }
);
return {
updatedData: this._getUpdatedData(props, scaledFeatures),
editType,
editContext: {
featureIndexes: props.selectedIndexes,
},
};
};
updateCursor = (props: ModeProps<FeatureCollection>) => {
if (this._selectedEditHandle) {
if (this._cursor) {
props.onUpdateCursor(this._cursor);
}
const cursorGeometry = this.getSelectedFeaturesAsFeatureCollection(props);
// Get resize cursor direction from the hovered scale editHandle (e.g. nesw or nwse)
// @ts-expect-error turf types diff
const centroid = turfCentroid(cursorGeometry);
const bearing = turfBearing(centroid, this._selectedEditHandle);
const positiveBearing = bearing < 0 ? bearing + 180 : bearing;
if (
(positiveBearing >= 0 && positiveBearing <= 90) ||
(positiveBearing >= 180 && positiveBearing <= 270)
) {
this._cursor = 'nesw-resize';
props.onUpdateCursor('nesw-resize');
} else {
this._cursor = 'nwse-resize';
props.onUpdateCursor('nwse-resize');
}
} else {
props.onUpdateCursor(null);
this._cursor = null;
}
};
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
if (!this._isScaling) {
const selectedEditHandle = getPickedEditHandle(event.picks);
this._selectedEditHandle =
selectedEditHandle && selectedEditHandle.properties.editHandleType === 'scale'
? selectedEditHandle
: null;
this.updateCursor(props);
}
}
handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._selectedEditHandle) {
this._isScaling = true;
this._geometryBeingScaled = this.getSelectedFeaturesAsFeatureCollection(props);
}
}
handleDragging(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
if (!this._isScaling) {
return;
}
props.onUpdateCursor(this._cursor);
const scaleAction = this.getScaleAction(
event.pointerDownMapCoords,
event.mapCoords,
'scaling',
props
);
if (scaleAction) {
props.onEdit(scaleAction);
}
event.cancelPan();
}
handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._isScaling) {
// Scale the geometry
const scaleAction = this.getScaleAction(
event.pointerDownMapCoords,
event.mapCoords,
'scaled',
props
);
if (scaleAction) {
props.onEdit(scaleAction);
}
props.onUpdateCursor(null);
this._geometryBeingScaled = null;
this._selectedEditHandle = null;
this._cursor = null;
this._isScaling = false;
}
}
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
this._cornerGuidePoints = [];
const selectedGeometry = this.getSelectedFeaturesAsFeatureCollection(props);
// Add buffer to the enveloping box if a single Point feature is selected
if (this._isSinglePointGeometrySelected(selectedGeometry)) {
return { type: 'FeatureCollection', features: [] };
}
const boundingBox = bboxPolygon(bbox(selectedGeometry));
boundingBox.properties.mode = 'scale';
const cornerGuidePoints = [];
coordEach(boundingBox, (coord, coordIndex) => {
if (coordIndex < 4) {
// Get corner midpoint guides from the enveloping box
const cornerPoint = point(coord, {
guideType: 'editHandle',
editHandleType: 'scale',
positionIndexes: [coordIndex],
});
cornerGuidePoints.push(cornerPoint);
}
});
this._cornerGuidePoints = cornerGuidePoints;
// @ts-expect-error turf types diff
return featureCollection([polygonToLine(boundingBox), ...this._cornerGuidePoints]);
}
}
function getScaleFactor(centroid: Position, startDragPoint: Position, currentPoint: Position) {
const startDistance = turfDistance(centroid, startDragPoint);
const endDistance = turfDistance(centroid, currentPoint);
return endDistance / startDistance;
}