Files
sdk4.0/src/Event/index.js
2025-07-03 15:30:26 +08:00

194 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @name: index
* @author: Administrator
* @date: 2022-06-14 14:44
* @descriptionindex
* @update: 2022-06-14 14:44
*/
export default class MouseEvent {
constructor(sdk) {
this.sdk = sdk
this.viewer = sdk.viewer
this.handler = new Cesium.ScreenSpaceEventHandler(
this.viewer.canvas
)
}
/*事件*/
mouse_left(cb) {
// //左键点击事件
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
// cartesian = this.earth.czm.viewer.scene.pickPosition(movement.position)
// if (!cartesian) {
// cartesian = this.viewer.scene.camera.pickEllipsoid(
// movement.position,
// this.viewer.scene.globe.ellipsoid
// )
// }
// if (cartesian) {
// cb(movement, cartesian)
// }
if (cartesian) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
}
mouse_left_down(cb) {
// //左键按下事件
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.LEFT_DOWN)
}
mouse_left_up(cb) {
// //左键抬起事件
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.LEFT_UP)
}
mouse_move(cb, allowNull = false) {
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian || allowNull) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}
mouse_right(cb, allowNull = false) {
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian || allowNull) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
}
mouse_right_down(cb, allowNull = false) {
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian || allowNull) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.RIGHT_DOWN)
}
mouse_right_up(cb, allowNull = false) {
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian || allowNull) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.RIGHT_UP)
}
mouse_wheel(cb) {
this.handler && this.handler.setInputAction(() => {
cb()
}, Cesium.ScreenSpaceEventType.WHEEL)
}
getcartesian(movement) {
// if (movement.endPosition) {
// movement.endPosition.y -= 2
// }
let position = movement.position || movement.endPosition
if(movement.position1 && movement.position2) {
position = {
x: (movement.position1.x + movement.position2.x) / 2,
y: (movement.position1.y + movement.position2.y) / 2,
}
}
let cartesian = this.viewer.scene.pickPosition(position)
if (!cartesian) {
const ray = this.viewer.camera.getPickRay(position); //相交的射线
let pickedObjects = this.viewer.scene.drillPickFromRay(ray, 10);
let result = {}
for (let i = 0; i < pickedObjects.length; i++) {
if (pickedObjects[i].position) {
result = pickedObjects[i]
break
}
}
cartesian = result.position
if(!cartesian) {
cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
}
}
return cartesian
// return this.earth.czm.viewer.scene.pickPosition(position)
}
//鼠标右键+键盘ctrl
mouse_right_keyboard_ctrl(cb) {
// //左键点击事件
this.handler && this.handler.setInputAction(
(movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian) {
cb(movement, cartesian)
}
},
Cesium.ScreenSpaceEventType.RIGHT_CLICK,
Cesium.KeyboardEventModifier.CTRL
)
}
// 手势-双指触摸开始
gesture_pinck_start(cb) {
this.handler && this.handler.setInputAction((movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian) {
cb(movement, cartesian)
}
}, Cesium.ScreenSpaceEventType.PINCH_START)
}
//手势-双指触摸开始+键盘ctrl
gesture_pinck_start_keyboard_ctrl(cb) {
this.handler && this.handler.setInputAction(
(movement) => {
let cartesian = this.getcartesian(movement)
if (cartesian) {
cb(movement, cartesian)
}
},
Cesium.ScreenSpaceEventType.PINCH_START,
Cesium.KeyboardEventModifier.CTRL
)
}
// 手势-双指触摸结束
gesture_pinck_end(cb) {
this.handler && this.handler.setInputAction((movement) => {
cb()
}, Cesium.ScreenSpaceEventType.PINCH_END)
}
// 手势-双指触摸修改
gesture_pinck_move(cb) {
this.handler && this.handler.setInputAction((movement) => {
// let cartesian = this.getcartesian(movement)
// if (cartesian) {
// cb(movement, cartesian)
// }
}, Cesium.ScreenSpaceEventType.PINCH_MOVE)
}
destroy() {
if (this.handler)
this.handler.destroy() //关闭事件句柄
this.handler = null
}
}