From fc605e01717865937b0666a064de9647f0381caa Mon Sep 17 00:00:00 2001 From: atlasan Date: Fri, 22 Oct 2021 16:09:08 +0200 Subject: [PATCH] Fixes Autoconnect: allows generic types * Touches, they should be evaluated when pointerEvents are not available? first glimpse --- src/litegraph.js | 139 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 34 deletions(-) diff --git a/src/litegraph.js b/src/litegraph.js index e14487eec8..73744005db 100644 --- a/src/litegraph.js +++ b/src/litegraph.js @@ -611,11 +611,13 @@ * @return {Boolean} true if they can be connected */ isValidConnection: function(type_a, type_b) { + if (type_a=="" || type_a==="*") type_a = 0; + if (type_b=="" || type_b==="*") type_b = 0; if ( - !type_a || //generic output - !type_b || //generic input - type_a == type_b || //same type (is valid for triggers) - (type_a == LiteGraph.EVENT && type_b == LiteGraph.ACTION) + !generic //generic output + || !type_b // generic input + || type_a == type_b //same type (is valid for triggers) + || (type_a == LiteGraph.EVENT && type_b == LiteGraph.ACTION) ) { return true; } @@ -636,7 +638,8 @@ var supported_types_b = type_b.split(","); for (var i = 0; i < supported_types_a.length; ++i) { for (var j = 0; j < supported_types_b.length; ++j) { - if (supported_types_a[i] == supported_types_b[j]) { + if(this.isValidConnection(supported_types_a[i],supported_types_b[j])){ + //if (supported_types_a[i] == supported_types_b[j]) { return true; } } @@ -3973,15 +3976,19 @@ if (!aSlots) { return -1; } + // !! empty string type is considered 0, * !! + if (type == "" || type == "*") type = 0; for (var i = 0, l = aSlots.length; i < l; ++i) { var tFound = false; var aSource = (type+"").toLowerCase().split(","); - var aDest = (aSlots[i].type+"").toLowerCase().split(","); + var aDest = aSlots[i].type=="0"||aSlots[i].type=="*"?"0":aSlots[i].type; + aDest = (aDest+"").toLowerCase().split(","); for(sI=0;sI= 0 && target_slot !== null){ + // connect to the first general output slot if not found a specific type and + if (opts.generalTypeInCase){ + var target_slot = target_node.findInputSlotByType(0, false, true, true); + //console.debug("connect TO a general type (*, 0), if not found the specific type ",target_slotType," to ",target_node,"RES_SLOT:",target_slot); + if (target_slot >= 0){ return this.connect(slot, target_node, target_slot); } - }else{ - //console.debug("no way to connect "+target_slotType+" to "+target_node); } + // connect to the first free input slot if not found a specific type and this output is general + if (opts.firstFreeIfOutputGeneralInCase && (target_slotType == 0 || target_slotType == "*" || target_slotType == "")){ + var target_slot = target_node.findInputSlotFree({typesNotAccepted: [LiteGraph.EVENT] }); + //console.debug("connect TO TheFirstFREE ",target_slotType," to ",target_node,"RES_SLOT:",target_slot); + if (target_slot >= 0){ + return this.connect(slot, target_node, target_slot); + } + } + + console.debug("no way to connect type: ",target_slotType," to targetNODE ",target_node); + //TODO filter + return null; } } @@ -4096,6 +4117,9 @@ } } + console.debug("no way to connect byOUT type: ",source_slotType," to sourceNODE ",source_node); + //TODO filter + //console.log("type OUT! "+source_slotType+" not found or not free?") return null; } @@ -4205,6 +4229,8 @@ if(changed) this.graph.connectionChange(this, link_info); return null; + }else{ + //console.debug("valid connection",output.type, input.type); } //allows nodes to block connection, callback @@ -5525,7 +5551,7 @@ LGraphNode.prototype.executeAction = function(action) this._mousemove_callback = this.processMouseMove.bind(this); this._mouseup_callback = this.processMouseUp.bind(this); - //touch events -- THIS WAY DOES NOT WORK, finish implementing pointerevents, than clean the touchevents + //touch events -- TODO IMPLEMENT //this._touch_callback = this.touchHandler.bind(this); LiteGraph.pointerListenerAdd(canvas,"down", this._mousedown_callback, true); //down do not need to store the binded @@ -6166,16 +6192,21 @@ LGraphNode.prototype.executeAction = function(action) } else if (e.which == 3 || this.pointer_is_double) { //right button - if(!this.read_only){ - if(Object.keys(this.selected_nodes).length - && (this.selected_nodes[node.id] || e.shiftKey || e.ctrlKey || e.metaKey) - ){ - // is multiselected or using shift to include the now node - if (!this.selected_nodes[node.id]) this.selectNodes([node],true); // add this if not present - }else{ - // update selection - this.selectNodes([node]); + if (this.allow_interaction && !skip_action && !this.read_only){ + + // is it hover a node ? + if (node){ + if(Object.keys(this.selected_nodes).length + && (this.selected_nodes[node.id] || e.shiftKey || e.ctrlKey || e.metaKey) + ){ + // is multiselected or using shift to include the now node + if (!this.selected_nodes[node.id]) this.selectNodes([node],true); // add this if not present + }else{ + // update selection + this.selectNodes([node]); + } } + // show menu on this node this.processContextMenu(node, e); } @@ -10143,8 +10174,6 @@ LGraphNode.prototype.executeAction = function(action) }; /* this is an implementation for touch not in production and not ready - * the idea is maybe good: simulate a similar event - * so let's try with pointerevents (working with both mouse and touch), and simulate the old mouseevents IF NECESSARY for retrocompatibility: existing old good nodes */ /*LGraphCanvas.prototype.touchHandler = function(event) { //alert("foo"); @@ -13983,23 +14012,65 @@ LGraphNode.prototype.executeAction = function(action) .filter(Boolean); // split & filter [""] }; - // helper pointerListener vs mouseListener, used by LGraphCanvas DragAndScale ContextMenu - LiteGraph.pointerListenerAdd = function(oDOM, sEvent, fCall, capture=false) { - if (!oDOM || !oDOM.addEventListener || !sEvent || typeof fCall!=="function"){ + /* helper for interaction: pointer, touch, mouse Listeners + used by LGraphCanvas DragAndScale ContextMenu*/ + LiteGraph.pointerListenerAdd = function(oDOM, sEvIn, fCall, capture=false) { + if (!oDOM || !oDOM.addEventListener || !sEvIn || typeof fCall!=="function"){ //console.log("cant pointerListenerAdd "+oDOM+", "+sEvent+", "+fCall); return; // -- break -- } + + var sMethod = LiteGraph.pointerevents_method; + var sEvent = sEvIn; + + // UNDER CONSTRUCTION + // convert pointerevents to touch event when not available + if (sMethod=="pointer" && !window.PointerEvent){ + console.warn("sMethod=='pointer' && !window.PointerEvent"); + console.log("Converting pointer["+sEvent+"] : down move up cancel enter TO touchstart touchmove touchend, etc .."); + switch(sEvent){ + case "down":{ + sMethod = "touch"; + sEvent = "start"; + break; + } + case "move":{ + sMethod = "touch"; + //sEvent = "move"; + break; + } + case "up":{ + sMethod = "touch"; + sEvent = "end"; + break; + } + case "cancel":{ + sMethod = "touch"; + //sEvent = "cancel"; + break; + } + case "enter":{ + console.log("debug: Should I send a move event?"); // ??? + break; + } + // case "over": case "out": not used at now + default:{ + console.warn("PointerEvent not available in this browser ? The event "+sEvent+" would not be called"); + } + } + } + switch(sEvent){ //both pointer and move events case "down": case "up": case "move": case "over": case "out": case "enter": { - oDOM.addEventListener(LiteGraph.pointerevents_method+sEvent, fCall, capture); + oDOM.addEventListener(sMethod+sEvent, fCall, capture); } // only pointerevents case "leave": case "cancel": case "gotpointercapture": case "lostpointercapture": { - if (LiteGraph.pointerevents_method!="mouse"){ - return oDOM.addEventListener(LiteGraph.pointerevents_method+sEvent, fCall, capture); + if (sMethod!="mouse"){ + return oDOM.addEventListener(sMethod+sEvent, fCall, capture); } } // not "pointer" || "mouse"