1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

improve pointer

This commit is contained in:
Kevin Levron 2021-04-03 21:42:52 +02:00
parent 4d9643d461
commit 9eb6bac736

View File

@ -10,6 +10,9 @@ export default function usePointer(options) {
resetOnEnd = false, resetOnEnd = false,
resetPosition = new Vector2(Infinity, Infinity), resetPosition = new Vector2(Infinity, Infinity),
resetPositionV3 = new Vector3(Infinity, Infinity, Infinity), resetPositionV3 = new Vector3(Infinity, Infinity, Infinity),
onEnter = () => {},
onMove = () => {},
onLeave = () => {},
onIntersectEnter = () => {}, onIntersectEnter = () => {},
onIntersectOver = () => {}, onIntersectOver = () => {},
onIntersectMove = () => {}, onIntersectMove = () => {},
@ -81,13 +84,13 @@ export default function usePointer(options) {
const enterEvent = { ...overEvent, type: 'pointerenter' }; const enterEvent = { ...overEvent, type: 'pointerenter' };
onIntersectOver(overEvent); onIntersectOver(overEvent);
onIntersectEnter(enterEvent); onIntersectEnter(enterEvent);
if (component.onPointerOver) component.onPointerOver(overEvent); component.onPointerOver?.(overEvent);
if (component.onPointerEnter) component.onPointerEnter(enterEvent); component.onPointerEnter?.(enterEvent);
} }
const moveEvent = { type: 'pointermove', component, intersect }; const moveEvent = { type: 'pointermove', component, intersect };
onIntersectMove(moveEvent); onIntersectMove(moveEvent);
if (component.onPointerMove) component.onPointerMove(moveEvent); component.onPointerMove?.(moveEvent);
offObjects.splice(offObjects.indexOf(object), 1); offObjects.splice(offObjects.indexOf(object), 1);
}); });
@ -100,8 +103,8 @@ export default function usePointer(options) {
const leaveEvent = { ...overEvent, type: 'pointerleave' }; const leaveEvent = { ...overEvent, type: 'pointerleave' };
onIntersectOver(overEvent); onIntersectOver(overEvent);
onIntersectLeave(leaveEvent); onIntersectLeave(leaveEvent);
if (component.onPointerOver) component.onPointerOver(overEvent); component.onPointerOver?.(overEvent);
if (component.onPointerLeave) component.onPointerLeave(leaveEvent); component.onPointerLeave?.(leaveEvent);
} }
}); });
} }
@ -109,10 +112,12 @@ export default function usePointer(options) {
function pointerEnter(event) { function pointerEnter(event) {
updatePosition(event); updatePosition(event);
onEnter({ type: 'pointerenter', position, positionN, positionV3 });
}; };
function pointerMove(event) { function pointerMove(event) {
updatePosition(event); updatePosition(event);
onMove({ type: 'pointermove', position, positionN, positionV3 });
intersect(); intersect();
}; };
@ -133,13 +138,14 @@ export default function usePointer(options) {
const event = { type: 'click', component, intersect }; const event = { type: 'click', component, intersect };
onIntersectClick(event); onIntersectClick(event);
if (component.onClick) component.onClick(event); component.onClick?.(event);
}); });
} }
}; };
function pointerLeave() { function pointerLeave() {
if (resetOnEnd) reset(); if (resetOnEnd) reset();
onLeave({ type: 'pointerleave' });
}; };
function addListeners() { function addListeners() {