Line data Source code
1 : import type { MinBounds } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { ref } from 'vue'
3 : import type { Ref, ShallowRef } from 'vue'
4 :
5 : import type { MinimapCanvas } from '@/renderer/extensions/minimap/types'
6 :
7 2102 : export function useMinimapInteraction(
8 : containerRef: Readonly<ShallowRef<HTMLDivElement | null>>,
9 : bounds: Ref<MinBounds>,
10 : scale: Ref<number>,
11 : width: number,
12 : height: number,
13 : centerViewOn: (worldX: number, worldY: number) => void,
14 : canvas: Ref<MinimapCanvas | null>
15 : ) {
16 2102 : const isDragging = ref(false)
17 2102 : const containerRect = ref({
18 : left: 0,
19 : top: 0,
20 : width: width,
21 : height: height
22 : })
23 :
24 2102 : const updateContainerRect = () => {
25 72 : if (!containerRef.value) return
26 :
27 70 : const rect = containerRef.value.getBoundingClientRect()
28 70 : containerRect.value = {
29 : left: rect.left,
30 : top: rect.top,
31 : width: rect.width,
32 : height: rect.height
33 : }
34 : }
35 :
36 2102 : const handlePointerDown = (e: PointerEvent) => {
37 4 : isDragging.value = true
38 4 : updateContainerRect()
39 4 : const target = e.currentTarget
40 4 : if (target instanceof HTMLElement) {
41 4 : target.setPointerCapture(e.pointerId)
42 : }
43 4 : handlePointerMove(e)
44 : }
45 :
46 2102 : const handlePointerMove = (e: PointerEvent) => {
47 12 : if (!isDragging.value || !canvas.value) return
48 :
49 8 : const x = e.clientX - containerRect.value.left
50 8 : const y = e.clientY - containerRect.value.top
51 :
52 8 : const offsetX = (width - bounds.value.width * scale.value) / 2
53 8 : const offsetY = (height - bounds.value.height * scale.value) / 2
54 :
55 : const worldX = (x - offsetX) / scale.value + bounds.value.minX
56 : const worldY = (y - offsetY) / scale.value + bounds.value.minY
57 :
58 8 : centerViewOn(worldX, worldY)
59 : }
60 :
61 2102 : const releasePointer = (e?: PointerEvent) => {
62 7 : isDragging.value = false
63 7 : if (!e) return
64 :
65 7 : const target = e.currentTarget
66 7 : if (
67 : target instanceof HTMLElement &&
68 : target.hasPointerCapture(e.pointerId)
69 : ) {
70 2 : target.releasePointerCapture(e.pointerId)
71 : }
72 : }
73 :
74 2102 : const handlePointerUp = releasePointer
75 :
76 2102 : const handlePointerCancel = releasePointer
77 :
78 2102 : const handleWheel = (e: WheelEvent) => {
79 0 : e.preventDefault()
80 :
81 0 : const c = canvas.value
82 0 : if (!c) return
83 :
84 0 : if (
85 : containerRect.value.left === 0 &&
86 : containerRect.value.top === 0 &&
87 : containerRef.value
88 : ) {
89 0 : updateContainerRect()
90 : }
91 :
92 0 : const ds = c.ds
93 0 : const delta = e.deltaY > 0 ? 0.9 : 1.1
94 :
95 0 : const newScale = ds.scale * delta
96 :
97 : const MIN_SCALE = 0.1
98 : const MAX_SCALE = 10
99 :
100 0 : if (newScale < MIN_SCALE || newScale > MAX_SCALE) return
101 :
102 0 : const x = e.clientX - containerRect.value.left
103 0 : const y = e.clientY - containerRect.value.top
104 :
105 0 : const offsetX = (width - bounds.value.width * scale.value) / 2
106 0 : const offsetY = (height - bounds.value.height * scale.value) / 2
107 :
108 0 : const worldX = (x - offsetX) / scale.value + bounds.value.minX
109 0 : const worldY = (y - offsetY) / scale.value + bounds.value.minY
110 :
111 0 : ds.scale = newScale
112 :
113 0 : centerViewOn(worldX, worldY)
114 : }
115 :
116 2102 : return {
117 : isDragging,
118 : containerRect,
119 : updateContainerRect,
120 : handlePointerDown,
121 : handlePointerMove,
122 : handlePointerUp,
123 : handlePointerCancel,
124 : handleWheel
125 : }
126 : }
|