import { getNavigationFlowFrame, toGridCellStateMap, type NavigationTransitionDirection, type NavigationTransitionPhase, } from "./gridAnimations.ts"; import { deriveNavigationToneLevels, getNavigationConvergeFrame, getNavigationExpandFrame, getNavigationNodeFlickerFrame, getNavigationOriginArrivalFrame, type NavigationToneLevels, } from "./navigationTransitions.ts"; import type { HexCell } from "./hex.ts"; export type NavigationAnimationName = | "flow" | "converge" | "expand" | "originArrival" | "nodeFlicker"; export interface TopologySceneNavigationPlaybackOptions { animation?: NavigationAnimationName; phase: NavigationTransitionPhase; direction?: NavigationTransitionDirection; duration?: number; intensity?: number; origin?: { q: number; r: number }; maxRadius?: number; trail?: number; seed?: string | number; tones?: Partial; } export interface TopologySceneAnimationAdapterOptions { scene: HTMLElement; reducedMotion?: boolean | (() => boolean); } export interface TopologySceneAnimationAdapter { playNavigation(options: TopologySceneNavigationPlaybackOptions): Promise; setOccupantsVisible(visible: boolean): void; setConnectorsVisible(visible: boolean): void; destroy(): void; } interface ElementSnapshot { className: string; state: string | null; signalType: string | null; intensity: string; } type LayerSnapshot = Map; function readCells(scene: HTMLElement): HexCell[] { const cells = new Map(); scene.querySelectorAll("[data-cell-id]").forEach((element) => { const id = element.dataset.cellId; if (!id || cells.has(id)) return; cells.set(id, { id, q: Number(element.dataset.q ?? 0), r: Number(element.dataset.r ?? 0), center: { x: Number(element.dataset.cx ?? 0), y: Number(element.dataset.cy ?? 0), }, path: "", }); }); return [...cells.values()]; } function isReducedMotion(value: TopologySceneAnimationAdapterOptions["reducedMotion"]) { return typeof value === "function" ? value() : value === true; } export function createTopologySceneAnimationAdapter({ scene, reducedMotion = false, }: TopologySceneAnimationAdapterOptions): TopologySceneAnimationAdapter { const elements = new Map(); const occupantElements = new Map(); scene.querySelectorAll("[data-cell-id]").forEach((element) => { const id = element.dataset.cellId; if (!id) return; const group = elements.get(id) ?? []; group.push(element); elements.set(id, group); }); const cells = readCells(scene); scene.querySelectorAll(".grid-occupants [data-cell-id]").forEach((element) => { const id = element.dataset.cellId; if (!id) return; const group = occupantElements.get(id) ?? []; group.push(element); occupantElements.set(id, group); }); const snapshots = new Map(); const layerSnapshots: LayerSnapshot = new Map(); let destroyed = false; let playbackId = 0; const snapshot = () => { elements.forEach((group) => group.forEach((element) => { if (snapshots.has(element)) return; snapshots.set(element, { className: element.getAttribute("class") ?? "", state: element.getAttribute("data-cell-state"), signalType: element.getAttribute("data-cell-signal-type"), intensity: element.style.getPropertyValue("--cell-intensity"), }); })); }; const restore = () => { snapshots.forEach((value, element) => { element.setAttribute("class", value.className); if (value.state === null) element.removeAttribute("data-cell-state"); else element.setAttribute("data-cell-state", value.state); if (value.signalType === null) element.removeAttribute("data-cell-signal-type"); else element.setAttribute("data-cell-signal-type", value.signalType); if (value.intensity) element.style.setProperty("--cell-intensity", value.intensity); else element.style.removeProperty("--cell-intensity"); }); snapshots.clear(); layerSnapshots.forEach((opacity, layer) => { layer.style.opacity = opacity; }); layerSnapshots.clear(); }; const applyFrame = (frame: number, options: TopologySceneNavigationPlaybackOptions) => { const animation = options.animation ?? "flow"; const common = { cells, frame, phase: options.phase, direction: options.direction ?? "forward", duration: options.duration, intensity: options.intensity, origin: options.origin, maxRadius: options.maxRadius, trail: options.trail, }; const states = animation === "converge" ? getNavigationConvergeFrame(common) : animation === "expand" ? getNavigationExpandFrame(common) : animation === "originArrival" ? getNavigationOriginArrivalFrame(common) : animation === "nodeFlicker" ? getNavigationNodeFlickerFrame({ nodes: [...occupantElements.keys()].map((id) => ({ id, active: true })), frame, phase: options.phase, duration: options.duration, intensity: options.intensity, seed: options.seed, tones: options.tones ?? deriveNavigationToneLevels(), }) : getNavigationFlowFrame(common); const stateMap = toGridCellStateMap(states); const targetElements = animation === "nodeFlicker" ? occupantElements : elements; targetElements.forEach((group, id) => { const state = stateMap[id] ?? { state: "idle", intensity: 0.1, type: "" }; group.forEach((element) => { element.classList.toggle("is-active", state.state === "active"); element.classList.toggle("is-trail", state.state === "trail"); element.setAttribute("data-cell-state", state.state); element.setAttribute("data-cell-signal-type", state.type ?? ""); element.style.setProperty("--cell-intensity", String(state.intensity)); }); }); }; const setLayerVisibility = (selector: string, visible: boolean) => { scene.querySelectorAll(selector).forEach((layer) => { if (!layerSnapshots.has(layer)) layerSnapshots.set(layer, layer.style.opacity); layer.style.opacity = visible ? layerSnapshots.get(layer) ?? "" : "0"; }); }; return { playNavigation(options) { const duration = Math.max(0, options.duration ?? (options.phase === "out" ? 240 : 280)); const id = ++playbackId; snapshot(); if (destroyed || isReducedMotion(reducedMotion) || duration === 0) { restore(); return Promise.resolve(); } return new Promise((resolve) => { const startedAt = performance.now(); const render = (now: number) => { if (destroyed || id !== playbackId) { restore(); resolve(); return; } const frame = Math.min(duration, now - startedAt); applyFrame(frame, { ...options, duration }); if (frame < duration) window.requestAnimationFrame(render); else { restore(); resolve(); } }; window.requestAnimationFrame(render); }); }, setOccupantsVisible: (visible) => setLayerVisibility(".grid-occupants", visible), setConnectorsVisible: (visible) => setLayerVisibility(".grid-connectors", visible), destroy() { destroyed = true; playbackId += 1; restore(); }, }; }