added method to create nodes from functions, wrapFunctionAsNode

This commit is contained in:
tamat
2018-03-28 16:35:52 +02:00
parent e54e2409d1
commit 7485fc68aa
11 changed files with 822 additions and 498 deletions

View File

@@ -132,6 +132,35 @@ var LiteGraph = global.LiteGraph = {
}
},
/**
* create a new node type by passing a function, it wraps it with a propper class
* @method wrapFunctionAsNode
* @param {String} name node name with namespace (p.e.: 'math/sum')
* @param {Function} func
* @param {Array} param_types [optional] an array containing the type of every parameter, otherwise parameters will accept any type
* @param {String} return_type [optional] string with the return type, otherwise it will be generic
*/
wrapFunctionAsNode: function( name, func, param_types, return_type )
{
var params = Array(func.length);
var code = "";
var names = LiteGraph.getParameterNames( func );
for(var i = 0; i < names.length; ++i)
code += "this.addInput('"+names[i]+"',"+(param_types && param_types[i] ? "'" + param_types[i] + "'" : "0") + ");\n";
code += "this.addOutput('out',"+( return_type ? "'" + return_type + "'" : 0 )+");\n";
var classobj = Function(code);
classobj.title = name.split("/").pop();
classobj.desc = "Generated from " + func.name;
classobj.prototype.onExecute = function onExecute()
{
for(var i = 0; i < params.length; ++i)
params[i] = this.getInputData(i);
var r = func.apply( this, params );
this.setOutputData(0,r);
}
this.registerNodeType( name, classobj );
},
/**
* Adds this method to all nodetypes, existing and to be created
* (You can add it to LGraphNode.prototype but then existing node types wont have it)
@@ -305,8 +334,20 @@ var LiteGraph = global.LiteGraph = {
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.EVENT && type_a.toLowerCase() == type_b.toLowerCase()) ) //same type
return true;
type_a == LiteGraph.EVENT && type_b == LiteGraph.ACTION )
return true;
type_a = type_a.toLowerCase();
type_b = type_b.toLowerCase();
if( type_a.indexOf(",") == -1 && type_b.indexOf(",") == -1 )
return type_a == type_b;
var supported_types_a = type_a.split(",");
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[i] )
return true;
return false;
}
};
@@ -3543,8 +3584,8 @@ LGraphCanvas.prototype.processMouseUp = function(e)
if(this.connecting_output.type == LiteGraph.EVENT)
this.connecting_node.connect( this.connecting_slot, node, LiteGraph.EVENT );
else
if(input && !input.link && input.type == this.connecting_output.type) //toLowerCase missing
this.connecting_node.connect(this.connecting_slot, node, 0);
if(input && !input.link && LiteGraph.isValidConnection( input.type && this.connecting_output.type ) )
this.connecting_node.connect( this.connecting_slot, node, 0 );
}
}
}
@@ -3677,7 +3718,8 @@ LGraphCanvas.prototype.processKey = function(e)
if(e.type == "keydown")
{
console.log(e);
//console.log(e); //debug
//select all Control A
if(e.keyCode == 65 && e.ctrlKey)
{
@@ -6190,13 +6232,15 @@ LiteGraph.extendClass = function ( target, origin )
}
}
/*
LiteGraph.createNodetypeWrapper = function( class_object )
{
//create Nodetype object
}
//LiteGraph.registerNodeType("scene/global", LGraphGlobal );
*/
LiteGraph.getParameterNames = function(func) {
return (func + '')
.replace(/[/][/].*$/mg,'') // strip single-line comments
.replace(/\s+/g, '') // strip white space
.replace(/[/][*][^/*]*[*][/]/g, '') // strip multi-line comments /**/
.split('){', 1)[0].replace(/^[^(]*[(]/, '') // extract the parameters
.replace(/=[^,]+/g, '') // strip any ES6 defaults
.split(',').filter(Boolean); // split & filter [""]
}
if( typeof(window) != "undefined" && !window["requestAnimationFrame"] )
{

View File

@@ -363,6 +363,24 @@ Watch.prototype.onDrawBackground = function(ctx)
LiteGraph.registerNodeType("basic/watch", Watch);
//Watch a value in the editor
function Pass()
{
this.addInput("in",0);
this.addOutput("out",0);
this.size = [40,20];
}
Pass.title = "Pass";
Pass.desc = "Allows to connect different types";
Pass.prototype.onExecute = function()
{
this.setOutputData( 0, this.getInputData(0) );
}
LiteGraph.registerNodeType("basic/pass", Pass);
//Show value inside the debug console
function Console()