Merge branch 'master' into pr/41

This commit is contained in:
Javi Agenjo
2018-06-08 19:19:29 +02:00
23 changed files with 3528 additions and 10750 deletions

View File

@@ -84,7 +84,7 @@ var LiteGraph = global.LiteGraph = {
console.log("Node registered: " + type);
var categories = type.split("/");
var classname = base_class.constructor.name;
var classname = base_class.name;
var pos = type.lastIndexOf("/");
base_class.category = type.substr(0,pos);
@@ -205,7 +205,7 @@ var LiteGraph = global.LiteGraph = {
var node = new base_class( title );
node.type = type;
if(!node.title) node.title = title;
if(!node.title && title) node.title = title;
if(!node.properties) node.properties = {};
if(!node.properties_info) node.properties_info = [];
if(!node.flags) node.flags = {};
@@ -336,7 +336,10 @@ var LiteGraph = global.LiteGraph = {
!type_b || //generic input
type_a == type_b || //same type (is valid for triggers)
type_a == LiteGraph.EVENT && type_b == LiteGraph.ACTION )
return true;
return true;
type_a = String(type_a) //* Enforce string type to handle toLowerCase call (-1 number not ok)
type_b = String(type_b)
type_a = type_a.toLowerCase();
type_b = type_b.toLowerCase();
@@ -438,7 +441,7 @@ LGraph.prototype.clear = function()
this.catch_errors = true;
//globals
//subgraph_data
this.global_inputs = {};
this.global_outputs = {};
@@ -726,8 +729,23 @@ LGraph.prototype.computeExecutionOrder = function( only_onExecute, set_level )
if( L.length != this._nodes.length && LiteGraph.debug )
console.warn("something went wrong, nodes missing");
var l = L.length;
//save order number in the node
for(var i = 0; i < L.length; ++i)
for(var i = 0; i < l; ++i)
L[i].order = i;
//sort now by priority
L = L.sort(function(A,B){
var Ap = A.constructor.priority || A.priority || 0;
var Bp = B.constructor.priority || B.priority || 0;
if(Ap == Bp)
return A.order - B.order;
return Ap - Bp;
});
//save order number in the node, again...
for(var i = 0; i < l; ++i)
L[i].order = i;
return L;
@@ -1026,7 +1044,7 @@ LGraph.prototype.findNodesByType = function(type)
/**
* Returns a list of nodes that matches a name
* @method findNodesByName
* @method findNodesByTitle
* @param {String} name the name of the node to search
* @return {Array} a list with all the nodes with this name
*/
@@ -1048,7 +1066,6 @@ LGraph.prototype.findNodesByTitle = function(title)
* @param {Array} nodes_list a list with all the nodes to search from, by default is all the nodes in the graph
* @return {Array} a list with all the nodes that intersect this coordinate
*/
LGraph.prototype.getNodeOnPos = function(x,y, nodes_list)
{
nodes_list = nodes_list || this._nodes;
@@ -1063,7 +1080,13 @@ LGraph.prototype.getNodeOnPos = function(x,y, nodes_list)
// ********** GLOBALS *****************
//Tell this graph has a global input of this type
/**
* Tell this graph it has a global graph input of this type
* @method addGlobalInput
* @param {String} name
* @param {String} type
* @param {*} value [optional]
*/
LGraph.prototype.addGlobalInput = function(name, type, value)
{
this.global_inputs[name] = { name: name, type: type, value: value };
@@ -1075,7 +1098,12 @@ LGraph.prototype.addGlobalInput = function(name, type, value)
this.onGlobalsChange();
}
//assign a data to the global input
/**
* Assign a data to the global graph input
* @method setGlobalInputData
* @param {String} name
* @param {*} data
*/
LGraph.prototype.setGlobalInputData = function(name, data)
{
var input = this.global_inputs[name];
@@ -1084,7 +1112,21 @@ LGraph.prototype.setGlobalInputData = function(name, data)
input.value = data;
}
//assign a data to the global input
/**
* Assign a data to the global graph input (same as setGlobalInputData)
* @method setInputData
* @param {String} name
* @param {*} data
*/
LGraph.prototype.setInputData = LGraph.prototype.setGlobalInputData;
/**
* Returns the current value of a global graph input
* @method getGlobalInputData
* @param {String} name
* @return {*} the data
*/
LGraph.prototype.getGlobalInputData = function(name)
{
var input = this.global_inputs[name];
@@ -1093,7 +1135,12 @@ LGraph.prototype.getGlobalInputData = function(name)
return input.value;
}
//rename the global input
/**
* Changes the name of a global graph input
* @method renameGlobalInput
* @param {String} old_name
* @param {String} new_name
*/
LGraph.prototype.renameGlobalInput = function(old_name, name)
{
if(name == old_name)
@@ -1118,6 +1165,12 @@ LGraph.prototype.renameGlobalInput = function(old_name, name)
this.onGlobalsChange();
}
/**
* Changes the type of a global graph input
* @method changeGlobalInputType
* @param {String} name
* @param {String} type
*/
LGraph.prototype.changeGlobalInputType = function(name, type)
{
if(!this.global_inputs[name])
@@ -1131,6 +1184,12 @@ LGraph.prototype.changeGlobalInputType = function(name, type)
this.onGlobalInputTypeChanged(name, type);
}
/**
* Removes a global graph input
* @method removeGlobalInput
* @param {String} name
* @param {String} type
*/
LGraph.prototype.removeGlobalInput = function(name)
{
if(!this.global_inputs[name])
@@ -1146,7 +1205,13 @@ LGraph.prototype.removeGlobalInput = function(name)
return true;
}
/**
* Creates a global graph output
* @method addGlobalOutput
* @param {String} name
* @param {String} type
* @param {*} value
*/
LGraph.prototype.addGlobalOutput = function(name, type, value)
{
this.global_outputs[name] = { name: name, type: type, value: value };
@@ -1158,7 +1223,12 @@ LGraph.prototype.addGlobalOutput = function(name, type, value)
this.onGlobalsChange();
}
//assign a data to the global output
/**
* Assign a data to the global output
* @method setGlobalOutputData
* @param {String} name
* @param {String} value
*/
LGraph.prototype.setGlobalOutputData = function(name, value)
{
var output = this.global_outputs[ name ];
@@ -1167,7 +1237,12 @@ LGraph.prototype.setGlobalOutputData = function(name, value)
output.value = value;
}
//assign a data to the global input
/**
* Returns the current value of a global graph output
* @method getGlobalOutputData
* @param {String} name
* @return {*} the data
*/
LGraph.prototype.getGlobalOutputData = function(name)
{
var output = this.global_outputs[name];
@@ -1176,8 +1251,21 @@ LGraph.prototype.getGlobalOutputData = function(name)
return output.value;
}
/**
* Returns the current value of a global graph output (sames as getGlobalOutputData)
* @method getOutputData
* @param {String} name
* @return {*} the data
*/
LGraph.prototype.getOutputData = LGraph.prototype.getGlobalOutputData;
//rename the global output
/**
* Renames a global graph output
* @method renameGlobalOutput
* @param {String} old_name
* @param {String} new_name
*/
LGraph.prototype.renameGlobalOutput = function(old_name, name)
{
if(!this.global_outputs[old_name])
@@ -1199,6 +1287,12 @@ LGraph.prototype.renameGlobalOutput = function(old_name, name)
this.onGlobalsChange();
}
/**
* Changes the type of a global graph output
* @method changeGlobalOutputType
* @param {String} name
* @param {String} type
*/
LGraph.prototype.changeGlobalOutputType = function(name, type)
{
if(!this.global_outputs[name])
@@ -1212,6 +1306,11 @@ LGraph.prototype.changeGlobalOutputType = function(name, type)
this.onGlobalOutputTypeChanged(name, type);
}
/**
* Removes a global graph output
* @method removeGlobalOutput
* @param {String} name
*/
LGraph.prototype.removeGlobalOutput = function(name)
{
if(!this.global_outputs[name])
@@ -1226,49 +1325,16 @@ LGraph.prototype.removeGlobalOutput = function(name)
return true;
}
/**
* Assigns a value to all the nodes that matches this name. This is used to create global variables of the node that
* can be easily accesed from the outside of the graph
* @method setInputData
* @param {String} name the name of the node
* @param {*} value value to assign to this node
*/
LGraph.prototype.setInputData = function(name,value)
{
var nodes = this.findNodesByName( name );
for(var i = 0, l = nodes.length; i < l; ++i)
nodes[i].setValue(value);
}
/**
* Returns the value of the first node with this name. This is used to access global variables of the graph from the outside
* @method setInputData
* @param {String} name the name of the node
* @return {*} value of the node
*/
LGraph.prototype.getOutputData = function(name)
{
var n = this.findNodesByName(name);
if(n.length)
return m[0].getValue();
return null;
}
//This feature is not finished yet, is to create graphs where nodes are not executed unless a trigger message is received
LGraph.prototype.triggerInput = function(name,value)
{
var nodes = this.findNodesByName(name);
var nodes = this.findNodesByTitle(name);
for(var i = 0; i < nodes.length; ++i)
nodes[i].onTrigger(value);
}
LGraph.prototype.setCallback = function(name,func)
{
var nodes = this.findNodesByName(name);
var nodes = this.findNodesByTitle(name);
for(var i = 0; i < nodes.length; ++i)
nodes[i].setTrigger(func);
}
@@ -1492,7 +1558,7 @@ LGraph.prototype.onNodeTrace = function(node, msg, color)
function LGraphNode(title)
{
this._ctor();
this._ctor(title);
}
global.LGraphNode = LiteGraph.LGraphNode = LGraphNode;
@@ -1564,6 +1630,7 @@ LGraphNode.prototype.configure = function(info)
if(info[j] == null)
continue;
else if (typeof(info[j]) == 'object') //object
{
if(this[j] && this[j].configure)
@@ -1575,6 +1642,9 @@ LGraphNode.prototype.configure = function(info)
this[j] = info[j];
}
if(!info.title)
this.title = this.constructor.title;
if(this.onConnectionsChange)
{
if(this.inputs)
@@ -1642,25 +1712,31 @@ LGraphNode.prototype.configure = function(info)
LGraphNode.prototype.serialize = function()
{
//clear outputs last data (because data in connections is never serialized but stored inside the outputs info)
if(this.outputs)
for(var i = 0; i < this.outputs.length; i++)
delete this.outputs[i]._data;
//create serialization object
var o = {
id: this.id,
title: this.title,
type: this.type,
pos: this.pos,
size: this.size,
data: this.data,
flags: LiteGraph.cloneObject(this.flags),
inputs: this.inputs,
outputs: this.outputs,
mode: this.mode
};
if(this.inputs)
o.inputs = this.inputs;
if(this.outputs)
{
//clear outputs last data (because data in connections is never serialized but stored inside the outputs info)
for(var i = 0; i < this.outputs.length; i++)
delete this.outputs[i]._data;
o.outputs = this.outputs;
}
if( this.title && this.title != this.constructor.title )
o.title = this.title;
if(this.properties)
o.properties = LiteGraph.cloneObject(this.properties);
@@ -1773,7 +1849,7 @@ LGraphNode.prototype.setOutputData = function(slot, data)
}
/**
* retrieves the input data (data traveling through the connection) from one slot
* Retrieves the input data (data traveling through the connection) from one slot
* @method getInputData
* @param {number} slot
* @param {boolean} force_update if set to true it will force the connected node of this slot to output data into this link
@@ -1792,10 +1868,10 @@ LGraphNode.prototype.getInputData = function( slot, force_update )
if(!link) //bug: weird case but it happens sometimes
return null;
//used to extract data from the incomming connection
if(!force_update)
return link.data;
//special case: used to extract data from the incomming connection before the graph has been executed
var node = this.graph.getNodeById( link.origin_id );
if(!node)
return link.data;
@@ -1808,6 +1884,22 @@ LGraphNode.prototype.getInputData = function( slot, force_update )
return link.data;
}
/**
* Retrieves the input data from one slot using its name instead of slot number
* @method getInputDataByName
* @param {String} slot_name
* @param {boolean} force_update if set to true it will force the connected node of this slot to output data into this link
* @return {*} data or if it is not connected returns null
*/
LGraphNode.prototype.getInputDataByName = function( slot_name, force_update )
{
var slot = this.findInputSlot( slot_name );
if( slot == -1 )
return null;
return this.getInputData( slot, force_update );
}
/**
* tells you if there is a connection in one input slot
* @method isInputConnected
@@ -1857,6 +1949,31 @@ LGraphNode.prototype.getInputNode = function( slot )
return this.graph.getNodeById( link_info.origin_id );
}
/**
* returns the value of an input with this name, otherwise checks if there is a property with that name
* @method getInputOrProperty
* @param {string} name
* @return {*} value
*/
LGraphNode.prototype.getInputOrProperty = function( name )
{
if(!this.inputs || !this.inputs.length)
return this.properties ? this.properties[name] : null;
for(var i = 0, l = this.inputs.length; i < l; ++i)
if(name == this.inputs[i].name)
{
var link_id = this.inputs[i].link;
var link = this.graph.links[ link_id ];
return link ? link.data : null;
}
return this.properties[name];
}
/**
* tells you the last output data that went in that slot
* @method getOutputData
@@ -1900,10 +2017,26 @@ LGraphNode.prototype.getOutputInfo = function(slot)
LGraphNode.prototype.isOutputConnected = function(slot)
{
if(!this.outputs)
return null;
return false;
return (slot < this.outputs.length && this.outputs[slot].links && this.outputs[slot].links.length);
}
/**
* tells you if there is any connection in the output slots
* @method isAnyOutputConnected
* @return {boolean}
*/
LGraphNode.prototype.isAnyOutputConnected = function()
{
if(!this.outputs)
return false;
for(var i = 0; i < this.outputs.length; ++i)
if( this.outputs[i].links && this.outputs[i].links.length )
return true;
return false;
}
/**
* retrieves all the nodes connected to this output slot
* @method getOutputNodes
@@ -2810,10 +2943,10 @@ function LGraphCanvas( canvas, graph, options )
this.node_title_color = LiteGraph.NODE_TITLE_COLOR;
this.default_link_color = "#AAC";
this.default_connection_color = {
input_off: "#AAC",
input_off: "#AAB",
input_on: "#7F7",
output_off: "#AAC",
output_on: "#7F7",
output_off: "#AAB",
output_on: "#7F7"
};
this.highquality_render = true;
@@ -6160,6 +6293,17 @@ function ContextMenu( values, options )
}
}, true);
function on_mouse_wheel(e)
{
var pos = parseInt( root.style.top );
root.style.top = (pos + e.deltaY * 0.1).toFixed() + "px";
e.preventDefault();
return true;
}
root.addEventListener("wheel", on_mouse_wheel, true);
root.addEventListener("mousewheel", on_mouse_wheel, true);
this.root = root;
@@ -6453,6 +6597,7 @@ LiteGraph.extendClass = function ( target, origin )
}
}
//used to create nodes from wrapping functions
LiteGraph.getParameterNames = function(func) {
return (func + '')
.replace(/[/][/].*$/mg,'') // strip single-line comments
@@ -6463,6 +6608,8 @@ LiteGraph.getParameterNames = function(func) {
.split(',').filter(Boolean); // split & filter [""]
}
Math.clamp = function(v,a,b) { return (a > v ? a : (b < v ? b : v)); }
if( typeof(window) != "undefined" && !window["requestAnimationFrame"] )
{
window.requestAnimationFrame = window.webkitRequestAnimationFrame ||