All files / player-browser/src/view/shared chart-text.ts

47.36% Statements 27/57
58.06% Branches 18/31
83.33% Functions 5/6
46.8% Lines 22/47

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                                                                11x 11x       11x 11x                     11x 11x 11x                 9x 9x 9x 9x 9x                           9x                     11x 11x     56x 56x     30x 30x 30x 30x 30x      
import { Container, Text } from 'pixi.js';
import { measureLayer } from './display-area-info.ts';
import { setHidden, setStyle } from './dom-style.ts';
 
/** Plain chart-authored text, shared by all play families. No HTML interpretation or pointer handling. */
export class ChartTextOverlay {
  private root: HTMLElement | undefined;
  private canvas: Container | undefined;
  private label: Text | undefined;
 
  attach(parent: HTMLElement): void {
    this.canvas?.removeFromParent();
    this.root ??= parent.ownerDocument.createElement('div');
    this.root.dataset.chartText = '';
    Object.assign(this.root.style, {
      position: 'absolute',
      pointerEvents: 'none',
      whiteSpace: 'pre-wrap',
      overflowWrap: 'anywhere',
      textAlign: 'center',
      color: 'white',
      background: '#000b',
      padding: '0.25em 0.5em',
      boxSizing: 'border-box',
      fontFamily: 'sans-serif',
      lineHeight: '1.3',
      overflow: 'hidden',
    });
    parent.append(this.root);
  }
 
  attachCanvas(parent: Container): void {
    this.root?.remove();
    Iif (this.canvas?.destroyed) {
      this.canvas = undefined;
      this.label = undefined;
    }
    this.canvas ??= new Container({ label: 'chart-text', eventMode: 'none' });
    this.label ??= new Text({
      text: '',
      style: {
        fontFamily: 'sans-serif',
        fill: 'white',
        align: 'center',
        wordWrap: true,
        breakWords: true,
        stroke: { color: 'black', width: 3 },
      },
    });
    this.label.anchor.set(0.5, 0);
    if (this.label.parent !== this.canvas) this.canvas.addChild(this.label);
    if (this.canvas.parent !== parent) parent.addChild(this.canvas);
  }
 
  show(
    text: string,
    stage: { width: number; height: number },
    screen: { width: number; height: number },
    opacity = 1,
  ): void {
    const size = Math.max(12, Math.min(28, stage.height / 32));
    Eif (this.canvas?.parent && this.label) {
      this.canvas.visible = text.length > 0;
      this.label.text = text;
      Eif (text.length === 0) return;
      this.label.style.fontSize = size;
      this.label.style.wordWrapWidth = stage.width * 0.9;
      this.label.position.set(stage.width / 2, stage.height * 0.08);
      this.label.scale.set(1);
      if (this.label.height > stage.height * 0.3) this.label.scale.set((stage.height * 0.3) / this.label.height);
      return;
    }
    const root = this.root;
    if (!root) return;
    setHidden(root, text.length === 0);
    // Nothing to show: skip the measure, which would force a layout every frame.
    if (text.length === 0) return;
    const { width, height } = measureLayer(root.parentElement ?? root, screen);
    Iif (root.textContent !== text) root.textContent = text;
    const scale = Math.min(width / stage.width, height / stage.height);
    setStyle(root, 'left', `${(width - stage.width * scale) / 2 + stage.width * scale * 0.05}px`);
    setStyle(root, 'top', `${(height - stage.height * scale) / 2 + stage.height * scale * 0.08}px`);
    setStyle(root, 'width', `${stage.width * scale * 0.9}px`);
    setStyle(root, 'maxHeight', `${stage.height * scale * 0.3}px`);
    setStyle(root, 'fontSize', `${size * scale}px`);
    setStyle(root, 'opacity', String(opacity));
  }
 
  hide(): void {
    Iif (this.root) this.root.hidden = true;
    if (this.canvas) this.canvas.visible = false;
  }
  detach(): void {
    this.root?.remove();
    this.canvas?.removeFromParent();
  }
  dispose(): void {
    this.detach();
    if (this.canvas && !this.canvas.destroyed) this.canvas.destroy({ children: true });
    this.root = undefined;
    this.canvas = undefined;
    this.label = undefined;
  }
}