All files / player-browser/src/session/controls button-tracker.ts

100% Statements 27/27
94.73% Branches 18/19
100% Functions 6/6
100% Lines 25/25

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                                                                                          18x               255x 255x   255x   342x 342x 306x 102x 102x 76x 3x 3x   73x 73x 73x 73x           76x   26x 22x 22x   306x   160x   2x 2x     39x        
/**
 * Which control buttons are down, and since when, from a screen's key events: what the screens need to read a button
 * held (START held opens a panel), held long (START+SELECT held quits a play), or pressed twice (START twice turns the
 * lane cover on and off). It reads nothing but the events and their `timeStamp`s, so it is as deterministic as they
 * are.
 *
 * A button is down from the first of its keys going down to the last of them coming up: a player who gave START two
 * keys holds it while either is held. A key's auto-repeats change nothing.
 */
 
import type { ControlButton } from './control-buttons.ts';
 
/** A button going down or up, as the tracker saw it. */
export interface ControlButtonChange {
  readonly button: ControlButton;
  readonly type: 'down' | 'up';
  readonly timeStamp: number;
  /** On a `down`: when the button last went down before, if it ever did (for a double press). */
  readonly previousDownAt?: number;
  /** On an `up`: how long it was down. */
  readonly heldMs?: number;
}
 
/** A key event as the tracker reads it: the key's physical code and the buttons it is. */
export interface ControlButtonKeyEvent {
  readonly type: 'down' | 'up';
  readonly code: string;
  readonly repeat: boolean;
  readonly timeStamp: number;
  readonly buttons: readonly ControlButton[];
}
 
export interface ControlButtonTracker {
  /** Takes a key event; returns the buttons that went down or up with it. */
  handle(event: ControlButtonKeyEvent): ControlButtonChange[];
  /** Whether `button` is down. */
  isDown(button: ControlButton): boolean;
  /** How long `button` has been down at `nowMs`, or `undefined` when it is up. */
  heldMs(button: ControlButton, nowMs: number): number | undefined;
  /** Lets every button go, without changes (the page lost focus, the screen left). */
  reset(): void;
}
 
/** Whether a side's START and SELECT are both down (LR2's and beatoraja's back-out and quit). */
export function isStartAndSelectDown(tracker: Pick<ControlButtonTracker, 'isDown'>): boolean {
  return (
    (tracker.isDown('1P-start') && tracker.isDown('1P-select')) ||
    (tracker.isDown('2P-start') && tracker.isDown('2P-select'))
  );
}
 
export function createControlButtonTracker(): ControlButtonTracker {
  /** The keys (codes) holding each button down, and when it went down. */
  const down = new Map<ControlButton, { readonly codes: Set<string>; readonly at: number }>();
  const lastDownAt = new Map<ControlButton, number>();
 
  return {
    handle: (event) => {
      const changes: ControlButtonChange[] = [];
      if (event.repeat) return changes;
      for (const button of event.buttons) {
        const held = down.get(button);
        if (event.type === 'down') {
          if (held !== undefined) {
            held.codes.add(event.code);
            continue;
          }
          down.set(button, { codes: new Set([event.code]), at: event.timeStamp });
          const previousDownAt = lastDownAt.get(button);
          lastDownAt.set(button, event.timeStamp);
          changes.push({
            button,
            type: 'down',
            timeStamp: event.timeStamp,
            ...(previousDownAt !== undefined ? { previousDownAt } : {}),
          });
          continue;
        }
        if (held === undefined || !held.codes.delete(event.code) || held.codes.size > 0) continue;
        down.delete(button);
        changes.push({ button, type: 'up', timeStamp: event.timeStamp, heldMs: event.timeStamp - held.at });
      }
      return changes;
    },
    isDown: (button) => down.has(button),
    heldMs: (button, nowMs) => {
      const held = down.get(button);
      return held === undefined ? undefined : nowMs - held.at;
    },
    reset: () => {
      down.clear();
    },
  };
}