All files / web/src/components/toys/euclid/engine tickAnimations.ts

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import type { RAFContext } from './rafContext'
import { tickMacroAnimation } from './macroExecution'
import { replayConstruction } from './replayConstruction'
import { mergeProofFacts } from './factStore'
import { rotatePoint } from './viewportMath'
import { getPoint, getAllPoints } from './constructionState'
import type { ConstructionElement } from '../types'
import { triangleCentroid } from './superpositionMath'

/**
 * Tick all running animations: macro reveal, ceremony, relocate-point.
 * Returns true if any animation is in progress (needs continued drawing).
 */
export function tickAnimations(ctx: RAFContext): boolean {
  let animating = false

  // ── Tick macro animation ──
  const macroAnim = ctx.macroAnimationRef.current
  if (macroAnim && macroAnim.revealedCount < macroAnim.elements.length) {
    const newCount = tickMacroAnimation(macroAnim)
    if (newCount !== macroAnim.revealedCount) {
      macroAnim.revealedCount = newCount
      ctx.needsDrawRef.current = true
    }
    if (newCount >= macroAnim.elements.length) {
      ctx.macroAnimationRef.current = null
    }
    animating = true
  }

  // ── Tick macro reveal ceremony ──
  const ceremony = ctx.macroRevealRef.current
  if (ceremony) {
    const now = performance.now()
    const cdbg = ctx.ceremonyDebugRef.current
    const speed = Math.max(0.01, cdbg.speedMultiplier)
    if (!cdbg.paused && ceremony.revealed < ceremony.sequence.length) {
      // Still revealing groups — check if the next one is due
      const entry = ceremony.sequence[ceremony.revealed]
      if (now - ceremony.lastRevealMs >= entry.msDelay / speed) {
        ceremony.revealed++
        ceremony.lastRevealMs = now
        ctx.needsDrawRef.current = true
        // Start draw animations for each element in this newly revealed group
        const revealedEntry = ceremony.sequence[ceremony.revealed - 1]
        const layer = ctx.ghostLayersRef.current.find(
          (gl) => `${gl.atStep}:${gl.depth}` === revealedEntry.layerKey
        )
        if (layer?.revealGroups) {
          const group = layer.revealGroups[revealedEntry.groupIndex - 1]
          if (group) {
            for (const idx of group) {
              const el = layer.elements[idx]
              if (!el) continue
              const baseDurationMs = el.kind === 'circle' ? 700 : el.kind === 'segment' ? 400 : 0
              ceremony.elementAnims.set(`${revealedEntry.layerKey}:${idx}`, {
                startMs: now,
                durationMs: baseDurationMs / speed,
              })
            }
          }
        }
        if (ceremony.revealed >= ceremony.sequence.length) {
          ceremony.allShownMs = now
        }
      } else {
        // Not yet due — keep animating so we check again next frame
        ctx.needsDrawRef.current = true
      }
    } else if (!cdbg.paused && ceremony.allShownMs !== null) {
      // All groups shown — fire narration once, then advance step after delay
      if (!ceremony.narrationFired) {
        ceremony.narrationFired = true
        if (ceremony.narrationText) {
          ctx.sayMacroRevealRef.current({
            say: { en: ceremony.narrationText },
            tone: 'tutorial-instruction',
          })
        }
      }
      if (now - ceremony.allShownMs >= ceremony.postNarrationDelayMs / speed) {
        ceremony.advanceStep()
        ctx.macroRevealRef.current = null
      } else {
        ctx.needsDrawRef.current = true
      }
    }
    // When paused, keep drawing so ghost opacity lerps and the canvas stays live
    if (cdbg.paused) {
      ctx.needsDrawRef.current = true
    }
    animating = true
  }

  // ── Tick relocate-point animation ──
  const relocAnim = ctx.relocatePointAnimRef.current
  if (relocAnim) {
    const now = performance.now()
    const rawT = Math.min(1, (now - relocAnim.startTime) / relocAnim.durationMs)
    const easedT = 1 - (1 - rawT) * (1 - rawT) // ease-out quadratic

    // Build interpolated actions
    const interpX = relocAnim.fromX + (relocAnim.toX - relocAnim.fromX) * easedT
    const interpY = relocAnim.fromY + (relocAnim.toY - relocAnim.fromY) * easedT
    const interpActions = ctx.postCompletionActionsRef.current.map((a, i) =>
      i === relocAnim.actionIndex && a.type === 'free-point' ? { ...a, x: interpX, y: interpY } : a
    )

    // Full replay with interpolated coordinates (same pattern as drag/wiggle)
    const prop = ctx.propositionRef.current
    const result = replayConstruction(
      prop.givenElements,
      prop.steps,
      prop,
      interpActions,
      ctx.stepDataRef.current
    )
    ctx.constructionRef.current = {
      ...result.state,
      elements: [...result.state.elements, ...relocAnim.untrackedElements],
      nextLabelIndex: Math.max(
        result.state.nextLabelIndex,
        ctx.constructionRef.current.nextLabelIndex
      ),
      nextColorIndex: Math.max(
        result.state.nextColorIndex,
        ctx.constructionRef.current.nextColorIndex
      ),
    }
    ctx.candidatesRef.current = result.candidates
    ctx.ghostLayersRef.current = result.ghostLayers
    ctx.factStoreRef.current = result.factStore
    mergeProofFacts(ctx.factStoreRef.current, ctx.proofFactsRef.current)
    ctx.needsDrawRef.current = true

    if (rawT >= 1) {
      // Animation complete — finalize state and notify ledger
      // Don't overwrite proofFactsRef — it already has author-declared facts
      // that the replay doesn't know about.
      ctx.eventBusRef.current.emit({ action: 'revert', shouldPrompt: false, reset: true })
      ctx.relocatePointAnimRef.current = null
    }
    animating = true
  }

  // ── Tick correction animation ──
  const correction = ctx.correctionRef.current
  if (correction?.active) {
    const t = Math.min(1, (performance.now() - correction.startTime) / correction.duration)
    if (t >= 1) {
      // Finalize: apply rotation via replay for geometric consistency
      ctx.correctionRef.current = null
      ctx.correctionActiveRef.current = false
      ctx.setIsCorrectionActive(false)
      if (ctx.propositionRef.current.draggablePointIds) {
        ctx.setActiveTool('move')
        ctx.activeToolRef.current = 'move'
      }
      const prop = ctx.propositionRef.current
      const center = correction.center
      const angleFinal = correction.toAngle
      let givenElements: ConstructionElement[]
      if (prop.computeGivenElements) {
        const positions = new Map<string, { x: number; y: number }>()
        for (const el of ctx.constructionRef.current.elements) {
          if (el.kind === 'point' && el.origin === 'given') {
            const rotated = rotatePoint({ x: el.x, y: el.y }, center, angleFinal)
            positions.set(el.id, rotated)
          }
        }
        const pA = positions.get('pt-A')
        const pB = positions.get('pt-B')
        if (pA && pB && pA.x > pB.x) {
          positions.set('pt-A', pB)
          positions.set('pt-B', pA)
        }
        givenElements = prop.computeGivenElements(positions)
      } else {
        const rotatedPoints = new Map<string, { x: number; y: number }>()
        for (const el of prop.givenElements) {
          if (el.kind === 'point') {
            const rotated = rotatePoint({ x: el.x, y: el.y }, center, angleFinal)
            rotatedPoints.set(el.id, rotated)
          }
        }
        const pA = rotatedPoints.get('pt-A')
        const pB = rotatedPoints.get('pt-B')
        if (pA && pB && pA.x > pB.x) {
          rotatedPoints.set('pt-A', pB)
          rotatedPoints.set('pt-B', pA)
        }
        givenElements = prop.givenElements.map((el) => {
          if (el.kind === 'point' && rotatedPoints.has(el.id)) {
            const rotated = rotatedPoints.get(el.id)!
            return { ...el, x: rotated.x, y: rotated.y }
          }
          return el
        })
      }
      const result = replayConstruction(
        givenElements,
        prop.steps,
        prop,
        ctx.postCompletionActionsRef.current,
        ctx.stepDataRef.current
      )
      ctx.constructionRef.current = result.state
      ctx.factStoreRef.current = result.factStore
      ctx.candidatesRef.current = result.candidates
      ctx.proofFactsRef.current = result.proofFacts
      ctx.setProofFacts(result.proofFacts)
    }
    animating = true
  }

  // ── Tick superposition interaction ──
  const sp = ctx.superpositionPhaseRef.current
  if (sp.tag === 'lifting') {
    const now = performance.now()
    if (now - sp.startTime >= 300) {
      // Transition lifting → dragging
      const state = ctx.constructionRef.current
      const srcVerts = sp.srcTriIds.map((id) => {
        const p = getPoint(state, id)
        return p ? { x: p.x, y: p.y } : { x: 0, y: 0 }
      }) as [{ x: number; y: number }, { x: number; y: number }, { x: number; y: number }]
      const centroid = triangleCentroid(srcVerts[0], srcVerts[1], srcVerts[2])
      ctx.superpositionPhaseRef.current = {
        tag: 'dragging',
        srcTriIds: sp.srcTriIds,
        tgtTriIds: sp.tgtTriIds,
        mapping: sp.mapping,
        cutoutVertices: srcVerts,
        dragAnchor: centroid,
        initialCentroid: centroid,
      }
    }
    animating = true
    ctx.needsDrawRef.current = true
  }
  if (sp.tag === 'flipping') {
    const now = performance.now()
    if (now - sp.startTime >= 800) {
      // Transition flipping → snapping
      ctx.superpositionPhaseRef.current = {
        tag: 'snapping',
        startTime: now,
        fromVertices: sp.postFlipVertices,
        toVertices: sp.postFlipVertices,
        srcTriIds: sp.srcTriIds,
        tgtTriIds: sp.tgtTriIds,
        mapping: sp.mapping,
      }
    }
    animating = true
    ctx.needsDrawRef.current = true
  }
  if (sp.tag === 'snapping') {
    const now = performance.now()
    if (now - sp.startTime >= 200) {
      // Transition snapping → settled
      ctx.superpositionPhaseRef.current = { tag: 'settled' }
      // Fire the settled callback (fact cascade + step advancement)
      ctx.onSuperpositionSettledRef.current?.()
      ctx.onSuperpositionSettledRef.current = null
    }
    animating = true
    ctx.needsDrawRef.current = true
  }
  if (sp.tag === 'settled') {
    // Transition settled → idle (cleanup)
    ctx.superpositionPhaseRef.current = { tag: 'idle' }
  }
  if (sp.tag === 'dragging' || sp.tag === 'mismatched') {
    animating = true
    ctx.needsDrawRef.current = true
  }

  return animating
}