Line data Source code
1 : import type { Point2D, Size2D } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { computed } from 'vue'
3 :
4 : import { snapPoint } from '@/lib/litegraph/src/measure'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 :
7 9725 : export function useNodeSnap() {
8 9725 : const settingStore = useSettingStore()
9 :
10 9725 : const gridSize = computed(() => settingStore.get('Comfy.SnapToGrid.GridSize'))
11 9725 : const alwaysSnap = computed(() => settingStore.get('pysssss.SnapToGrid'))
12 :
13 176 : function shouldSnap(event: PointerEvent): boolean {
14 176 : return event.shiftKey || alwaysSnap.value
15 : }
16 :
17 0 : function applySnapToPosition(position: Point2D): Point2D {
18 0 : const size = gridSize.value
19 0 : if (!size) return { ...position }
20 :
21 0 : const posArray: [number, number] = [position.x, position.y]
22 0 : if (snapPoint(posArray, size)) {
23 0 : return { x: posArray[0], y: posArray[1] }
24 : }
25 0 : return { ...position }
26 : }
27 :
28 0 : function applySnapToSize(size: Size2D): Size2D {
29 0 : const gridSizeValue = gridSize.value
30 0 : if (!gridSizeValue) return { ...size }
31 :
32 0 : const sizeArray: [number, number] = [size.width, size.height]
33 0 : if (snapPoint(sizeArray, gridSizeValue, 'ceil')) {
34 0 : return { width: sizeArray[0], height: sizeArray[1] }
35 : }
36 0 : return { ...size }
37 : }
38 :
39 9725 : return {
40 : gridSize,
41 : alwaysSnap,
42 : shouldSnap,
43 : applySnapToPosition,
44 : applySnapToSize
45 : }
46 : }
|