import { useRef, type KeyboardEvent, type PointerEvent } from 'react'; import { dialAngle, normAngle } from './paintEdit'; /** * An angle, set by turning a dial. * * A number field alone asks people to know that a gradient running to the * bottom right is 135 degrees; a dial lets them point at it. It snaps to the * eight compass angles when the pointer passes close to one (Alt turns the * snap off), and takes the arrow keys, Shift for steps of fifteen. * * `done` is false while the dial is turned and true on the value it is let go * on, so the turn is one undo step (usePaintChannel.ts). */ export function AngleDial({ value, onChange, label, }: { value: number; onChange: (angle: number, done: boolean) => void; label: string; }) { const dial = useRef(null); const dragging = useRef(false); const last = useRef(value); const read = (e: PointerEvent) => { const box = dial.current?.getBoundingClientRect(); if (!box) return last.current; return dialAngle(e.clientX - (box.left + box.width / 2), e.clientY - (box.top + box.height / 2), e.altKey); }; const onPointerDown = (e: PointerEvent) => { if (e.button !== 0) return; e.preventDefault(); e.currentTarget.setPointerCapture?.(e.pointerId); dragging.current = true; last.current = read(e); onChange(last.current, false); }; const onPointerMove = (e: PointerEvent) => { if (!dragging.current) return; const a = read(e); if (a === last.current) return; last.current = a; onChange(a, false); }; const onPointerUp = () => { if (!dragging.current) return; dragging.current = false; onChange(last.current, true); }; const onKeyDown = (e: KeyboardEvent) => { const step = e.shiftKey ? 15 : 1; const by = e.key === 'ArrowRight' || e.key === 'ArrowUp' ? step : e.key === 'ArrowLeft' || e.key === 'ArrowDown' ? -step : 0; if (!by) return; e.preventDefault(); last.current = normAngle(value + by); onChange(last.current, false); }; const onKeyUp = (e: KeyboardEvent) => { if (e.key.startsWith('Arrow')) onChange(last.current, true); }; return (
); }