|
@@ -1,12 +1,15 @@
|
|
-import type { EmitEventFn, EmitEventName, Intersection, TresEvent, TresInstance, TresObject } from 'src/types'
|
|
|
|
|
|
+import type { Intersection, PointerEventType, TresEvent, TresInstance, TresObject, TresPointerEvent } from 'src/types'
|
|
import type { Object3D, Object3DEventMap, Scene } from 'three'
|
|
import type { Object3D, Object3DEventMap, Scene } from 'three'
|
|
import type { TresContext } from '../useTresContextProvider'
|
|
import type { TresContext } from '../useTresContextProvider'
|
|
import { shallowRef } from 'vue'
|
|
import { shallowRef } from 'vue'
|
|
import { hyphenate } from '../../utils'
|
|
import { hyphenate } from '../../utils'
|
|
import { useRaycaster } from '../useRaycaster'
|
|
import { useRaycaster } from '../useRaycaster'
|
|
import { isObject3D, isTresObject } from '../../utils/is'
|
|
import { isObject3D, isTresObject } from '../../utils/is'
|
|
|
|
+import type { EventHookOff } from '@vueuse/core'
|
|
|
|
+import { createEventHook } from '@vueuse/core'
|
|
|
|
|
|
export interface TresEventManager {
|
|
export interface TresEventManager {
|
|
|
|
+ onEvent: EventHookOff<TresPointerEvent>
|
|
/**
|
|
/**
|
|
* Forces the event system to refire events with the previous mouse event
|
|
* Forces the event system to refire events with the previous mouse event
|
|
*/
|
|
*/
|
|
@@ -21,7 +24,6 @@ export interface TresEventManager {
|
|
registerPointerMissedObject: (object: unknown) => void
|
|
registerPointerMissedObject: (object: unknown) => void
|
|
deregisterPointerMissedObject: (object: unknown) => void
|
|
deregisterPointerMissedObject: (object: unknown) => void
|
|
}
|
|
}
|
|
-
|
|
|
|
function executeEventListeners(
|
|
function executeEventListeners(
|
|
listeners: (event: TresEvent) => void | ((event: TresEvent) => void)[],
|
|
listeners: (event: TresEvent) => void | ((event: TresEvent) => void)[],
|
|
event: TresEvent,
|
|
event: TresEvent,
|
|
@@ -42,7 +44,6 @@ function executeEventListeners(
|
|
export function useTresEventManager(
|
|
export function useTresEventManager(
|
|
scene: Scene,
|
|
scene: Scene,
|
|
context: TresContext,
|
|
context: TresContext,
|
|
- emit: EmitEventFn,
|
|
|
|
) {
|
|
) {
|
|
const _scene = shallowRef<Scene>()
|
|
const _scene = shallowRef<Scene>()
|
|
const _context = shallowRef<TresContext>()
|
|
const _context = shallowRef<TresContext>()
|
|
@@ -55,6 +56,8 @@ export function useTresEventManager(
|
|
// TODO: Optimize to not hit test on the whole scene
|
|
// TODO: Optimize to not hit test on the whole scene
|
|
const objectsWithEvents = shallowRef((_scene.value?.children as TresInstance[]).filter(hasChildrenWithEvents) || [])
|
|
const objectsWithEvents = shallowRef((_scene.value?.children as TresInstance[]).filter(hasChildrenWithEvents) || [])
|
|
|
|
|
|
|
|
+ const eventHook = createEventHook<TresPointerEvent>()
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* propogateEvent
|
|
* propogateEvent
|
|
*
|
|
*
|
|
@@ -98,8 +101,9 @@ export function useTresEventManager(
|
|
}
|
|
}
|
|
|
|
|
|
// Convert eventName to kebab case and emit event from TresCanvas
|
|
// Convert eventName to kebab case and emit event from TresCanvas
|
|
- const kebabEventName = hyphenate(eventName.slice(2)) as EmitEventName
|
|
|
|
- emit(kebabEventName, { intersection, event })
|
|
|
|
|
|
+ const kebabEventName = hyphenate(eventName.slice(2)) as PointerEventType
|
|
|
|
+
|
|
|
|
+ eventHook.trigger({ type: kebabEventName, event, intersection })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -178,8 +182,8 @@ export function useTresEventManager(
|
|
|
|
|
|
executeEventListeners(object.onPointerMissed, event)
|
|
executeEventListeners(object.onPointerMissed, event)
|
|
})
|
|
})
|
|
- // Emit pointer-missed from TresCanvas
|
|
|
|
- emit('pointer-missed', { event })
|
|
|
|
|
|
+
|
|
|
|
+ eventHook.trigger({ type: 'pointer-missed', event })
|
|
})
|
|
})
|
|
|
|
|
|
function registerObject(maybeTresObject: unknown) {
|
|
function registerObject(maybeTresObject: unknown) {
|
|
@@ -214,6 +218,7 @@ export function useTresEventManager(
|
|
|
|
|
|
// Attach methods to tres context
|
|
// Attach methods to tres context
|
|
context.eventManager = {
|
|
context.eventManager = {
|
|
|
|
+ onEvent: eventHook.on,
|
|
forceUpdate,
|
|
forceUpdate,
|
|
registerObject,
|
|
registerObject,
|
|
deregisterObject,
|
|
deregisterObject,
|
|
@@ -222,6 +227,7 @@ export function useTresEventManager(
|
|
}
|
|
}
|
|
|
|
|
|
return {
|
|
return {
|
|
|
|
+ onEvent: eventHook.on,
|
|
forceUpdate,
|
|
forceUpdate,
|
|
registerObject,
|
|
registerObject,
|
|
deregisterObject,
|
|
deregisterObject,
|