fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
//  移动位置、旋转 3dtiles
class TilesEditor extends mars3d.BaseClass {
  //= ========= 构造方法 ==========
  constructor(options) {
    super(options)
 
    this.options = options
    this.map = options.map
    this.scene = this.map.scene
 
    this._position = options.position
    this._heading = options.heading || 0
    this._range = options.range || 100
 
    this.dragging = false
    this.rotating = false
    this._enabled = false
 
    this.billboards = this.map.scene.primitives.add(new Cesium.BillboardCollection())
    this.handler = new Cesium.ScreenSpaceEventHandler(this.map.canvas)
 
    // 用来平移位置的指示器
    this.movep = this.billboards.add({
      position: this.position,
      color: Cesium.Color.fromCssColorString("#FFFF00"),
      image: options.moveImg,
      show: false,
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    })
    // 用来旋转的指示器
    this.rotatep = this.billboards.add({
      position: this.rotationPosition,
      color: Cesium.Color.fromCssColorString("#FFFF00"),
      image: options.rotateImg,
      show: false,
      disableDepthTestDistance: Number.POSITIVE_INFINITY
    })
  }
 
  //= ========= 对外属性 ==========
  // 启用状态
  get enabled() {
    return this._enabled
  }
 
  set enabled(val) {
    this._enabled = val
    if (val) {
      this.handler.setInputAction((p) => {
        this.handlerOnLeafDown(p)
      }, Cesium.ScreenSpaceEventType.LEFT_DOWN)
      this.handler.setInputAction((p) => {
        this.handlerOnMouseMove(p)
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
      this.handler.setInputAction((p) => {
        this.handlerOnLeftUp(p)
      }, Cesium.ScreenSpaceEventType.LEFT_UP)
 
      this.rotatep.show = true
      this.movep.show = true
    } else {
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN)
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
 
      this.rotatep.show = false
      this.movep.show = false
    }
  }
 
  // 移动位置的图标位置
  get position() {
    return this._position
  }
 
  set position(value) {
    this._position = value
 
    this.movep.position = this.position
    this.rotatep.position = this.rotationPosition
  }
 
  // 旋转方向的图标位置(依据位置和朝向计算)
  get rotationPosition() {
    if (this._position) {
      return mars3d.PointUtil.getPositionByDirectionAndLen(this._position, this._heading, this._range)
    } else {
      return null
    }
  }
 
  get heading() {
    return this._heading
  }
 
  set heading(value) {
    this._heading = value
 
    if (this._position) {
      this.rotatep.position = this.rotationPosition
    }
  }
 
  get range() {
    return this._range
  }
 
  set range(value) {
    this._range = value
 
    if (this._position) {
      this.rotatep.position = this.rotationPosition
    }
  }
 
  //= ========= 方法 ==========
  handlerOnLeafDown(event) {
    const pickedObjects = this.scene.drillPick(event.position, 2)
 
    for (let i = 0; i < pickedObjects.length; i++) {
      const pickedObject = pickedObjects[i]
 
      if (Cesium.defined(pickedObject) && pickedObject.primitive === this.movep) {
        this.dragging = true
        this.scene.screenSpaceCameraController.enableRotate = false
        break
      } else if (Cesium.defined(pickedObject) && pickedObject.primitive === this.rotatep) {
        this.rotating = true
        this.scene.screenSpaceCameraController.enableRotate = false
        break
      }
    }
  }
 
  handlerOnMouseMove(event) {
    const position = this.pickTerrain(event.endPosition)
    if (!position) {
      return
    }
 
    if (this.dragging) {
      this.position = position
      this.movep.position = this.position
      this.rotatep.position = this.rotationPosition
 
      this.fire(mars3d.EventType.change, {
        position: this._position
      })
    } else if (this.rotating) {
      this.rotatep.position = position
      this._range = Cesium.Cartesian3.distance(this._position, position)
      this._heading = mars3d.MeasureUtil.getAngle(this._position, position) // 模型是正东为0
 
      this.fire(mars3d.EventType.change, {
        heading: this._heading
      })
    }
  }
 
  handlerOnLeftUp(event) {
    if (this.dragging || this.rotating) {
      this.rotating = this.dragging = false
      this.scene.screenSpaceCameraController.enableRotate = true
      // 如果没有这句话 会导致billboards的某些没有刷新,无法再次点击
      this.billboards._createVertexArray = true
    }
  }
 
  pickTerrain(wndpos) {
    const ray = this.map.camera.getPickRay(wndpos)
    const pos = this.map.scene.globe.pick(ray, this.map.scene)
    return pos
  }
 
  remove() {
    // 从场景中移除
    if (this.billboards) {
      this.scene.primitives.remove(this.billboards)
      this.billboards = undefined
    }
    this.rotatep.show = false
    this.movep.show = false
  }
 
  destroy() {
    this.remove()
    this.handler.destroy()
 
    // 删除所有绑定的数据
    for (const i in this) {
      delete this[i]
    }
  }
}