All files / src/packlets/editor-core/slices level-columns-slice.ts

90.9% Statements 20/22
50% Branches 2/4
100% Functions 6/6
100% Lines 19/19

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                      1x     51x   51x 107x   51x 51x     51x 5x         107x 107x   107x 107x   107x 19x 19x 19x 152x                 2x                     107x      
import { Slice } from "../slice";
import type { EditorContext } from "../editor-context";
import { ColumnsSlice } from "./columns-slice";
import { ChartSlice } from "./chart-slice";
import { LevelSlice } from "./level-slice";
import { EVENT, NOTE, LEVEL_REF, CHART_REF } from "../components";
import { getGameModeLayout } from "../lane-layouts";
import { EntityBuilder } from "../../entity-manager";
import type { ColumnDefinition } from "../types";
 
export class LevelColumnsSlice extends Slice {
  static readonly sliceKey = "level-columns";
 
  constructor(ctx: EditorContext) {
    super(ctx);
 
    const columns = ctx.get(ColumnsSlice);
    columns.registerColumnProvider(2, () => this.getColumns());
 
    ctx.get(ChartSlice).$selectedChartId.subscribe(() => {
      columns.refreshColumns();
    });
 
    ctx.get(LevelSlice).onLevelsChanged(() => {
      columns.refreshColumns();
    });
  }
 
  getColumns(): ColumnDefinition[] {
    const chartId = this.ctx.get(ChartSlice).$selectedChartId.get();
    Iif (!chartId) return [];
 
    const visibleLevels = this.ctx.get(LevelSlice).getVisibleLevels(chartId);
    const defs: ColumnDefinition[] = [];
 
    for (const level of visibleLevels) {
      const layout = getGameModeLayout(level.mode);
      Iif (!layout) continue;
      for (const lane of layout.lanes) {
        defs.push({
          id: `level-${level.id}-lane-${lane.laneIndex}`,
          title: lane.name,
          width: lane.width,
          backgroundColor: lane.backgroundColor,
          noteColor: lane.noteColor,
          levelId: level.id,
          laneIndex: lane.laneIndex,
          placementHandler: (pulse) => {
            return new EntityBuilder()
              .with(EVENT, { y: pulse })
              .with(NOTE, { lane: lane.laneIndex })
              .with(LEVEL_REF, { levelId: level.id })
              .with(CHART_REF, { chartId })
              .build();
          },
        });
      }
    }
 
    return defs;
  }
}