@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
87 lines (86 loc) • 2.8 kB
JavaScript
"use strict";
import { TypedSopNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { InputCloneMode } from "../../poly/InputCloneMode";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { NodeEvent } from "../../poly/NodeEvent";
const DEFAULT_INPUTS_COUNT = 4;
class SwitchSopParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
/** @param sets which input is used */
this.input = ParamConfig.INTEGER(0, {
range: [0, 3],
rangeLocked: [true, false],
callback: (node) => {
SwitchSopNode.PARAM_CALLBACK_setInputsEvaluation(node);
}
});
/** @param number of inputs that this node can merge geometries from */
this.inputsCount = ParamConfig.INTEGER(DEFAULT_INPUTS_COUNT, {
range: [1, 32],
rangeLocked: [true, false],
separatorBefore: true,
callback: (node) => {
SwitchSopNode.PARAM_CALLBACK_setInputsCount(node);
}
});
}
}
const ParamsConfig = new SwitchSopParamsConfig();
export class SwitchSopNode extends TypedSopNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return SopType.SWITCH;
}
initializeNode() {
this.io.inputs.setCount(0, 4);
this.io.inputs.initInputsClonedState(InputCloneMode.NEVER);
this.io.inputs.onEnsureListenToSingleInputIndexUpdated(async () => {
await this._callbackUpdateInputsEvaluation();
});
this.params.onParamsCreated("update inputs", () => {
this._callbackUpdateInputsCount();
});
}
async cook() {
const inputIndex = this.pv.input;
if (!this.io.inputs.hasInput(inputIndex)) {
this.states.error.set(`no input ${inputIndex}`);
this.cookController.endCook();
return;
}
const container = await this.containerController.requestInputContainer(inputIndex);
if (!container) {
this.states.error.set(`invalid input ${inputIndex}`);
this.cookController.endCook();
return;
}
const coreGroup = container.coreContent();
if (!coreGroup) {
this.states.error.set(`invalid input ${inputIndex}`);
this.cookController.endCook();
return;
}
this.setObjects(coreGroup.allObjects());
}
async _callbackUpdateInputsEvaluation() {
if (this.p.input.isDirty()) {
await this.p.input.compute();
}
this.io.inputs.listenToSingleInputIndex(this.pv.input);
}
static PARAM_CALLBACK_setInputsEvaluation(node) {
node._callbackUpdateInputsEvaluation();
}
_callbackUpdateInputsCount() {
this.io.inputs.setCount(1, this.pv.inputsCount);
this.emit(NodeEvent.INPUTS_UPDATED);
}
static PARAM_CALLBACK_setInputsCount(node) {
node._callbackUpdateInputsCount();
}
}