@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.
161 lines • 5.9 kB
JavaScript
import { RegisterClass } from "../Misc/typeStore.js";
import { FlowGraphConnection } from "./flowGraphConnection.js";
import { Observable } from "../Misc/observable.js";
import { defaultValueSerializationFunction } from "./serialization.js";
/**
* Represents a connection point for data.
* An unconnected input point can have a default value.
* An output point will only have a value if it is connected to an input point. Furthermore,
* if the point belongs to a "function" node, the node will run its function to update the value.
*/
export class FlowGraphDataConnection extends FlowGraphConnection {
/**
* Create a new data connection point.
* @param name the name of the connection
* @param connectionType the type of the connection
* @param ownerBlock the block that owns this connection
* @param richType the type of the data in this block
* @param _defaultValue the default value of the connection
* @param _optional if the connection is optional
*/
constructor(name, connectionType, ownerBlock,
/**
* the type of the data in this block
*/
richType,
/**
* [any] the default value of the connection
*/
_defaultValue = richType.defaultValue,
/**
* [false] if the connection is optional
*/
_optional = false) {
super(name, connectionType, ownerBlock);
this.richType = richType;
this._defaultValue = _defaultValue;
this._optional = _optional;
this._isDisabled = false;
/**
* This is used for debugging purposes! It is the last value that was set to this connection with ANY context.
* Do not use this value for anything else, as it might be wrong if used in a different context.
*/
this._lastValue = null;
/**
* a data transformer function, if needed.
* This can be used, for example, to force seconds into milliseconds output, if it makes sense in your case.
*/
this.dataTransformer = null;
/**
* An observable that is triggered when the value of the connection changes.
*/
this.onValueChangedObservable = new Observable();
}
/**
* Whether or not the connection is optional.
* Currently only used for UI control.
*/
get optional() {
return this._optional;
}
/**
* is this connection disabled
* If the connection is disabled you will not be able to connect anything to it.
*/
get isDisabled() {
return this._isDisabled;
}
set isDisabled(value) {
if (this._isDisabled === value) {
return;
}
this._isDisabled = value;
if (this._isDisabled) {
this.disconnectFromAll();
}
}
/**
* An output data block can connect to multiple input data blocks,
* but an input data block can only connect to one output data block.
* @returns true if the connection is singular
*/
_isSingularConnection() {
return this.connectionType === 0 /* FlowGraphConnectionType.Input */;
}
/**
* Set the value of the connection in a specific context.
* @param value the value to set
* @param context the context to which the value is set
*/
setValue(value, context) {
// check if the value is different
if (context._getConnectionValue(this) === value) {
return;
}
context._setConnectionValue(this, value);
this.onValueChangedObservable.notifyObservers(value);
}
/**
* Reset the value of the connection to the default value.
* @param context the context in which the value is reset
*/
resetToDefaultValue(context) {
context._setConnectionValue(this, this._defaultValue);
}
/**
* Connect this point to another point.
* @param point the point to connect to.
*/
connectTo(point) {
if (this._isDisabled) {
return;
}
super.connectTo(point);
}
_getValueOrDefault(context) {
const val = context._getConnectionValue(this) ?? this._defaultValue;
return this.dataTransformer ? this.dataTransformer(val) : val;
}
/**
* Gets the value of the connection in a specific context.
* @param context the context from which the value is retrieved
* @returns the value of the connection
*/
getValue(context) {
if (this.connectionType === 1 /* FlowGraphConnectionType.Output */) {
context._notifyExecuteNode(this._ownerBlock);
this._ownerBlock._updateOutputs(context);
const value = this._getValueOrDefault(context);
this._lastValue = value;
return this.richType.typeTransformer ? this.richType.typeTransformer(value) : value;
}
const value = !this.isConnected() ? this._getValueOrDefault(context) : this._connectedPoint[0].getValue(context);
this._lastValue = value;
return this.richType.typeTransformer ? this.richType.typeTransformer(value) : value;
}
/**
* @internal
*/
_getLastValue() {
return this._lastValue;
}
/**
* @returns class name of the object.
*/
getClassName() {
return "FlowGraphDataConnection";
}
/**
* Serializes this object.
* @param serializationObject the object to serialize to
*/
serialize(serializationObject = {}) {
super.serialize(serializationObject);
serializationObject.richType = {};
this.richType.serialize(serializationObject.richType);
serializationObject.optional = this._optional;
defaultValueSerializationFunction("defaultValue", this._defaultValue, serializationObject);
}
}
RegisterClass("FlowGraphDataConnection", FlowGraphDataConnection);
//# sourceMappingURL=flowGraphDataConnection.js.map