@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
55 lines • 2.27 kB
JavaScript
import { FlowGraphBlock } from "../../flowGraphBlock.js";
import { RichTypeAny } from "../../flowGraphRichTypes.js";
import { getNumericValue, isNumeric } from "../../utils.js";
import { RegisterClass } from "../../../Misc/typeStore.js";
/**
* This block conditionally outputs one of its inputs, based on a condition and a list of cases.
*
* This of it as a passive (data) version of the switch statement in programming languages.
*/
export class FlowGraphDataSwitchBlock extends FlowGraphBlock {
constructor(
/**
* the configuration of the block
*/
config) {
super(config);
this.config = config;
this._inputCases = new Map();
this.case = this.registerDataInput("case", RichTypeAny, NaN);
this.default = this.registerDataInput("default", RichTypeAny);
this.value = this.registerDataOutput("value", RichTypeAny);
// iterate the set not using for of
const array = this.config.cases || [];
for (let caseValue of array) {
// if treat as integers, make sure not to set it again if it exists
caseValue = getNumericValue(caseValue);
if (this.config.treatCasesAsIntegers) {
caseValue = caseValue | 0;
if (this._inputCases.has(caseValue)) {
return;
}
}
this._inputCases.set(caseValue, this.registerDataInput(`in_${caseValue}`, RichTypeAny));
}
}
_updateOutputs(context) {
const selectionValue = this.case.getValue(context);
let outputValue;
if (isNumeric(selectionValue)) {
outputValue = this._getOutputValueForCase(getNumericValue(selectionValue), context);
}
else {
outputValue = this.default.getValue(context);
}
this.value.setValue(outputValue, context);
}
_getOutputValueForCase(caseValue, context) {
return this._inputCases.get(caseValue)?.getValue(context);
}
getClassName() {
return "FlowGraphDataSwitchBlock" /* FlowGraphBlockNames.DataSwitch */;
}
}
RegisterClass("FlowGraphDataSwitchBlock" /* FlowGraphBlockNames.DataSwitch */, FlowGraphDataSwitchBlock);
//# sourceMappingURL=flowGraphDataSwitchBlock.js.map