/**
* 场景联动类
* @class
*/
class SyncViewer {
/**
* 构造函数
* @param {Object} viewer1 主地图场景对象
* @param {Array<Viewer>} syncList 联动地图场景对象集合
*/
constructor(viewer1, syncList) {
this._viewer1 = viewer1
this._syncList = syncList
this.focusIndex = 0
this._isSync = false
}
/**
* 联动状态,控制开启或者关闭
* @type {Boolean}
*/
get sync() {
return this._isSync
}
set sync(bool) {
if (this._isSync === bool) return
this._isSync = bool
bool ? this.startSync() : this.cancelSync()
}
startSync() {
this._viewer1.scene.postRender.addEventListener(this.syncEventHandler, this)
const e = this
const list = [this._viewer1, ...this._syncList]
for (let index = 0; index < list.length; index++) {
const viewer = list[index]
viewer.container.onmouseenter = function () {
e.focusIndex = index
}
}
}
cancelSync() {
const list = [this._viewer1, ...this._syncList]
for (let index = 0; index < list.length; index++) {
const viewer = list[index]
viewer.container.onmouseenter = undefined
}
this._viewer1.scene.postRender.removeEventListener(
this.syncEventHandler,
this
)
}
syncEventHandler() {
if (!this._isSync) return
this.focusIndex === 0
? this.syncOtherToMain()
: this.syncMainToOther(this.focusIndex)
}
syncOtherToMain() {
const view = {
destination: this._viewer1.camera.position,
orientation: {
direction: this._viewer1.camera._direction,
up: this._viewer1.camera.up,
heading: this._viewer1.camera.heading,
pitch: this._viewer1.camera.pitch,
roll: this._viewer1.camera.roll,
},
}
this._syncList.forEach((viewer) => {
viewer.camera.setView(view)
})
}
syncMainToOther(index) {
const list = [this._viewer1, ...this._syncList]
const curViewer = list[index]
const view = {
destination: curViewer.camera.position,
orientation: {
direction: curViewer.camera._direction,
up: curViewer.camera.up,
heading: curViewer.camera.heading,
pitch: curViewer.camera.pitch,
roll: curViewer.camera.roll,
},
}
list.forEach((viewer, curIndex) => {
if (curIndex != index) {
viewer.camera.setView(view)
}
})
}
/**
* 注销对象
*/
destroy() {
this.cancelSync()
this._viewer1 = undefined
this._syncList = []
}
}
export default SyncViewer