polygonjs-engine
Version:
node-based webgl 3D engine https://polygonjs.com
76 lines (75 loc) • 2.16 kB
JavaScript
import {CoreWalker} from "./Walker";
export class DecomposedPath {
constructor() {
this._index = -1;
this._path_elements = [];
this._named_nodes = [];
this._graph_node_ids = [];
this._node_element_by_graph_node_id = new Map();
}
reset() {
this._index = -1;
this._path_elements = [];
this._named_nodes = [];
this._graph_node_ids = [];
this._node_element_by_graph_node_id.clear();
}
add_node(name, node) {
this._index += 1;
if (name == node.name()) {
this._named_nodes[this._index] = node;
}
this._graph_node_ids[this._index] = node.graphNodeId();
this._node_element_by_graph_node_id.set(node.graphNodeId(), name);
}
add_path_element(path_element) {
this._index += 1;
this._path_elements[this._index] = path_element;
}
named_graph_nodes() {
return this._named_nodes;
}
named_nodes() {
const nodes = [];
for (let graph_node of this._named_nodes) {
if (graph_node) {
const node = graph_node;
if (node.nameController) {
nodes.push(node);
}
}
}
return nodes;
}
update_from_name_change(node) {
const named_graph_node_ids = this._named_nodes.map((n) => n?.graphNodeId());
if (named_graph_node_ids.includes(node.graphNodeId())) {
this._node_element_by_graph_node_id.set(node.graphNodeId(), node.name());
}
}
to_path() {
const elements = new Array(this._index);
for (let i = 0; i <= this._index; i++) {
const node = this._named_nodes[i];
if (node) {
const node_name = this._node_element_by_graph_node_id.get(node.graphNodeId());
if (node_name) {
elements[i] = node_name;
}
} else {
const path_element = this._path_elements[i];
if (path_element) {
elements[i] = path_element;
}
}
}
let joined_path = elements.join(CoreWalker.SEPARATOR);
const first_char = joined_path[0];
if (first_char) {
if (!CoreWalker.NON_LETTER_PREFIXES.includes(first_char)) {
joined_path = `${CoreWalker.SEPARATOR}${joined_path}`;
}
}
return joined_path;
}
}