2023-07-10 09:09:03 +00:00
|
|
|
import { ComfyWidgets, addValueControlWidget } from "../../scripts/widgets.js";
|
|
|
|
import { app } from "../../scripts/app.js";
|
2023-03-23 21:37:19 +00:00
|
|
|
|
|
|
|
const CONVERTED_TYPE = "converted-widget";
|
2023-08-04 00:24:52 +00:00
|
|
|
const VALID_TYPES = ["STRING", "combo", "number", "BOOLEAN"];
|
2023-10-04 19:48:55 +00:00
|
|
|
const CONFIG = Symbol();
|
2023-10-06 20:48:30 +00:00
|
|
|
const GET_CONFIG = Symbol();
|
2023-03-23 21:37:19 +00:00
|
|
|
|
2023-10-03 19:19:12 +00:00
|
|
|
function getConfig(widgetName) {
|
|
|
|
const { nodeData } = this.constructor;
|
|
|
|
return nodeData?.input?.required[widgetName] ?? nodeData?.input?.optional?.[widgetName];
|
|
|
|
}
|
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
function isConvertableWidget(widget, config) {
|
2023-08-04 02:49:52 +00:00
|
|
|
return (VALID_TYPES.includes(widget.type) || VALID_TYPES.includes(config[0])) && !widget.options?.forceInput;
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideWidget(node, widget, suffix = "") {
|
|
|
|
widget.origType = widget.type;
|
|
|
|
widget.origComputeSize = widget.computeSize;
|
|
|
|
widget.origSerializeValue = widget.serializeValue;
|
|
|
|
widget.computeSize = () => [0, -4]; // -4 is due to the gap litegraph adds between widgets automatically
|
|
|
|
widget.type = CONVERTED_TYPE + suffix;
|
|
|
|
widget.serializeValue = () => {
|
|
|
|
// Prevent serializing the widget if we have no input linked
|
2023-09-02 15:48:44 +00:00
|
|
|
if (!node.inputs) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-09-02 16:17:30 +00:00
|
|
|
let node_input = node.inputs.find((i) => i.widget?.name === widget.name);
|
|
|
|
|
|
|
|
if (!node_input || !node_input.link) {
|
2023-03-23 21:37:19 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
2023-04-02 12:43:40 +00:00
|
|
|
return widget.origSerializeValue ? widget.origSerializeValue() : widget.value;
|
2023-03-23 21:37:19 +00:00
|
|
|
};
|
|
|
|
|
2023-04-13 00:57:13 +00:00
|
|
|
// Hide any linked widgets, e.g. seed+seedControl
|
2023-03-23 21:37:19 +00:00
|
|
|
if (widget.linkedWidgets) {
|
|
|
|
for (const w of widget.linkedWidgets) {
|
|
|
|
hideWidget(node, w, ":" + widget.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showWidget(widget) {
|
|
|
|
widget.type = widget.origType;
|
|
|
|
widget.computeSize = widget.origComputeSize;
|
|
|
|
widget.serializeValue = widget.origSerializeValue;
|
|
|
|
|
|
|
|
delete widget.origType;
|
|
|
|
delete widget.origComputeSize;
|
|
|
|
delete widget.origSerializeValue;
|
|
|
|
|
2023-04-13 00:57:13 +00:00
|
|
|
// Hide any linked widgets, e.g. seed+seedControl
|
2023-03-23 21:37:19 +00:00
|
|
|
if (widget.linkedWidgets) {
|
|
|
|
for (const w of widget.linkedWidgets) {
|
|
|
|
showWidget(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertToInput(node, widget, config) {
|
|
|
|
hideWidget(node, widget);
|
|
|
|
|
2023-10-05 18:16:39 +00:00
|
|
|
const { type } = getWidgetType(config);
|
2023-03-23 21:37:19 +00:00
|
|
|
|
|
|
|
// Add input and store widget config for creating on primitive node
|
2023-03-24 16:36:11 +00:00
|
|
|
const sz = node.size;
|
2023-10-05 18:16:39 +00:00
|
|
|
node.addInput(widget.name, type, {
|
2023-10-06 20:48:30 +00:00
|
|
|
widget: { name: widget.name, [GET_CONFIG]: () => config },
|
2023-03-23 21:37:19 +00:00
|
|
|
});
|
2023-03-24 16:36:11 +00:00
|
|
|
|
2023-06-20 16:03:46 +00:00
|
|
|
for (const widget of node.widgets) {
|
|
|
|
widget.last_y += LiteGraph.NODE_SLOT_HEIGHT;
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:36:11 +00:00
|
|
|
// Restore original size but grow if needed
|
|
|
|
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])]);
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function convertToWidget(node, widget) {
|
|
|
|
showWidget(widget);
|
2023-03-24 16:36:11 +00:00
|
|
|
const sz = node.size;
|
2023-03-23 21:37:19 +00:00
|
|
|
node.removeInput(node.inputs.findIndex((i) => i.widget?.name === widget.name));
|
2023-03-24 16:36:11 +00:00
|
|
|
|
2023-06-20 16:03:46 +00:00
|
|
|
for (const widget of node.widgets) {
|
|
|
|
widget.last_y -= LiteGraph.NODE_SLOT_HEIGHT;
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:36:11 +00:00
|
|
|
// Restore original size but grow if needed
|
|
|
|
node.setSize([Math.max(sz[0], node.size[0]), Math.max(sz[1], node.size[1])]);
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:16:39 +00:00
|
|
|
function getWidgetType(config) {
|
2023-03-23 21:37:19 +00:00
|
|
|
// Special handling for COMBO so we restrict links based on the entries
|
|
|
|
let type = config[0];
|
|
|
|
if (type instanceof Array) {
|
|
|
|
type = "COMBO";
|
|
|
|
}
|
2023-10-05 18:16:39 +00:00
|
|
|
return { type };
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.registerExtension({
|
|
|
|
name: "Comfy.WidgetInputs",
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
|
|
|
// Add menu options to conver to/from widgets
|
|
|
|
const origGetExtraMenuOptions = nodeType.prototype.getExtraMenuOptions;
|
|
|
|
nodeType.prototype.getExtraMenuOptions = function (_, options) {
|
|
|
|
const r = origGetExtraMenuOptions ? origGetExtraMenuOptions.apply(this, arguments) : undefined;
|
|
|
|
|
|
|
|
if (this.widgets) {
|
|
|
|
let toInput = [];
|
|
|
|
let toWidget = [];
|
|
|
|
for (const w of this.widgets) {
|
2023-08-04 02:49:52 +00:00
|
|
|
if (w.options?.forceInput) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
if (w.type === CONVERTED_TYPE) {
|
|
|
|
toWidget.push({
|
|
|
|
content: `Convert ${w.name} to widget`,
|
|
|
|
callback: () => convertToWidget(this, w),
|
|
|
|
});
|
|
|
|
} else {
|
2023-10-03 19:19:12 +00:00
|
|
|
const config = getConfig.call(this, w.name) ?? [w.type, w.options || {}];
|
2023-03-23 21:37:19 +00:00
|
|
|
if (isConvertableWidget(w, config)) {
|
|
|
|
toInput.push({
|
|
|
|
content: `Convert ${w.name} to input`,
|
|
|
|
callback: () => convertToInput(this, w, config),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toInput.length) {
|
|
|
|
options.push(...toInput, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toWidget.length) {
|
|
|
|
options.push(...toWidget, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2023-10-03 19:19:12 +00:00
|
|
|
nodeType.prototype.onGraphConfigured = function () {
|
|
|
|
if (!this.inputs) return;
|
|
|
|
|
|
|
|
for (const input of this.inputs) {
|
|
|
|
if (input.widget) {
|
2023-10-06 20:48:30 +00:00
|
|
|
if (!input.widget[GET_CONFIG]) {
|
|
|
|
input.widget[GET_CONFIG] = () => getConfig.call(this, input.widget.name);
|
2023-10-03 19:19:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 18:16:39 +00:00
|
|
|
// Cleanup old widget config
|
|
|
|
if (input.widget.config) {
|
|
|
|
if (input.widget.config[0] instanceof Array) {
|
|
|
|
// If we are an old converted combo then replace the input type and the stored link data
|
|
|
|
input.type = "COMBO";
|
|
|
|
|
|
|
|
const link = app.graph.links[input.link];
|
|
|
|
if (link) {
|
|
|
|
link.type = input.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete input.widget.config;
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:19:12 +00:00
|
|
|
const w = this.widgets.find((w) => w.name === input.widget.name);
|
|
|
|
if (w) {
|
|
|
|
hideWidget(this, w);
|
|
|
|
} else {
|
|
|
|
convertToWidget(this, input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const origOnNodeCreated = nodeType.prototype.onNodeCreated;
|
2023-08-04 02:49:52 +00:00
|
|
|
nodeType.prototype.onNodeCreated = function () {
|
|
|
|
const r = origOnNodeCreated ? origOnNodeCreated.apply(this) : undefined;
|
2023-10-03 19:19:12 +00:00
|
|
|
|
|
|
|
// When node is created, convert any force/default inputs
|
|
|
|
if (!app.configuringGraph && this.widgets) {
|
2023-08-04 02:49:52 +00:00
|
|
|
for (const w of this.widgets) {
|
2023-09-08 04:53:29 +00:00
|
|
|
if (w?.options?.forceInput || w?.options?.defaultInput) {
|
2023-10-03 19:19:12 +00:00
|
|
|
const config = getConfig.call(this, w.name) ?? [w.type, w.options || {}];
|
2023-08-04 02:49:52 +00:00
|
|
|
convertToInput(this, w, config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 19:19:12 +00:00
|
|
|
|
2023-08-04 02:49:52 +00:00
|
|
|
return r;
|
2023-10-03 19:19:12 +00:00
|
|
|
};
|
2023-08-04 02:49:52 +00:00
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
const origOnConfigure = nodeType.prototype.onConfigure;
|
|
|
|
nodeType.prototype.onConfigure = function () {
|
|
|
|
const r = origOnConfigure ? origOnConfigure.apply(this, arguments) : undefined;
|
2023-10-03 19:19:12 +00:00
|
|
|
if (!app.configuringGraph && this.inputs) {
|
|
|
|
// On copy + paste of nodes, ensure that widget configs are set up
|
2023-03-23 21:37:19 +00:00
|
|
|
for (const input of this.inputs) {
|
2023-10-06 20:48:30 +00:00
|
|
|
if (input.widget && !input.widget[GET_CONFIG]) {
|
|
|
|
input.widget[GET_CONFIG] = () => getConfig.call(this, input.widget.name);
|
2023-10-08 08:04:25 +00:00
|
|
|
const w = this.widgets.find((w) => w.name === input.widget.name);
|
|
|
|
if (w) {
|
|
|
|
hideWidget(this, w);
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2023-03-24 20:20:34 +00:00
|
|
|
function isNodeAtPos(pos) {
|
|
|
|
for (const n of app.graph._nodes) {
|
|
|
|
if (n.pos[0] === pos[0] && n.pos[1] === pos[1]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
// Double click a widget input to automatically attach a primitive
|
|
|
|
const origOnInputDblClick = nodeType.prototype.onInputDblClick;
|
2023-03-24 20:20:34 +00:00
|
|
|
const ignoreDblClick = Symbol();
|
2023-03-23 21:37:19 +00:00
|
|
|
nodeType.prototype.onInputDblClick = function (slot) {
|
|
|
|
const r = origOnInputDblClick ? origOnInputDblClick.apply(this, arguments) : undefined;
|
|
|
|
|
2023-03-24 20:20:34 +00:00
|
|
|
const input = this.inputs[slot];
|
2023-04-15 16:34:46 +00:00
|
|
|
if (!input.widget || !input[ignoreDblClick]) {
|
|
|
|
// Not a widget input or already handled input
|
2023-10-06 20:48:30 +00:00
|
|
|
if (!(input.type in ComfyWidgets) && !(input.widget[GET_CONFIG]?.()?.[0] instanceof Array)) {
|
2023-04-15 16:34:46 +00:00
|
|
|
return r; //also Not a ComfyWidgets input or combo (do nothing)
|
|
|
|
}
|
2023-04-08 17:05:22 +00:00
|
|
|
}
|
2023-03-24 20:20:34 +00:00
|
|
|
|
2023-04-08 17:05:22 +00:00
|
|
|
// Create a primitive node
|
|
|
|
const node = LiteGraph.createNode("PrimitiveNode");
|
|
|
|
app.graph.add(node);
|
2023-03-24 20:20:34 +00:00
|
|
|
|
2023-04-08 17:05:22 +00:00
|
|
|
// Calculate a position that wont directly overlap another node
|
|
|
|
const pos = [this.pos[0] - node.size[0] - 30, this.pos[1]];
|
|
|
|
while (isNodeAtPos(pos)) {
|
|
|
|
pos[1] += LiteGraph.NODE_TITLE_HEIGHT;
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 17:05:22 +00:00
|
|
|
node.pos = pos;
|
|
|
|
node.connect(0, this, slot);
|
|
|
|
node.title = input.name;
|
|
|
|
|
|
|
|
// Prevent adding duplicates due to triple clicking
|
|
|
|
input[ignoreDblClick] = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
delete input[ignoreDblClick];
|
|
|
|
}, 300);
|
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
return r;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
registerCustomNodes() {
|
|
|
|
class PrimitiveNode {
|
|
|
|
constructor() {
|
|
|
|
this.addOutput("connect to widget input", "*");
|
|
|
|
this.serialize_widgets = true;
|
|
|
|
this.isVirtualNode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyToGraph() {
|
|
|
|
if (!this.outputs[0].links?.length) return;
|
|
|
|
|
2023-06-09 06:48:42 +00:00
|
|
|
function get_links(node) {
|
|
|
|
let links = [];
|
|
|
|
for (const l of node.outputs[0].links) {
|
|
|
|
const linkInfo = app.graph.links[l];
|
|
|
|
const n = node.graph.getNodeById(linkInfo.target_id);
|
|
|
|
if (n.type == "Reroute") {
|
|
|
|
links = links.concat(get_links(n));
|
|
|
|
} else {
|
|
|
|
links.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
|
|
|
let links = get_links(this);
|
2023-03-23 21:37:19 +00:00
|
|
|
// For each output link copy our value over the original widget value
|
2023-06-09 06:48:42 +00:00
|
|
|
for (const l of links) {
|
2023-03-23 21:37:19 +00:00
|
|
|
const linkInfo = app.graph.links[l];
|
|
|
|
const node = this.graph.getNodeById(linkInfo.target_id);
|
|
|
|
const input = node.inputs[linkInfo.target_slot];
|
|
|
|
const widgetName = input.widget.name;
|
|
|
|
if (widgetName) {
|
|
|
|
const widget = node.widgets.find((w) => w.name === widgetName);
|
|
|
|
if (widget) {
|
|
|
|
widget.value = this.widgets[0].value;
|
|
|
|
if (widget.callback) {
|
|
|
|
widget.callback(widget.value, app.canvas, node, app.canvas.graph_mouse, {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 19:19:12 +00:00
|
|
|
refreshComboInNode() {
|
|
|
|
const widget = this.widgets?.[0];
|
|
|
|
if (widget?.type === "combo") {
|
2023-10-06 20:48:30 +00:00
|
|
|
widget.options.values = this.outputs[0].widget[GET_CONFIG]()[0];
|
2023-10-03 19:19:12 +00:00
|
|
|
|
|
|
|
if (!widget.options.values.includes(widget.value)) {
|
|
|
|
widget.value = widget.options.values[0];
|
|
|
|
widget.callback(widget.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onAfterGraphConfigured() {
|
|
|
|
if (this.outputs[0].links?.length && !this.widgets?.length) {
|
|
|
|
this.#onFirstConnection();
|
|
|
|
|
|
|
|
// Populate widget values from config data
|
2023-10-04 19:48:55 +00:00
|
|
|
if (this.widgets) {
|
|
|
|
for (let i = 0; i < this.widgets_values.length; i++) {
|
|
|
|
const w = this.widgets[i];
|
|
|
|
if (w) {
|
|
|
|
w.value = this.widgets_values[i];
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 19:19:12 +00:00
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
|
|
|
|
// Merge values if required
|
|
|
|
this.#mergeWidgetConfig();
|
2023-10-03 19:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
onConnectionsChange(_, index, connected) {
|
2023-10-03 19:19:12 +00:00
|
|
|
if (app.configuringGraph) {
|
|
|
|
// Dont run while the graph is still setting up
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
const links = this.outputs[0].links;
|
2023-03-23 21:37:19 +00:00
|
|
|
if (connected) {
|
2023-10-04 19:48:55 +00:00
|
|
|
if (links?.length && !this.widgets?.length) {
|
2023-10-03 19:19:12 +00:00
|
|
|
this.#onFirstConnection();
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
} else {
|
|
|
|
// We may have removed a link that caused the constraints to change
|
|
|
|
this.#mergeWidgetConfig();
|
|
|
|
|
|
|
|
if (!links?.length) {
|
|
|
|
this.#onLastDisconnect();
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onConnectOutput(slot, type, input, target_node, target_slot) {
|
|
|
|
// Fires before the link is made allowing us to reject it if it isn't valid
|
|
|
|
|
|
|
|
// No widget, we cant connect
|
2023-04-08 16:58:47 +00:00
|
|
|
if (!input.widget) {
|
|
|
|
if (!(input.type in ComfyWidgets)) return false;
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
|
|
|
|
if (this.outputs[slot].links?.length) {
|
|
|
|
return this.#isValidConnection(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
#onFirstConnection(recreating) {
|
2023-03-23 21:37:19 +00:00
|
|
|
// First connection can fire before the graph is ready on initial load so random things can be missing
|
|
|
|
const linkId = this.outputs[0].links[0];
|
|
|
|
const link = this.graph.links[linkId];
|
|
|
|
if (!link) return;
|
|
|
|
|
|
|
|
const theirNode = this.graph.getNodeById(link.target_id);
|
|
|
|
if (!theirNode || !theirNode.inputs) return;
|
|
|
|
|
|
|
|
const input = theirNode.inputs[link.target_slot];
|
|
|
|
if (!input) return;
|
|
|
|
|
2023-10-03 19:19:12 +00:00
|
|
|
let widget;
|
2023-04-08 16:58:47 +00:00
|
|
|
if (!input.widget) {
|
|
|
|
if (!(input.type in ComfyWidgets)) return;
|
2023-10-06 20:48:30 +00:00
|
|
|
widget = { name: input.name, [GET_CONFIG]: () => [input.type, {}] }; //fake widget
|
2023-04-08 16:58:47 +00:00
|
|
|
} else {
|
2023-10-03 19:19:12 +00:00
|
|
|
widget = input.widget;
|
2023-04-08 16:58:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 20:48:30 +00:00
|
|
|
const { type } = getWidgetType(widget[GET_CONFIG]());
|
2023-03-23 21:37:19 +00:00
|
|
|
// Update our output to restrict to the widget type
|
2023-10-05 18:16:39 +00:00
|
|
|
this.outputs[0].type = type;
|
2023-03-23 21:37:19 +00:00
|
|
|
this.outputs[0].name = type;
|
|
|
|
this.outputs[0].widget = widget;
|
|
|
|
|
2023-10-06 20:48:30 +00:00
|
|
|
this.#createWidget(widget[CONFIG] ?? widget[GET_CONFIG](), theirNode, widget.name, recreating);
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
#createWidget(inputData, node, widgetName, recreating) {
|
2023-03-23 21:37:19 +00:00
|
|
|
let type = inputData[0];
|
|
|
|
|
|
|
|
if (type instanceof Array) {
|
|
|
|
type = "COMBO";
|
|
|
|
}
|
|
|
|
|
|
|
|
let widget;
|
|
|
|
if (type in ComfyWidgets) {
|
|
|
|
widget = (ComfyWidgets[type](this, "value", inputData, app) || {}).widget;
|
|
|
|
} else {
|
2023-10-03 19:19:12 +00:00
|
|
|
widget = this.addWidget(type, "value", null, () => {}, {});
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node?.widgets && widget) {
|
|
|
|
const theirWidget = node.widgets.find((w) => w.name === widgetName);
|
|
|
|
if (theirWidget) {
|
|
|
|
widget.value = theirWidget.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 07:18:11 +00:00
|
|
|
if (widget.type === "number" || widget.type === "combo") {
|
2023-04-13 00:57:13 +00:00
|
|
|
addValueControlWidget(this, widget, "fixed");
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 10:27:19 +00:00
|
|
|
// When our value changes, update other widgets to reflect our changes
|
|
|
|
// e.g. so LoadImage shows correct image
|
|
|
|
const callback = widget.callback;
|
|
|
|
const self = this;
|
|
|
|
widget.callback = function () {
|
|
|
|
const r = callback ? callback.apply(this, arguments) : undefined;
|
|
|
|
self.applyToGraph();
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
if (!recreating) {
|
|
|
|
// Grow our node if required
|
|
|
|
const sz = this.computeSize();
|
|
|
|
if (this.size[0] < sz[0]) {
|
|
|
|
this.size[0] = sz[0];
|
|
|
|
}
|
|
|
|
if (this.size[1] < sz[1]) {
|
|
|
|
this.size[1] = sz[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (this.onResize) {
|
|
|
|
this.onResize(this.size);
|
|
|
|
}
|
|
|
|
});
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#recreateWidget() {
|
|
|
|
const values = this.widgets.map((w) => w.value);
|
|
|
|
this.#removeWidgets();
|
|
|
|
this.#onFirstConnection(true);
|
|
|
|
for (let i = 0; i < this.widgets?.length; i++) this.widgets[i].value = values[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
#mergeWidgetConfig() {
|
|
|
|
// Merge widget configs if the node has multiple outputs
|
|
|
|
const output = this.outputs[0];
|
|
|
|
const links = output.links;
|
|
|
|
|
|
|
|
const hasConfig = !!output.widget[CONFIG];
|
|
|
|
if (hasConfig) {
|
|
|
|
delete output.widget[CONFIG];
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
if (links?.length < 2 && hasConfig) {
|
|
|
|
// Copy the widget options from the source
|
|
|
|
if (links.length) {
|
|
|
|
this.#recreateWidget();
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-06 20:48:30 +00:00
|
|
|
const config1 = output.widget[GET_CONFIG]();
|
2023-10-04 19:48:55 +00:00
|
|
|
const isNumber = config1[0] === "INT" || config1[0] === "FLOAT";
|
|
|
|
if (!isNumber) return;
|
|
|
|
|
|
|
|
for (const linkId of links) {
|
|
|
|
const link = app.graph.links[linkId];
|
|
|
|
if (!link) continue; // Can be null when removing a node
|
|
|
|
|
|
|
|
const theirNode = app.graph.getNodeById(link.target_id);
|
|
|
|
const theirInput = theirNode.inputs[link.target_slot];
|
|
|
|
|
|
|
|
// Call is valid connection so it can merge the configs when validating
|
|
|
|
this.#isValidConnection(theirInput, hasConfig);
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
#isValidConnection(input, forceUpdate) {
|
2023-03-23 21:37:19 +00:00
|
|
|
// Only allow connections where the configs match
|
2023-10-04 19:48:55 +00:00
|
|
|
const output = this.outputs[0];
|
2023-10-06 20:48:30 +00:00
|
|
|
const config1 = output.widget[CONFIG] ?? output.widget[GET_CONFIG]();
|
|
|
|
const config2 = input.widget[GET_CONFIG]();
|
2023-03-23 21:37:19 +00:00
|
|
|
|
2023-04-15 09:53:30 +00:00
|
|
|
if (config1[0] instanceof Array) {
|
|
|
|
// New input isnt a combo
|
2023-10-04 19:48:55 +00:00
|
|
|
if (!(config2[0] instanceof Array)) {
|
|
|
|
console.log(`connection rejected: tried to connect combo to ${config2[0]}`);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-15 09:53:30 +00:00
|
|
|
// New imput combo has a different size
|
2023-10-04 19:48:55 +00:00
|
|
|
if (config1[0].length !== config2[0].length) {
|
|
|
|
console.log(`connection rejected: combo lists dont match`);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-15 09:53:30 +00:00
|
|
|
// New input combo has different elements
|
2023-10-04 19:48:55 +00:00
|
|
|
if (config1[0].find((v, i) => config2[0][i] !== v)) {
|
|
|
|
console.log(`connection rejected: combo lists dont match`);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-15 09:53:30 +00:00
|
|
|
} else if (config1[0] !== config2[0]) {
|
2023-10-04 19:48:55 +00:00
|
|
|
// Types dont match
|
|
|
|
console.log(`connection rejected: types dont match`, config1[0], config2[0]);
|
2023-04-15 09:53:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
const keys = new Set([...Object.keys(config1[1] ?? {}), ...Object.keys(config2[1] ?? {})]);
|
|
|
|
|
|
|
|
let customConfig;
|
|
|
|
const getCustomConfig = () => {
|
|
|
|
if (!customConfig) {
|
|
|
|
if (typeof structuredClone === "undefined") {
|
|
|
|
customConfig = JSON.parse(JSON.stringify(config1[1] ?? {}));
|
|
|
|
} else {
|
|
|
|
customConfig = structuredClone(config1[1] ?? {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return customConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isNumber = config1[0] === "INT" || config1[0] === "FLOAT";
|
|
|
|
for (const k of keys.values()) {
|
|
|
|
if (k !== "default" && k !== "forceInput" && k !== "defaultInput") {
|
|
|
|
let v1 = config1[1][k];
|
|
|
|
let v2 = config2[1][k];
|
|
|
|
|
|
|
|
if (v1 === v2 || (!v1 && !v2)) continue;
|
|
|
|
|
|
|
|
if (isNumber) {
|
|
|
|
if (k === "min") {
|
|
|
|
const theirMax = config2[1]["max"];
|
|
|
|
if (theirMax != null && v1 > theirMax) {
|
2023-10-05 16:13:13 +00:00
|
|
|
console.log("connection rejected: min > max", v1, theirMax);
|
2023-10-04 19:48:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
getCustomConfig()[k] = v1 == null ? v2 : v2 == null ? v1 : Math.max(v1, v2);
|
|
|
|
continue;
|
|
|
|
} else if (k === "max") {
|
|
|
|
const theirMin = config2[1]["min"];
|
|
|
|
if (theirMin != null && v1 < theirMin) {
|
2023-10-05 16:13:13 +00:00
|
|
|
console.log("connection rejected: max < min", v1, theirMin);
|
2023-10-04 19:48:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
getCustomConfig()[k] = v1 == null ? v2 : v2 == null ? v1 : Math.min(v1, v2);
|
|
|
|
continue;
|
|
|
|
} else if (k === "step") {
|
|
|
|
let step;
|
|
|
|
if (v1 == null) {
|
2023-10-05 16:13:13 +00:00
|
|
|
// No current step
|
2023-10-04 19:48:55 +00:00
|
|
|
step = v2;
|
|
|
|
} else if (v2 == null) {
|
2023-10-05 16:13:13 +00:00
|
|
|
// No new step
|
2023-10-04 19:48:55 +00:00
|
|
|
step = v1;
|
|
|
|
} else {
|
|
|
|
if (v1 < v2) {
|
2023-10-05 16:13:13 +00:00
|
|
|
// Ensure v1 is larger for the mod
|
2023-10-04 19:48:55 +00:00
|
|
|
const a = v2;
|
|
|
|
v2 = v1;
|
|
|
|
v1 = a;
|
|
|
|
}
|
|
|
|
if (v1 % v2) {
|
2023-10-05 16:13:13 +00:00
|
|
|
console.log("connection rejected: steps not divisible", "current:", v1, "new:", v2);
|
2023-10-04 19:48:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
step = v1;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCustomConfig()[k] = step;
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
|
|
|
|
console.log(`connection rejected: config ${k} values dont match`, v1, v2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (customConfig || forceUpdate) {
|
|
|
|
if (customConfig) {
|
|
|
|
output.widget[CONFIG] = [config1[0], customConfig];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#recreateWidget();
|
|
|
|
|
|
|
|
const widget = this.widgets[0];
|
|
|
|
// When deleting a node this can be null
|
|
|
|
if (widget) {
|
|
|
|
const min = widget.options.min;
|
|
|
|
const max = widget.options.max;
|
|
|
|
if (min != null && widget.value < min) widget.value = min;
|
|
|
|
if (max != null && widget.value > max) widget.value = max;
|
|
|
|
widget.callback(widget.value);
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:48:55 +00:00
|
|
|
#removeWidgets() {
|
2023-03-23 21:37:19 +00:00
|
|
|
if (this.widgets) {
|
|
|
|
// Allow widgets to cleanup
|
|
|
|
for (const w of this.widgets) {
|
|
|
|
if (w.onRemove) {
|
|
|
|
w.onRemove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.widgets.length = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 19:48:55 +00:00
|
|
|
|
|
|
|
#onLastDisconnect() {
|
|
|
|
// We cant remove + re-add the output here as if you drag a link over the same link
|
|
|
|
// it removes, then re-adds, causing it to break
|
|
|
|
this.outputs[0].type = "*";
|
|
|
|
this.outputs[0].name = "connect to widget input";
|
|
|
|
delete this.outputs[0].widget;
|
|
|
|
|
|
|
|
this.#removeWidgets();
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LiteGraph.registerNodeType(
|
|
|
|
"PrimitiveNode",
|
|
|
|
Object.assign(PrimitiveNode, {
|
|
|
|
title: "Primitive",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
PrimitiveNode.category = "utils";
|
|
|
|
},
|
|
|
|
});
|