feat: 新增Tag类【标签功能】

This commit is contained in:
liqikun 2024-05-29 20:13:32 +08:00
parent 311792e54b
commit 6fea1e764f
21 changed files with 165 additions and 36 deletions

View File

@ -2,7 +2,7 @@ import Base from '../base';
import * as Utils from '../utils'; import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class AttackArrow extends Base { export default class AttackArrow extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -26,7 +26,7 @@ export default class AttackArrow extends Base {
this.onDoubleClick(); this.onDoubleClick();
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import * as Utils from '../utils';
import Base from '../base'; import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { LineStyle } from '../interface'; import { LineStyle, Shape } from '../interface';
export default class CurvedArrow extends Base { export default class CurvedArrow extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -20,7 +20,7 @@ export default class CurvedArrow extends Base {
this.onDoubleClick(); this.onDoubleClick();
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'line'; return 'line';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
import * as Utils from '../utils'; import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
type Position = [number, number]; type Position = [number, number];
export default class DoubleArrow extends Base { export default class DoubleArrow extends Base {
@ -35,7 +35,7 @@ export default class DoubleArrow extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
import * as Utils from '../utils'; import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class FineArrow extends Base { export default class FineArrow extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -27,7 +27,7 @@ export default class FineArrow extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import * as Utils from '../utils';
import Base from '../base'; import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { LineStyle } from '../interface'; import { LineStyle, Shape } from '../interface';
export default class StraightArrow extends Base { export default class StraightArrow extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -17,7 +17,7 @@ export default class StraightArrow extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'line'; return 'line';
} }

BIN
src/assets/tag_active.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
src/assets/tag_default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -5,27 +5,32 @@ import {
GeometryStyle, GeometryStyle,
PolygonStyle, PolygonStyle,
LineStyle, LineStyle,
TagStyle,
EventType, EventType,
EventListener, EventListener,
VisibleAnimationOpts, VisibleAnimationOpts,
GrowthAnimationOpts, GrowthAnimationOpts,
Shape
} from './interface'; } from './interface';
import EventDispatcher from './events'; import EventDispatcher from './events';
import cloneDeep from 'lodash.clonedeep'; import cloneDeep from 'lodash.clonedeep';
// import merge from 'lodash.merge'; // import merge from 'lodash.merge';
import * as Utils from './utils'; import * as Utils from './utils';
import TagDefault from './assets/tag_default.png';
import TagActive from './assets/tag_active.png';
export default class Base { export default class Base {
cesium: typeof CesiumTypeOnly; cesium: typeof CesiumTypeOnly;
viewer: CesiumTypeOnly.Viewer; viewer: CesiumTypeOnly.Viewer;
eventHandler: CesiumTypeOnly.ScreenSpaceEventHandler; eventHandler: CesiumTypeOnly.ScreenSpaceEventHandler;
polygonEntity: CesiumTypeOnly.Entity; polygonEntity: CesiumTypeOnly.Entity;
tagEntity: CesiumTypeOnly.Entity;
geometryPoints: CesiumTypeOnly.Cartesian3[] = []; geometryPoints: CesiumTypeOnly.Cartesian3[] = [];
state: State = 'drawing'; state: State = 'drawing';
controlPoints: CesiumTypeOnly.EntityCollection = []; controlPoints: CesiumTypeOnly.EntityCollection = [];
controlPointsEventHandler: CesiumTypeOnly.ScreenSpaceEventHandler; controlPointsEventHandler: CesiumTypeOnly.ScreenSpaceEventHandler;
lineEntity: CesiumTypeOnly.Entity; lineEntity: CesiumTypeOnly.Entity;
type!: 'polygon' | 'line'; type!: Shape;
freehand!: boolean; freehand!: boolean;
style: GeometryStyle | undefined; style: GeometryStyle | undefined;
outlineEntity: CesiumTypeOnly.Entity; outlineEntity: CesiumTypeOnly.Entity;
@ -71,6 +76,16 @@ export default class Base {
}, },
style, style,
); );
}else if(this.type === 'tag') {
this.style = Object.assign(
{
image: TagDefault,
activeImage: TagActive,
width: 32,
height: 32,
},
style,
);
} }
//Cache the initial settings to avoid modification of properties due to reference type assignment. //Cache the initial settings to avoid modification of properties due to reference type assignment.
this.styleCache = cloneDeep(this.style); this.styleCache = cloneDeep(this.style);
@ -189,7 +204,7 @@ export default class Base {
this.setState('edit'); this.setState('edit');
this.addControlPoints(); this.addControlPoints();
this.draggable(); this.draggable();
const entity = this.polygonEntity || this.lineEntity; const entity = this.polygonEntity || this.lineEntity || this.tagEntity;
this.entityId = entity.id; this.entityId = entity.id;
/** /**
* "I've noticed that CallbackProperty can lead to significant performance issues. * "I've noticed that CallbackProperty can lead to significant performance issues.
@ -260,6 +275,22 @@ export default class Base {
} }
} }
drawTag() {
const style = this.style as TagStyle;
if (!this.tagEntity) {
this.tagEntity = this.viewer.entities.add({
position: this.geometryPoints[0],
billboard: {
image: style.image,
width: style.width,
height: style.height,
}
});
} else {
this.tagEntity.position = this.geometryPoints[0];
}
}
drawLine() { drawLine() {
if (!this.lineEntity) { if (!this.lineEntity) {
const style = this.style as LineStyle; const style = this.style as LineStyle;
@ -824,7 +855,7 @@ export default class Base {
this.polygonEntity = null; this.polygonEntity = null;
this.outlineEntity = null; this.outlineEntity = null;
this.lineEntity = null; this.lineEntity = null;
if(this.points.length <= 2) this.removeTempLine(); if (this.points.length <= 2) this.removeTempLine();
} else if (this.type === 'line') { } else if (this.type === 'line') {
this.viewer.entities.remove(this.lineEntity); this.viewer.entities.remove(this.lineEntity);
} }
@ -864,7 +895,7 @@ export default class Base {
//Abstract method that must be implemented by subclasses. //Abstract method that must be implemented by subclasses.
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
//Abstract method that must be implemented by subclasses. //Abstract method that must be implemented by subclasses.
} }

View File

@ -17,6 +17,7 @@ import Triangle from './polygon/triangle';
import Polygon from './polygon/polygon'; import Polygon from './polygon/polygon';
import Circle from './polygon/circle'; import Circle from './polygon/circle';
import Sector from './polygon/sector'; import Sector from './polygon/sector';
import Tag from './tag/tag';
import { GeometryStyle } from './interface'; import { GeometryStyle } from './interface';
import * as CesiumTypeOnly from 'cesium'; import * as CesiumTypeOnly from 'cesium';
@ -41,6 +42,7 @@ const CesiumPlot: any = {
Polygon, Polygon,
Circle, Circle,
Sector, Sector,
Tag,
}; };
type CreateGeometryFromDataOpts = { type CreateGeometryFromDataOpts = {
@ -61,8 +63,10 @@ CesiumPlot.createGeometryFromData = (cesium: any, viewer: any, opts: CreateGeome
geometry.setGeometryPoints(geometryPoints); geometry.setGeometryPoints(geometryPoints);
if (geometry.type == 'polygon') { if (geometry.type == 'polygon') {
geometry.drawPolygon(); geometry.drawPolygon();
} else { } else if (geometry.type == 'line'){
geometry.drawLine(); geometry.drawLine();
}else if (geometry.type == 'tag'){
geometry.drawTag();
} }
geometry.finishDrawing(); geometry.finishDrawing();
geometry.onClick(); geometry.onClick();

View File

@ -12,8 +12,15 @@ export type LineStyle = {
lineWidth?: number; lineWidth?: number;
}; };
export type TagStyle = {
image?: string;
activeImage?: string;
width?: number;
height?: number;
};
export type State = 'drawing' | 'edit' | 'static' | 'animating' | 'hidden'; export type State = 'drawing' | 'edit' | 'static' | 'animating' | 'hidden';
export type GeometryStyle = PolygonStyle | LineStyle; export type GeometryStyle = PolygonStyle | LineStyle | TagStyle;
export type EventType = 'drawStart' | 'drawUpdate' | 'drawEnd' | 'editEnd' | 'editStart'; export type EventType = 'drawStart' | 'drawUpdate' | 'drawEnd' | 'editEnd' | 'editStart';
export type EventListener = (eventData?: any) => void; export type EventListener = (eventData?: any) => void;
@ -29,3 +36,5 @@ export type GrowthAnimationOpts = {
delay: number; delay: number;
callback: Function; callback: Function;
}; };
export type Shape = 'polygon' | 'line' | 'tag';

View File

@ -2,7 +2,7 @@ import * as Utils from '../utils';
import Base from '../base'; import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'kmap-3d-engine'; import { Cartesian3 } from 'kmap-3d-engine';
import { LineStyle } from '../interface'; import { LineStyle, Shape } from '../interface';
export default class Curve extends Base { export default class Curve extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -18,7 +18,7 @@ export default class Curve extends Base {
this.onDoubleClick(); this.onDoubleClick();
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'line'; return 'line';
} }

View File

@ -1,7 +1,7 @@
import Base from '../base'; import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class FreehandLine extends Base { export default class FreehandLine extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -14,7 +14,7 @@ export default class FreehandLine extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'line'; return 'line';
} }

View File

@ -3,7 +3,7 @@ import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Circle extends Base { export default class Circle extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -16,7 +16,7 @@ export default class Circle extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -3,7 +3,7 @@ import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Ellipse extends Base { export default class Ellipse extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -16,7 +16,7 @@ export default class Ellipse extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -1,7 +1,7 @@
import Base from '../base'; import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class FreehandPolygon extends Base { export default class FreehandPolygon extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -14,7 +14,7 @@ export default class FreehandPolygon extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
import * as Utils from '../utils'; import * as Utils from '../utils';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Lune extends Base { export default class Lune extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -15,7 +15,7 @@ export default class Lune extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Polygon extends Base { export default class Polygon extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -14,7 +14,7 @@ export default class Polygon extends Base {
this.onDoubleClick(); this.onDoubleClick();
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Rectangle extends Base { export default class Rectangle extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -13,7 +13,7 @@ export default class Rectangle extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import * as Utils from '../utils'; import * as Utils from '../utils';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Sector extends Base { export default class Sector extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -13,7 +13,7 @@ export default class Sector extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

View File

@ -2,7 +2,7 @@ import Base from '../base';
// @ts-ignore // @ts-ignore
import { Cartesian3 } from 'cesium'; import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface'; import { PolygonStyle, Shape } from '../interface';
export default class Triangle extends Base { export default class Triangle extends Base {
points: Cartesian3[] = []; points: Cartesian3[] = [];
@ -13,7 +13,7 @@ export default class Triangle extends Base {
this.setState('drawing'); this.setState('drawing');
} }
getType(): 'polygon' | 'line' { getType(): Shape {
return 'polygon'; return 'polygon';
} }

85
src/tag/tag.ts Normal file
View File

@ -0,0 +1,85 @@
import Base from '../base';
import { Shape, TagStyle } from '../interface';
import { Cartesian3 } from 'cesium';
export default class Tag extends Base {
points: Cartesian3[] = [];
constructor(cesium: any, viewer: any, style?: TagStyle) {
super(cesium, viewer, style);
this.cesium = cesium;
this.setState('drawing');
this.onMouseMove();
}
getType(): Shape {
return 'tag';
}
/**
* Draw a shape based on mouse movement points during the initial drawing.
*/
updateMovingPoint(cartesian: Cartesian3) {
this.setGeometryPoints([cartesian]);
this.drawTag();
}
onClick() {
this.eventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
this.eventHandler.setInputAction((evt: any) => {
const pickedObject = this.viewer.scene.pick(evt.position);
const hitEntities = this.cesium.defined(pickedObject) && pickedObject.id instanceof this.cesium.Entity;
const activeEntity = this.tagEntity;
if (this.state === 'drawing') {
this.finishDrawing();
} else if (this.state === 'edit') {
if (!hitEntities || activeEntity.id !== pickedObject.id.id) {
this.tagEntity.billboard.image = this.style.image;
this.setState('static');
this.removeControlPoints();
this.disableDrag();
this.eventDispatcher.dispatchEvent('editEnd', this.getPoints());
}
} else if (this.state === 'static') {
if (hitEntities && activeEntity.id === pickedObject.id.id) {
this.tagEntity.billboard.image = this.style.activeImage;
this.setState('edit');
this.draggable();
this.eventDispatcher.dispatchEvent('editStart');
}
}
}, this.cesium.ScreenSpaceEventType.LEFT_CLICK);
}
draggable() {
let dragging = false;
this.dragEventHandler = new this.cesium.ScreenSpaceEventHandler(this.viewer.canvas);
this.dragEventHandler.setInputAction((event: any) => {
const pickRay = this.viewer.scene.camera.getPickRay(event.position);
if (pickRay) {
const pickedObject = this.viewer.scene.pick(event.position);
if (this.cesium.defined(pickedObject) && pickedObject.id instanceof this.cesium.Entity) {
const clickedEntity = pickedObject.id;
if (this.isCurrentEntity(clickedEntity.id)) {
dragging = true;
this.viewer.scene.screenSpaceCameraController.enableRotate = false;
}
}
}
}, this.cesium.ScreenSpaceEventType.LEFT_DOWN);
this.dragEventHandler.setInputAction((event: any) => {
if (dragging) {
console.log(666, event);
const cartesian = this.pixelToCartesian(event.endPosition);
console.log(cartesian, this.state);
this.tagEntity.position = cartesian;
}
}, this.cesium.ScreenSpaceEventType.MOUSE_MOVE);
this.dragEventHandler.setInputAction(() => {
dragging = false;
this.viewer.scene.screenSpaceCameraController.enableRotate = true;
}, this.cesium.ScreenSpaceEventType.LEFT_UP);
}
/**
* tag not need to add control points
*/
addControlPoints() { }
}