Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Snap to Grid control to LiteGraphGlobal #329

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ type ParamsArray<T extends Record<any, any>, K extends MethodNames<T>> =
export interface LGraphConfig {
/** @deprecated Legacy config - unused */
align_to_grid?: any
/**
* When set to a positive number, when nodes are moved their positions will
* be rounded to the nearest multiple of this value. Half up.
* Default: `undefined`
* @todo Not implemented - see {@link LiteGraph.CANVAS_GRID_SIZE}
*/
snapToGrid?: number
/**
* If `true`, items always snap to the grid - modifier keys are ignored.
* When {@link snapToGrid} is falsy, a value of `1` is used.
* Default: `false`
*/
alwaysSnapToGrid?: boolean
links_ontop?: any
}

Expand Down Expand Up @@ -791,7 +778,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
const { state } = this

// Ensure created items are snapped
if (this.config.alwaysSnapToGrid) {
if (LiteGraph.alwaysSnapToGrid) {
const snapTo = this.getSnapToGridSize()
if (snapTo) node.snapToGrid(snapTo)
}
Expand Down Expand Up @@ -1060,7 +1047,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
*
* Item positions are reounded to the nearest multiple of {@link LiteGraph.CANVAS_GRID_SIZE}.
*
* When {@link config}.{@link LGraphConfig.alwaysSnapToGrid alwaysSnapToGrid} is enabled
* When {@link LiteGraph.alwaysSnapToGrid} is enabled
* and the grid size is falsy, a default of 1 is used.
* @param items The items to be snapped to the grid
* @todo Currently only snaps nodes.
Expand All @@ -1080,7 +1067,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
*/
getSnapToGridSize(): number {
// Default to 1 when always snapping
return this.config.alwaysSnapToGrid
return LiteGraph.alwaysSnapToGrid
? LiteGraph.CANVAS_GRID_SIZE || 1
: LiteGraph.CANVAS_GRID_SIZE
}
Expand Down
4 changes: 2 additions & 2 deletions src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ export class LGraphCanvas {
*/
#processDraggedItems(e: CanvasPointerEvent): void {
const { graph } = this
if (e.shiftKey || graph.config.alwaysSnapToGrid)
if (e.shiftKey || LiteGraph.alwaysSnapToGrid)
graph.snapToGrid(this.selectedItems)

this.dirty_canvas = true
Expand Down Expand Up @@ -4180,7 +4180,7 @@ export class LGraphCanvas {
}

// TODO: Set snapping value when changed instead of once per frame
this.#snapToGrid = this.#shiftDown || this.graph.config.alwaysSnapToGrid
this.#snapToGrid = this.#shiftDown || LiteGraph.alwaysSnapToGrid
? this.graph.getSnapToGridSize()
: undefined

Expand Down
15 changes: 15 additions & 0 deletions src/LiteGraphGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ export class LiteGraphGlobal {
/** [true!] renders a partial border to highlight when a dragged link is snapped to a node */
snap_highlights_node = true

/**
* If `true`, items always snap to the grid - modifier keys are ignored.
* When {@link snapToGrid} is falsy, a value of `1` is used.
* Default: `false`
*/
alwaysSnapToGrid?: boolean

/**
* When set to a positive number, when nodes are moved their positions will
* be rounded to the nearest multiple of this value. Half up.
* Default: `undefined`
* @todo Not implemented - see {@link LiteGraph.CANVAS_GRID_SIZE}
*/
snapToGrid?: number

/** [false on mobile] better true if not touch device, TODO add an helper/listener to close if false */
search_hide_on_mouse_leave = true
/**
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/litegraph.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ LiteGraphGlobal {
"allow_multi_output_for_events": true,
"allow_scripts": false,
"alt_drag_do_clone_nodes": false,
"alwaysSnapToGrid": undefined,
"auto_load_slot_types": false,
"auto_sort_node_types": false,
"catch_exceptions": true,
Expand Down Expand Up @@ -170,6 +171,7 @@ LiteGraphGlobal {
"slot_types_default_out": {},
"slot_types_in": [],
"slot_types_out": [],
"snapToGrid": undefined,
"snap_highlights_node": true,
"snaps_for_comfy": true,
"throw_errors": true,
Expand Down
Loading