All files / player-ui/src/components preview-hud.ts

7.4% Statements 2/27
0% Branches 0/24
0% Functions 0/10
9.09% Lines 2/22

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 264 265 266 267                                  48x               48x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
import { css, html, nothing, type CSSResultGroup, type PropertyDeclarations, type TemplateResult } from 'lit';
import type { BeMusicDOMGameplayFrame } from '@be-music/player-skin-sdk';
import { FrameElement } from '../base.ts';
import { resolveGameplayPanels } from '../gameplay-data.ts';
import { PREVIEW_PORTRAIT_FONT_RATIO, resolvePreviewInfoBox } from '../preview-layout.ts';
import { structureStyles } from '../styles/structure.ts';
 
/**
 * The HUD of a preview: the chart's facts, score, combo, gauge and judgments beside or under the BGA
 * (`resolvePreviewInfoBox`), and video-player controls along the bottom of the screen (the host), not of the stage, so
 * a stage letterboxed into a screen turned the other way (a DP chart on a phone held upright) keeps its lanes clear of
 * them. Lay it over `be-music-playfield`; the volume, mute and full-screen state are the
 * host's, set as properties, and so is hiding the controls (`controlsHidden`) while the viewer is idle. A click on the
 * stage pauses and resumes the play unless the host turns it off (`stageTapPauses`), as it does while the viewer plays.
 * A paused play shows a centred play button and, under it, Back (`quit-request`), which leaves the play.
 */
export class BeMusicPreviewHUDElement extends FrameElement<BeMusicDOMGameplayFrame> {
  static override properties: PropertyDeclarations = {
    ...FrameElement.properties,
    volume: { type: Number },
    muted: { type: Boolean },
    fullscreen: { type: Boolean },
    controlsHidden: { type: Boolean, attribute: 'controls-hidden', reflect: true },
    stageTapPauses: { type: Boolean, attribute: false },
  };
  static override styles: CSSResultGroup = [
    structureStyles,
    css`
      :host {
        display: block;
        position: absolute;
        inset: 0;
        container-type: size;
        pointer-events: none;
      }
      .stage {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: min(100cqw, calc(100cqh * var(--stage-ratio, 1)));
        height: min(100cqh, calc(100cqw / var(--stage-ratio, 1)));
        container-type: size;
        font-size: clamp(9px, 2.6cqh, 15px);
        font-variant-numeric: tabular-nums;
      }
      /* A tall stage sets the facts in their own band, so their type follows the stage's width to fit it. */
      .stage[data-orientation='portrait'] {
        font-size: max(9px, calc(min(100cqw, 100cqh * var(--stage-ratio, 1)) * var(--portrait-font-ratio)));
      }
      .stage-hit {
        position: absolute;
        inset: 0;
        padding: 0;
        border: 0;
        border-radius: 0;
        background: transparent;
        pointer-events: auto;
        cursor: pointer;
      }
      /* The shared button hover fills the control; this hit target must stay clear of the lanes and BGA. */
      .stage-hit:hover,
      .stage-hit:active,
      .stage-hit:focus,
      .stage-hit:focus-visible {
        background: transparent;
        border-color: transparent;
      }
      .info {
        position: absolute;
        display: grid;
        /* Rows keep their height in a box too short for them: the facts clip at the bottom instead of overlapping. */
        grid-auto-rows: max-content;
        align-content: start;
        gap: 0.4em;
        overflow: hidden;
        pointer-events: none;
        container-type: inline-size;
        container-name: preview-info;
      }
      @container preview-info (max-width: 22rem) {
        .judgments {
          gap: 0 0.45em;
        }
      }
      h2 {
        font-size: 1.15em;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .line {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .judgments {
        display: flex;
        flex-wrap: wrap;
        gap: 0 0.9em;
        margin: 0;
      }
      .judgments div {
        display: flex;
        gap: 0.35em;
      }
      dd {
        margin: 0;
      }
      .resume {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        /* Sized by its icon, which follows the stage's height, not by the theme's fixed icon-button size. */
        width: auto;
        height: auto;
        padding: 0.6em;
        pointer-events: auto;
      }
      .resume svg {
        width: clamp(30px, 10cqh, 52px);
        height: clamp(30px, 10cqh, 52px);
        margin-left: 6%;
        background: none;
        fill: currentColor;
      }
      /* Leaves a paused play: the way back to the list for a phone or tablet, which has no Escape key. */
      .back {
        position: absolute;
        left: 50%;
        top: calc(50% + clamp(30px, 10cqh, 52px) / 2 + 2.4em);
        transform: translateX(-50%);
        pointer-events: auto;
      }
      be-music-playback-controls {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        pointer-events: auto;
        transition: opacity 0.3s ease;
      }
      :host([controls-hidden]) be-music-playback-controls {
        opacity: 0;
        pointer-events: none;
      }
    `,
  ];
  declare volume: number;
  declare muted: boolean;
  declare fullscreen: boolean;
  /** Fades the video controls out; the host sets it when the viewer has been idle during a play. */
  declare controlsHidden: boolean;
  /** A click on the stage pauses and resumes the play. Off, the stage takes no clicks: only the controls pause. */
  declare stageTapPauses: boolean;
 
  constructor() {
    super();
    this.volume = 1;
    this.muted = false;
    this.fullscreen = false;
    this.controlsHidden = false;
    this.stageTapPauses = true;
  }
 
  protected override render(): TemplateResult | typeof nothing {
    const f = this.frame;
    if (!f) return nothing;
    const { score, gauge, judgments, title, facts, seconds } = resolveGameplayPanels(f);
    const { stage } = f.layout;
    const info = resolvePreviewInfoBox(f.layout);
    const box = `left:${(info.left / stage.width) * 100}%;top:${(info.top / stage.height) * 100}%;width:${((info.right - info.left) / stage.width) * 100}%;bottom:${((stage.height - info.bottom) / stage.height) * 100}%`;
    const bpm = f.bpm === undefined ? undefined : `BPM ${Math.round(f.bpm)}`;
    const rate = score.maximum > 0 ? `${((score.exScore / score.maximum) * 100).toFixed(2)}%` : '—';
    const playing = f.stage !== 'paused' && f.stage !== 'ending';
    return html`<section
        class="stage"
        part="stage"
        data-orientation=${stage.height > stage.width ? 'portrait' : 'landscape'}
        style=${`--stage-ratio:${stage.width / stage.height};--portrait-font-ratio:${PREVIEW_PORTRAIT_FONT_RATIO}`}
      >
        ${
          this.stageTapPauses
            ? html`<button
                class="stage-hit"
                part="stage-hit"
                aria-label=${playing ? 'Pause' : 'Play'}
                @click=${() => this.emit('pause-request', undefined)}
              ></button>`
            : nothing
        }
        <div class="info" part="info" style=${box} aria-label="Chart and play status">
          ${
            f.course === undefined
              ? nothing
              : html`<p class="line muted" part="course">
                  ${f.course.title} ·
                  ${f.course.stageIndex === undefined ? `${f.course.stages.length} stages` : `Stage ${f.course.stageIndex + 1} / ${f.course.stages.length}`}
                </p>`
          }
          <h2 title=${title}>${title}</h2>
          <p class="line muted">${f.chart?.artist ?? ''}</p>
          <p class="line muted">${[facts, bpm].filter(Boolean).join(' · ')}</p>
          <p class="line">
            EX SCORE <b>${score.exScore}</b> · ${score.rank ?? '—'} · ${rate} · Combo <b>${score.combo}</b> /
            ${score.maxCombo}
          </p>
          <be-music-gauge-panel compact exportparts="meter:gauge-meter" .data=${gauge}></be-music-gauge-panel>
          <dl class="judgments" part="judgments">
            ${(
              [
                ['PG', judgments.perfect],
                ['GR', judgments.great],
                ['GD', judgments.good],
                ['BD', judgments.bad],
                ['PR', judgments.poor + judgments.emptyPoor],
                ['FAST', judgments.fast],
                ['SLOW', judgments.slow],
              ] as const
            ).map(
              ([label, value]) =>
                html`<div>
                  <dt>${label}</dt>
                  <dd>${value}</dd>
                </div>`,
            )}
          </dl>
        </div>
        ${
          f.stage === 'paused'
            ? html`<button
                  class="btn resume"
                  data-size="icon-lg"
                  part="resume"
                  aria-label="Play"
                  @click=${() => this.emit('pause-request', undefined)}
                >
                  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 5v14l11-7z" /></svg>
                </button>
                <button
                  class="btn back"
                  data-size="sm"
                  data-variant="outline"
                  part="back"
                  @click=${() => this.emit('quit-request', undefined)}
                >
                  Back
                </button>`
            : nothing
        }
      </section>
      <be-music-playback-controls
        exportparts="bar:controls,seek,play,mute,volume,time,fullscreen"
        .data=${{
          currentSeconds: seconds,
          durationSeconds: f.state.totalSeconds,
          playing,
          seekable: f.seekable,
          volume: this.volume,
          muted: this.muted,
          fullscreen: this.fullscreen,
        }}
      ></be-music-playback-controls>`;
  }
}