added events support for subgraphs

This commit is contained in:
tamat
2019-04-11 20:15:10 +02:00
parent c03f0eb3b7
commit 57eead4ad6
7 changed files with 520 additions and 388 deletions

View File

@@ -1167,14 +1167,14 @@ LGraph.prototype.getNodeById = function( id )
* @param {Class} classObject the class itself (not an string)
* @return {Array} a list with all the nodes of this type
*/
LGraph.prototype.findNodesByClass = function(classObject)
LGraph.prototype.findNodesByClass = function( classObject, result )
{
var r = [];
result = result || [];
result.length = 0;
for(var i = 0, l = this._nodes.length; i < l; ++i)
if(this._nodes[i].constructor === classObject)
r.push(this._nodes[i]);
return r;
result.push( this._nodes[i] );
return result;
}
/**
@@ -1183,15 +1183,29 @@ LGraph.prototype.findNodesByClass = function(classObject)
* @param {String} type the name of the node type
* @return {Array} a list with all the nodes of this type
*/
LGraph.prototype.findNodesByType = function(type)
LGraph.prototype.findNodesByType = function( type, result )
{
var type = type.toLowerCase();
var r = [];
result = result || [];
result.length = 0;
for(var i = 0, l = this._nodes.length; i < l; ++i)
if(this._nodes[i].type.toLowerCase() == type )
r.push(this._nodes[i]);
return r;
result.push(this._nodes[i]);
return result;
}
/**
* Returns the first node that matches a name in its title
* @method findNodeByTitle
* @param {String} name the name of the node to search
* @return {Node} the node or null
*/
LGraph.prototype.findNodeByTitle = function(title)
{
for(var i = 0, l = this._nodes.length; i < l; ++i)
if(this._nodes[i].title == title)
return this._nodes[i];
return null;
}
/**
@@ -1200,7 +1214,6 @@ LGraph.prototype.findNodesByType = function(type)
* @param {String} name the name of the node to search
* @return {Array} a list with all the nodes with this name
*/
LGraph.prototype.findNodesByTitle = function(title)
{
var result = [];
@@ -1250,6 +1263,26 @@ LGraph.prototype.getGroupOnPos = function(x,y)
// ********** GLOBALS *****************
LGraph.prototype.onAction = function(action, param)
{
this._input_nodes = this.findNodesByClass( LiteGraph.GraphInput, this._input_nodes );
for(var i = 0; i < this._input_nodes.length; ++i)
{
var node = this._input_nodes[i];
if( node.properties.name != action )
continue;
node.onAction(action,param);
break;
}
}
LGraph.prototype.trigger = function(action, param)
{
if(this.onTrigger)
this.onTrigger(action,param);
}
/**
* Tell this graph it has a global graph input of this type
* @method addGlobalInput
@@ -1257,7 +1290,7 @@ LGraph.prototype.getGroupOnPos = function(x,y)
* @param {String} type
* @param {*} value [optional]
*/
LGraph.prototype.addInput = function(name, type, value)
LGraph.prototype.addInput = function( name, type, value )
{
var input = this.inputs[ name ];
if( input ) //already exist
@@ -1343,7 +1376,7 @@ LGraph.prototype.changeInputType = function(name, type)
if(!this.inputs[name])
return false;
if(this.inputs[name].type && this.inputs[name].type.toLowerCase() == type.toLowerCase() )
if(this.inputs[name].type && String(this.inputs[name].type).toLowerCase() == String(type).toLowerCase() )
return;
this.inputs[name].type = type;
@@ -1460,7 +1493,7 @@ LGraph.prototype.changeOutputType = function(name, type)
if(!this.outputs[name])
return false;
if(this.outputs[name].type && this.outputs[name].type.toLowerCase() == type.toLowerCase() )
if(this.outputs[name].type && String(this.outputs[name].type).toLowerCase() == String(type).toLowerCase() )
return;
this.outputs[name].type = type;
@@ -8848,6 +8881,8 @@ function Subgraph()
this.subgraph._subgraph_node = this;
this.subgraph._is_subgraph = true;
this.subgraph.onTrigger = this.onSubgraphTrigger.bind(this);
this.subgraph.onInputAdded = this.onSubgraphNewInput.bind(this);
this.subgraph.onInputRenamed = this.onSubgraphRenamedInput.bind(this);
this.subgraph.onInputTypeChanged = this.onSubgraphTypeChangeInput.bind(this);
@@ -8895,6 +8930,11 @@ Subgraph.prototype.onMouseDown = function(e,pos,graphcanvas)
}
}
Subgraph.prototype.onAction = function( action, param )
{
this.subgraph.onAction( action, param );
}
Subgraph.prototype.onExecute = function()
{
if( !this.getInputOrProperty("enabled") )
@@ -8923,11 +8963,17 @@ Subgraph.prototype.onExecute = function()
}
//**** INPUTS ***********************************
Subgraph.prototype.onSubgraphTrigger = function(event, param)
{
var slot = this.findOutputSlot(event);
if(slot != -1)
this.triggerSlot(slot);
}
Subgraph.prototype.onSubgraphNewInput = function(name, type)
{
//add input to the node
var slot = this.findInputSlot(name);
if(slot == -1)
if(slot == -1) //add input to the node
this.addInput(name, type);
}
@@ -9058,6 +9104,8 @@ function GraphInput()
Object.defineProperty( this.properties, "type", {
get: function() { return that.outputs[0].type; },
set: function(v) {
if(v == "event")
v = LiteGraph.EVENT;
that.outputs[0].type = v;
if(that.name_in_graph) //already added
that.graph.changeInputType( that.name_in_graph, that.outputs[0].type);
@@ -9089,6 +9137,12 @@ GraphInput.prototype.getTitle = function()
return this.title;
}
GraphInput.prototype.onAction = function(action, param)
{
if(this.properties.type == LiteGraph.EVENT)
this.triggerSlot(0, param);
}
GraphInput.prototype.onExecute = function()
{
var name = this.properties.name;
@@ -9108,6 +9162,7 @@ GraphInput.prototype.onRemoved = function()
this.graph.removeInput( this.name_in_graph );
}
LiteGraph.GraphInput = GraphInput;
LiteGraph.registerNodeType("graph/input", GraphInput);
@@ -9140,6 +9195,8 @@ function GraphOutput()
Object.defineProperty( this.properties, "type", {
get: function() { return that.inputs[0].type; },
set: function(v) {
if(v == "action" || v == "event")
v = LiteGraph.ACTION;
that.inputs[0].type = v;
if(that.name_in_graph) //already added
that.graph.changeOutputType( that.name_in_graph, that.inputs[0].type);
@@ -9170,6 +9227,12 @@ GraphOutput.prototype.onExecute = function()
this.graph.setOutputData( this.properties.name, this._value );
}
GraphOutput.prototype.onAction = function(action, param)
{
if(this.properties.type == LiteGraph.ACTION)
this.graph.trigger( this.properties.name, param );
}
GraphOutput.prototype.onRemoved = function()
{
if(this.name_in_graph)
@@ -9183,6 +9246,7 @@ GraphOutput.prototype.getTitle = function()
return this.title;
}
LiteGraph.GraphOutput = GraphOutput;
LiteGraph.registerNodeType("graph/output", GraphOutput);