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.

132 lines 4.12 kB
import { NeedleXRSession } from "./NeedleXRSession.js"; const onXRSessionStartListeners = []; /** * Add a listener for when an XR session starts * This event is triggered when the XR session is started, either by the user or by the application before all other XR start events * @param fn The function to call when the XR session starts * @example * ```js * onXRSessionStart((evt) => { * console.log("XR session started", evt); * }); * ``` * @returns A function to remove the listener */ export function onXRSessionStart(fn) { if (onXRSessionStartListeners.indexOf(fn) === -1) { onXRSessionStartListeners.push(fn); } return () => offXRSessionStart(fn); } /** * Remove a listener for when an XR session starts * @param fn The function to remove from the listeners * @example * ```js * const myFunction = (evt) => { * console.log("XR session started", evt); * }; * onXRSessionStart(myFunction); * offXRSessionStart(myFunction); * ``` */ export function offXRSessionStart(fn) { const index = onXRSessionStartListeners.indexOf(fn); if (index !== -1) { onXRSessionStartListeners.splice(index, 1); } } const onXRSessionEndListeners = []; /** * Add a listener for when an XR session ends * This event is triggered when the XR session is ended, either by the user or by the application before all other XR end events * @param fn The function to call when the XR session ends * @example * ```js * onXRSessionEnd((evt) => { * console.log("XR session ended", evt); * }); * ``` * @returns A function to remove the listener */ export function onXRSessionEnd(fn) { if (onXRSessionEndListeners.indexOf(fn) === -1) { onXRSessionEndListeners.push(fn); } return () => offXRSessionEnd(fn); } ; /** * Remove a listener for when an XR session ends * @param fn The function to remove from the listeners * @example * ```js * const myFunction = (evt) => { * console.log("XR session ended", evt); * }; * onXRSessionEnd(myFunction); * offXRSessionEnd(myFunction); * ``` */ export function offXRSessionEnd(fn) { const index = onXRSessionEndListeners.indexOf(fn); if (index !== -1) { onXRSessionEndListeners.splice(index, 1); } } /** * Add a listener that fires before an XR session is requested. * Use this to modify the session init options, e.g. to add optional features like `camera-access`. * @param fn The function to call before the XR session is requested * @example * ```js * onBeforeXRSession((args) => { * args.init.optionalFeatures ??= []; * args.init.optionalFeatures.push("camera-access"); * }); * ``` * @return A function to remove the listener */ export function onBeforeXRSession(fn) { if (onBeforeXRSessionListeners.indexOf(fn) === -1) { onBeforeXRSessionListeners.push(fn); // Delegate to NeedleXRSession which owns the actual invocation NeedleXRSession.onSessionRequestStart(fn); } return () => offBeforeXRSession(fn); } /** * Remove a listener for before an XR session is requested * @param fn The function to remove from the listeners */ export function offBeforeXRSession(fn) { const index = onBeforeXRSessionListeners.indexOf(fn); if (index !== -1) { onBeforeXRSessionListeners.splice(index, 1); NeedleXRSession.offSessionRequestStart(fn); } } const onBeforeXRSessionListeners = []; /** * @internal * Invoke the XRSessionStart event * @param evt The XRSession event arguments */ export function invokeXRSessionStart(evt) { globalThis.dispatchEvent(new CustomEvent("needle-xrsession-start", { detail: evt })); for (let i = 0; i < onXRSessionStartListeners.length; i++) { onXRSessionStartListeners[i](evt); } } /** * @internal * Invoke the XRSessionEnd event * @param evt The XRSession event arguments */ export function invokeXRSessionEnd(evt) { globalThis.dispatchEvent(new CustomEvent("needle-xrsession-end", { detail: evt })); for (let i = 0; i < onXRSessionEndListeners.length; i++) { onXRSessionEndListeners[i](evt); } } //# sourceMappingURL=events.js.map