@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.
77 lines • 3.86 kB
JavaScript
import { RegisterClass } from "../../../Misc/typeStore.js";
import { FlowGraphExecutionBlockWithOutSignal } from "../../flowGraphExecutionBlockWithOutSignal.js";
import { RichTypeAny } from "../../flowGraphRichTypes.js";
/**
* This block will set a variable on the context.
*/
export class FlowGraphSetVariableBlock extends FlowGraphExecutionBlockWithOutSignal {
constructor(config) {
super(config);
// check if the variable is defined
if (!config.variable && !config.variables) {
throw new Error("FlowGraphSetVariableBlock: variable/variables is not defined");
}
// check if the variable is an array
if (config.variables && config.variable) {
throw new Error("FlowGraphSetVariableBlock: variable and variables are both defined");
}
// check if we have either a variable or variables. If we have variables, set the inputs correctly
if (config.variables) {
for (const variable of config.variables) {
this.registerDataInput(variable, RichTypeAny);
}
}
else {
this.registerDataInput("value", RichTypeAny);
}
}
_execute(context, _callingSignal) {
if (this.config?.variables) {
for (const variable of this.config.variables) {
this._saveVariable(context, variable);
}
}
else {
this._saveVariable(context, this.config?.variable, "value");
}
this.out._activateSignal(context);
}
_saveVariable(context, variableName, inputName) {
// check if there is an animation(group) running on this variable. If there is, stop the animation - a value was force-set.
const currentlyRunningAnimationGroups = context._getGlobalContextVariable("currentlyRunningAnimationGroups", []);
for (const animationUniqueId of currentlyRunningAnimationGroups) {
const animationGroup = context.assetsContext.animationGroups.find((animationGroup) => animationGroup.uniqueId == animationUniqueId);
if (animationGroup) {
// check if there is a target animation that has the target set to be the context
for (const targetAnimation of animationGroup.targetedAnimations) {
// check if the target property is the variable we are setting
if (targetAnimation.target === context) {
// check the variable name
if (targetAnimation.animation.targetProperty === variableName) {
// stop the animation
animationGroup.stop();
// remove the animation from the currently running animations
const index = currentlyRunningAnimationGroups.indexOf(animationUniqueId);
if (index > -1) {
currentlyRunningAnimationGroups.splice(index, 1);
}
context._setGlobalContextVariable("currentlyRunningAnimationGroups", currentlyRunningAnimationGroups);
break;
}
}
}
}
}
const value = this.getDataInput(inputName || variableName)?.getValue(context);
context.setVariable(variableName, value);
}
getClassName() {
return "FlowGraphSetVariableBlock" /* FlowGraphBlockNames.SetVariable */;
}
serialize(serializationObject) {
super.serialize(serializationObject);
serializationObject.config.variable = this.config?.variable;
}
}
RegisterClass("FlowGraphSetVariableBlock" /* FlowGraphBlockNames.SetVariable */, FlowGraphSetVariableBlock);
//# sourceMappingURL=flowGraphSetVariableBlock.js.map