"web/index.html" did not exist on "3637e19effc55d80ec0a642833d29b21d39c5dcd"
snapToGrid.js 2.77 KB
Newer Older
pythongosssss's avatar
pythongosssss committed
1
2
import { app } from "/scripts/app.js";

pythongosssss's avatar
pythongosssss committed
3
// Shift + drag/resize to snap to grid
pythongosssss's avatar
pythongosssss committed
4
5
6
7
8
9
10
11

app.registerExtension({
	name: "Comfy.SnapToGrid",
	init() {
		// Add setting to control grid size
		app.ui.settings.addSetting({
			id: "Comfy.SnapToGrid.GridSize",
			name: "Grid Size",
missionfloyd's avatar
missionfloyd committed
12
			type: "slider",
pythongosssss's avatar
pythongosssss committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
			attrs: {
				min: 1,
				max: 500,
			},
			tooltip:
				"When dragging and resizing nodes while holding shift they will be aligned to the grid, this controls the size of that grid.",
			defaultValue: LiteGraph.CANVAS_GRID_SIZE,
			onChange(value) {
				LiteGraph.CANVAS_GRID_SIZE = +value;
			},
		});

		// After moving a node, if the shift key is down align it to grid
		const onNodeMoved = app.canvas.onNodeMoved;
		app.canvas.onNodeMoved = function (node) {
			const r = onNodeMoved?.apply(this, arguments);

			if (app.shiftDown) {
pythongosssss's avatar
pythongosssss committed
31
32
33
34
				// Ensure all selected nodes are realigned
				for (const id in this.selected_nodes) {
					this.selected_nodes[id].alignToGrid();
				}
pythongosssss's avatar
pythongosssss committed
35
36
37
38
39
			}

			return r;
		};

pythongosssss's avatar
pythongosssss committed
40
		// When a node is added, add a resize handler to it so we can fix align the size with the grid
pythongosssss's avatar
pythongosssss committed
41
42
43
44
		const onNodeAdded = app.graph.onNodeAdded;
		app.graph.onNodeAdded = function (node) {
			const onResize = node.onResize;
			node.onResize = function () {
pythongosssss's avatar
pythongosssss committed
45
46
47
48
49
50
				if (app.shiftDown) {
					const w = LiteGraph.CANVAS_GRID_SIZE * Math.round(node.size[0] / LiteGraph.CANVAS_GRID_SIZE);
					const h = LiteGraph.CANVAS_GRID_SIZE * Math.round(node.size[1] / LiteGraph.CANVAS_GRID_SIZE);
					node.size[0] = w;
					node.size[1] = h;
				}
pythongosssss's avatar
pythongosssss committed
51
52
53
54
55
				return onResize?.apply(this, arguments);
			};
			return onNodeAdded?.apply(this, arguments);
		};

pythongosssss's avatar
pythongosssss committed
56
		// Draw a preview of where the node will go if holding shift and the node is selected
pythongosssss's avatar
pythongosssss committed
57
58
		const origDrawNode = LGraphCanvas.prototype.drawNode;
		LGraphCanvas.prototype.drawNode = function (node, ctx) {
pythongosssss's avatar
pythongosssss committed
59
			if (app.shiftDown && this.node_dragged && node.id in this.selected_nodes) {
pythongosssss's avatar
pythongosssss committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
				const x = LiteGraph.CANVAS_GRID_SIZE * Math.round(node.pos[0] / LiteGraph.CANVAS_GRID_SIZE);
				const y = LiteGraph.CANVAS_GRID_SIZE * Math.round(node.pos[1] / LiteGraph.CANVAS_GRID_SIZE);

				const shiftX = x - node.pos[0];
				let shiftY = y - node.pos[1];

				let w, h;
				if (node.flags.collapsed) {
					w = node._collapsed_width;
					h = LiteGraph.NODE_TITLE_HEIGHT;
					shiftY -= LiteGraph.NODE_TITLE_HEIGHT;
				} else {
					w = node.size[0];
					h = node.size[1];
					let titleMode = node.constructor.title_mode;
					if (titleMode !== LiteGraph.TRANSPARENT_TITLE && titleMode !== LiteGraph.NO_TITLE) {
						h += LiteGraph.NODE_TITLE_HEIGHT;
						shiftY -= LiteGraph.NODE_TITLE_HEIGHT;
					}
				}
				const f = ctx.fillStyle;
				ctx.fillStyle = "rgba(100, 100, 100, 0.5)";
				ctx.fillRect(shiftX, shiftY, w, h);
				ctx.fillStyle = f;
			}

			return origDrawNode.apply(this, arguments);
		};
	},
});