diff --git a/src/lib/raycaster.js b/src/lib/raycaster.js index f5a75d08..bbd35781 100644 --- a/src/lib/raycaster.js +++ b/src/lib/raycaster.js @@ -43,8 +43,6 @@ export function initRaycaster(inspector) { mouseCursor.addEventListener('click', handleClick); mouseCursor.addEventListener('mouseenter', onMouseEnter); mouseCursor.addEventListener('mouseleave', onMouseLeave); - inspector.container.addEventListener('mousedown', onMouseDown); - inspector.container.addEventListener('mouseup', onMouseUp); inspector.container.addEventListener('dblclick', onDoubleClick); inspector.sceneEl.canvas.addEventListener('mouseleave', () => { @@ -53,10 +51,6 @@ export function initRaycaster(inspector) { }); }); - const onDownPosition = new THREE.Vector2(); - const onUpPosition = new THREE.Vector2(); - const onDoubleClickPosition = new THREE.Vector2(); - function onMouseEnter() { Events.emit( 'raycastermouseenter', @@ -72,50 +66,13 @@ export function initRaycaster(inspector) { } function handleClick(evt) { - // Check to make sure not dragging. - const DRAG_THRESHOLD = 0.03; - if (onDownPosition.distanceTo(onUpPosition) >= DRAG_THRESHOLD) { - return; - } - inspector.selectEntity(evt.detail.intersectedEl); - } - - function onMouseDown(event) { - if (event instanceof CustomEvent) { - return; - } - event.preventDefault(); - const array = getMousePosition( - inspector.container, - event.clientX, - event.clientY - ); - onDownPosition.fromArray(array); - } - - function onMouseUp(event) { - if (event instanceof CustomEvent) { - return; - } - event.preventDefault(); - const array = getMousePosition( - inspector.container, - event.clientX, - event.clientY - ); - onUpPosition.fromArray(array); + Events.emit('raycasterclick', evt.detail.intersectedEl); } /** * Focus on double click. */ function onDoubleClick(event) { - const array = getMousePosition( - inspector.container, - event.clientX, - event.clientY - ); - onDoubleClickPosition.fromArray(array); const intersectedEl = mouseCursor.components.cursor.intersectedEl; if (!intersectedEl) { return; @@ -127,20 +84,11 @@ export function initRaycaster(inspector) { el: mouseCursor, enable: () => { mouseCursor.setAttribute('raycaster', 'enabled', true); - inspector.container.addEventListener('mousedown', onMouseDown); - inspector.container.addEventListener('mouseup', onMouseUp); inspector.container.addEventListener('dblclick', onDoubleClick); }, disable: () => { mouseCursor.setAttribute('raycaster', 'enabled', false); - inspector.container.removeEventListener('mousedown', onMouseDown); - inspector.container.removeEventListener('mouseup', onMouseUp); inspector.container.removeEventListener('dblclick', onDoubleClick); } }; } - -function getMousePosition(dom, x, y) { - const rect = dom.getBoundingClientRect(); - return [(x - rect.left) / rect.width, (y - rect.top) / rect.height]; -} diff --git a/src/lib/viewport.js b/src/lib/viewport.js index 451b8eb5..582fa94d 100644 --- a/src/lib/viewport.js +++ b/src/lib/viewport.js @@ -32,6 +32,15 @@ export function Viewport(inspector) { selectionBox.visible = false; sceneHelpers.add(selectionBox); + let lastControlsEventTime = 0; + Events.on('raycasterclick', (el) => { + if (Date.now() - lastControlsEventTime < 500) { + // Ignore click if we used EditorControls or TransformControls in the last 500ms. + return; + } + inspector.selectEntity(el); + }); + function updateHelpers(object) { object.traverse((node) => { if (inspector.helpers[node.uuid] && inspector.helpers[node.uuid].update) { @@ -47,6 +56,7 @@ export function Viewport(inspector) { ); transformControls.size = 0.75; transformControls.addEventListener('objectChange', (evt) => { + lastControlsEventTime = Date.now(); const object = transformControls.object; if (object === undefined) { return; @@ -107,6 +117,9 @@ export function Viewport(inspector) { controls.rotationSpeed = 0.0035; controls.zoomSpeed = 0.05; controls.setAspectRatio(sceneEl.canvas.width / sceneEl.canvas.height); + controls.addEventListener('change', () => { + lastControlsEventTime = Date.now(); + }); Events.on('cameratoggle', (data) => { controls.setCamera(data.camera);