// deck.gl-community // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { ClickEvent, StartDraggingEvent, StopDraggingEvent, PointerMoveEvent, ModeProps, GuideFeatureCollection, TentativeFeature } from './types'; import { Polygon, FeatureCollection, Feature, Position, SimpleFeatureCollection } from '../utils/geojson-types'; import {GeoJsonEditMode} from './geojson-edit-mode'; import omit from 'lodash.omit'; export class TwoClickPolygonMode extends GeoJsonEditMode { handleClick(event: ClickEvent, props: ModeProps) { if (props.modeConfig && props.modeConfig.dragToDraw) { // handled in drag handlers return; } this.addClickSequence(event); this.checkAndFinishPolygon(props); } handleStartDragging(event: StartDraggingEvent, props: ModeProps): void { if (!props.modeConfig || !props.modeConfig.dragToDraw) { // handled in click handlers return; } this.addClickSequence(event); event.cancelPan(); } handleStopDragging(event: StopDraggingEvent, props: ModeProps): void { if (!props.modeConfig || !props.modeConfig.dragToDraw) { // handled in click handlers return; } this.addClickSequence(event); this.checkAndFinishPolygon(props); } checkAndFinishPolygon(props: ModeProps) { const clickSequence = this.getClickSequence(); const tentativeFeature = this.getTentativeGuide(props); if ( clickSequence.length > 1 && tentativeFeature && tentativeFeature.geometry.type === 'Polygon' ) { const feature: Feature = { type: 'Feature', properties: omit(tentativeFeature.properties, 'guideType'), geometry: { type: 'Polygon', coordinates: tentativeFeature.geometry.coordinates } }; const editAction = this.getAddFeatureOrBooleanPolygonAction(feature, props); this.resetClickSequence(); if (editAction) { props.onEdit(editAction); } } } getGuides(props: ModeProps): GuideFeatureCollection { const {lastPointerMoveEvent, modeConfig} = props; const clickSequence = this.getClickSequence(); const guides: GuideFeatureCollection = { type: 'FeatureCollection', features: [] }; if (clickSequence.length === 0 || !lastPointerMoveEvent) { // nothing to do yet return guides; } const corner1 = clickSequence[0]; const corner2 = lastPointerMoveEvent.mapCoords; const polygon = this.getTwoClickPolygon(corner1, corner2, modeConfig); if (polygon) { guides.features.push({ type: 'Feature', properties: { ...polygon.properties, guideType: 'tentative' }, geometry: polygon.geometry }); } return guides; } getTwoClickPolygon( coord1: Position, coord2: Position, modeConfig: any ): Feature | null | undefined { return null; } handlePointerMove(event: PointerMoveEvent, props: ModeProps) { props.onUpdateCursor('cell'); super.handlePointerMove(event, props); } createTentativeFeature(props: ModeProps): TentativeFeature { const {lastPointerMoveEvent} = props; const clickSequence = this.getClickSequence(); const lastCoords = lastPointerMoveEvent ? [lastPointerMoveEvent.mapCoords] : []; let tentativeFeature; if (clickSequence.length === 1) { tentativeFeature = this.getTwoClickPolygon(clickSequence[0], lastCoords[0], props.modeConfig); } return tentativeFeature; } }