All files / src/packlets/editor-core editor-controller.ts

85% Statements 68/80
100% Branches 3/3
79.16% Functions 38/48
84.81% Lines 67/79

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                                                                                51x   51x             8x       2x               15x       3x       35x       210x               5x       5x       1x               204x       139x       17x       51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x   51x 87x 87x     51x 10x     51x 10x     51x           51x 107x     51x               2x       1x       2x               36x       51x               37x       12x       3x       10x       4x       1x       1x               3x       4x       1x       4x       51x 51x 51x 21x               17x               5x       1x      
/**
 * @packageDocumentation
 *
 * Central state brain for the beatmap editor. React is a dumb view layer;
 * this packlet owns all editor-relevant state, model data, and interaction logic.
 */
 
import { createNanoEvents } from "nanoevents";
import type { Emitter } from "nanoevents";
import { EntityManager } from "../entity-manager";
import type { TimingEngine } from "../timing-engine";
import { Point } from "../geometry";
import {
  type EditorControllerOptions,
  type LevelInfo,
  type EditorOutboxEvents,
  type UserAction,
} from "./types";
import { EditorContext } from "./editor-context";
import { SnapSlice } from "./slices/snap-slice";
import { ZoomSlice } from "./slices/zoom-slice";
import { ProjectSlice } from "./slices/project-slice";
import { ChartSlice } from "./slices/chart-slice";
import { LevelSlice } from "./slices/level-slice";
import { ViewportSlice } from "./slices/viewport-slice";
import { CursorSlice } from "./slices/cursor-slice";
import { SelectionSlice } from "./slices/selection-slice";
import { HistorySlice } from "./slices/history-slice";
import { BoxSelectionSlice } from "./slices/box-selection-slice";
import { ToolSlice } from "./slices/tool-slice";
import { TimingSlice } from "./slices/timing-slice";
import { ColumnsSlice } from "./slices/columns-slice";
import { TimingColumnsSlice } from "./slices/timing-columns-slice";
import { LevelColumnsSlice } from "./slices/level-columns-slice";
import { RenderSlice } from "./slices/render-slice";
import { PointerInteractionSlice } from "./slices/pointer-interaction-slice";
import { ViewCommandSlice } from "./slices/view-command-slice";
import { EditorCommandSlice } from "./slices/editor-command-slice";
 
export class EditorController {
  outbox: Emitter<EditorOutboxEvents> = createNanoEvents<EditorOutboxEvents>();
 
  ctx = new EditorContext();
 
  get $snap() {
    return this.ctx.get(SnapSlice).$snap;
  }
 
  get $zoom() {
    return this.ctx.get(ZoomSlice).$zoom;
  }
 
  get $selectedChartId() {
    return this.ctx.get(ChartSlice).$selectedChartId;
  }
 
  get $hiddenLevelIds() {
    return this.ctx.get(LevelSlice).$hiddenLevelIds;
  }
 
  get $cursorPulse() {
    return this.ctx.get(CursorSlice).$cursorPulse;
  }
 
  get $activeTool() {
    return this.ctx.get(ToolSlice).$activeTool;
  }
 
  get $visibleRenderObjects() {
    return this.ctx.get(RenderSlice).$visibleRenderObjects;
  }
 
  private get viewport(): ViewportSlice {
    return this.ctx.get(ViewportSlice);
  }
 
  private get cursor(): CursorSlice {
    return this.ctx.get(CursorSlice);
  }
 
  private get history(): HistorySlice {
    return this.ctx.get(HistorySlice);
  }
 
  private get timing(): TimingSlice {
    return this.ctx.get(TimingSlice);
  }
 
  private get columnsSlice(): ColumnsSlice {
    return this.ctx.get(ColumnsSlice);
  }
 
  get $lastPlacedEntityInfo() {
    return this.ctx.get(PointerInteractionSlice).$lastPlacedEntityInfo;
  }
 
  private get render(): RenderSlice {
    return this.ctx.get(RenderSlice);
  }
 
  private get pointer(): PointerInteractionSlice {
    return this.ctx.get(PointerInteractionSlice);
  }
 
  private get entityManager(): EntityManager {
    return this.ctx.get(ProjectSlice).entityManager;
  }
 
  constructor(options: EditorControllerOptions) {
    this.ctx.register(ProjectSlice, (ctx) => new ProjectSlice(ctx, options.project));
    this.ctx.register(ChartSlice);
    this.ctx.register(LevelSlice);
    this.ctx.register(ViewportSlice);
    this.ctx.register(CursorSlice);
    this.ctx.register(SelectionSlice);
    this.ctx.register(BoxSelectionSlice);
    this.ctx.register(SnapSlice);
    this.ctx.register(ZoomSlice);
    this.ctx.register(HistorySlice);
    this.ctx.register(ToolSlice);
    this.ctx.register(TimingSlice);
    this.ctx.register(ColumnsSlice);
    this.ctx.register(TimingColumnsSlice);
    this.ctx.register(LevelColumnsSlice);
    this.ctx.register(RenderSlice);
    this.ctx.register(PointerInteractionSlice);
    this.ctx.register(ViewCommandSlice);
    this.ctx.register(EditorCommandSlice);
 
    this.ctx.get(ViewportSlice).onViewportChanged(() => {
      this.pointer.recomputeCursorPulse();
      this.render.refresh();
    });
 
    this.ctx.get(ViewportSlice).onScrollRequest((point) => {
      this.outbox.emit("setScroll", point);
    });
 
    this.ctx.get(ToolSlice).onToolChanged(() => {
      this.render.refresh();
    });
 
    this.ctx.get(SnapSlice).onSnapChanged(() => {
      const currentPulse = this.cursor.$cursorPulse.get();
      const snapped = this.snapToGrid(currentPulse);
      this.cursor.$cursorPulse.set(snapped);
    });
 
    this.ctx.get(ColumnsSlice).$columns.subscribe(() => {
      this.render.refresh();
    });
 
    this.ctx.get(ColumnsSlice).refreshColumns();
  }
 
  getLevelsForChart(chartId: string): LevelInfo[] {
    return this.ctx.get(LevelSlice).getLevelsForChart(chartId);
  }
 
  addLevel(chartId: string, name: string, mode: string): string {
    return this.ctx.get(LevelSlice).addLevel(chartId, name, mode);
  }
 
  removeLevel(levelId: string): void {
    this.ctx.get(LevelSlice).removeLevel(levelId);
  }
 
  toggleLevelVisibility(levelId: string): void {
    this.ctx.get(LevelSlice).toggleLevelVisibility(levelId);
  }
 
  getContentHeight(): number {
    return this.viewport.getContentHeight();
  }
 
  setScroll(point: Point): void {
    this.viewport.setScroll(point);
  }
 
  setViewportSize(width: number, height: number): void {
    this.viewport.setViewportSize(width, height);
  }
 
  setSnap(snap: string): void {
    this.ctx.get(SnapSlice).setSnap(snap);
  }
 
  handlePointerDown(point: Point, shiftKey: boolean = false): void {
    this.pointer.handlePointerDown(point, shiftKey);
  }
 
  handlePointerMove(viewportX: number, viewportY: number): void {
    this.pointer.handlePointerMove(viewportX, viewportY);
  }
 
  handlePointerUp(): void {
    this.pointer.handlePointerUp();
  }
 
  setTool(tool: "select" | "pencil" | "erase" | "pan"): void {
    this.ctx.get(ToolSlice).setTool(tool);
  }
 
  setZoom(zoom: number): void {
    this.ctx.get(ViewCommandSlice).setZoom(zoom);
  }
 
  zoomIn(): void {
    this.ctx.get(ViewCommandSlice).zoomIn();
  }
 
  zoomOut(): void {
    this.ctx.get(ViewCommandSlice).zoomOut();
  }
 
  applyAction(action: UserAction): void {
    this.history.applyAction(action);
  }
 
  deleteSelection(): void {
    this.ctx.get(EditorCommandSlice).deleteSelection();
  }
 
  undo(): void {
    this.history.undo();
  }
 
  redo(): void {
    this.history.redo();
  }
 
  navigateSnap(direction: "up" | "down"): void {
    this.ctx.get(ViewCommandSlice).navigateSnap(direction);
  }
 
  onConnected(): void {
    const contentHeight = this.viewport.getContentHeight();
    const viewportHeight = this.viewport.$viewportSize.get().height;
    if (contentHeight > viewportHeight) {
      this.outbox.emit("setScroll", {
        x: this.viewport.$scroll.get().x,
        y: contentHeight - viewportHeight,
      });
    }
  }
 
  getEntityManager(): EntityManager {
    return this.entityManager;
  }
 
  snapToGrid(pulse: number): number {
    return this.getTimingEngine().snapPulse(pulse, this.$snap.get());
  }
 
  getTimingEngine(): TimingEngine {
    return this.timing.getTimingEngine();
  }
 
  getTimelineWidth(): number {
    return this.columnsSlice.$timelineWidth.get();
  }
}