All files / skin-lr2/src/skin elements.ts

61.8% Statements 123/199
39.58% Branches 38/96
100% Functions 18/18
58.69% Lines 108/184

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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495                                                57x                 57x             111x 111x 9x 9x 9x 1x   8x 8x     8x 8x                                       111x       111x 111x 3x 3x 3x     3x 3x     3x 3x     3x 3x                                       111x       111x 111x                                   111x       111x 111x 2x 2x 2x     2x 2x     2x 2x             111x       111x 111x 2x 2x 2x     2x 2x               111x       111x 111x 1x 1x 1x     1x 1x     1x 1x                 111x       111x 111x                                   111x       111x 111x                                       111x                           111x 111x 5x 5x 5x 1x   4x 5x 5x                               111x       111x 111x 3x 3x 3x     3x 3x 3x                             111x       111x 111x                                         111x       111x 111x                                           111x       111x 111x                                             111x       111x 111x                           111x       111x 111x                                   111x       111x       111x                       222x 222x 1332x 1332x 1332x 1332x 1324x   8x       8x                                     222x    
import type { ParseContext, SourceRect } from './parse-context.ts';
import type {
  LR2BarGraphElement,
  LR2BGAElement,
  LR2ButtonElement,
  LR2DestinationRect,
  LR2GaugeChartElement,
  LR2GrooveGaugeElement,
  LR2JudgeLineElement,
  LR2MeasureLineElement,
  LR2MouseCursorElement,
  LR2NowComboElement,
  LR2NumberElement,
  LR2OnMouseElement,
  LR2ReadmeElement,
  LR2ScoreChartElement,
  LR2Skin,
  LR2SliderElement,
  LR2TextElement,
} from './types.ts';
 
// LR2 NOWJUDGE_1P index mapping (per the LR2skin spec): 0 = early POOR (empty-press / kara-poor), 1 = POOR
// (miss / minogashi-poor), 2 = BAD, 3 = GOOD, 4 = GREAT, 5 = PERFECT (= JUST GREAT). Both POOR variants render
// with the same kind.
const NOW_JUDGE_1P_KIND_BY_INDEX: ReadonlyMap<number, keyof LR2Skin['judges']> = new Map([
  [0, 'poor'],
  [1, 'poor'],
  [2, 'bad'],
  [3, 'good'],
  [4, 'great'],
  [5, 'perfect'],
]);
// LR2 NOWCOMBO_1P index mapping. BAD or worse breaks the combo, so only GOOD/GREAT/PERFECT have meaningful entries.
const NOW_COMBO_1P_KIND_BY_INDEX: ReadonlyMap<number, 'good' | 'great' | 'perfect'> = new Map([
  [3, 'good'],
  [4, 'great'],
  [5, 'perfect'],
]);
 
export function createNumberElements(context: ParseContext): LR2NumberElement[] {
  const elements: LR2NumberElement[] = [];
  for (let index = 0; index < context.numberSources.length; index += 1) {
    const entry = context.numberSources[index]!;
    const dstGroup = context.numberDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: {
        imagePath,
        x: entry.source.x,
        y: entry.source.y,
        w: entry.source.w,
        h: entry.source.h,
        divx: entry.source.divx,
        divy: entry.source.divy,
        cycle: entry.source.cycle,
        timer: entry.source.timer,
        num: entry.num,
        alignment: entry.alignment,
        padding: entry.padding,
      },
      destination,
      keyframes: [...dstGroup],
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createNowComboElements(context: ParseContext): LR2NowComboElement[] {
  const elements: LR2NowComboElement[] = [];
  for (let index = 0; index < context.nowComboSources.length; index += 1) {
    const entry = context.nowComboSources[index]!;
    const dstGroup = context.nowComboDstGroups[index];
    Iif (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const kind = NOW_COMBO_1P_KIND_BY_INDEX.get(entry.index);
    Iif (!kind) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: {
        imagePath,
        x: entry.source.x,
        y: entry.source.y,
        w: entry.source.w,
        h: entry.source.h,
        divx: entry.source.divx,
        divy: entry.source.divy,
        cycle: entry.source.cycle,
        timer: entry.source.timer,
        alignment: entry.alignment,
        padding: entry.padding,
      },
      destination,
      keyframes: [...dstGroup],
      kind,
      side: entry.side,
    });
  }
  return elements;
}
 
export function createJudgeLineElements(context: ParseContext): LR2JudgeLineElement[] {
  const elements: LR2JudgeLineElement[] = [];
  for (let index = 0; index < context.judgeLineSources.length; index += 1) {
    const entry = context.judgeLineSources[index]!;
    const dstGroup = context.judgeLineDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      index: entry.index,
    });
  }
  return elements;
}
 
export function createMeasureLineElements(context: ParseContext): LR2MeasureLineElement[] {
  const elements: LR2MeasureLineElement[] = [];
  for (let index = 0; index < context.measureLineSources.length; index += 1) {
    const entry = context.measureLineSources[index]!;
    const dstGroup = context.measureLineDstGroups[index];
    Iif (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      index: entry.index,
    });
  }
  return elements;
}
 
export function createBGAElements(context: ParseContext): LR2BGAElement[] {
  const elements: LR2BGAElement[] = [];
  for (let index = 0; index < context.bgaSources.length; index += 1) {
    const entry = context.bgaSources[index]!;
    const dstGroup = context.bgaDstGroups[index];
    Iif (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      destination,
      keyframes: [...dstGroup],
      noBase: entry.noBase,
      noLayer: entry.noLayer,
      noPoor: entry.noPoor,
    });
  }
  return elements;
}
 
export function createGrooveGaugeElements(context: ParseContext): LR2GrooveGaugeElement[] {
  const elements: LR2GrooveGaugeElement[] = [];
  for (let index = 0; index < context.grooveGaugeSources.length; index += 1) {
    const entry = context.grooveGaugeSources[index]!;
    const dstGroup = context.grooveGaugeDstGroups[index];
    Iif (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    Iif (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      index: entry.index,
      addX: entry.addX,
      addY: entry.addY,
    });
  }
  return elements;
}
 
export function createTextElements(context: ParseContext): LR2TextElement[] {
  const elements: LR2TextElement[] = [];
  for (let index = 0; index < context.textSources.length; index += 1) {
    const entry = context.textSources[index]!;
    const dstGroup = context.textDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      font: entry.font,
      st: entry.st,
      alignment: entry.alignment,
      edit: entry.edit,
      panel: entry.panel,
      destination,
      keyframes: [...dstGroup],
      declarationOrder: entry.declarationOrder,
    });
  }
  return elements;
}
 
export function createBarGraphElements(context: ParseContext): LR2BarGraphElement[] {
  const elements: LR2BarGraphElement[] = [];
  for (let index = 0; index < context.bargraphSources.length; index += 1) {
    const entry = context.bargraphSources[index]!;
    const dstGroup = context.bargraphDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      type: entry.type,
      muki: entry.muki,
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
/**
 * Finalizes `#SRC_GAUGECHART_*` / `#DST_*` pairs into the runtime-friendly element shape. Mirrors
 * `createSliderElements`' structure so the failure modes (unmatched DST, missing image) surface the same way: silently
 * drop the entry, no throw.
 *
 * `imagePath` resolution differs slightly from sliders: gauge-chart SRCs in real LR2 default skins use `gr = 0` (the
 * main atlas), but the **tint** drives the line color rather than the sampled pixel — even so we still pin a path for
 * `loadSkinAssetTexture` fallback consumers, falling back to the first declared `#IMAGE` when `gr` indexes outside the
 * table.
 */
export function createGaugeChartElements(context: ParseContext): LR2GaugeChartElement[] {
  const elements: LR2GaugeChartElement[] = [];
  for (let index = 0; index < context.gaugeChartSources.length; index += 1) {
    const entry = context.gaugeChartSources[index]!;
    const dstGroup = context.gaugeChartDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr] ?? context.imagePaths[0] ?? '';
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: {
        ...entry.source,
        imagePath,
        index: entry.index,
        fieldWidth: entry.fieldWidth,
        fieldHeight: entry.fieldHeight,
        start: entry.start,
        end: entry.end,
      },
      destination,
      keyframes: [...dstGroup],
      side: entry.side,
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createScoreChartElements(context: ParseContext): LR2ScoreChartElement[] {
  const elements: LR2ScoreChartElement[] = [];
  for (let index = 0; index < context.scoreChartSources.length; index += 1) {
    const entry = context.scoreChartSources[index]!;
    const dstGroup = context.scoreChartDstGroups[index];
    Iif (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr] ?? context.imagePaths[0] ?? '';
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: {
        ...entry.source,
        imagePath,
        index: entry.index,
        fieldWidth: entry.fieldWidth,
        fieldHeight: entry.fieldHeight,
        start: entry.start,
        end: entry.end,
      },
      destination,
      keyframes: [...dstGroup],
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createSliderElements(context: ParseContext): LR2SliderElement[] {
  const elements: LR2SliderElement[] = [];
  for (let index = 0; index < context.sliderSources.length; index += 1) {
    const entry = context.sliderSources[index]!;
    const dstGroup = context.sliderDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      type: entry.type,
      muki: entry.muki,
      range: entry.range,
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createButtonElements(context: ParseContext): LR2ButtonElement[] {
  const elements: LR2ButtonElement[] = [];
  for (let index = 0; index < context.buttonSources.length; index += 1) {
    const entry = context.buttonSources[index]!;
    const dstGroup = context.buttonDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      type: entry.type,
      click: entry.click,
      panel: entry.panel,
      plusOnly: entry.plusOnly,
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createOnMouseElements(context: ParseContext): LR2OnMouseElement[] {
  const elements: LR2OnMouseElement[] = [];
  for (let index = 0; index < context.onMouseSources.length; index += 1) {
    const entry = context.onMouseSources[index]!;
    const dstGroup = context.onMouseDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[entry.source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...entry.source, imagePath },
      destination,
      keyframes: [...dstGroup],
      panel: entry.panel,
      hitOffsetX: entry.hitOffsetX,
      hitOffsetY: entry.hitOffsetY,
      hitWidth: entry.hitWidth,
      hitHeight: entry.hitHeight,
      declarationOrder: entry.source.declarationOrder,
    });
  }
  return elements;
}
 
export function createReadmeElements(context: ParseContext): LR2ReadmeElement[] {
  const elements: LR2ReadmeElement[] = [];
  for (let index = 0; index < context.readmeSources.length; index += 1) {
    const entry = context.readmeSources[index]!;
    const dstGroup = context.readmeDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      font: entry.font,
      lineSpacing: entry.lineSpacing,
      destination,
      keyframes: [...dstGroup],
    });
  }
  return elements;
}
 
export function createMouseCursorElements(context: ParseContext): LR2MouseCursorElement[] {
  const elements: LR2MouseCursorElement[] = [];
  for (let index = 0; index < context.mouseCursorSources.length; index += 1) {
    const source = context.mouseCursorSources[index]!;
    const dstGroup = context.mouseCursorDstGroups[index];
    if (!dstGroup || dstGroup.length === 0) {
      continue;
    }
    const imagePath = context.imagePaths[source.gr];
    if (!imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    elements.push({
      source: { ...source, imagePath },
      destination,
      keyframes: [...dstGroup],
      declarationOrder: source.declarationOrder,
    });
  }
  return elements;
}
 
export function createJudgeElements(context: ParseContext): LR2Skin['judges'] {
  return buildJudgeMap(context.nowJudge1PSources, context.nowJudge1PDstGroups, context);
}
 
export function createJudge2PElements(context: ParseContext): LR2Skin['judges2P'] {
  return buildJudgeMap(context.nowJudge2PSources, context.nowJudge2PDstGroups, context);
}
 
/**
 * Shared body for the per-side judge factories. Walks the kind map and emits an `LR2ImageElement[]` per verdict (or
 * `[]` for verdicts the skin omitted on this side). Kept as a helper so the 1P / 2P paths can't drift apart silently.
 */
function buildJudgeMap(
  sources: SourceRect[],
  dstGroups: LR2DestinationRect[][],
  context: ParseContext,
): LR2Skin['judges'] {
  const judges: LR2Skin['judges'] = {};
  for (const [id, kind] of NOW_JUDGE_1P_KIND_BY_INDEX) {
    const source = sources[id];
    const dstGroup = dstGroups[id];
    const imagePath = source ? context.imagePaths[source.gr] : undefined;
    if (!source || !dstGroup || dstGroup.length === 0 || !imagePath) {
      continue;
    }
    const destination = dstGroup[dstGroup.length - 1]!;
    // 0 (early POOR) and 1 (regular POOR) both map to 'poor'; the second one wins by virtue of being processed later.
    // Keep the FULL source rect (w/h spanning all divx*divy cells) so the renderer can cycle frames at runtime per
    // `cycle`.
    judges[kind] = [
      {
        source: {
          imagePath,
          x: source.x,
          y: source.y,
          w: source.w,
          h: source.h,
          divx: source.divx,
          divy: source.divy,
          cycle: source.cycle,
          timer: source.timer,
        },
        destination,
        keyframes: [...dstGroup],
        declarationOrder: source.declarationOrder,
      },
    ];
  }
  return judges;
}