Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): added Object3DEventMap to Object3D generics for point… #491

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(types): added Object3DEventMap to Object3D generics for point…
… event handling
  • Loading branch information
alvarosabu committed Jan 2, 2024
commit 6481939ccaca934cd1c28979a52235f72d7138b0
16 changes: 8 additions & 8 deletions src/composables/usePointerEventHandler/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Intersection, Event, Object3D } from 'three'
import type { Intersection, Object3D, Object3DEventMap } from 'three'
import type { TresScene } from 'src/types'
import { computed, reactive, ref } from 'vue'
import { uniqueBy } from '../../utils'
import { useRaycaster } from '../useRaycaster'

import type { TresContext } from '../useTresContextProvider'

type CallbackFn = (intersection: Intersection<Object3D<Event>>, event: PointerEvent) => void
type CallbackFnPointerLeave = (object: Object3D<Event>, event: PointerEvent) => void
type CallbackFn = (intersection: Intersection<Object3D<Object3DEventMap>>, event: PointerEvent) => void
type CallbackFnPointerLeave = (object: Object3D, event: PointerEvent) => void

export interface EventProps {
onClick?: CallbackFn
Expand All @@ -24,10 +24,10 @@ export const usePointerEventHandler = (
},
) => {
const objectsWithEventListeners = reactive({
click: new Map<Object3D, CallbackFn>(),
pointerMove: new Map<Object3D, CallbackFn>(),
pointerEnter: new Map<Object3D, CallbackFn>(),
pointerLeave: new Map<Object3D, CallbackFnPointerLeave>(),
click: new Map<Object3D<Object3DEventMap>, CallbackFn>(),
pointerMove: new Map<Object3D<Object3DEventMap>, CallbackFn>(),
pointerEnter: new Map<Object3D<Object3DEventMap>, CallbackFn>(),
pointerLeave: new Map<Object3D<Object3DEventMap>, CallbackFnPointerLeave>(),
})

const blockingObjects = ref(new Set<Object3D>())
Expand Down Expand Up @@ -79,7 +79,7 @@ export const usePointerEventHandler = (
if (intersects.length) objectsWithEventListeners.click.get(intersects[0].object)?.(intersects[0], event)
})

let previouslyIntersectedObject: Object3D<Event> | null
let previouslyIntersectedObject: Object3D | null

onPointerMove(({ intersects, event }) => {
const firstObject = intersects?.[0]?.object
Expand Down
4 changes: 2 additions & 2 deletions src/composables/useRaycaster/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Vector2 } from 'three'
import type { Object3D, type Intersection } from 'three'
import type { Object3D, Intersection } from 'three'
import type { Ref } from 'vue'
import { computed, onUnmounted } from 'vue'
import type { EventHook } from '@vueuse/core'
import { createEventHook, useElementBounding, usePointer } from '@vueuse/core'

import { type TresContext } from '../useTresContextProvider'

export type Intersects = Intersection<THREE.Object3D<THREE.Event>>[]
export type Intersects = Intersection<THREE.Object3D<THREE.Object3DEventMap>>[]
interface PointerMoveEventPayload {
intersects?: Intersects
event: PointerEvent
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface TresBaseObject {
}

// Custom type for geometry and material properties in Object3D
export interface TresObject3D extends THREE.Object3D {
export interface TresObject3D extends THREE.Object3D<THREE.Object3DEventMap> {
geometry?: THREE.BufferGeometry & TresBaseObject
material?: THREE.Material & TresBaseObject
userData: {
Expand Down