All files / app-play/src/ui/options play-lock.ts

100% Statements 25/25
100% Branches 11/11
100% Functions 6/6
100% Lines 21/21

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                                                    5x 5x         5x         2x         3x               5x 5x 5x 8x   5x   5x 5x         3x 3x 3x 3x 4x   3x                   8x 8x 20x        
// The locks a play puts on the Options dialog: the options it latched as it started stay disabled until it ends, and so do
// the skin controls, which would make the select screen again and end it. A play whose tempo changes live keeps the
// tempo slider. It works on the controls it is handed, so it is tested against the native disabled-state contract.
 
/** Native bindings and buttons expose the same disabled-state contract. */
export interface LockableControl {
  disabled: boolean;
}
 
/** The Options dialog's controls a play locks, as they are when it locks or unlocks (a picker can be made again meanwhile). */
export interface PlayLockControls {
  /** "Auto-save replays", latched as a play starts. */
  readonly replaySave: LockableControl | undefined;
  /** Every option read as a play starts (`OptionsDialog.trackPlayOption`). */
  readonly playOptions: readonly LockableControl[];
  /** "Tempo (%)": one of the play options, which a play whose tempo changes live keeps enabled. */
  readonly tempo: LockableControl | undefined;
  /**
   * LR2 theme, Built-in skin, Skin effects and the Add skin buttons. Skin system is not here: during a play it swaps
   * the drawing family and keeps the chart. One not made yet is `undefined`.
   */
  readonly skin: readonly (LockableControl | undefined)[];
}
 
/** Whether a play runs, and the Options dialog controls it disables meanwhile. */
export class PlayLock {
  private skinLocked = false;
  private liveTempo = false;
  private readonly controls: () => PlayLockControls;
 
  /** `controls` is read each time the lock changes. */
  public constructor(controls: () => PlayLockControls) {
    this.controls = controls;
  }
 
  /** Whether a play runs, so the skin controls are disabled: a picker made meanwhile starts disabled too. */
  public get skinControlsLocked(): boolean {
    return this.skinLocked;
  }
 
  /** Whether the running play takes the tempo slider live (`allowsLiveTempo` at its start). */
  public get liveTempoPlay(): boolean {
    return this.liveTempo;
  }
 
  /**
   * Disables the options a play latches as it starts, and the skin controls with them, until {@link unlock}. With
   * `liveTempo`, the tempo slider stays enabled.
   */
  public lock(liveTempo = false): void {
    const controls = this.controls();
    if (controls.replaySave) controls.replaySave.disabled = true;
    for (const controller of controls.playOptions) {
      controller.disabled = true;
    }
    this.setSkinControlsLocked(controls, true);
    // The tempo slider changes this play's tempo live (`allowsLiveTempo`).
    this.liveTempo = liveTempo;
    if (liveTempo && controls.tempo) controls.tempo.disabled = false;
  }
 
  /** Re-enables the replay option controllers, and the skin controls, once no play is in flight. */
  public unlock(): void {
    this.liveTempo = false;
    const controls = this.controls();
    if (controls.replaySave) controls.replaySave.disabled = false;
    for (const controller of controls.playOptions) {
      controller.disabled = false;
    }
    this.setSkinControlsLocked(controls, false);
  }
 
  /**
   * Disables the Options dialog's skin controls for a play, or enables them again: Built-in skin, Skin effects, the LR2
   * theme picker and the Add skin buttons make the select screen again, which would end the play. Skin system is not
   * among them. A drop that opens the select screen still can, and the router then ends the course run
   * (`CourseRunService.endRun`). A skin dropped on its own switches the play's family instead.
   */
  private setSkinControlsLocked(controls: PlayLockControls, locked: boolean): void {
    this.skinLocked = locked;
    for (const controller of controls.skin) {
      if (controller) controller.disabled = locked;
    }
  }
}