Merge pull request #190 from benhar-dev/multi-level-add-node-context-menu

multi-level context menus
This commit is contained in:
Javi Agenjo
2021-01-12 10:47:39 +01:00
committed by GitHub

View File

@@ -9180,58 +9180,74 @@ LGraphNode.prototype.executeAction = function(action)
canvas.graph.add(group); canvas.graph.add(group);
}; };
LGraphCanvas.onMenuAdd = function(node, options, e, prev_menu, callback) { LGraphCanvas.onMenuAdd = function (node, options, e, prev_menu, callback) {
var canvas = LGraphCanvas.active_canvas; var canvas = LGraphCanvas.active_canvas;
var ref_window = canvas.getCanvasWindow(); var ref_window = canvas.getCanvasWindow();
var graph = canvas.graph; var graph = canvas.graph;
if(!graph) if (!graph)
return; return;
var values = LiteGraph.getNodeTypesCategories( canvas.filter || graph.filter ); function inner_onMenuAdded(base_category ,prev_menu){
var entries = [];
for (var i=0; i < values.length; i++) { var categories = LiteGraph.getNodeTypesCategories(canvas.filter || graph.filter).filter(function(category){return category.startsWith(base_category)});
if (values[i]) { var entries = [];
var name = values[i];
if(name.indexOf("::") != -1) //in case it has a namespace like "shader::math/rand" it hides the namespace categories.map(function(category){
name = name.split("::")[1];
entries.push({ value: values[i], content: name, has_submenu: true }); if (!category)
} return;
}
var base_category_regex = new RegExp('^(' + base_category + ')');
//show categories var category_name = category.replace(base_category_regex,"").split('/')[0];
var menu = new LiteGraph.ContextMenu( entries, { event: e, callback: inner_clicked, parentMenu: prev_menu }, ref_window ); var category_path = base_category === '' ? category_name + '/' : base_category + category_name + '/';
function inner_clicked(v, option, e) { var name = category_name;
var category = v.value; if(name.indexOf("::") != -1) //in case it has a namespace like "shader::math/rand" it hides the namespace
var node_types = LiteGraph.getNodeTypesInCategory( category, canvas.filter || graph.filter ); name = name.split("::")[1];
var values = [];
for (var i=0; i < node_types.length; i++) { var index = entries.findIndex(function(entry){return entry.value === category_path});
if (!node_types[i].skip_list) { if (index === -1) {
values.push({ entries.push({ value: category_path, content: name, has_submenu: true, callback : function(value, event, mouseEvent, contextMenu){
content: node_types[i].title, inner_onMenuAdded(value.value, contextMenu)
value: node_types[i].type }});
});
} }
}
});
new LiteGraph.ContextMenu( values, { event: e, callback: inner_create, parentMenu: menu }, ref_window );
return false; var nodes = LiteGraph.getNodeTypesInCategory(base_category.slice(0, -1), canvas.filter || graph.filter );
nodes.map(function(node){
if (node.skip_list)
return;
var entry = { value: node.type, content: node.title, has_submenu: false , callback : function(value, event, mouseEvent, contextMenu){
var first_event = contextMenu.getFirstEvent();
canvas.graph.beforeChange();
var node = LiteGraph.createNode(value.value);
if (node) {
node.pos = canvas.convertEventToCanvasOffset(first_event);
canvas.graph.add(node);
}
if(callback)
callback(node);
canvas.graph.afterChange();
}
}
entries.push(entry);
});
new LiteGraph.ContextMenu( entries, { event: e, parentMenu: prev_menu }, ref_window );
} }
function inner_create(v, e) { inner_onMenuAdded('',prev_menu);
var first_event = prev_menu.getFirstEvent();
canvas.graph.beforeChange();
var node = LiteGraph.createNode(v.value);
if (node) {
node.pos = canvas.convertEventToCanvasOffset(first_event);
canvas.graph.add(node);
}
if(callback)
callback(node);
canvas.graph.afterChange();
}
return false; return false;
}; };
LGraphCanvas.onMenuCollapseAll = function() {}; LGraphCanvas.onMenuCollapseAll = function() {};