added setValue to some nodes

This commit is contained in:
tamat
2020-04-03 17:45:05 +02:00
parent 52b82f23ca
commit 2158aff0b0
9 changed files with 1147 additions and 811 deletions

View File

@@ -441,27 +441,8 @@
enumerable: true
});
this.name_widget = this.addWidget(
"text",
"Name",
this.properties.name,
function(v) {
if (!v) {
return;
}
that.properties.name = v;
}
);
this.type_widget = this.addWidget(
"text",
"Type",
this.properties.type,
function(v) {
v = v || "";
that.properties.type = v;
}
);
this.name_widget = this.addWidget("text","Name",this.properties.name,"name");
this.type_widget = this.addWidget("text","Type",this.properties.type,"type");
this.widgets_up = true;
this.size = [180, 60];
}
@@ -500,12 +481,7 @@
function ConstantNumber() {
this.addOutput("value", "number");
this.addProperty("value", 1.0);
this.widget = this.addWidget(
"number",
"value",
1,
"value"
);
this.widget = this.addWidget("number","value",1,"value");
this.widgets_up = true;
this.size = [180, 30];
}
@@ -524,6 +500,11 @@
return this.title;
};
ConstantNumber.prototype.setValue = function(v)
{
this.setProperty("value",v);
}
ConstantNumber.prototype.onDrawBackground = function(ctx) {
//show the current value
this.outputs[0].label = this.properties["value"].toFixed(3);
@@ -534,12 +515,7 @@
function ConstantBoolean() {
this.addOutput("", "boolean");
this.addProperty("value", true);
this.widget = this.addWidget(
"toggle",
"value",
true,
"value"
);
this.widget = this.addWidget("toggle","value",true,"value");
this.widgets_up = true;
this.size = [140, 30];
}
@@ -552,17 +528,14 @@
this.setOutputData(0, this.properties["value"]);
};
ConstantBoolean.prototype.setValue = ConstantNumber.prototype.setValue;
LiteGraph.registerNodeType("basic/boolean", ConstantBoolean);
function ConstantString() {
this.addOutput("", "string");
this.addProperty("value", "");
this.widget = this.addWidget(
"text",
"value",
"",
"value" //link to property value
);
this.widget = this.addWidget("text","value","","value"); //link to property value
this.widgets_up = true;
this.size = [180, 30];
}
@@ -576,17 +549,124 @@
this.setOutputData(0, this.properties["value"]);
};
ConstantString.prototype.setValue = ConstantNumber.prototype.setValue;
ConstantString.prototype.onDropFile = function(file)
{
var that = this;
var reader = new FileReader();
reader.onload = function(e)
{
that.setProperty("value",e.target.result);
}
reader.readAsText(file);
}
LiteGraph.registerNodeType("basic/string", ConstantString);
function ConstantFile() {
this.addInput("url", "");
this.addOutput("", "");
this.addProperty("url", "");
this.addProperty("type", "text");
this.widget = this.addWidget("text","url","","url");
this._data = null;
}
ConstantFile.title = "Const File";
ConstantFile.desc = "Fetches a file from an url";
ConstantFile["@type"] = { type: "enum", values: ["text","arraybuffer","blob","json"] };
ConstantFile.prototype.onPropertyChanged = function(name, value) {
if (name == "url")
{
if( value == null || value == "")
this._data = null;
else
{
this.fetchFile(value);
}
}
}
ConstantFile.prototype.onExecute = function() {
var url = this.getInputData(0) || this.properties.url;
if(url && (url != this._url || this._type != this.properties.type))
this.fetchFile(url);
this.setOutputData(0, this._data );
};
ConstantFile.prototype.setValue = ConstantNumber.prototype.setValue;
ConstantFile.prototype.fetchFile = function(url) {
var that = this;
if(!url || url.constructor !== String)
{
that._data = null;
that.boxcolor = null;
return;
}
this._url = url;
this._type = this.properties.type;
if (url.substr(0, 4) == "http" && LiteGraph.proxy) {
url = LiteGraph.proxy + url.substr(url.indexOf(":") + 3);
}
fetch(url)
.then(function(response) {
if(!response.ok)
throw new Error("File not found");
if(that.properties.type == "arraybuffer")
return response.arrayBuffer();
else if(that.properties.type == "text")
return response.text();
else if(that.properties.type == "json")
return response.json();
else if(that.properties.type == "blob")
return response.blob();
})
.then(function(data) {
that._data = data;
that.boxcolor = "#AEA";
})
.catch(function(error) {
that._data = null;
that.boxcolor = "red";
console.error("error fetching file:",url);
});
};
ConstantFile.prototype.onDropFile = function(file)
{
var that = this;
this._url = file.name;
this._type = this.properties.type;
this.properties.url = file.name;
var reader = new FileReader();
reader.onload = function(e)
{
that.boxcolor = "#AEA";
var v = e.target.result;
if( that.properties.type == "json" )
v = JSON.parse(v);
that._data = v;
}
if(that.properties.type == "arraybuffer")
reader.readAsArrayBuffer(file);
else if(that.properties.type == "text" || that.properties.type == "json")
reader.readAsText(file);
else if(that.properties.type == "blob")
return reader.readAsBinaryString(file);
}
LiteGraph.registerNodeType("basic/file", ConstantFile);
//to store json objects
function ConstantData() {
this.addOutput("", "");
this.addProperty("value", "");
this.widget = this.addWidget(
"text",
"json",
"",
this.setValue.bind(this)
);
this.widget = this.addWidget("text","json","","value");
this.widgets_up = true;
this.size = [140, 30];
this._value = null;
@@ -595,11 +675,6 @@
ConstantData.title = "Const Data";
ConstantData.desc = "Constant Data";
ConstantData.prototype.setValue = function(v) {
this.properties.value = v;
this.onPropertyChanged("value", v);
};
ConstantData.prototype.onPropertyChanged = function(name, value) {
this.widget.value = value;
if (value == null || value == "") {
@@ -618,18 +693,68 @@
this.setOutputData(0, this._value);
};
ConstantData.prototype.setValue = ConstantNumber.prototype.setValue;
LiteGraph.registerNodeType("basic/data", ConstantData);
function ArrayElement() {
this.addInput("array", "array,table,string");
this.addInput("index", "number");
this.addOutput("value", "");
this.addProperty("index",0);
}
ArrayElement.title = "Array[i]";
ArrayElement.desc = "Returns an element from an array";
ArrayElement.prototype.onExecute = function() {
var array = this.getInputData(0);
var index = this.getInputData(1);
if(index == null)
index = this.properties.index;
if(array == null || index == null )
return;
this.setOutputData(0, array[Math.floor(Number(index))] );
};
LiteGraph.registerNodeType("basic/array[]", ArrayElement);
function TableElement() {
this.addInput("table", "table");
this.addInput("row", "number");
this.addInput("col", "number");
this.addOutput("value", "");
this.addProperty("row",0);
this.addProperty("column",0);
}
TableElement.title = "Table[row][col]";
TableElement.desc = "Returns an element from a table";
TableElement.prototype.onExecute = function() {
var table = this.getInputData(0);
var row = this.getInputData(1);
var col = this.getInputData(2);
if(row == null)
row = this.properties.row;
if(col == null)
col = this.properties.column;
if(table == null || row == null || col == null)
return;
var row = table[Math.floor(Number(row))];
if(row)
this.setOutputData(0, row[Math.floor(Number(col))] );
else
this.setOutputData(0, null );
};
LiteGraph.registerNodeType("basic/table[][]", TableElement);
function ObjectProperty() {
this.addInput("obj", "");
this.addOutput("", "");
this.addProperty("value", "");
this.widget = this.addWidget(
"text",
"prop.",
"",
this.setValue.bind(this)
);
this.widget = this.addWidget("text","prop.","",this.setValue.bind(this) );
this.widgets_up = true;
this.size = [140, 30];
this._value = null;
@@ -738,6 +863,18 @@
LiteGraph.registerNodeType("basic/variable", Variable);
function length(v) {
if(v && v.length != null)
return Number(v.length);
return 0;
}
LiteGraph.wrapFunctionAsNode(
"basic/length",
length,
["*"],
"number"
);
function DownloadData() {
this.size = [60, 30];