UNPKG

lingo3d

Version:

Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor

192 lines 5.59 kB
import { Events } from "@lincode/events"; import { Disposable } from "@lincode/promiselikes"; import { createEffect, Reactive } from "@lincode/reactivity"; import { forceGetInstance } from "@lincode/utils"; import { nanoid } from "nanoid"; import getStaticProperties from "../utils/getStaticProperties"; import { emitDispose } from "../../events/onDispose"; import unsafeSetValue from "../../utils/unsafeSetValue"; import { appendableRoot } from "../../collections/appendableRoot"; import { userIdMap, uuidMap } from "../../collections/idCollections"; import { emitId } from "../../events/onId"; import { loopSystem } from "../../systems/loopSystem"; import { emitSceneGraphChangeSystem } from "../../systems/configSystems/emitSceneGraphChangeSystem"; export default class Appendable extends Disposable { constructor() { super(); appendableRoot.add(this); emitSceneGraphChangeSystem.add(this); } get componentName() { return getStaticProperties(this).componentName; } runtimeData; runtimeDefaults; parent; children; get firstChild() { const [firstChild] = this.children ?? [undefined]; return firstChild; } _firstChildState; get firstChildState() { return (this._firstChildState ??= new Reactive(this.firstChild)); } refreshFirstChildState() { this._firstChildState?.set(this.firstChild); } $appendNode(child) { appendableRoot.delete(child); emitSceneGraphChangeSystem.add(child); const { parent } = child; if (parent) { parent.children.delete(child); parent.refreshFirstChildState(); } child.parent = this; (this.children ??= new Set()).add(child); this.refreshFirstChildState(); } append(child) { this.$appendNode(child); } attach(child) { this.$appendNode(child); } _deleteSystemSet; get $deleteSystemSet() { return (this._deleteSystemSet ??= new Set()); } disposeNode() { this._uuid && uuidMap.delete(this._uuid); this._id && userIdMap.get(this._id).delete(this); if (this.handles) for (const handle of this.handles.values()) handle.cancel(); if (this._deleteSystemSet) for (const deleteSystem of this._deleteSystemSet) deleteSystem(this); emitDispose(this); } disposeChildren() { super.dispose(); this.disposeNode(); if (this.children) for (const child of this.children) child.disposeChildren(); } dispose() { if (this.done) return this; this.disposeChildren(); const { parent } = this; if (parent) { parent.children.delete(this); parent.refreshFirstChildState(); } else appendableRoot.delete(this); emitSceneGraphChangeSystem.add(this); return this; } traverse(cb) { for (const child of this.children ?? []) { cb(child); child.traverse(cb); } } traverseSome(cb) { for (const child of this.children ?? []) { if (cb(child)) return true; child.traverseSome(cb); } return false; } _events; get $events() { return (this._events ??= new Events()); } $emitEvent(key, payload) { this._events?.emit(key, payload); } _name; get name() { return this._name; } set name(val) { this._name = val; this.$emitEvent("name"); } _id; get id() { return this._id; } set id(val) { this._id && userIdMap.get(this._id).delete(this); this._id = val; val && forceGetInstance(userIdMap, val, Set).add(this); emitId(this); } _uuid; get uuid() { if (this._uuid) return this._uuid; const val = (this._uuid = nanoid()); uuidMap.set(val, this); return val; } set uuid(val) { if (this._uuid) return; this._uuid = val; uuidMap.set(val, this); } _proxy; get proxy() { return this._proxy; } set proxy(val) { this._proxy && unsafeSetValue(this._proxy, "__target", undefined); this._proxy = val; val && unsafeSetValue(val, "__target", this); } createEffect(cb, getStates) { return this.watch(createEffect(cb, getStates)); } handles; cancelHandle(name, lazyHandle) { const handles = (this.handles ??= new Map()); handles.get(name)?.cancel(); if (!lazyHandle) return; const handle = lazyHandle(); handles.set(name, handle); return handle; } _onLoop; get onLoop() { return this._onLoop; } set onLoop(cb) { this._onLoop = cb; cb ? loopSystem.add(this) : loopSystem.delete(this); } $disableSerialize; $disableSceneGraph; $disableUnload; $disableSelection; $ghost(disableSelection = true) { this.$disableSerialize = true; this.$disableSceneGraph = true; this.$disableSelection = disableSelection; emitSceneGraphChangeSystem.delete(this); } $unghost() { this.$disableSerialize = false; this.$disableSceneGraph = false; this.$disableSelection = false; emitSceneGraphChangeSystem.delete(this); } } //# sourceMappingURL=Appendable.js.map