From 9e758929dd86edf5d233b0fa074f3cc557a669e7 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Tue, 18 Jun 2024 17:19:35 -0400 Subject: [PATCH] Migrate snap to grid (#41) * rename * migrate --- .../core/{snapToGrid.js => snapToGrid.ts} | 14 ++++++++++++-- src/types/litegraph.d.ts | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) rename src/extensions/core/{snapToGrid.js => snapToGrid.ts} (96%) diff --git a/src/extensions/core/snapToGrid.js b/src/extensions/core/snapToGrid.ts similarity index 96% rename from src/extensions/core/snapToGrid.js rename to src/extensions/core/snapToGrid.ts index 4ca89ad32..5af1120db 100644 --- a/src/extensions/core/snapToGrid.js +++ b/src/extensions/core/snapToGrid.ts @@ -58,7 +58,9 @@ app.registerExtension({ }; // Draw a preview of where the node will go if holding shift and the node is selected + // @ts-ignore const origDrawNode = LGraphCanvas.prototype.drawNode; + // @ts-ignore LGraphCanvas.prototype.drawNode = function (node, ctx) { if (app.shiftDown && this.node_dragged && node.id in this.selected_nodes) { const [x, y] = roundVectorToGrid([...node.pos]); @@ -88,8 +90,6 @@ app.registerExtension({ return origDrawNode.apply(this, arguments); }; - - /** * The currently moving, selected group only. Set after the `selected_group` has actually started * moving. @@ -100,7 +100,9 @@ app.registerExtension({ * Handles moving a group; tracking when a group has been moved (to show the ghost in `drawGroups` * below) as well as handle the last move call from LiteGraph's `processMouseUp`. */ + // @ts-ignore const groupMove = LGraphGroup.prototype.move; + // @ts-ignore LGraphGroup.prototype.move = function(deltax, deltay, ignore_nodes) { const v = groupMove.apply(this, arguments); // When we've started moving, set `selectedAndMovingGroup` as LiteGraph sets `selected_group` @@ -120,6 +122,7 @@ app.registerExtension({ for (const node of this._nodes) { node.alignToGrid(); } + // @ts-ignore LGraphNode.prototype.alignToGrid.apply(this); } return v; @@ -130,7 +133,9 @@ app.registerExtension({ * drawing a ghost box when one is actively being moved. This mimics the node snapping behavior for * both. */ + // @ts-ignore const drawGroups = LGraphCanvas.prototype.drawGroups; + // @ts-ignore LGraphCanvas.prototype.drawGroups = function (canvas, ctx) { if (this.selected_group && app.shiftDown) { if (this.selected_group_resizing) { @@ -155,13 +160,18 @@ app.registerExtension({ /** Handles adding a group in a snapping-enabled state. */ + // @ts-ignore const onGroupAdd = LGraphCanvas.onGroupAdd; + // @ts-ignore LGraphCanvas.onGroupAdd = function() { const v = onGroupAdd.apply(app.canvas, arguments); if (app.shiftDown) { + // @ts-ignore const lastGroup = app.graph._groups[app.graph._groups.length - 1]; if (lastGroup) { + // @ts-ignore roundVectorToGrid(lastGroup.pos); + // @ts-ignore roundVectorToGrid(lastGroup.size); } } diff --git a/src/types/litegraph.d.ts b/src/types/litegraph.d.ts index df4e2a057..254bcb0eb 100644 --- a/src/types/litegraph.d.ts +++ b/src/types/litegraph.d.ts @@ -411,6 +411,8 @@ export type SerializedLGraphNode = { /** https://github.com/jagenjo/litegraph.js/blob/master/guides/README.md#lgraphnode */ export declare class LGraphNode { + onResize?: Function; + // Used in group node setInnerNodes(nodes: any) { throw new Error("Method not implemented."); @@ -1028,6 +1030,8 @@ export declare class LGraphCanvas { node_over: LGraphNode | null; node_title_color: string; node_widget: [LGraphNode, IWidget] | null; + last_mouse_dragging: boolean; + /** Called by `LGraphCanvas.drawBackCanvas` */ onDrawBackground: | ((ctx: CanvasRenderingContext2D, visibleArea: Vector4) => void)