Skip to content
Snippets Groups Projects
Commit 9f1cbbb1 authored by David Sehnal's avatar David Sehnal
Browse files

mol-canvas3d: added "spin" option to trackball controls

parent 33ec88b6
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ export const TrackballControlsParams = { ...@@ -21,6 +21,9 @@ export const TrackballControlsParams = {
zoomSpeed: PD.Numeric(6.0, { min: 0.1, max: 10, step: 0.1 }), zoomSpeed: PD.Numeric(6.0, { min: 0.1, max: 10, step: 0.1 }),
panSpeed: PD.Numeric(0.8, { min: 0.1, max: 5, step: 0.1 }), panSpeed: PD.Numeric(0.8, { min: 0.1, max: 5, step: 0.1 }),
spin: PD.Boolean(false),
spinSpeed: PD.Numeric(1, { min: -100, max: 100, step: 1 }),
staticMoving: PD.Boolean(true, { isHidden: true }), staticMoving: PD.Boolean(true, { isHidden: true }),
dynamicDampingFactor: PD.Numeric(0.2, {}, { isHidden: true }), dynamicDampingFactor: PD.Numeric(0.2, {}, { isHidden: true }),
...@@ -50,9 +53,12 @@ namespace TrackballControls { ...@@ -50,9 +53,12 @@ namespace TrackballControls {
let disposed = false let disposed = false
const dragSub = input.drag.subscribe(onDrag) const dragSub = input.drag.subscribe(onDrag)
const interactionEndSub = input.interactionEnd.subscribe(onInteractionEnd)
const wheelSub = input.wheel.subscribe(onWheel) const wheelSub = input.wheel.subscribe(onWheel)
const pinchSub = input.pinch.subscribe(onPinch) const pinchSub = input.pinch.subscribe(onPinch)
let _isInteracting = false;
// For internal use // For internal use
const lastPosition = Vec3.zero() const lastPosition = Vec3.zero()
...@@ -234,6 +240,8 @@ namespace TrackballControls { ...@@ -234,6 +240,8 @@ namespace TrackballControls {
// listeners // listeners
function onDrag({ pageX, pageY, buttons, modifiers, isStart }: DragInput) { function onDrag({ pageX, pageY, buttons, modifiers, isStart }: DragInput) {
_isInteracting = true;
if (isStart) { if (isStart) {
if (buttons === ButtonsType.Flag.Primary) { if (buttons === ButtonsType.Flag.Primary) {
Vec2.copy(_moveCurr, getMouseOnCircle(pageX, pageY)) Vec2.copy(_moveCurr, getMouseOnCircle(pageX, pageY))
...@@ -257,11 +265,17 @@ namespace TrackballControls { ...@@ -257,11 +265,17 @@ namespace TrackballControls {
} }
} }
function onInteractionEnd() {
_isInteracting = false;
}
function onWheel({ dy }: WheelInput) { function onWheel({ dy }: WheelInput) {
_zoomStart[1] -= dy * 0.0001 _zoomStart[1] -= dy * 0.0001
} }
function onPinch({ distance, isStart }: PinchInput) { function onPinch({ distance, isStart }: PinchInput) {
_isInteracting = true;
if (isStart) { if (isStart) {
_touchZoomDistanceStart = distance _touchZoomDistanceStart = distance
} }
...@@ -279,16 +293,30 @@ namespace TrackballControls { ...@@ -279,16 +293,30 @@ namespace TrackballControls {
dragSub.unsubscribe() dragSub.unsubscribe()
wheelSub.unsubscribe() wheelSub.unsubscribe()
pinchSub.unsubscribe() pinchSub.unsubscribe()
interactionEndSub.unsubscribe()
}
const _spinSpeed = Vec2.create(0.005, 0);
function spin() {
_spinSpeed[0] = (p.spinSpeed || 0) / 1000;
if (!_isInteracting) Vec2.add(_moveCurr, _movePrev, _spinSpeed);
if (p.spin) requestAnimationFrame(spin);
} }
// force an update at start // force an update at start
update(); update();
if (props.spin) { spin(); }
return { return {
viewport, viewport,
get props() { return p as Readonly<TrackballControlsProps> }, get props() { return p as Readonly<TrackballControlsProps> },
setProps: (props: Partial<TrackballControlsProps>) => { Object.assign(p, props) }, setProps: (props: Partial<TrackballControlsProps>) => {
const wasSpinning = p.spin
Object.assign(p, props)
if (p.spin && !wasSpinning) requestAnimationFrame(spin)
},
update, update,
reset, reset,
......
...@@ -136,6 +136,8 @@ interface InputObserver { ...@@ -136,6 +136,8 @@ interface InputObserver {
noContextMenu: boolean noContextMenu: boolean
drag: Subject<DragInput>, drag: Subject<DragInput>,
// Equivalent to mouseUp and touchEnd
interactionEnd: Subject<undefined>,
wheel: Subject<WheelInput>, wheel: Subject<WheelInput>,
pinch: Subject<PinchInput>, pinch: Subject<PinchInput>,
click: Subject<ClickInput>, click: Subject<ClickInput>,
...@@ -169,6 +171,7 @@ namespace InputObserver { ...@@ -169,6 +171,7 @@ namespace InputObserver {
let buttons = 0 let buttons = 0
const drag = new Subject<DragInput>() const drag = new Subject<DragInput>()
const interactionEnd = new Subject<undefined>();
const click = new Subject<ClickInput>() const click = new Subject<ClickInput>()
const move = new Subject<MoveInput>() const move = new Subject<MoveInput>()
const wheel = new Subject<WheelInput>() const wheel = new Subject<WheelInput>()
...@@ -186,6 +189,7 @@ namespace InputObserver { ...@@ -186,6 +189,7 @@ namespace InputObserver {
set noContextMenu (value: boolean) { noContextMenu = value }, set noContextMenu (value: boolean) { noContextMenu = value },
drag, drag,
interactionEnd,
wheel, wheel,
pinch, pinch,
click, click,
...@@ -297,7 +301,9 @@ namespace InputObserver { ...@@ -297,7 +301,9 @@ namespace InputObserver {
} }
} }
function onTouchEnd (ev: TouchEvent) {} function onTouchEnd (ev: TouchEvent) {
endDrag()
}
function onTouchMove (ev: TouchEvent) { function onTouchMove (ev: TouchEvent) {
if (ev.touches.length === 1) { if (ev.touches.length === 1) {
...@@ -331,6 +337,11 @@ namespace InputObserver { ...@@ -331,6 +337,11 @@ namespace InputObserver {
function onMouseUp (ev: MouseEvent) { function onMouseUp (ev: MouseEvent) {
onPointerUp(ev) onPointerUp(ev)
endDrag()
}
function endDrag() {
interactionEnd.next()
} }
function onPointerDown (ev: PointerEvent) { function onPointerDown (ev: PointerEvent) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment