-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathSelection.tsx
More file actions
41 lines (37 loc) · 1.45 KB
/
Selection.tsx
File metadata and controls
41 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as THREE from 'three'
import React, { createContext, useState, useContext, useEffect, useRef, useMemo } from 'react'
export type Api = {
selected: THREE.Object3D[]
select: React.Dispatch<React.SetStateAction<THREE.Object3D[]>>
enabled: boolean
}
export type SelectApi = JSX.IntrinsicElements['group'] & {
enabled?: boolean
}
export const selectionContext = createContext<Api | null>(null)
export function Selection({ children, enabled = true }: { enabled?: boolean; children: React.ReactNode }) {
const [selected, select] = useState<THREE.Object3D[]>([])
const value = useMemo(() => ({ selected, select, enabled }), [selected, select, enabled])
return <selectionContext.Provider value={value}>{children}</selectionContext.Provider>
}
export function Select({ enabled = false, children, ...props }: SelectApi) {
const group = useRef<THREE.Group>(null!)
const api = useContext(selectionContext)
useEffect(() => {
if (!api) return
const current: THREE.Object3D<THREE.Event>[] = []
group.current.traverse((o) => {
if (o.type === 'Mesh') current.push(o)
})
if (enabled && current.some(o => !api.selected.includes(o))) {
api.select(state => [...state, ...current])
} else if (!enabled && current.some(o => api.selected.includes(o))) {
api.select(state => state.filter(o => !current.includes(o)))
}
}, [enabled, children, api])
return (
<group ref={group} {...props}>
{children}
</group>
)
}