@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
109 lines (108 loc) • 4.4 kB
JavaScript
"use strict";
import { TypedSopNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { StringParamLanguage } from "../../params/utils/OptionsController";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { Poly } from "../../Poly";
import { CoreCSSObjectAttribute, DEFAULT_CSS2DOBJECT } from "../../../core/render/CSSRenderers/CSSObjectAttribute";
import { createCSS2DObject } from "../../../core/render/CSSRenderers/CSS2DObject";
import { stringToAttribNames } from "../../../core/String";
import { InputCloneMode } from "../../poly/InputCloneMode";
import { replaceChild } from "../../poly/PolyOnObjectsAddRemoveHooksController";
class CSS2DObjectSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param toggles on if attributes are copied from the geometry to the html element */
this.copyAttributes = ParamConfig.BOOLEAN(true);
/** @param names of the attributes that are copied from the geometry to the html element */
this.attributesToCopy = ParamConfig.STRING("", {
visibleIf: { copyAttributes: true }
});
/** @param defines if the htmlId attribute is used to create the html id attribute */
this.overrideId = ParamConfig.BOOLEAN(true);
/** @param value of the html element id attribute */
this.id = ParamConfig.STRING(DEFAULT_CSS2DOBJECT.id, {
visibleIf: { overrideId: 1 }
});
/** @param defines if the htmlClass attribute is used to create the html class */
this.overrideClassName = ParamConfig.BOOLEAN(true);
/** @param value of the html class */
this.className = ParamConfig.STRING(DEFAULT_CSS2DOBJECT.className, {
visibleIf: { overrideClassName: 1 }
});
/** @param defines if the html attribute is used to create the html content */
this.overrideHTML = ParamConfig.BOOLEAN(true);
/** @param value of the html content */
this.html = ParamConfig.STRING(DEFAULT_CSS2DOBJECT.html, {
visibleIf: { overrideHTML: 1 },
language: StringParamLanguage.HTML
});
}
}
const ParamsConfig = new CSS2DObjectSopParamsConfig();
export class CSS2DObjectSopNode extends TypedSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return SopType.CSS2D_OBJECT;
}
initializeNode() {
this.io.inputs.setCount(0, 1);
this.io.inputs.initInputsClonedState(InputCloneMode.FROM_NODE);
}
cook(inputCoreGroups) {
const coreGroup = inputCoreGroups[0];
if (coreGroup) {
const objects = coreGroup.allObjects();
for (const object of objects) {
this._addAttributes(object);
}
this.setCoreGroup(coreGroup);
} else {
const attribNames = [];
const group = createCSS2DObject({
id: this.pv.id,
className: this.pv.className,
html: this.pv.html,
copyAttributes: this.pv.copyAttributes,
attributesToCopy: stringToAttribNames(this.pv.attributesToCopy, attribNames)
});
group.name = this.name();
this._addAttributes(group);
this.setObjects([group]);
}
}
_addAttributes(object) {
Poly.onObjectsAddRemoveHooks.assignOnAddHookHandler(object, this);
if (this.pv.overrideId) {
CoreCSSObjectAttribute.setElementId(object, this.pv.id);
}
if (this.pv.overrideClassName) {
CoreCSSObjectAttribute.setElementClass(object, this.pv.className);
}
if (this.pv.overrideHTML) {
CoreCSSObjectAttribute.setElementHTML(object, this.pv.html);
}
CoreCSSObjectAttribute.setCopyAttributes(object, this.pv.copyAttributes);
CoreCSSObjectAttribute.setAttributesToCopy(object, this.pv.attributesToCopy);
}
updateObjectOnAdd(object, parent) {
const id = CoreCSSObjectAttribute.getElementId(object);
const className = CoreCSSObjectAttribute.getElementClass(object);
const html = CoreCSSObjectAttribute.getElementHTML(object);
const copyAttributes = CoreCSSObjectAttribute.getCopyAttributes(object);
const attributesToCopy = CoreCSSObjectAttribute.getAttributesToCopy(object);
const attribNames = [];
const CSSObject = createCSS2DObject({
object,
id,
className,
html,
copyAttributes,
attributesToCopy: stringToAttribNames(attributesToCopy, attribNames)
});
replaceChild(parent, object, CSSObject);
}
}