All files / web/src/components/toys/coordinate-plane/ruler useRulerInteraction.ts

0% Statements 0/319
0% Branches 0/1
0% Functions 0/1
0% Lines 0/319

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useEffect, useCallback } from 'react'
import type { RulerState, RulerHitZone, SlopeGuideState } from './types'
import type { CoordinatePlaneState } from '../types'
import { worldToScreen2D, screenToWorld2D } from '../../shared/coordinateConversions'
import { HANDLE_HIT_RADIUS, BODY_HALF_WIDTH } from './renderRuler'
import { computeSlopeGuides } from './slopeGuides'

interface UseRulerInteractionOptions {
  rulerRef: React.MutableRefObject<RulerState>
  stateRef: React.MutableRefObject<CoordinatePlaneState>
  canvasRef: React.RefObject<HTMLCanvasElement | null>
  pointerCapturedRef: React.MutableRefObject<boolean>
  onRulerChange: () => void
  /** Called with the active drag zone (for rendering highlights) or null */
  onActiveHandleChange: (zone: 'handleA' | 'handleB' | 'body' | null) => void
  /** When false, ruler interaction is disabled (no hit testing or dragging) */
  enabled?: boolean
  /** Ref to write slope guide state into during handle drags */
  slopeGuideRef?: React.MutableRefObject<SlopeGuideState | null>
}

/** Distance from a point to the segment AB, clamped to segment */
function distToSegment(
  px: number,
  py: number,
  ax: number,
  ay: number,
  bx: number,
  by: number
): number {
  const dx = bx - ax
  const dy = by - ay
  const lenSq = dx * dx + dy * dy
  if (lenSq < 1e-8) {
    // Degenerate segment
    return Math.sqrt((px - ax) ** 2 + (py - ay) ** 2)
  }
  let t = ((px - ax) * dx + (py - ay) * dy) / lenSq
  t = Math.max(0, Math.min(1, t))
  const cx = ax + t * dx
  const cy = ay + t * dy
  return Math.sqrt((px - cx) ** 2 + (py - cy) ** 2)
}

function hitTest(
  screenX: number,
  screenY: number,
  ruler: RulerState,
  planeState: CoordinatePlaneState,
  cssWidth: number,
  cssHeight: number
): RulerHitZone {
  const s = planeState
  const a = worldToScreen2D(
    ruler.ax,
    ruler.ay,
    s.center.x,
    s.center.y,
    s.pixelsPerUnit.x,
    s.pixelsPerUnit.y,
    cssWidth,
    cssHeight
  )
  const b = worldToScreen2D(
    ruler.bx,
    ruler.by,
    s.center.x,
    s.center.y,
    s.pixelsPerUnit.x,
    s.pixelsPerUnit.y,
    cssWidth,
    cssHeight
  )

  const dA = Math.sqrt((screenX - a.x) ** 2 + (screenY - a.y) ** 2)
  const dB = Math.sqrt((screenX - b.x) ** 2 + (screenY - b.y) ** 2)

  if (dA < HANDLE_HIT_RADIUS) return 'handleA'
  if (dB < HANDLE_HIT_RADIUS) return 'handleB'

  const bodyHit = BODY_HALF_WIDTH + 12 // generous body hit area
  const dist = distToSegment(screenX, screenY, a.x, a.y, b.x, b.y)
  if (dist < bodyHit) return 'body'

  return 'miss'
}

export function useRulerInteraction({
  rulerRef,
  stateRef,
  canvasRef,
  pointerCapturedRef,
  onRulerChange,
  onActiveHandleChange,
  enabled = true,
  slopeGuideRef,
}: UseRulerInteractionOptions) {
  const getCanvasRect = useCallback(() => {
    return canvasRef.current?.getBoundingClientRect()
  }, [canvasRef])

  useEffect(() => {
    const canvas = canvasRef.current
    if (!canvas || !enabled) return

    // Current drag state
    let dragZone: RulerHitZone = 'miss'
    // For body drag: world coords of both handles at drag start
    let bodyDragAnchorA = { x: 0, y: 0 }
    let bodyDragAnchorB = { x: 0, y: 0 }
    let bodyDragStartWorld = { x: 0, y: 0 }

    function snapToInt(v: number): number {
      return Math.round(v)
    }

    function handlePointerDown(sx: number, sy: number): boolean {
      const rect = getCanvasRect()
      if (!rect) return false
      const cssW = rect.width
      const cssH = rect.height

      const zone = hitTest(sx, sy, rulerRef.current, stateRef.current, cssW, cssH)
      if (zone === 'miss') return false

      dragZone = zone
      pointerCapturedRef.current = true
      onActiveHandleChange(zone)

      if (zone === 'body') {
        const ruler = rulerRef.current
        bodyDragAnchorA = { x: ruler.ax, y: ruler.ay }
        bodyDragAnchorB = { x: ruler.bx, y: ruler.by }
        const s = stateRef.current
        const w = screenToWorld2D(
          sx,
          sy,
          s.center.x,
          s.center.y,
          s.pixelsPerUnit.x,
          s.pixelsPerUnit.y,
          cssW,
          cssH
        )
        bodyDragStartWorld = { x: w.x, y: w.y }
      }

      return true
    }

    function handlePointerMove(sx: number, sy: number) {
      if (dragZone === 'miss') return
      const rect = getCanvasRect()
      if (!rect) return
      const cssW = rect.width
      const cssH = rect.height
      const s = stateRef.current
      const ruler = rulerRef.current

      if (dragZone === 'handleA' || dragZone === 'handleB') {
        const w = screenToWorld2D(
          sx,
          sy,
          s.center.x,
          s.center.y,
          s.pixelsPerUnit.x,
          s.pixelsPerUnit.y,
          cssW,
          cssH
        )
        const snappedX = snapToInt(w.x)
        const snappedY = snapToInt(w.y)

        // Slope guides: anchor is the *stationary* handle
        const anchorX = dragZone === 'handleA' ? ruler.bx : ruler.ax
        const anchorY = dragZone === 'handleA' ? ruler.by : ruler.ay
        if (slopeGuideRef) {
          slopeGuideRef.current = computeSlopeGuides(anchorX, anchorY, snappedX, snappedY)
        }

        if (dragZone === 'handleA') {
          ruler.ax = snappedX
          ruler.ay = snappedY
        } else {
          ruler.bx = snappedX
          ruler.by = snappedY
        }
        onRulerChange()
      } else if (dragZone === 'body') {
        const w = screenToWorld2D(
          sx,
          sy,
          s.center.x,
          s.center.y,
          s.pixelsPerUnit.x,
          s.pixelsPerUnit.y,
          cssW,
          cssH
        )
        const rawDx = w.x - bodyDragStartWorld.x
        const rawDy = w.y - bodyDragStartWorld.y
        const intDx = Math.round(rawDx)
        const intDy = Math.round(rawDy)

        ruler.ax = bodyDragAnchorA.x + intDx
        ruler.ay = bodyDragAnchorA.y + intDy
        ruler.bx = bodyDragAnchorB.x + intDx
        ruler.by = bodyDragAnchorB.y + intDy
        onRulerChange()
      }
    }

    function handlePointerUp() {
      dragZone = 'miss'
      pointerCapturedRef.current = false
      onActiveHandleChange(null)
      if (slopeGuideRef) slopeGuideRef.current = null
    }

    // ── Mouse handlers ─────────────────────────────────────────

    function onMouseDown(e: MouseEvent) {
      if (e.button !== 0) return
      const rect = getCanvasRect()
      if (!rect) return
      const sx = e.clientX - rect.left
      const sy = e.clientY - rect.top

      if (handlePointerDown(sx, sy)) {
        e.preventDefault()
        e.stopPropagation()
      }
    }

    function onMouseMove(e: MouseEvent) {
      if (dragZone === 'miss') return
      const rect = getCanvasRect()
      if (!rect) return
      const sx = e.clientX - rect.left
      const sy = e.clientY - rect.top
      handlePointerMove(sx, sy)
    }

    function onMouseUp() {
      if (dragZone !== 'miss') {
        handlePointerUp()
      }
    }

    // ── Touch handlers ─────────────────────────────────────────

    function onTouchStart(e: TouchEvent) {
      if (e.touches.length !== 1) return
      const rect = getCanvasRect()
      if (!rect) return
      const t = e.touches[0]
      const sx = t.clientX - rect.left
      const sy = t.clientY - rect.top

      if (handlePointerDown(sx, sy)) {
        e.preventDefault()
        e.stopPropagation()
      }
    }

    function onTouchMove(e: TouchEvent) {
      if (dragZone === 'miss') return
      if (e.touches.length !== 1) {
        handlePointerUp()
        return
      }
      const rect = getCanvasRect()
      if (!rect) return
      const t = e.touches[0]
      const sx = t.clientX - rect.left
      const sy = t.clientY - rect.top
      handlePointerMove(sx, sy)
      e.preventDefault()
    }

    function onTouchEnd(e: TouchEvent) {
      if (dragZone !== 'miss') {
        handlePointerUp()
        if (e.touches.length === 0) {
          e.preventDefault()
        }
      }
    }

    // Ruler handlers go on canvas with capture to intercept before pan/zoom
    canvas.addEventListener('mousedown', onMouseDown, { capture: true })
    window.addEventListener('mousemove', onMouseMove)
    window.addEventListener('mouseup', onMouseUp)
    canvas.addEventListener('touchstart', onTouchStart, { capture: true, passive: false })
    canvas.addEventListener('touchmove', onTouchMove, { capture: true, passive: false })
    canvas.addEventListener('touchend', onTouchEnd, { capture: true })
    canvas.addEventListener('touchcancel', onTouchEnd, { capture: true })

    return () => {
      canvas.removeEventListener('mousedown', onMouseDown, { capture: true })
      window.removeEventListener('mousemove', onMouseMove)
      window.removeEventListener('mouseup', onMouseUp)
      canvas.removeEventListener('touchstart', onTouchStart, { capture: true })
      canvas.removeEventListener('touchmove', onTouchMove, { capture: true })
      canvas.removeEventListener('touchend', onTouchEnd, { capture: true })
      canvas.removeEventListener('touchcancel', onTouchEnd, { capture: true })
    }
  }, [
    canvasRef,
    rulerRef,
    stateRef,
    pointerCapturedRef,
    onRulerChange,
    onActiveHandleChange,
    getCanvasRect,
    enabled,
    slopeGuideRef,
  ])
}