Browse Source

EsDragControl2 拖拽管理器完成

master
修宁 7 months ago
parent
commit
5945edd41f
  1. 17
      src/core/controls/EsDragControl2.ts

17
src/core/controls/EsDragControl2.ts

@ -66,18 +66,20 @@ export default class DragControl2 implements IControls {
* *
*/ */
private cleanupDrag(): void { private cleanupDrag(): void {
this.viewport.renderer.domElement.style.cursor = 'auto' if (this.domElement) {
this.domElement.style.cursor = 'auto'
this.isDragging = false this.isDragging = false
this.isPointerDown = false this.isPointerDown = false
this.dragStartMouse.set(NaN, NaN) this.dragStartMouse.set(NaN, NaN)
this.removeShadows() this.removeShadows()
} }
}
/** /**
* *
*/ */
private getMousePosition(clientX: number, clientY: number): THREE.Vector2 { private getMousePosition(clientX: number, clientY: number): THREE.Vector2 {
const rect = this.viewport.renderer.domElement.getBoundingClientRect() const rect = this.domElement.getBoundingClientRect()
return new THREE.Vector2( return new THREE.Vector2(
((clientX - rect.left) / rect.width) * 2 - 1, ((clientX - rect.left) / rect.width) * 2 - 1,
((clientY - rect.top) / rect.height) * -2 + 1 ((clientY - rect.top) / rect.height) * -2 + 1
@ -187,12 +189,12 @@ export default class DragControl2 implements IControls {
* pointermove * pointermove
*/ */
private onPointerMove = (event: PointerEvent): void => { private onPointerMove = (event: PointerEvent): void => {
if (!this.enabled) return if (!this.enabled || !this.domElement) return
if (!isNaN(this.dragStartMouse.x) && !isNaN(this.dragStartMouse.y) && this.dragShadows) { if (!isNaN(this.dragStartMouse.x) && !isNaN(this.dragStartMouse.y) && this.dragShadows) {
this.isDragging = true this.isDragging = true
// 更新鼠标样式 // 更新鼠标样式
this.viewport.renderer.domElement.style.cursor = 'grabbing' this.domElement.style.cursor = 'grabbing'
// 更新阴影位置(锁定 Y 轴) // 更新阴影位置(锁定 Y 轴)
this.updateShadows(new THREE.Vector2(CurrentMouseInfo.x, CurrentMouseInfo.z)) this.updateShadows(new THREE.Vector2(CurrentMouseInfo.x, CurrentMouseInfo.z))
@ -202,9 +204,9 @@ export default class DragControl2 implements IControls {
const mouse = this.getMousePosition(event.clientX, event.clientY) const mouse = this.getMousePosition(event.clientX, event.clientY)
const intersected = this.getIntersectedDraggableObject(mouse) const intersected = this.getIntersectedDraggableObject(mouse)
if (intersected) { if (intersected) {
this.viewport.renderer.domElement.style.cursor = 'grab' this.domElement.style.cursor = 'grab'
} else { } else {
this.viewport.renderer.domElement.style.cursor = 'auto' this.domElement.style.cursor = 'auto'
} }
} }
} }
@ -269,9 +271,10 @@ export default class DragControl2 implements IControls {
* *
*/ */
cancelDrag(): void { cancelDrag(): void {
if (!this.domElement) return
if (this.isDragging || this.isPointerDown) { if (this.isDragging || this.isPointerDown) {
this.cleanupDrag() this.cleanupDrag()
this.viewport.renderer.domElement.style.cursor = 'auto' this.domElement.style.cursor = 'auto'
this.viewport.controls.enabled = true this.viewport.controls.enabled = true
} }
} }

Loading…
Cancel
Save