@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
85 lines (84 loc) • 2.5 kB
JavaScript
"use strict";
import { CoreWalker } from "./Walker";
export class DecomposedPath {
constructor() {
this._index = -1;
this._pathElements = [];
this._namedNodes = [];
this._graphNodeIds = [];
this._nodeElementByGraphNodeId = /* @__PURE__ */ new Map();
this._absolutePath = "/";
}
reset() {
this._index = -1;
this._pathElements = [];
this._namedNodes = [];
this._graphNodeIds = [];
this._nodeElementByGraphNodeId.clear();
}
addNamedNode(namedNode) {
this._index += 1;
if (namedNode.name == namedNode.node.name()) {
this._namedNodes[this._index] = namedNode;
}
this._graphNodeIds[this._index] = namedNode.node.graphNodeId();
this._nodeElementByGraphNodeId.set(namedNode.node.graphNodeId(), namedNode.name);
this._absolutePath = [this._absolutePath, namedNode.name].join(CoreWalker.SEPARATOR);
}
addPathElement(pathElement) {
this._index += 1;
this._pathElements[this._index] = pathElement;
if (pathElement.node) {
this._absolutePath = pathElement.node.path();
}
}
namedGraphNodes() {
return this._namedNodes;
}
namedNodes(target) {
target.length = 0;
for (const namedNode of this._namedNodes) {
if (namedNode) {
const node = namedNode.node;
if (node.nameController) {
target.push(node);
}
}
}
return target;
}
updateFromNameChange(node) {
const namedGraphNodeIds = this._namedNodes.map((n) => n == null ? void 0 : n.node.graphNodeId());
if (namedGraphNodeIds.includes(node.graphNodeId())) {
this._nodeElementByGraphNodeId.set(node.graphNodeId(), node.name());
}
}
toPath() {
const elements = new Array(this._index);
for (let i = 0; i <= this._index; i++) {
const namedNode = this._namedNodes[i];
if (namedNode) {
const nodeName = this._nodeElementByGraphNodeId.get(namedNode.node.graphNodeId());
if (nodeName) {
elements[i] = nodeName;
}
} else {
const pathElement = this._pathElements[i];
if (pathElement) {
elements[i] = pathElement.path;
}
}
}
let joinedPath = elements.join(CoreWalker.SEPARATOR);
const firstChar = joinedPath[0];
if (firstChar) {
if (!CoreWalker.NON_LETTER_PREFIXES.includes(firstChar)) {
joinedPath = `${CoreWalker.SEPARATOR}${joinedPath}`;
}
}
return joinedPath;
}
toAbsolutePath() {
return this._absolutePath;
}
}