Skip to content

Commit d4e1fe2

Browse files
authored
chore: type issues (#663)
* fix: fix some internal types * chore: fix linters * fix: typescript issues on event manager
1 parent 0badfb1 commit d4e1fe2

File tree

14 files changed

+162
-180
lines changed

14 files changed

+162
-180
lines changed

playground/components.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ declare module 'vue' {
99
export interface GlobalComponents {
1010
AkuAku: typeof import('./src/components/AkuAku.vue')['default']
1111
BlenderCube: typeof import('./src/components/BlenderCube.vue')['default']
12-
<<<<<<< HEAD
13-
DynamicModel: typeof import('./src/components/DynamicModel.vue')['default']
14-
=======
1512
Box: typeof import('./src/components/Box.vue')['default']
1613
CameraOperator: typeof import('./src/components/CameraOperator.vue')['default']
1714
Cameras: typeof import('./src/components/Cameras.vue')['default']
@@ -23,7 +20,6 @@ declare module 'vue' {
2320
EventsPropogation: typeof import('./src/components/EventsPropogation.vue')['default']
2421
FBXModels: typeof import('./src/components/FBXModels.vue')['default']
2522
Gltf: typeof import('./src/components/gltf/index.vue')['default']
26-
>>>>>>> v4
2723
GraphPane: typeof import('./src/components/GraphPane.vue')['default']
2824
LocalOrbitControls: typeof import('./src/components/LocalOrbitControls.vue')['default']
2925
RenderingLogger: typeof import('./src/components/RenderingLogger.vue')['default']

src/components/TresCanvas.vue

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { registerTresDevtools } from '../devtools'
3838
import { disposeObject3D } from '../utils/'
3939
4040
import type { RendererPresetsType } from '../composables/useRenderer/const'
41-
import type { TresCamera, TresObject } from '../types/'
41+
import type { TresCamera, TresObject, TresScene } from '../types/'
4242
4343
export interface TresCanvasProps
4444
extends Omit<WebGLRendererParameters, 'canvas'> {
@@ -105,7 +105,7 @@ const canvas = ref<HTMLCanvasElement>()
105105
renderer uses it to mount the app nodes. This happens before `useTresContextProvider` is called.
106106
The custom renderer requires `scene` to be editable (not readonly).
107107
*/
108-
const scene = shallowRef(new Scene())
108+
const scene = shallowRef<TresScene | Scene>(new Scene())
109109
110110
const { resume } = useRenderLoop()
111111
@@ -136,34 +136,17 @@ const mountCustomRenderer = (context: TresContext) => {
136136
}
137137
138138
const dispose = (context: TresContext, force = false) => {
139-
disposeObject3D(context.scene.value)
139+
disposeObject3D(context.scene.value as unknown as TresObject)
140140
if (force) {
141141
context.renderer.value.dispose()
142142
context.renderer.value.renderLists.dispose()
143143
context.renderer.value.forceContextLoss()
144144
}
145-
scene.value.__tres = {
145+
(scene.value as TresScene).__tres = {
146146
root: context,
147147
}
148148
mountCustomRenderer(context)
149149
resume()
150-
/* disposeObject3D(scene.value) */
151-
/* scene.value.children.forEach((child) => {
152-
child.removeFromParent()
153-
disposeObject3D(child)
154-
})
155-
context.scene.value.children.forEach((child) => {
156-
child.removeFromParent()
157-
disposeObject3D(child)
158-
}) */
159-
/* console.log('disposing', scene.value.children)
160-
if (force) {
161-
context.renderer.value.dispose()
162-
context.renderer.value.renderLists.dispose()
163-
context.renderer.value.forceContextLoss()
164-
}
165-
mountCustomRenderer(context)
166-
resume() */
167150
}
168151
169152
const disableRender = computed(() => props.disableRender)
@@ -176,7 +159,7 @@ onMounted(() => {
176159
const existingCanvas = canvas as Ref<HTMLCanvasElement>
177160
178161
context.value = useTresContextProvider({
179-
scene: scene.value,
162+
scene: scene.value as TresScene,
180163
canvas: existingCanvas,
181164
windowSize: props.windowSize ?? false,
182165
disableRender: disableRender.value ?? false,

src/composables/usePointerEventHandler/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Intersection, Object3D, Object3DEventMap } from 'three'
22
import { computed, reactive, ref } from 'vue'
3+
import type { TresObject } from 'src/types'
34
import { uniqueBy } from '../../utils'
45
import { useRaycaster } from '../useRaycaster'
56

@@ -27,26 +28,26 @@ export const usePointerEventHandler = (
2728

2829
const blockingObjects = ref(new Set<Object3D>())
2930

30-
const registerBlockingObject = (object: Object3D) => {
31-
blockingObjects.value.add(object)
31+
const registerBlockingObject = (object: TresObject) => {
32+
blockingObjects.value.add(object as Object3D)
3233
}
3334

34-
const deregisterBlockingObject = (object: Object3D) => {
35-
blockingObjects.value.delete(object)
35+
const deregisterBlockingObject = (object: TresObject) => {
36+
blockingObjects.value.delete(object as Object3D)
3637
}
3738

38-
const deregisterObject = (object: Object3D) => {
39-
Object.values(objectsWithEventListeners).forEach(map => map.delete(object))
39+
const deregisterObject = (object: TresObject) => {
40+
Object.values(objectsWithEventListeners).forEach(map => map.delete(object as Object3D))
4041
deregisterBlockingObject(object)
4142
}
4243

43-
const registerObject = (object: Object3D & EventProps) => {
44+
const registerObject = (object: TresObject & EventProps) => {
4445
const { onClick, onPointerMove, onPointerEnter, onPointerLeave } = object
4546

46-
if (onClick) { objectsWithEventListeners.click.set(object, onClick) }
47-
if (onPointerMove) { objectsWithEventListeners.pointerMove.set(object, onPointerMove) }
48-
if (onPointerEnter) { objectsWithEventListeners.pointerEnter.set(object, onPointerEnter) }
49-
if (onPointerLeave) { objectsWithEventListeners.pointerLeave.set(object, onPointerLeave) }
47+
if (onClick) { objectsWithEventListeners.click.set(object as Object3D, onClick) }
48+
if (onPointerMove) { objectsWithEventListeners.pointerMove.set(object as Object3D, onPointerMove) }
49+
if (onPointerEnter) { objectsWithEventListeners.pointerEnter.set(object as Object3D, onPointerEnter) }
50+
if (onPointerLeave) { objectsWithEventListeners.pointerLeave.set(object as Object3D, onPointerLeave) }
5051
}
5152

5253
const objectsToWatch = computed(() =>
@@ -70,7 +71,7 @@ export const usePointerEventHandler = (
7071
const { onClick, onPointerMove } = useRaycaster(objectsToWatch, ctx)
7172

7273
onClick(({ intersects, event }) => {
73-
if (intersects.length) { objectsWithEventListeners.click.get(intersects[0].object)?.(intersects[0], event) }
74+
if (intersects.length) { objectsWithEventListeners.click.get(intersects[0].object)?.(intersects[0], event as PointerEvent) }
7475
})
7576

7677
let previouslyIntersectedObject: Object3D | null
@@ -80,12 +81,12 @@ export const usePointerEventHandler = (
8081

8182
const { pointerLeave, pointerEnter, pointerMove } = objectsWithEventListeners
8283

83-
if (previouslyIntersectedObject && previouslyIntersectedObject !== firstObject) { pointerLeave.get(previouslyIntersectedObject)?.(previouslyIntersectedObject, event) }
84+
if (previouslyIntersectedObject && previouslyIntersectedObject !== firstObject) { pointerLeave.get(previouslyIntersectedObject)?.(previouslyIntersectedObject, event as PointerEvent) }
8485

8586
if (firstObject) {
86-
if (previouslyIntersectedObject !== firstObject) { pointerEnter.get(firstObject)?.(intersects[0], event) }
87+
if (previouslyIntersectedObject !== firstObject) { pointerEnter.get(firstObject)?.(intersects[0], event as PointerEvent) }
8788

88-
pointerMove.get(firstObject)?.(intersects[0], event)
89+
pointerMove.get(firstObject)?.(intersects[0], event as PointerEvent)
8990
}
9091

9192
previouslyIntersectedObject = firstObject || null

src/composables/useRaycaster/index.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
import { Vector2, Vector3 } from 'three'
2-
import type { Intersection, Object3D, Object3DEventMap } from 'three'
2+
import type { Intersection, Object3D } from 'three'
33
import type { Ref, ShallowRef } from 'vue'
44
import { computed, onUnmounted, shallowRef } from 'vue'
55
import type { EventHook } from '@vueuse/core'
66
import { createEventHook, useElementBounding, usePointer } from '@vueuse/core'
77

8+
import type { DomEvent, TresCamera, TresEvent } from 'src/types'
89
import type { TresContext } from '../useTresContextProvider'
910

10-
export type Intersects = Intersection<Object3D<Object3DEventMap>>[]
11-
interface PointerMoveEventPayload {
12-
intersects?: Intersects
13-
event: PointerEvent
14-
}
15-
16-
interface PointerClickEventPayload {
17-
intersects: Intersects
18-
event: PointerEvent
19-
}
20-
21-
interface WheelEventPayload {
22-
intersects: Intersects
23-
event: WheelEvent
24-
}
25-
2611
export const useRaycaster = (
2712
objects: Ref<Object3D[]>,
2813
ctx: TresContext,
2914
) => {
3015
// having a separate computed makes useElementBounding work
3116
const canvas = computed(() => ctx.renderer.value.domElement as HTMLCanvasElement)
32-
const intersects: ShallowRef<Intersects[]> = shallowRef([])
17+
const intersects: ShallowRef<Intersection[]> = shallowRef([])
3318
const { x, y } = usePointer({ target: canvas })
3419
let delta = 0
3520

@@ -53,7 +38,7 @@ export const useRaycaster = (
5338
return intersects.value
5439
}
5540

56-
const getIntersects = (event?: PointerEvent | MouseEvent | WheelEvent) => {
41+
const getIntersects = (event?: DomEvent) => {
5742
const pointerPosition = getRelativePointerPosition({
5843
x: event?.clientX ?? x.value,
5944
y: event?.clientY ?? y.value,
@@ -63,14 +48,14 @@ export const useRaycaster = (
6348
return getIntersectsByRelativePointerPosition(pointerPosition) || []
6449
}
6550

66-
const eventHookClick = createEventHook<PointerClickEventPayload>()
67-
const eventHookDblClick = createEventHook<PointerClickEventPayload>()
68-
const eventHookPointerMove = createEventHook<PointerMoveEventPayload>()
69-
const eventHookPointerUp = createEventHook<PointerMoveEventPayload>()
70-
const eventHookPointerDown = createEventHook<PointerMoveEventPayload>()
71-
const eventHookPointerMissed = createEventHook<PointerClickEventPayload>()
72-
const eventHookContextMenu = createEventHook<PointerClickEventPayload>()
73-
const eventHookWheel = createEventHook<WheelEventPayload>()
51+
const eventHookClick = createEventHook<TresEvent>()
52+
const eventHookDblClick = createEventHook<TresEvent>()
53+
const eventHookPointerMove = createEventHook<TresEvent>()
54+
const eventHookPointerUp = createEventHook<TresEvent>()
55+
const eventHookPointerDown = createEventHook<TresEvent>()
56+
const eventHookPointerMissed = createEventHook<TresEvent>()
57+
const eventHookContextMenu = createEventHook<TresEvent>()
58+
const eventHookWheel = createEventHook<TresEvent>()
7459

7560
/* ({
7661
...DomEvent // All the original event data
@@ -92,19 +77,19 @@ export const useRaycaster = (
9277

9378
for (const property in event) {
9479
// Copy all non-function properties
95-
if (typeof property !== 'function') { mouseEventProperties[property] = event[property] }
80+
if (typeof property !== 'function') { mouseEventProperties[property] = (event as Record<string, any>)[property] }
9681
}
9782
return mouseEventProperties
9883
}
9984

10085
const triggerEventHook = (eventHook: EventHook, event: PointerEvent | MouseEvent | WheelEvent) => {
10186
const eventProperties = copyMouseEventProperties(event)
102-
87+
const unprojectedPoint = new Vector3(event?.clientX, event?.clientY, 0).unproject(ctx.camera?.value as TresCamera)
10388
eventHook.trigger({
10489
...eventProperties,
10590
intersections: intersects.value,
10691
// The unprojectedPoint is wrong, math needs to be fixed
107-
unprojectedPoint: new Vector3(event?.clientX, event?.clientY, 0).unproject(ctx.camera?.value),
92+
unprojectedPoint,
10893
ray: ctx.raycaster?.value.ray,
10994
camera: ctx.camera?.value,
11095
sourceEvent: event,
@@ -127,8 +112,8 @@ export const useRaycaster = (
127112

128113
// a click event is fired whenever a pointerdown happened after pointerup on the same object
129114
let mouseDownObject: Object3D | undefined
130-
let mouseDownPosition
131-
let mouseUpPosition
115+
let mouseDownPosition: Vector2
116+
let mouseUpPosition: Vector2
132117

133118
const onPointerDown = (event: PointerEvent) => {
134119
mouseDownObject = intersects.value[0]?.object
@@ -160,7 +145,7 @@ export const useRaycaster = (
160145
)
161146

162147
// Compute the distance between the mouse down and mouse up events
163-
delta = mouseDownPosition.distanceTo(mouseUpPosition)
148+
delta = mouseDownPosition?.distanceTo(mouseUpPosition)
164149

165150
if (event.button === 0) {
166151
// Left click
@@ -214,14 +199,14 @@ export const useRaycaster = (
214199

215200
return {
216201
intersects,
217-
onClick: (fn: (value: PointerClickEventPayload) => void) => eventHookClick.on(fn).off,
218-
onDblClick: (fn: (value: PointerClickEventPayload) => void) => eventHookDblClick.on(fn).off,
219-
onContextMenu: (fn: (value: PointerClickEventPayload) => void) => eventHookContextMenu.on(fn).off,
220-
onPointerMove: (fn: (value: PointerMoveEventPayload) => void) => eventHookPointerMove.on(fn).off,
221-
onPointerUp: (fn: (value: PointerMoveEventPayload) => void) => eventHookPointerUp.on(fn).off,
222-
onPointerDown: (fn: (value: PointerMoveEventPayload) => void) => eventHookPointerDown.on(fn).off,
223-
onPointerMissed: (fn: (value: PointerClickEventPayload) => void) => eventHookPointerMissed.on(fn).off,
224-
onWheel: (fn: (value: WheelEventPayload) => void) => eventHookWheel.on(fn).off,
202+
onClick: (fn: (value: TresEvent) => void) => eventHookClick.on(fn).off,
203+
onDblClick: (fn: (value: TresEvent) => void) => eventHookDblClick.on(fn).off,
204+
onContextMenu: (fn: (value: TresEvent) => void) => eventHookContextMenu.on(fn).off,
205+
onPointerMove: (fn: (value: TresEvent) => void) => eventHookPointerMove.on(fn).off,
206+
onPointerUp: (fn: (value: TresEvent) => void) => eventHookPointerUp.on(fn).off,
207+
onPointerDown: (fn: (value: TresEvent) => void) => eventHookPointerDown.on(fn).off,
208+
onPointerMissed: (fn: (value: TresEvent) => void) => eventHookPointerMissed.on(fn).off,
209+
onWheel: (fn: (value: TresEvent) => void) => eventHookWheel.on(fn).off,
225210
forceUpdate,
226211
}
227212
}

src/composables/useRenderer/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import type { ColorSpace, Scene, ShadowMapType, ToneMapping, WebGLRendererParameters } from 'three'
1212
import { useLogger } from '../useLogger'
13-
import type { TresColor } from '../../types'
13+
import type { EmitEventFn, TresColor } from '../../types'
1414
import { useRenderLoop } from '../useRenderLoop'
1515
import { normalizeColor } from '../../utils/normalize'
1616

@@ -108,7 +108,7 @@ export function useRenderer(
108108
canvas: MaybeRef<HTMLCanvasElement>
109109
scene: Scene
110110
options: UseRendererOptions
111-
emit: (event: string, ...args: any[]) => void
111+
emit: EmitEventFn
112112
contextParts: Pick<TresContext, 'sizes' | 'camera' | 'render'> & { invalidate: () => void, advance: () => void }
113113
disableRender: MaybeRefOrGetter<boolean>
114114
},

src/composables/useTresContextProvider/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useFps, useMemory, useRafFn } from '@vueuse/core'
22
import { computed, inject, onUnmounted, provide, readonly, ref, shallowRef } from 'vue'
3-
import type { Camera, EventDispatcher, Object3D, WebGLRenderer } from 'three'
3+
import type { Camera, EventDispatcher, WebGLRenderer } from 'three'
44
import { Raycaster } from 'three'
55
import type { ComputedRef, DeepReadonly, MaybeRef, MaybeRefOrGetter, Ref, ShallowRef } from 'vue'
66
import { calculateMemoryUsage } from '../../utils/perf'
@@ -9,7 +9,7 @@ import type { UseRendererOptions } from '../useRenderer'
99
import { useRenderer } from '../useRenderer'
1010
import { extend } from '../../core/catalogue'
1111
import { useLogger } from '../useLogger'
12-
import type { TresScene } from '../../types'
12+
import type { EmitEventFn, TresObject, TresScene } from '../../types'
1313
import type { EventProps } from '../usePointerEventHandler'
1414
import type { TresEventManager } from '../useTresEventManager'
1515
import useSizes, { type SizesType } from '../useSizes'
@@ -69,14 +69,14 @@ export interface TresContext {
6969
registerCamera: (camera: Camera) => void
7070
setCameraActive: (cameraOrUuid: Camera | string) => void
7171
deregisterCamera: (camera: Camera) => void
72-
eventManager: TresEventManager
72+
eventManager?: TresEventManager
7373
// Events
7474
// Temporaly add the methods to the context, this should be handled later by the EventManager state on the context https://github.com/Tresjs/tres/issues/515
7575
// When thats done maybe we can short the names of the methods since the parent will give the context.
76-
registerObjectAtPointerEventHandler: (object: Object3D & EventProps) => void
77-
deregisterObjectAtPointerEventHandler: (object: Object3D) => void
78-
registerBlockingObjectAtPointerEventHandler: (object: Object3D) => void
79-
deregisterBlockingObjectAtPointerEventHandler: (object: Object3D) => void
76+
registerObjectAtPointerEventHandler?: (object: TresObject & EventProps) => void
77+
deregisterObjectAtPointerEventHandler?: (object: TresObject) => void
78+
registerBlockingObjectAtPointerEventHandler?: (object: TresObject) => void
79+
deregisterBlockingObjectAtPointerEventHandler?: (object: TresObject) => void
8080
}
8181

8282
export function useTresContextProvider({
@@ -92,7 +92,8 @@ export function useTresContextProvider({
9292
windowSize: MaybeRefOrGetter<boolean>
9393
disableRender: MaybeRefOrGetter<boolean>
9494
rendererOptions: UseRendererOptions
95-
emit: (event: string, ...args: any[]) => void
95+
emit: EmitEventFn
96+
9697
}): TresContext {
9798
const { logWarning } = useLogger()
9899

0 commit comments

Comments
 (0)