|
@@ -1,10 +1,8 @@
|
|
|
-import type { EmitEventFn, EmitEventName } from '../../types'
|
|
|
-
|
|
|
export type Events = CreateEventsReturn<any, any, any, any>
|
|
|
export type EventsProps = CreateEventsProps<any, any, any, any, any, any>
|
|
|
|
|
|
-export interface CreateEventsReturn<TEvent, TIntersection, TObj, TTarget> {
|
|
|
- connect: (target: TTarget) => void
|
|
|
+export interface CreateEventsReturn<TEvent, TIntersection, TObj, TSource> {
|
|
|
+ connect: (target: TSource) => void
|
|
|
disconnect: () => void
|
|
|
handle: (event: TEvent) => TIntersection[]
|
|
|
update: () => TIntersection[]
|
|
@@ -14,7 +12,7 @@ export interface CreateEventsReturn<TEvent, TIntersection, TObj, TTarget> {
|
|
|
patchProp: (instanceOrObj: TObj, propName: string, prevValue: any, nextValue: any) => boolean
|
|
|
config: any
|
|
|
enabled: boolean
|
|
|
- target: TTarget | undefined
|
|
|
+ target: TSource | undefined
|
|
|
priority: number
|
|
|
isEvents: true
|
|
|
}
|
|
@@ -25,13 +23,13 @@ export interface CreateEventsProps<
|
|
|
TEvt,
|
|
|
TIntersection,
|
|
|
TObj,
|
|
|
- TTarget,
|
|
|
+ TSource,
|
|
|
> {
|
|
|
- getInitialConfig: (context: TCtx, emit: EmitEventFn) => TConfig
|
|
|
+ getInitialConfig: (context: TCtx) => TConfig
|
|
|
getInitialEvent: () => TEvt
|
|
|
|
|
|
connect: (
|
|
|
- target: TTarget,
|
|
|
+ source: TSource,
|
|
|
handle: (e: TEvt) => TIntersection[],
|
|
|
config: TConfig,
|
|
|
) => { disconnect: () => void }
|
|
@@ -52,19 +50,18 @@ export interface CreateEventsProps<
|
|
|
getLastEvent: (config: TConfig) => TEvt
|
|
|
}
|
|
|
|
|
|
-export function createEvents<TConfig, TCtx, TEvent, TIntersection, TObj, TTarget>(
|
|
|
- props: CreateEventsProps<TConfig, TCtx, TEvent, TIntersection, TObj, TTarget>,
|
|
|
+export function createEvents<TConfig, TCtx, TEvent, TIntersection, TObj, TSource>(
|
|
|
+ props: CreateEventsProps<TConfig, TCtx, TEvent, TIntersection, TObj, TSource>,
|
|
|
context: TCtx,
|
|
|
- emit: EmitEventFn = (_event: EmitEventName, _args: any) => {},
|
|
|
-): CreateEventsReturn<TEvent, TIntersection, TObj, TTarget> {
|
|
|
- const config = props.getInitialConfig(context, emit)
|
|
|
+): CreateEventsReturn<TEvent, TIntersection, TObj, TSource> {
|
|
|
+ const config = props.getInitialConfig(context)
|
|
|
let enabled = true
|
|
|
let priority = 1
|
|
|
- let target = undefined as TTarget | undefined
|
|
|
+ let source = undefined as TSource | undefined
|
|
|
let lastIntersections = []
|
|
|
let propsDisconnect = () => {}
|
|
|
|
|
|
- const handle: CreateEventsReturn<TEvent, TIntersection, TObj, TTarget>['handle'] = (event) => {
|
|
|
+ const handle: CreateEventsReturn<TEvent, TIntersection, TObj, TSource>['handle'] = (event) => {
|
|
|
if (!enabled) {
|
|
|
return []
|
|
|
}
|
|
@@ -104,24 +101,43 @@ export function createEvents<TConfig, TCtx, TEvent, TIntersection, TObj, TTarget
|
|
|
const disconnect = () => {
|
|
|
propsDisconnect()
|
|
|
propsDisconnect = () => {}
|
|
|
- target = undefined
|
|
|
+ source = undefined
|
|
|
}
|
|
|
|
|
|
- const connect = (eventTarget: TTarget) => {
|
|
|
- if (eventTarget === target) { return }
|
|
|
+ /**
|
|
|
+ * Connects this Events instance to an event source, e.g., an HTML canvas or div.
|
|
|
+ * @param eventTarget – the event source
|
|
|
+ */
|
|
|
+ const connect = (eventTarget: TSource) => {
|
|
|
+ if (eventTarget === source) { return }
|
|
|
disconnect()
|
|
|
propsDisconnect = props.connect(eventTarget, handle, config).disconnect
|
|
|
- target = eventTarget
|
|
|
+ source = eventTarget
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This hook is called when a new object – e.g., a TresObject instance – is added to the scene.
|
|
|
+ * @param instanceOrObject – the added object
|
|
|
+ */
|
|
|
const insert = (instanceOrObject: TObj) => {
|
|
|
props.insert(instanceOrObject, config)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This hook is called when a new object – e.g., a TresObject instance – is remove from the scene.
|
|
|
+ * @param instanceOrObject – the removed object
|
|
|
+ */
|
|
|
const remove = (instanceOrObject: TObj) => {
|
|
|
props.remove(instanceOrObject, config)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This hook is called when an object – e.g., a TresObject instance – prop updates.
|
|
|
+ * @param instanceOrObj – the object whose prop will update
|
|
|
+ * @param propName – the prop name
|
|
|
+ * @param prevValue – the previous prop value
|
|
|
+ * @param nextValue – the next prop value
|
|
|
+ */
|
|
|
const patchProp = (instanceOrObj: TObj, propName: string, prevValue: any, nextValue: any) => {
|
|
|
return props.patchProp(instanceOrObj, propName, prevValue, nextValue, config)
|
|
|
}
|
|
@@ -140,7 +156,7 @@ export function createEvents<TConfig, TCtx, TEvent, TIntersection, TObj, TTarget
|
|
|
get enabled() { return enabled },
|
|
|
set priority(n: number) { priority = n },
|
|
|
get priority() { return priority },
|
|
|
- get target() { return target },
|
|
|
+ get target() { return source },
|
|
|
isEvents: true,
|
|
|
}
|
|
|
}
|