Add category rings and beat labels

This commit is contained in:
Ninosaurier committed 2026-11-15 14:35:36 +01:00
1 parent 9d8f277971
commit 4f221ea122
21 files changed
+752 -339

No files matched your search

+82 -46
View File
@@ -8,7 +8,7 @@ import {
import {
getPointForStep,
getLoopPositionPercent,
getSoundOrbitRadius,
getCategoryOrbitRadius,
getStepFromPosition,
ORBIT_RADIUS,
SVG_CENTER,
@@ -16,20 +16,22 @@ import {
getStackedPosition,
wrapStepPosition,
} from '../domain/timeline';
import { INSTRUMENT_BY_ID } from '../domain/instruments';
import type { InstrumentId } from '../domain/instruments';
import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments';
import type { InstrumentCategoryId } from '../domain/instruments';
import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels';
interface CircularTimelineProps {
beats: BeatPoint[];
soundIds: readonly InstrumentId[];
colorBySoundId: Partial<Record<InstrumentId, string>>;
categoryIds: readonly InstrumentCategoryId[];
colorByCategoryId: Partial<Record<InstrumentCategoryId, string>>;
labelByCategoryId: CategoryLabelOverrides;
currentStep: number;
totalSteps: number;
focusedSoundId: InstrumentId | null;
focusedCategoryId: InstrumentCategoryId | null;
selectedBeatId: string | null;
onMoveBeat: (beatId: string, step: number) => void;
onSelectBeat: (beatId: string) => void;
onSelectSound: (soundId: InstrumentId) => void;
onSelectCategory: (categoryId: InstrumentCategoryId) => void;
}
function getPointerCoordinates(event: ReactPointerEvent<SVGSVGElement>) {
@@ -42,25 +44,29 @@ function getPointerCoordinates(event: ReactPointerEvent<SVGSVGElement>) {
export function CircularTimeline({
beats,
soundIds,
colorBySoundId,
categoryIds,
colorByCategoryId,
labelByCategoryId,
currentStep,
totalSteps,
focusedSoundId,
focusedCategoryId,
selectedBeatId,
onMoveBeat,
onSelectBeat,
onSelectSound,
onSelectCategory,
}: CircularTimelineProps) {
const draggingBeatIdRef = useRef<string | null>(null);
const selectedBeatSoundId = beats.find((beat) => beat.id === selectedBeatId)?.soundId;
const activeSoundId = selectedBeatSoundId ?? focusedSoundId;
const orderedSoundIds = activeSoundId
const selectedBeatCategoryId = selectedBeatSoundId
? INSTRUMENT_BY_ID[selectedBeatSoundId].categoryId
: null;
const activeCategoryId = selectedBeatCategoryId ?? focusedCategoryId;
const orderedCategoryIds = activeCategoryId
? [
...soundIds.filter((soundId) => soundId === activeSoundId),
...soundIds.filter((soundId) => soundId !== activeSoundId),
...categoryIds.filter((categoryId) => categoryId === activeCategoryId),
...categoryIds.filter((categoryId) => categoryId !== activeCategoryId),
]
: soundIds;
: categoryIds;
function handlePointerDown(event: ReactPointerEvent<SVGGElement>, beatId: string): void {
event.preventDefault();
@@ -96,11 +102,11 @@ export function CircularTimeline({
function handleOrbitKeyDown(
event: ReactKeyboardEvent<SVGCircleElement>,
soundId: InstrumentId,
categoryId: InstrumentCategoryId,
): void {
if (event.key !== 'Enter' && event.key !== ' ') return;
event.preventDefault();
onSelectSound(soundId);
onSelectCategory(categoryId);
}
return (
@@ -108,25 +114,44 @@ export function CircularTimeline({
className="circular-timeline"
viewBox={`0 0 ${SVG_SIZE} ${SVG_SIZE}`}
role="group"
aria-label={`${totalSteps}-step circular beat timeline with a separate orbit per sound. Drag sound points to move them.`}
aria-label={`${totalSteps}-step circular beat timeline with one orbit per instrument category. Drag beat points to move them.`}
onPointerMove={handlePointerMove}
onPointerUp={() => { draggingBeatIdRef.current = null; }}
onPointerCancel={() => { draggingBeatIdRef.current = null; }}
>
{orderedSoundIds.map((soundId, soundIndex) => (
<circle
key={soundId}
cx={SVG_CENTER}
cy={SVG_CENTER}
r={getSoundOrbitRadius(soundIndex, orderedSoundIds.length)}
className={`timeline-orbit${soundId === activeSoundId ? ' is-focused' : ''}`}
style={{
stroke: colorBySoundId[soundId] ?? '#b8b4e0',
opacity: soundId === activeSoundId ? 0.55 : 0.32,
strokeWidth: soundId === activeSoundId ? 2.2 : 1.5,
}}
/>
))}
{orderedCategoryIds.map((categoryId, categoryIndex) => {
const category = INSTRUMENT_CATEGORY_BY_ID[categoryId];
const color = colorByCategoryId[categoryId] ?? '#b8b4e0';
const radius = getCategoryOrbitRadius(categoryIndex, orderedCategoryIds.length);
const letterPosition = getPointForStep(totalSteps * 0.12, radius, SVG_CENTER, totalSteps);
const focused = categoryId === activeCategoryId;
return (
<g key={categoryId}>
<circle
cx={SVG_CENTER}
cy={SVG_CENTER}
r={radius}
className={`timeline-orbit${focused ? ' is-focused' : ''}`}
style={{
stroke: color,
opacity: focused ? 0.55 : 0.32,
strokeWidth: focused ? 2.2 : 1.5,
}}
/>
<g className="timeline-category-letter" style={{ color }} aria-hidden="true">
<circle cx={letterPosition.x} cy={letterPosition.y} r="9" />
<text
x={letterPosition.x}
y={letterPosition.y}
textAnchor="middle"
dominantBaseline="middle"
>
{category.letter}
</text>
</g>
</g>
);
})}
{Array.from({ length: totalSteps * PLAYBACK_SUBSTEPS_PER_STEP }, (_, tick) => {
const step = tick / PLAYBACK_SUBSTEPS_PER_STEP;
@@ -155,12 +180,12 @@ export function CircularTimeline({
);
})}
{orderedSoundIds.map((soundId, soundIndex) => (
{orderedCategoryIds.map((categoryId, categoryIndex) => (
<circle
key={soundId}
key={categoryId}
cx={SVG_CENTER}
cy={SVG_CENTER}
r={getSoundOrbitRadius(soundIndex, orderedSoundIds.length)}
r={getCategoryOrbitRadius(categoryIndex, orderedCategoryIds.length)}
className="timeline-orbit-hit-area"
fill="none"
stroke="transparent"
@@ -168,11 +193,11 @@ export function CircularTimeline({
pointerEvents="stroke"
role="button"
tabIndex={0}
aria-label={`Select ${INSTRUMENT_BY_ID[soundId].label} ring`}
aria-pressed={soundId === activeSoundId}
onClick={() => onSelectSound(soundId)}
onKeyDown={(event) => handleOrbitKeyDown(event, soundId)}
onFocus={() => onSelectSound(soundId)}
aria-label={`Select ${INSTRUMENT_CATEGORY_BY_ID[categoryId].letter} ${labelByCategoryId[categoryId] ?? INSTRUMENT_CATEGORY_BY_ID[categoryId].label} ring`}
aria-pressed={categoryId === activeCategoryId}
onClick={() => onSelectCategory(categoryId)}
onKeyDown={(event) => handleOrbitKeyDown(event, categoryId)}
onFocus={() => onSelectCategory(categoryId)}
/>
))}
@@ -187,23 +212,24 @@ export function CircularTimeline({
})()}
{beats.map((beat) => {
const sound = INSTRUMENT_BY_ID[beat.soundId];
const soundIndex = orderedSoundIds.indexOf(beat.soundId);
const instrument = INSTRUMENT_BY_ID[beat.soundId];
const category = INSTRUMENT_CATEGORY_BY_ID[instrument.categoryId];
const categoryIndex = orderedCategoryIds.indexOf(instrument.categoryId);
const point = getStackedPosition(
beat,
beats,
totalSteps,
getSoundOrbitRadius(soundIndex, orderedSoundIds.length),
getCategoryOrbitRadius(categoryIndex, orderedCategoryIds.length),
);
const selected = selectedBeatId === beat.id;
return (
<g
key={beat.id}
className={`sound-point${selected ? ' is-selected' : ''}`}
style={{ color: colorBySoundId[beat.soundId] ?? '#f4f1ed' }}
style={{ color: colorByCategoryId[instrument.categoryId] ?? '#f4f1ed' }}
role="button"
tabIndex={0}
aria-label={`${sound.label}, ${getLoopPositionPercent(beat.step, totalSteps)}% around the loop. Drag to move freely, or use arrow keys for small steps.`}
aria-label={`${category.letter} ${labelByCategoryId[category.id] ?? category.label}, beat ${beat.letter}, ${instrument.label}, ${getLoopPositionPercent(beat.step, totalSteps)}% around the loop. Drag to move freely, or use arrow keys for small steps.`}
aria-pressed={selected}
onPointerDown={(event) => handlePointerDown(event, beat.id)}
onClick={() => onSelectBeat(beat.id)}
@@ -212,6 +238,16 @@ export function CircularTimeline({
>
<circle cx={point.x} cy={point.y} r="19" className="sound-point-hit-area" />
<circle cx={point.x} cy={point.y} r="10" className="sound-point-face" />
<text
x={point.x}
y={point.y}
className="sound-point-letter"
textAnchor="middle"
dominantBaseline="middle"
aria-hidden="true"
>
{beat.letter}
</text>
</g>
);
})}