diff --git a/src/litegraph.d.ts b/src/litegraph.d.ts index 4f2a875d4..9d3d37c02 100644 --- a/src/litegraph.d.ts +++ b/src/litegraph.d.ts @@ -740,7 +740,7 @@ export declare class LGraphNode { name: string, type: string | -1, extra_info?: Partial - ): void; + ): INodeOutputSlot; /** * add a new output slot to use in this node * @param array of triplets like [[name,type,extra_info],[...]] @@ -760,7 +760,7 @@ export declare class LGraphNode { name: string, type: string | -1, extra_info?: Partial - ): void; + ): INodeInputSlot; /** * add several new input slots in this node * @param array of triplets like [[name,type,extra_info],[...]] @@ -953,13 +953,34 @@ export declare class LGraphNode { /** * if returns false the incoming connection will be canceled * Called by `LGraph.connect` + * @param inputIndex target input slot number + * @param outputType type of output slot + * @param outputSlot output slot object + * @param outputNode node containing the output + * @param outputIndex index of output slot */ onConnectInput?( inputIndex: number, - type: INodeOutputSlot["type"], + outputType: INodeOutputSlot["type"], outputSlot: INodeOutputSlot, - _this: this, - slotIndex: number + outputNode: LGraphNode, + outputIndex: number + ): boolean; + /** + * if returns false the incoming connection will be canceled + * Called by `LGraph.connect` + * @param outputIndex target output slot number + * @param inputType type of input slot + * @param inputSlot input slot object + * @param inputNode node containing the input + * @param inputIndex index of input slot + */ + onConnectOutput?( + outputIndex: number, + inputType: INodeInputSlot["type"], + inputSlot: INodeInputSlot, + inputNode: LGraphNode, + inputIndex: number ): boolean; /** diff --git a/src/litegraph.js b/src/litegraph.js index 7b9557a18..b14851357 100755 --- a/src/litegraph.js +++ b/src/litegraph.js @@ -3685,99 +3685,94 @@ } var changed = false; - - //if there is something already plugged there, disconnect - if (target_node.inputs[target_slot].link != null) { - this.graph.beforeChange(); - target_node.disconnectInput(target_slot); - changed = true; - } - - //why here?? - //this.setDirtyCanvas(false,true); - //this.graph.connectionChange( this ); - - var output = this.outputs[slot]; - - //allows nodes to block connection - if (target_node.onConnectInput) { - if ( target_node.onConnectInput(target_slot, output.type, output, this, slot) === false ) { - return null; - } - } - var input = target_node.inputs[target_slot]; + var output = this.outputs[slot]; var link_info = null; - //this slots cannot be connected (different types) - if (!LiteGraph.isValidConnection(output.type, input.type)) - { - this.setDirtyCanvas(false, true); - if(changed) - this.graph.connectionChange(this, link_info); - return null; - } + if (LiteGraph.isValidConnection(output.type, input.type)) { + if (target_node.onBeforeConnectInput) { + // This way node can choose another slot (if selected is occupied) + target_slot = target_node.onBeforeConnectInput(target_slot); + } - if(!changed) - this.graph.beforeChange(); + //if there is something already plugged there, disconnect + if (target_node.inputs[target_slot].link != null) { + target_node.disconnectInput(target_slot); + } - //create link class - link_info = new LLink( - ++this.graph.last_link_id, - input.type, - this.id, - slot, - target_node.id, - target_slot - ); + //why here?? + //this.setDirtyCanvas(false,true); + //this.graph.connectionChange( this ); - //add to graph links list - this.graph.links[link_info.id] = link_info; + //allows nodes to block connection + if (target_node.onConnectInput) { + if ( target_node.onConnectInput(target_slot, output.type, output, this, slot) === false ) { + return null; + } + } + if (this.onConnectOutput) { + if ( this.onConnectOutput(slot, input.type, input, target_node, target_slot) === false ) { + return null; + } + } - //connect in output - if (output.links == null) { - output.links = []; - } - output.links.push(link_info.id); - //connect in input - target_node.inputs[target_slot].link = link_info.id; - if (this.graph) { - this.graph._version++; - } - if (this.onConnectionsChange) { - this.onConnectionsChange( - LiteGraph.OUTPUT, - slot, - true, - link_info, - output - ); - } //link_info has been created now, so its updated - if (target_node.onConnectionsChange) { - target_node.onConnectionsChange( - LiteGraph.INPUT, - target_slot, - true, - link_info, - input - ); - } - if (this.graph && this.graph.onNodeConnectionChange) { - this.graph.onNodeConnectionChange( - LiteGraph.INPUT, - target_node, - target_slot, - this, - slot - ); - this.graph.onNodeConnectionChange( - LiteGraph.OUTPUT, - this, - slot, - target_node, - target_slot - ); - } + link_info = new LLink( + ++this.graph.last_link_id, + input.type, + this.id, + slot, + target_node.id, + target_slot + ); + + //add to graph links list + this.graph.links[link_info.id] = link_info; + + //connect in output + if (output.links == null) { + output.links = []; + } + output.links.push(link_info.id); + //connect in input + target_node.inputs[target_slot].link = link_info.id; + if (this.graph) { + this.graph._version++; + } + if (this.onConnectionsChange) { + this.onConnectionsChange( + LiteGraph.OUTPUT, + slot, + true, + link_info, + output + ); + } //link_info has been created now, so its updated + if (target_node.onConnectionsChange) { + target_node.onConnectionsChange( + LiteGraph.INPUT, + target_slot, + true, + link_info, + input + ); + } + if (this.graph && this.graph.onNodeConnectionChange) { + this.graph.onNodeConnectionChange( + LiteGraph.INPUT, + target_node, + target_slot, + this, + slot + ); + this.graph.onNodeConnectionChange( + LiteGraph.OUTPUT, + this, + slot, + target_node, + target_slot + ); + } + } this.setDirtyCanvas(false, true); this.graph.afterChange();