@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.
98 lines • 3.69 kB
JavaScript
import { FlowGraphBlock } from "../../flowGraphBlock.js";
import { RichTypeAny } from "../../flowGraphRichTypes.js";
import { RegisterClass } from "../../../Misc/typeStore.js";
/**
* Maximum number of log entries stored by the debug block.
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
const MAX_LOG_ENTRIES = 100;
/**
* A passthrough block used to debug data values flowing through connections.
* Spliced into a data connection to observe the value passing through it.
*/
export class FlowGraphDebugBlock extends FlowGraphBlock {
constructor(config) {
super(config);
/**
* Log of values that have passed through this block.
* Each entry is a [displayString, tooltipString] tuple.
*/
this.log = [];
/**
* Whether this block is a debug block.
* Used by the editor to identify debug blocks.
*/
this._isDebug = true;
this.input = this.registerDataInput("input", RichTypeAny);
this.output = this.registerDataOutput("output", RichTypeAny);
}
/**
* @internal
*/
_updateOutputs(context) {
const value = this.input.getValue(context);
this.output.setValue(value, context);
this._logValue(value);
}
/**
* Format and store a value in the log.
* @param value
*/
_logValue(value) {
if (value === null || value === undefined) {
this.log.push(["null", "null"]);
}
else {
const formatted = FlowGraphDebugBlock._FormatValue(value);
this.log.push([formatted, String(value)]);
}
if (this.log.length > MAX_LOG_ENTRIES) {
this.log.shift();
}
}
/**
* Type-aware value formatting.
* @param value the value to format
* @returns a human-readable string
*/
static _FormatValue(value) {
if (typeof value === "number") {
return Number.isInteger(value) ? value.toString() : value.toFixed(4);
}
if (typeof value === "boolean" || typeof value === "string") {
return String(value);
}
// Vector-like objects with x, y, z, w
if (value && typeof value === "object") {
if ("w" in value && "x" in value && "y" in value && "z" in value) {
return `(${value.x.toFixed(3)}, ${value.y.toFixed(3)}, ${value.z.toFixed(3)}, ${value.w.toFixed(3)})`;
}
if ("z" in value && "x" in value && "y" in value) {
return `(${value.x.toFixed(3)}, ${value.y.toFixed(3)}, ${value.z.toFixed(3)})`;
}
if ("x" in value && "y" in value) {
return `(${value.x.toFixed(3)}, ${value.y.toFixed(3)})`;
}
// Color3/Color4 with r, g, b
if ("r" in value && "g" in value && "b" in value) {
const a = "a" in value ? `, ${value.a.toFixed(3)}` : "";
return `(${value.r.toFixed(3)}, ${value.g.toFixed(3)}, ${value.b.toFixed(3)}${a})`;
}
if (typeof value.toString === "function" && value.toString !== Object.prototype.toString) {
return value.toString();
}
}
try {
const str = JSON.stringify(value);
return str.length > 64 ? str.substring(0, 61) + "..." : str;
}
catch {
return String(value);
}
}
getClassName() {
return "FlowGraphDebugBlock" /* FlowGraphBlockNames.DebugBlock */;
}
}
RegisterClass("FlowGraphDebugBlock" /* FlowGraphBlockNames.DebugBlock */, FlowGraphDebugBlock);
//# sourceMappingURL=flowGraphDebugBlock.js.map