@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
76 lines (75 loc) • 3.23 kB
JavaScript
;
import { setToArray } from "../../../core/SetUtils";
import { Poly } from "../../Poly";
import { JsConnectionPoint, JsConnectionPointType, JS_CONNECTION_POINT_IN_NODE_DEF } from "../utils/io/connections/Js";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import {
getConnectedOutputNodes,
getOutputIndices,
nodeMethodName,
triggerInputIndex
} from "./code/assemblers/actor/ActorAssemblerUtils";
import { TRIGGER_CONNECTION_NAME, TypedJsNode } from "./_Base";
const CONNECTION_OPTIONS = JS_CONNECTION_POINT_IN_NODE_DEF;
class TriggerTwoWaySwitchJsParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param if true, trigger will be forward through the 1st output. If false, it will be forwarded through the 2nd output. */
this.condition = ParamConfig.BOOLEAN(1);
}
}
const ParamsConfig = new TriggerTwoWaySwitchJsParamsConfig();
const _TriggerTwoWaySwitchJsNode = class extends TypedJsNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "triggerTwoWaySwitch";
}
initializeNode() {
super.initializeNode();
this.io.inputs.setNamedInputConnectionPoints([
new JsConnectionPoint(TRIGGER_CONNECTION_NAME, JsConnectionPointType.TRIGGER, CONNECTION_OPTIONS)
]);
this.io.outputs.setNamedOutputConnectionPoints([
new JsConnectionPoint(
_TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_TRUE,
JsConnectionPointType.TRIGGER,
CONNECTION_OPTIONS
),
new JsConnectionPoint(
_TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_FALSE,
JsConnectionPointType.TRIGGER,
CONNECTION_OPTIONS
)
]);
}
setTriggerableLines(shadersCollectionController) {
const condition = this.variableForInputParam(shadersCollectionController, this.p.condition);
const methodNamesIfTrue = triggerMethod(this, _TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_TRUE);
const methodNamesIfFalse = triggerMethod(this, _TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_FALSE);
const func = Poly.namedFunctionsRegister.getFunction("triggerTwoWaySwitch", this, shadersCollectionController);
const bodyLine = func.asString(condition, methodNamesIfTrue, methodNamesIfFalse);
shadersCollectionController.addTriggerableLines(this, [bodyLine], { addTriggeredLines: false });
}
};
export let TriggerTwoWaySwitchJsNode = _TriggerTwoWaySwitchJsNode;
TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_TRUE = `${TRIGGER_CONNECTION_NAME}IfTrue`;
TriggerTwoWaySwitchJsNode.OUTPUT_NAME_IF_FALSE = `${TRIGGER_CONNECTION_NAME}IfFalse`;
function triggerMethod(node, outputName) {
const outputIndex = getOutputIndices(node, (c) => c.name() == outputName)[0];
const triggerableNodes = /* @__PURE__ */ new Set();
getConnectedOutputNodes({
node,
triggerOutputIndices: [outputIndex],
triggerableNodes,
recursive: false
});
const triggerableMethodNames = setToArray(triggerableNodes, []).map((triggerableNode) => {
const argIndex = triggerInputIndex(node, triggerableNode);
const m = nodeMethodName(triggerableNode);
return `this.${m}(${argIndex})`;
});
return `()=>{ ${triggerableMethodNames.join(";")} }`;
}