All files / skin-lr2/src/skin bar-layout.ts

63.79% Statements 37/58
54.76% Branches 23/42
62.5% Functions 5/8
65.45% Lines 36/55

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                              111x 3x 3x     3x     111x 111x 111x 3x 3x 3x     3x                   111x 1x 1x                 111x 3x 3x     3x             3x     111x 2x 2x     2x     111x 2x 2x     2x     111x 111x 111x         111x                         111x             111x     111x         111x           111x                                                            
import type { ParseContext } from './parse-context.ts';
import type {
  LR2BarBodySlot,
  LR2BarBodySource,
  LR2BarFlashElement,
  LR2BarLampSource,
  LR2BarLayout,
  LR2BarLevelSource,
  LR2BarRankSource,
  LR2BarRivalSource,
  LR2BarTitleElement,
  LR2NumberSourceRect,
} from './types.ts';
 
export function createBarLayout(context: ParseContext): LR2BarLayout {
  const bodies: LR2BarBodySource[] = context.barBodySources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      return [];
    }
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
 
  const slotCount = Math.max(context.barBodyOffDstGroups.length, context.barBodyOnDstGroups.length);
  const slots: LR2BarBodySlot[] = [];
  for (let index = 0; index < slotCount; index += 1) {
    const offGroup = context.barBodyOffDstGroups[index];
    const onGroup = context.barBodyOnDstGroups[index];
    Iif ((!offGroup || offGroup.length === 0) && (!onGroup || onGroup.length === 0)) {
      continue;
    }
    slots.push({
      index,
      off: offGroup && offGroup.length > 0 ? offGroup[offGroup.length - 1] : undefined,
      offKeyframes: offGroup ? [...offGroup] : [],
      on: onGroup && onGroup.length > 0 ? onGroup[onGroup.length - 1] : undefined,
      onKeyframes: onGroup ? [...onGroup] : [],
    });
  }
 
  let title: LR2BarTitleElement | undefined;
  if (context.barTitleSource && context.barTitleDst.length > 0) {
    const dstGroup = context.barTitleDst;
    title = {
      font: context.barTitleSource.font,
      destination: dstGroup[dstGroup.length - 1]!,
      keyframes: [...dstGroup],
    };
  }
 
  // BAR_LEVEL: keep the alignment / padding from each per-difficulty entry so the renderer can use
  // `renderNumberElement` directly.
  const levels: LR2BarLevelSource[] = context.barLevelSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      return [];
    }
    const numberSource: LR2NumberSourceRect = {
      ...entry.source,
      imagePath,
      num: 0, // unused — BAR_LEVEL pulls its value from the song, not the LR2 num field
      alignment: entry.alignment,
      padding: entry.padding,
    };
    return [{ kind: entry.kind, source: numberSource }];
  });
 
  const lamps: LR2BarLampSource[] = context.barLampSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      return [];
    }
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
 
  const ranks: LR2BarRankSource[] = context.barRankSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      return [];
    }
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
 
  const levelDestination = context.barLevelDst.at(-1);
  const lampDestination = context.barLampDst.at(-1);
  const rankDestination = context.barRankDst.at(-1);
 
  // BAR_FLASH — focused-bar overlay. Spec only allows one, so we pick the latest source / DST chain and resolve its
  // image path.
  let flash: LR2BarFlashElement | undefined;
  Iif (context.barFlashSource && context.barFlashDst.length > 0) {
    const imagePath = context.imagePaths[context.barFlashSource.gr];
    if (imagePath) {
      const dstGroup = context.barFlashDst;
      flash = {
        source: { ...context.barFlashSource, imagePath },
        destination: dstGroup[dstGroup.length - 1]!,
        keyframes: [...dstGroup],
      };
    }
  }
 
  // BAR_RIVAL — rival-mode WIN/LOSE/DRAW per-bar overlay.
  const rivalIndicators: LR2BarRivalSource[] = context.barRivalSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      return [];
    }
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
  const rivalDestination = context.barRivalDst.at(-1);
 
  // BAR_MY_LAMP / BAR_RIVAL_LAMP — rival-folder lamp variants.
  const myLamps: LR2BarLampSource[] = context.barMyLampSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) return [];
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
  const rivalLampSprites: LR2BarLampSource[] = context.barRivalLampSources.flatMap((entry) => {
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) return [];
    return [{ kind: entry.kind, source: { ...entry.source, imagePath } }];
  });
 
  return {
    bodies,
    slots,
    center: context.barCenter,
    available: context.barAvailable,
    title,
    levels,
    levelDestination,
    levelKeyframes: [...context.barLevelDst],
    lamps,
    lampDestination,
    lampKeyframes: [...context.barLampDst],
    ranks,
    rankDestination,
    rankKeyframes: [...context.barRankDst],
    flash,
    rivalIndicators,
    rivalDestination,
    rivalKeyframes: [...context.barRivalDst],
    rivalLamps: {
      myLamps,
      myLampDestination: context.barMyLampDst.at(-1),
      myLampKeyframes: [...context.barMyLampDst],
      rivalLamps: rivalLampSprites,
      rivalLampDestination: context.barRivalLampDst.at(-1),
      rivalLampKeyframes: [...context.barRivalLampDst],
    },
    declarationOrder: context.barLayoutDeclarationOrder,
  };
}