UNPKG

@needle-tools/engine

Version:

Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.

100 lines (99 loc) 3.63 kB
import { InstantiateContext } from "../engine/engine_gameobject.js"; import type { IEventList } from "../engine/engine_types.js"; /** * CallInfo represents a single callback method that can be invoked by the {@link EventList}. */ export declare class CallInfo { /** * When the CallInfo is enabled it will be invoked when the EventList is invoked */ enabled: boolean; /** * The target object to invoke the method on OR the function to invoke */ target: Object | Function; methodName: string | null; /** * The arguments to invoke this method with */ arguments?: Array<any>; get canClone(): boolean; constructor(target: Function); constructor(target: Object, methodName: string | null, args?: Array<any>, enabled?: boolean); invoke(...args: any): void; } export declare class EventListEvent<TArgs extends any> extends Event { args?: TArgs; } /** * EventList manages a list of callbacks that can be invoked together. * Used for Unity-style events that can be configured in the editor (Unity or Blender). * * **Serialization:** * EventLists are serializable - callbacks configured in Unity/Blender will work at runtime. * Mark fields with `@serializable(EventList)` for editor support. * * **Usage patterns:** * - Button click handlers * - Animation events * - Custom component callbacks * - Scene loading events * * ![](https://cloud.needle.tools/-/media/P7bEKQvfgRUMTb2Wi1hWXg.png) * *Screenshot of a Unity component with an EventList field* * * ![](https://cloud.needle.tools/-/media/i2hi2OHfbaDyHyBL6Gt58A.png) * *Screenshot of a Blender component with an EventList field* * * @example Create and use an EventList * ```ts * // Define in your component * @serializable(EventList) * onClick: EventList = new EventList(); * * // Add listeners * this.onClick.addEventListener(() => console.log("Clicked!")); * * // Invoke all listeners * this.onClick.invoke(); * ``` * * @example Listen with arguments * ```ts * const onScore = new EventList<{ points: number }>(); * onScore.addEventListener(data => console.log("Scored:", data.points)); * onScore.invoke({ points: 100 }); * ``` * * @category Events * @group Utilities * @see {@link CallInfo} for individual callback configuration * @see {@link Button} for UI button events */ export declare class EventList<TArgs extends any = any> implements IEventList { /** checked during instantiate to create a new instance */ readonly isEventList = true; /** * @internal Used by the Needle Engine instantiate call to remap the event listeners to the new instance */ __internalOnInstantiate(ctx: InstantiateContext): EventList<any>; private target?; private key?; /** set an event target to try invoke the EventTarget dispatchEvent when this EventList is invoked */ setEventTarget(key: string, target: object): void; /** How many callback methods are subscribed to this event */ get listenerCount(): number; /** If the event is currently being invoked */ get isInvoking(): boolean; private _isInvoking; private readonly methods; private readonly _methodsCopy; static from(...evts: Array<Function>): EventList<any>; constructor(evts?: Array<CallInfo | Function> | Function); /** Invoke all the methods that are subscribed to this event */ invoke(...args: Array<TArgs>): boolean; /** Add a new event listener to this event */ addEventListener(cb: (args: TArgs) => void): Function; removeEventListener(cb: Function | null | undefined): void; removeAllEventListeners(): void; }