Skip to content

Commit

Permalink
Fix snap to grid - now toggled at app level
Browse files Browse the repository at this point in the history
Was impl. as per workflow.
  • Loading branch information
webfiltered committed Nov 21, 2024
1 parent c912a36 commit 4a4762f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
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

0 comments on commit 4a4762f

Please sign in to comment.