import { defaultValue } from '../../util'
/**
* 草图图形集合
* @class SketchStage
* @moduleEX SketchEditorModule
* @param {Object} options 构造参数
* @param {MapView|SceneView} [options.view] 地图视图对象
* @param {GraphicsLayer} [options.layer] 草图图层管对象
* @param {SketchStyle} [options.sketchStyle] 草图符号
* @param {Function} [options._hitTestEvent] 草图拾取事件
*/
class SketchStage {
constructor(options) {
/**
*草图实体图形
*/
options = defaultValue(options, {
entityGraphic: null,
vertexGraphics: [],
midVertexGraphics: [],
currentVertexIndex: -1,
movingLineGraphics: [],
selectBoxGraphics: [],
selectBoxVertexGraphics: []
})
this.entityGraphic = defaultValue(options.entityGraphic, null)
/**
*草图顶点图形
*/
this.vertexGraphics = defaultValue(options.vertexGraphics, [])
/**
*草图中点实图形
*/
this.midVertexGraphics = defaultValue(options.midVertexGraphics, [])
/**
*当前顶点序号
*/
this.currentVertexIndex = defaultValue(options.currentVertexIndex, -1)
/**
*草图正移动线图形
*/
this.movingLineGraphics = defaultValue(options.movingLineGraphics, [])
/**
*选中草图时外包盒
*/
this.selectBoxGraphics = defaultValue(options.selectBoxGraphics, [])
/**
*选中草图时外包盒顶点
*/
this.selectBoxVertexGraphics = defaultValue(
options.selectBoxVertexGraphics,
[]
)
/**
*捕获时的图形
*/
this.snapGraphics = defaultValue(options.snapGraphics, [])
this.editMode = defaultValue(options.editMode, 0)
}
getFeatureById(id) {
const features = this.getAllFeatures()
let feature = null // features.filter(feature=>feature.id===id)
for (let i = 0; i < features.length; i++) {
if (features[i].id === id) {
feature = features[i]
break
}
}
return feature
}
getAllFeatures() {
let features = []
if (this.entityGraphic) {
features.push(this.entityGraphic)
features = features
.concat(this.vertexGraphics)
.concat(this.midVertexGraphics)
.concat(this.selectBoxGraphics)
.concat(this.selectBoxVertexGraphics)
}
return features
}
updateGraphic(feature) {
if (this.entityGraphic.id === feature.id) {
this.entityGraphic = feature
}
this.vertexGraphics.forEach((item, i) => {
if (feature.id === item.id) {
this.vertexGraphics[i] = feature
}
})
this.midVertexGraphics.forEach((item, i) => {
if (feature.id === item.id) {
this.midVertexGraphics[i] = feature
}
})
this.selectBoxGraphics.forEach((item, i) => {
if (feature.id === item.id) {
this.selectBoxGraphics[i] = feature
}
})
this.selectBoxVertexGraphics.forEach((item, i) => {
if (feature.id === item.id) {
this.selectBoxVertexGraphics[i] = feature
}
})
}
/**
* 克隆并返回新要素<a id='clone'></a>
* @return {Feature} 克隆后的新要素
*/
clone() {
return new SketchStage(this.toJSON())
}
/**
* 导出一个JSON对像<a id='toJSON'></a>
* @return {Object} JSON对像
*/
toJSON() {
const json = {}
json.entityGraphic = this.entityGraphic
? this.entityGraphic.clone()
: undefined // this.entityGraphic[0].toJSON()
json.vertexGraphics = this.vertexGraphics.map((feature) => {
return feature.clone()
})
json.midVertexGraphics = this.midVertexGraphics.map((feature) => {
return feature.clone()
})
json.selectBoxGraphics = this.selectBoxGraphics.map((feature) => {
return feature.clone()
})
json.selectBoxVertexGraphics = this.selectBoxVertexGraphics.map(
(feature) => {
return feature.clone()
}
)
json.snapGraphics = this.snapGraphics.map((feature) => {
return feature.clone()
})
json.currentVertexIndex = this.currentVertexIndex
json.editMode = this.editMode
return json
}
}
export default SketchStage