@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
89 lines (88 loc) • 2.84 kB
JavaScript
"use strict";
import { CoreGraphNode } from "../../../core/graph/CoreGraphNode";
import { NodeEvent } from "../../poly/NodeEvent";
import { coreTypeIsNaN } from "../../../core/Type";
export class NameController {
constructor(node) {
this.node = node;
this._graphNode = new CoreGraphNode(node.scene(), "nodeNameController");
}
dispose() {
this._graphNode.dispose();
this._onSetNameHooks = void 0;
this._onSetFullPathHooks = void 0;
}
graphNode() {
return this._graphNode;
}
static baseName(node) {
let base = node.type();
const last_char = base[base.length - 1];
if (!coreTypeIsNaN(parseInt(last_char))) {
base += "_";
}
return `${base}1`;
}
requestNameToParent(newName) {
const parent = this.node.parent();
if (parent && parent.childrenAllowed() && parent.childrenController) {
parent.childrenController.setChildName(this.node, newName);
} else {
console.warn("requestNameToParent failed, no parent found");
}
}
setName(newName) {
if (newName != this.node.name()) {
if (this.node.insideALockedParent()) {
const lockedParent = this.node.lockedParent();
console.warn(
`node '${this.node.path()}' cannot have its name changed, since it is inside '${lockedParent ? lockedParent.path() : ""}', which is locked`
);
return;
}
this.requestNameToParent(newName);
}
}
updateNameFromParent(new_name) {
var _a;
this.node._setCoreName(new_name);
this._postSetName();
this.runPostSetFullPathHooks();
if (this.node.childrenAllowed()) {
const children = (_a = this.node.childrenController) == null ? void 0 : _a.children();
if (children) {
for (const child_node of children) {
child_node.nameController.runPostSetFullPathHooks();
}
}
}
if (this.node.lifecycle.creationCompleted() && this.node.scene().loadingController.loaded()) {
this.node.scene().missingExpressionReferencesController.checkForMissingNodeReferences(this.node);
this.node.scene().expressionsController.regenerateReferringExpressions(this.node);
}
this.node.scene().referencesController.notifyNameUpdated(this.node);
this.node.emit(NodeEvent.NAME_UPDATED);
}
add_post_set_name_hook(hook) {
this._onSetNameHooks = this._onSetNameHooks || [];
this._onSetNameHooks.push(hook);
}
add_post_set_fullPath_hook(hook) {
this._onSetFullPathHooks = this._onSetFullPathHooks || [];
this._onSetFullPathHooks.push(hook);
}
_postSetName() {
if (this._onSetNameHooks) {
for (const hook of this._onSetNameHooks) {
hook();
}
}
}
runPostSetFullPathHooks() {
if (this._onSetFullPathHooks) {
for (const hook of this._onSetFullPathHooks) {
hook();
}
}
}
}