import { useRef } from 'react'; import type { KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent } from 'react'; import type { BeatPoint } from '../domain/sequencer'; import { PLAYBACK_SUBSTEPS_PER_STEP, STEPS_PER_BEAT, } from '../domain/sequencer'; import { getPointForStep, getLoopPositionPercent, getSoundOrbitRadius, getStepFromPosition, ORBIT_RADIUS, SVG_CENTER, SVG_SIZE, getStackedPosition, wrapStepPosition, } from '../domain/timeline'; import { INSTRUMENT_BY_ID } from '../domain/instruments'; import type { InstrumentId } from '../domain/instruments'; interface CircularTimelineProps { beats: BeatPoint[]; soundIds: readonly InstrumentId[]; colorBySoundId: Partial>; currentStep: number; totalSteps: number; focusedSoundId: InstrumentId | null; selectedBeatId: string | null; onMoveBeat: (beatId: string, step: number) => void; onSelectBeat: (beatId: string) => void; onSelectSound: (soundId: InstrumentId) => void; } function getPointerCoordinates(event: ReactPointerEvent) { const bounds = event.currentTarget.getBoundingClientRect(); return { x: ((event.clientX - bounds.left) / bounds.width) * SVG_SIZE, y: ((event.clientY - bounds.top) / bounds.height) * SVG_SIZE, }; } export function CircularTimeline({ beats, soundIds, colorBySoundId, currentStep, totalSteps, focusedSoundId, selectedBeatId, onMoveBeat, onSelectBeat, onSelectSound, }: CircularTimelineProps) { const draggingBeatIdRef = useRef(null); const selectedBeatSoundId = beats.find((beat) => beat.id === selectedBeatId)?.soundId; const activeSoundId = selectedBeatSoundId ?? focusedSoundId; const orderedSoundIds = activeSoundId ? [ ...soundIds.filter((soundId) => soundId === activeSoundId), ...soundIds.filter((soundId) => soundId !== activeSoundId), ] : soundIds; function handlePointerDown(event: ReactPointerEvent, beatId: string): void { event.preventDefault(); event.stopPropagation(); draggingBeatIdRef.current = beatId; onSelectBeat(beatId); event.currentTarget.setPointerCapture(event.pointerId); } function handlePointerMove(event: ReactPointerEvent): void { const beatId = draggingBeatIdRef.current; if (!beatId) return; const { x, y } = getPointerCoordinates(event); onMoveBeat(beatId, getStepFromPosition(x, y, SVG_CENTER, totalSteps)); } function handleMarkerKeyDown(event: ReactKeyboardEvent, beat: BeatPoint): void { let nextStep: number | null = null; const stepSize = event.shiftKey ? 1 : 1 / PLAYBACK_SUBSTEPS_PER_STEP; if (event.key === 'ArrowRight' || event.key === 'ArrowDown') { nextStep = wrapStepPosition(beat.step + stepSize, totalSteps); } if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') { nextStep = wrapStepPosition(beat.step - stepSize, totalSteps); } if (nextStep === null) return; event.preventDefault(); onSelectBeat(beat.id); onMoveBeat(beat.id, nextStep); } function handleOrbitKeyDown( event: ReactKeyboardEvent, soundId: InstrumentId, ): void { if (event.key !== 'Enter' && event.key !== ' ') return; event.preventDefault(); onSelectSound(soundId); } return ( { draggingBeatIdRef.current = null; }} onPointerCancel={() => { draggingBeatIdRef.current = null; }} > {orderedSoundIds.map((soundId, soundIndex) => { const focused = soundId === activeSoundId; return ( ); })} {Array.from({ length: totalSteps * PLAYBACK_SUBSTEPS_PER_STEP }, (_, tick) => { const step = tick / PLAYBACK_SUBSTEPS_PER_STEP; const major = tick % (STEPS_PER_BEAT * PLAYBACK_SUBSTEPS_PER_STEP) === 0; const fullStep = tick % PLAYBACK_SUBSTEPS_PER_STEP === 0; const inner = getPointForStep(step, ORBIT_RADIUS + 11, SVG_CENTER, totalSteps); const outer = getPointForStep( step, ORBIT_RADIUS + (major ? 24 : fullStep ? 18 : 14), SVG_CENTER, totalSteps, ); const label = getPointForStep(step, ORBIT_RADIUS + 53, SVG_CENTER, totalSteps); return ( {major && ( {String(Math.floor(step + 1)).padStart(2, '0')} )} ); })} {orderedSoundIds.map((soundId, soundIndex) => ( onSelectSound(soundId)} onKeyDown={(event) => handleOrbitKeyDown(event, soundId)} onFocus={() => onSelectSound(soundId)} /> ))} {currentStep >= 0 && (() => { const cursor = getPointForStep(currentStep, ORBIT_RADIUS + 1, SVG_CENTER, totalSteps); return ( ); })()} {beats.map((beat) => { const instrument = INSTRUMENT_BY_ID[beat.soundId]; const soundIndex = orderedSoundIds.indexOf(beat.soundId); const point = getStackedPosition( beat, beats, totalSteps, getSoundOrbitRadius(soundIndex, orderedSoundIds.length), ); const selected = selectedBeatId === beat.id; return ( handlePointerDown(event, beat.id)} onClick={() => onSelectBeat(beat.id)} onKeyDown={(event) => handleMarkerKeyDown(event, beat)} onFocus={() => onSelectBeat(beat.id)} > ); })} ); }