|
|
import { app } from '../../../../scripts/app.js'
|
|
|
import { $el } from "../../../../scripts/ui.js";
|
|
|
|
|
|
export function addStylesheet(url) {
|
|
|
if (url.endsWith(".js")) {
|
|
|
url = url.substr(0, url.length - 2) + "css";
|
|
|
}
|
|
|
$el("link", {
|
|
|
parent: document.head,
|
|
|
rel: "stylesheet",
|
|
|
type: "text/css",
|
|
|
href: url.startsWith("http") ? url : getUrl(url),
|
|
|
});
|
|
|
}
|
|
|
|
|
|
export function getUrl(path, baseUrl) {
|
|
|
if (baseUrl) {
|
|
|
return new URL(path, baseUrl).toString();
|
|
|
} else {
|
|
|
return new URL("../" + path, import.meta.url).toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export async function loadImage(url) {
|
|
|
return new Promise((res, rej) => {
|
|
|
const img = new Image();
|
|
|
img.onload = res;
|
|
|
img.onerror = rej;
|
|
|
img.src = url;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
export function addMenuHandler(nodeType, cb) {
|
|
|
|
|
|
const GROUPED_MENU_ORDER = {
|
|
|
"π Swap with...": 0,
|
|
|
"β Add link...": 1,
|
|
|
"π Add script...": 2,
|
|
|
"π View model info...": 3,
|
|
|
"π± Seed behavior...": 4,
|
|
|
"π Set Resolution...": 5,
|
|
|
"βοΈ Add π input...": 6,
|
|
|
"βοΈ Add π input...": 7
|
|
|
};
|
|
|
|
|
|
const originalGetOpts = nodeType.prototype.getExtraMenuOptions;
|
|
|
|
|
|
nodeType.prototype.getExtraMenuOptions = function () {
|
|
|
let r = originalGetOpts ? originalGetOpts.apply(this, arguments) || [] : [];
|
|
|
|
|
|
const insertOption = (option) => {
|
|
|
if (GROUPED_MENU_ORDER.hasOwnProperty(option.content)) {
|
|
|
|
|
|
let targetPos = r.length;
|
|
|
|
|
|
for (let i = 0; i < r.length; i++) {
|
|
|
if (GROUPED_MENU_ORDER.hasOwnProperty(r[i].content) &&
|
|
|
GROUPED_MENU_ORDER[option.content] < GROUPED_MENU_ORDER[r[i].content]) {
|
|
|
targetPos = i;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
r.splice(targetPos, 0, option);
|
|
|
} else {
|
|
|
|
|
|
r.push(option);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
cb.call(this, insertOption);
|
|
|
|
|
|
return r;
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function findWidgetByName(node, widgetName) {
|
|
|
return node.widgets.find(widget => widget.name === widgetName);
|
|
|
}
|
|
|
|
|
|
|
|
|
export function addNode(name, nextTo, options) {
|
|
|
options = { select: true, shiftX: 0, shiftY: 0, before: false, ...(options || {}) };
|
|
|
const node = LiteGraph.createNode(name);
|
|
|
app.graph.add(node);
|
|
|
node.pos = [
|
|
|
nextTo.pos[0] + options.shiftX,
|
|
|
nextTo.pos[1] + options.shiftY,
|
|
|
];
|
|
|
if (options.select) {
|
|
|
app.canvas.selectNode(node, false);
|
|
|
}
|
|
|
return node;
|
|
|
}
|
|
|
|