Add Apple Trackpad camera controls

This commit is contained in:
Jonathan Tran
2024-08-27 21:31:43 -04:00
parent e525b319d0
commit 7021be8360
3 changed files with 138 additions and 31 deletions

View File

@ -390,28 +390,7 @@ export class CameraControls {
return return
} }
// Implement camera movement logic here based on deltaMove this.moveCamera(interaction, deltaMove)
// For example, for rotating the camera around the target:
if (interaction === 'rotate') {
this.pendingRotation = this.pendingRotation
? this.pendingRotation
: new Vector2()
this.pendingRotation.x += deltaMove.x
this.pendingRotation.y += deltaMove.y
} else if (interaction === 'zoom') {
this.pendingZoom = this.pendingZoom ? this.pendingZoom : 1
this.pendingZoom *= 1 + deltaMove.y * 0.01
} else if (interaction === 'pan') {
this.pendingPan = this.pendingPan ? this.pendingPan : new Vector2()
let distance = this.camera.position.distanceTo(this.target)
if (this.camera instanceof OrthographicCamera) {
const zoomFudgeFactor = 2280
distance = zoomFudgeFactor / (this.camera.zoom * 45)
}
const panSpeed = (distance / 1000 / 45) * this.perspectiveFovBeforeOrtho
this.pendingPan.x += -deltaMove.x * panSpeed
this.pendingPan.y += deltaMove.y * panSpeed
}
} else { } else {
/** /**
* If we're not in sketch mode and not dragging, we can highlight entities * If we're not in sketch mode and not dragging, we can highlight entities
@ -433,6 +412,31 @@ export class CameraControls {
} }
} }
moveCamera(interaction: interactionType, deltaMove: Vector2) {
// Implement camera movement logic here based on deltaMove
// For example, for rotating the camera around the target:
if (interaction === 'rotate') {
this.pendingRotation = this.pendingRotation
? this.pendingRotation
: new Vector2()
this.pendingRotation.x += deltaMove.x
this.pendingRotation.y += deltaMove.y
} else if (interaction === 'zoom') {
this.pendingZoom = this.pendingZoom ? this.pendingZoom : 1
this.pendingZoom *= 1 + deltaMove.y * 0.01
} else if (interaction === 'pan') {
this.pendingPan = this.pendingPan ? this.pendingPan : new Vector2()
let distance = this.camera.position.distanceTo(this.target)
if (this.camera instanceof OrthographicCamera) {
const zoomFudgeFactor = 2280
distance = zoomFudgeFactor / (this.camera.zoom * 45)
}
const panSpeed = (distance / 1000 / 45) * this.perspectiveFovBeforeOrtho
this.pendingPan.x += -deltaMove.x * panSpeed
this.pendingPan.y += deltaMove.y * panSpeed
}
}
onMouseUp = (event: PointerEvent) => { onMouseUp = (event: PointerEvent) => {
this.domElement.releasePointerCapture(event.pointerId) this.domElement.releasePointerCapture(event.pointerId)
this.isDragging = false this.isDragging = false
@ -457,14 +461,56 @@ export class CameraControls {
if (interaction === 'none') return if (interaction === 'none') return
event.preventDefault() event.preventDefault()
const zoomDirection = this.interactionGuards.zoom.scrollReverseY ? -1 : 1
if (this.syncDirection === 'engineToClient') { if (this.syncDirection === 'engineToClient') {
if (interaction === 'zoom') { if (interaction === 'zoom') {
this.zoomDataFromLastFrame = event.deltaY this.zoomDataFromLastFrame = event.deltaY * zoomDirection
} else { } else {
// This case will get handled when we add pan and rotate using Apple trackpad. this.isDragging = true
console.error( this.handleStart()
`Unexpected interaction type for engineToClient wheel event: ${interaction}`
) this.engineCommandManager
.sendSceneCommand({
type: 'modeling_cmd_batch_req',
batch_id: uuidv4(),
requests: [
{
cmd: {
type: 'camera_drag_start',
interaction,
window: { x: event.clientX, y: event.clientY },
},
cmd_id: uuidv4(),
},
{
cmd: {
type: 'camera_drag_move',
interaction,
window: {
x: event.clientX - event.deltaX,
y: event.clientY - event.deltaY,
},
},
cmd_id: uuidv4(),
},
{
cmd: {
type: 'camera_drag_end',
interaction,
window: {
x: event.clientX - event.deltaX,
y: event.clientY - event.deltaY,
},
},
cmd_id: uuidv4(),
},
],
responses: false,
})
.catch(reportRejection)
this.isDragging = false
this.handleEnd()
} }
return return
} }
@ -478,12 +524,19 @@ export class CameraControls {
this.handleStart() this.handleStart()
if (interaction === 'zoom') { if (interaction === 'zoom') {
this.pendingZoom = 1 + (event.deltaY / window.devicePixelRatio) * 0.001 this.pendingZoom =
1 + (event.deltaY / window.devicePixelRatio) * 0.001 * zoomDirection
} else { } else {
// This case will get handled when we add pan and rotate using Apple trackpad. this.isDragging = true
console.error( this.mouseDownPosition.set(event.clientX, event.clientY)
`Unexpected interaction type for wheel event: ${interaction}`
this.moveCamera(interaction, new Vector2(-event.deltaX, -event.deltaY))
this.mouseDownPosition.set(
event.clientX + event.deltaX,
event.clientY + event.deltaY
) )
this.isDragging = false
} }
this.handleEnd() this.handleEnd()
} }
@ -1266,6 +1319,9 @@ function _getInteractionType(
enableZoom: boolean enableZoom: boolean
): interactionType | 'none' { ): interactionType | 'none' {
if (event instanceof WheelEvent) { if (event instanceof WheelEvent) {
if (enablePan && interactionGuards.pan.scrollCallback(event)) return 'pan'
if (enableRotate && interactionGuards.rotate.scrollCallback(event))
return 'rotate'
if (enableZoom && interactionGuards.zoom.scrollCallback(event)) if (enableZoom && interactionGuards.zoom.scrollCallback(event))
return 'zoom' return 'zoom'
} else { } else {

View File

@ -13,6 +13,7 @@ export type CameraSystem =
| 'KittyCAD' | 'KittyCAD'
| 'OnShape' | 'OnShape'
| 'Trackpad Friendly' | 'Trackpad Friendly'
| 'Apple Trackpad'
| 'Solidworks' | 'Solidworks'
| 'NX' | 'NX'
| 'Creo' | 'Creo'
@ -22,6 +23,7 @@ export const cameraSystems: CameraSystem[] = [
'KittyCAD', 'KittyCAD',
'OnShape', 'OnShape',
'Trackpad Friendly', 'Trackpad Friendly',
'Apple Trackpad',
'Solidworks', 'Solidworks',
'NX', 'NX',
'Creo', 'Creo',
@ -38,6 +40,8 @@ export function mouseControlsToCameraSystem(
return 'OnShape' return 'OnShape'
case 'trackpad_friendly': case 'trackpad_friendly':
return 'Trackpad Friendly' return 'Trackpad Friendly'
case 'apple_trackpad':
return 'Apple Trackpad'
case 'solidworks': case 'solidworks':
return 'Solidworks' return 'Solidworks'
case 'nx': case 'nx':
@ -54,6 +58,7 @@ export function mouseControlsToCameraSystem(
interface MouseGuardHandler { interface MouseGuardHandler {
description: string description: string
callback: (e: MouseEvent) => boolean callback: (e: MouseEvent) => boolean
scrollCallback: (e: WheelEvent) => boolean
lenientDragStartButton?: number lenientDragStartButton?: number
} }
@ -61,6 +66,7 @@ interface MouseGuardZoomHandler {
description: string description: string
dragCallback: (e: MouseEvent) => boolean dragCallback: (e: MouseEvent) => boolean
scrollCallback: (e: WheelEvent) => boolean scrollCallback: (e: WheelEvent) => boolean
scrollReverseY?: boolean
lenientDragStartButton?: number lenientDragStartButton?: number
} }
@ -83,6 +89,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
callback: (e) => callback: (e) =>
(btnName(e).middle && noModifiersPressed(e)) || (btnName(e).middle && noModifiersPressed(e)) ||
(btnName(e).right && e.shiftKey), (btnName(e).right && e.shiftKey),
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: 'Scroll or Ctrl + Right click drag', description: 'Scroll or Ctrl + Right click drag',
@ -92,6 +99,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: 'Right click drag', description: 'Right click drag',
callback: (e) => btnName(e).right && noModifiersPressed(e), callback: (e) => btnName(e).right && noModifiersPressed(e),
scrollCallback: () => false,
}, },
}, },
OnShape: { OnShape: {
@ -100,6 +108,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
callback: (e) => callback: (e) =>
(btnName(e).right && e.ctrlKey) || (btnName(e).right && e.ctrlKey) ||
(btnName(e).middle && noModifiersPressed(e)), (btnName(e).middle && noModifiersPressed(e)),
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: 'Scroll', description: 'Scroll',
@ -109,6 +118,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: 'Right click drag', description: 'Right click drag',
callback: (e) => btnName(e).right && noModifiersPressed(e), callback: (e) => btnName(e).right && noModifiersPressed(e),
scrollCallback: () => false,
}, },
}, },
'Trackpad Friendly': { 'Trackpad Friendly': {
@ -117,6 +127,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
callback: (e) => callback: (e) =>
(btnName(e).left && e.altKey && e.shiftKey && !e.metaKey) || (btnName(e).left && e.altKey && e.shiftKey && !e.metaKey) ||
(btnName(e).middle && noModifiersPressed(e)), (btnName(e).middle && noModifiersPressed(e)),
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: `Scroll or ${ALT} + ${META} + Left click drag`, description: `Scroll or ${ALT} + ${META} + Left click drag`,
@ -126,13 +137,44 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: `${ALT} + Left click drag`, description: `${ALT} + Left click drag`,
callback: (e) => btnName(e).left && e.altKey && !e.shiftKey && !e.metaKey, callback: (e) => btnName(e).left && e.altKey && !e.shiftKey && !e.metaKey,
scrollCallback: () => false,
lenientDragStartButton: 0, lenientDragStartButton: 0,
}, },
}, },
'Apple Trackpad': {
pan: {
description: `Scroll or one finger drag`,
callback: (e) => btnName(e).left && noModifiersPressed(e),
scrollCallback: (e) => e.deltaMode === 0 && noModifiersPressed(e),
lenientDragStartButton: 0,
},
zoom: {
description: `Shift + Scroll`,
dragCallback: (e) => false,
scrollCallback: (e) =>
e.deltaMode === 0 &&
e.shiftKey &&
!e.ctrlKey &&
!e.altKey &&
!e.metaKey,
scrollReverseY: true,
},
rotate: {
description: `${ALT} + Scroll`,
callback: (e) => false,
scrollCallback: (e) =>
e.deltaMode === 0 &&
e.altKey &&
!e.ctrlKey &&
!e.shiftKey &&
!e.metaKey,
},
},
Solidworks: { Solidworks: {
pan: { pan: {
description: 'Ctrl + Right click drag', description: 'Ctrl + Right click drag',
callback: (e) => btnName(e).right && e.ctrlKey, callback: (e) => btnName(e).right && e.ctrlKey,
scrollCallback: () => false,
lenientDragStartButton: 2, lenientDragStartButton: 2,
}, },
zoom: { zoom: {
@ -143,12 +185,14 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: 'Middle click drag', description: 'Middle click drag',
callback: (e) => btnName(e).middle && noModifiersPressed(e), callback: (e) => btnName(e).middle && noModifiersPressed(e),
scrollCallback: () => false,
}, },
}, },
NX: { NX: {
pan: { pan: {
description: 'Shift + Middle click drag', description: 'Shift + Middle click drag',
callback: (e) => btnName(e).middle && e.shiftKey, callback: (e) => btnName(e).middle && e.shiftKey,
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: 'Scroll or Ctrl + Middle click drag', description: 'Scroll or Ctrl + Middle click drag',
@ -158,12 +202,14 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: 'Middle click drag', description: 'Middle click drag',
callback: (e) => btnName(e).middle && noModifiersPressed(e), callback: (e) => btnName(e).middle && noModifiersPressed(e),
scrollCallback: () => false,
}, },
}, },
Creo: { Creo: {
pan: { pan: {
description: 'Ctrl + Left click drag', description: 'Ctrl + Left click drag',
callback: (e) => btnName(e).left && !btnName(e).right && e.ctrlKey, callback: (e) => btnName(e).left && !btnName(e).right && e.ctrlKey,
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: 'Scroll or Ctrl + Right click drag', description: 'Scroll or Ctrl + Right click drag',
@ -176,12 +222,14 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
const b = btnName(e) const b = btnName(e)
return (b.middle || (b.left && b.right)) && e.ctrlKey return (b.middle || (b.left && b.right)) && e.ctrlKey
}, },
scrollCallback: () => false,
}, },
}, },
AutoCAD: { AutoCAD: {
pan: { pan: {
description: 'Middle click drag', description: 'Middle click drag',
callback: (e) => btnName(e).middle && noModifiersPressed(e), callback: (e) => btnName(e).middle && noModifiersPressed(e),
scrollCallback: () => false,
}, },
zoom: { zoom: {
description: 'Scroll', description: 'Scroll',
@ -191,6 +239,7 @@ export const cameraMouseDragGuards: Record<CameraSystem, MouseGuard> = {
rotate: { rotate: {
description: 'Shift + Middle click drag', description: 'Shift + Middle click drag',
callback: (e) => btnName(e).middle && e.shiftKey, callback: (e) => btnName(e).middle && e.shiftKey,
scrollCallback: () => false,
}, },
}, },
} }

View File

@ -389,6 +389,8 @@ pub enum MouseControlType {
OnShape, OnShape,
#[serde(alias = "Trackpad Friendly")] #[serde(alias = "Trackpad Friendly")]
TrackpadFriendly, TrackpadFriendly,
#[serde(alias = "Apple Trackpad")]
AppleTrackpad,
#[serde(alias = "Solidworks")] #[serde(alias = "Solidworks")]
Solidworks, Solidworks,
#[serde(alias = "NX")] #[serde(alias = "NX")]