All files / player-browser/src/view/shared touch-lane-input.ts

96.07% Statements 49/51
84.21% Branches 16/19
100% Functions 13/13
100% Lines 43/43

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                                                              22x 5x 6x 10x             35x             35x               19x 19x 19x 19x 19x 19x 19x 19x 19x 19x         28x 28x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x     11x 11x 11x 11x 11x 8x       4x       8x     3x 2x 2x       1x             10x 10x                 2x                  
/**
 * A play view's touch lanes on the canvas, or on the element a DOM playfield is drawn in: follows the fingers through its
 * pointer events ({@link TouchLaneFingers}) and sends the session a `lane` intent for each lane a finger presses or
 * releases. While attached, the element takes `touch-action: none`, so the page does not scroll or zoom under the
 * fingers.
 */
 
import type { PixiSceneHost } from '../../host/scene-host.ts';
import type { IntentSink, PlayIntent, PlayfieldSkin, PlayfieldStage } from '../types.ts';
import type { PlayfieldRuntime } from './playfield/context.ts';
import { TouchLaneFingers, type TouchLanePointer, type TouchLaneRect } from './touch-lanes.ts';
 
/** Where a view's touch lanes are, and how often the play has lifted every finger (`PlayfieldRuntime.touchLiftCount`). */
export interface TouchLaneSource {
  /** The design stage the lanes are laid out on, as the view fits it into the canvas. */
  stage(): PlayfieldStage;
  /** The lanes as laid out on the stage. */
  lanes(): Iterable<TouchLaneRect>;
  /** How many times the play has lifted every finger (a pause). */
  liftCount(): number;
}
 
/**
 * The touch lanes of a view drawing the shared playfield (the built-in and LR2 views): the lanes it laid out
 * (`PlayfieldRuntime.laneX`) on its skin's stage. Read at each finger event, so a stage that turns with the screen and
 * lanes laid out anew count at once.
 */
export function playfieldTouchLanes(view: {
  readonly play: Pick<PlayfieldRuntime, 'laneX' | 'touchLiftCount'>;
  readonly playfieldSkin: Pick<PlayfieldSkin, 'stage'>;
}): TouchLaneSource {
  return {
    stage: () => view.playfieldSkin.stage,
    lanes: () => Array.from(view.play.laneX, ([channel, lane]) => ({ channel, ...lane })),
    liftCount: () => view.play.touchLiftCount,
  };
}
 
/** A play view's touch lanes. See the module comment. */
export class TouchLaneInput {
  private readonly source: TouchLaneSource;
  private readonly fingers = new TouchLaneFingers();
  private host: PixiSceneHost | undefined;
  private target: HTMLElement | undefined;
  private emit: IntentSink<PlayIntent> | undefined;
  private previousTouchAction: string | undefined;
 
  public constructor(source: TouchLaneSource) {
    this.source = source;
  }
 
  /**
   * Follows the fingers on `target` (`host`'s canvas by default; another element must cover the same area), sending
   * their intents to `emit`, until {@link detach}.
   */
  public attach(host: PixiSceneHost, emit: IntentSink<PlayIntent>, target: HTMLElement = host.app.canvas): void {
    Iif (this.host !== undefined) this.detach();
    this.host = host;
    this.target = target;
    this.emit = emit;
    this.previousTouchAction = target.style.touchAction;
    target.style.touchAction = 'none';
    target.addEventListener('pointerdown', this.handlePointer);
    target.addEventListener('pointermove', this.handlePointer);
    target.addEventListener('pointerup', this.handlePointer);
    target.addEventListener('pointercancel', this.handlePointer);
  }
 
  /** Stops following the fingers and forgets them (their lanes are the session's to let go), restoring the canvas. */
  public detach(): void {
    const target = this.target;
    if (target === undefined) return;
    target.removeEventListener('pointerdown', this.handlePointer);
    target.removeEventListener('pointermove', this.handlePointer);
    target.removeEventListener('pointerup', this.handlePointer);
    target.removeEventListener('pointercancel', this.handlePointer);
    Eif (this.previousTouchAction !== undefined) target.style.touchAction = this.previousTouchAction;
    this.previousTouchAction = undefined;
    this.fingers.reset();
    this.host = undefined;
    this.target = undefined;
    this.emit = undefined;
  }
 
  private readonly handlePointer = (event: PointerEvent): void => {
    const host = this.host;
    const target = this.target;
    Iif (host === undefined || target === undefined) return;
    if (target === host.app.canvas) {
      this.forward(
        event,
        this.fingers.handle(
          event,
          () => ({ stage: this.source.stage(), screen: host.app.screen, lanes: this.source.lanes() }),
          this.source.liftCount(),
        ),
      );
      return;
    }
    // A finger touching down on a control inside the element (a HUD button) is the control's.
    if (event.type === 'pointerdown' && event.target !== target) return;
    const box = target.getBoundingClientRect();
    this.forward(
      event,
      this.fingers.handle(
        pointerInBox(event, box),
        () => ({ stage: this.source.stage(), screen: box, lanes: this.source.lanes() }),
        this.source.liftCount(),
      ),
    );
  };
 
  private forward(event: PointerEvent, result: ReturnType<TouchLaneFingers['handle']>): void {
    if (result.preventDefault) event.preventDefault();
    for (const intent of result.intents) this.emit?.(intent);
  }
}
 
/** `event` with its position measured from the top-left corner of `box` (an element's client rect), in CSS pixels. */
export function pointerInBox(
  event: Pick<PointerEvent, 'type' | 'pointerId' | 'pointerType' | 'timeStamp' | 'clientX' | 'clientY'>,
  box: { readonly left: number; readonly top: number },
): TouchLanePointer {
  return {
    type: event.type,
    pointerId: event.pointerId,
    pointerType: event.pointerType,
    timeStamp: event.timeStamp,
    offsetX: event.clientX - box.left,
    offsetY: event.clientY - box.top,
  };
}