Merge pull request #245 from rgthree/patch-1

Update "AlwaysSnapToGrid" to work with groups.
This commit is contained in:
pythongosssss
2024-05-16 20:40:42 +01:00
committed by GitHub
+32 -16
View File
@@ -1,7 +1,21 @@
import { app } from "../../../scripts/app.js";
let setting;
const id = "pysssss.SnapToGrid";
/** Wraps the provided function call to set/reset shiftDown when setting is enabled. */
function wrapCallInSettingCheck(fn) {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = fn();
app.shiftDown = shift;
return r;
}
return fn();
}
const ext = {
name: id,
init() {
@@ -22,14 +36,7 @@ const ext = {
// Override drawNode to draw the drop position
const drawNode = LGraphCanvas.prototype.drawNode;
LGraphCanvas.prototype.drawNode = function () {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = drawNode.apply(this, arguments);
app.shiftDown = shift;
return r;
}
return drawNode.apply(this, arguments);
wrapCallInSettingCheck(() => drawNode.apply(this, arguments));
};
// Override node added to add a resize handler to force grid alignment
@@ -38,18 +45,27 @@ const ext = {
const r = onNodeAdded?.apply(this, arguments);
const onResize = node.onResize;
node.onResize = function () {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = onResize?.apply(this, arguments);
app.shiftDown = shift;
return r;
}
return onResize?.apply(this, arguments);
wrapCallInSettingCheck(() => onResize?.apply(this, arguments));
};
return r;
};
const groupMove = LGraphGroup.prototype.move;
LGraphGroup.prototype.move = function(deltax, deltay, ignore_nodes) {
wrapCallInSettingCheck(() => groupMove.apply(this, arguments));
}
const canvasDrawGroups = LGraphCanvas.prototype.drawGroups;
LGraphCanvas.prototype.drawGroups = function (canvas, ctx) {
wrapCallInSettingCheck(() => canvasDrawGroups.apply(this, arguments));
}
const canvasOnGroupAdd = LGraphCanvas.onGroupAdd;
LGraphCanvas.onGroupAdd = function() {
wrapCallInSettingCheck(() => canvasOnGroupAdd.apply(this, arguments));
}
return configure.apply(this, arguments);
};
},