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 | // The page around the stage: the shell's state as the scenes change (the play's overlays, the touch controls shown, the
// pinch zoom), the canvas's fullscreen toggle with its Escape handling, and the on-screen pause and quit buttons. The
// app wires it once the scene host is mounted (`PlayerWebApp.mountStage`).
import { logger } from '@be-music/player-browser';
import type { PixiSceneHost, PlayScene, SceneKind } from '@be-music/player-browser/host';
import type { DeviceProfile } from '../device-profile.ts';
import {
enterTouchFullscreen,
exitFullscreen,
FullscreenEscapeLock,
getFullscreenElement,
type KeyboardLockNavigatorLike,
playerFullscreenElement,
shouldCaptureFullscreenEscape,
toggleFullscreen,
} from '../fullscreen.ts';
import type { InlineSearch } from '../inline-search.ts';
import type { PlayerWebElements } from '../types.ts';
import { resolveShellState, shellStateKey } from './shell-state.ts';
const fullscreenLog = logger('fullscreen');
/** What the shell reads of the app: the stage, the shell element, the search field, the device and the play. */
export interface PlayerShellDeps {
readonly sceneHost: PixiSceneHost;
readonly elements: Pick<PlayerWebElements, 'shell'>;
readonly search: Pick<InlineSearch, 'skinHasBox'> & Partial<Pick<InlineSearch, 'ownsNativeInput'>>;
readonly deviceProfile: Pick<DeviceProfile, 'phone'>;
/** The play, whichever family draws it: loading behind a decide screen, or on the stage. */
readonly gameplayView: () => PlayScene | undefined;
/** The kind of scene on stage, `undefined` between scenes. */
readonly showing: () => SceneKind | undefined;
/** Whether the select screen is playing its launch outro (`SelectScene.launching`). */
readonly launching?: () => boolean;
}
/** The player shell around the stage: its gameplay state, fullscreen and touch controls. */
export class PlayerShell {
private readonly sceneHost: PlayerShellDeps['sceneHost'];
private readonly elements: PlayerShellDeps['elements'];
private readonly search: PlayerShellDeps['search'];
private readonly deviceProfile: PlayerShellDeps['deviceProfile'];
private readonly readGameplayView: PlayerShellDeps['gameplayView'];
private readonly readShowing: PlayerShellDeps['showing'];
private readonly readLaunching: PlayerShellDeps['launching'];
public constructor(deps: PlayerShellDeps) {
this.sceneHost = deps.sceneHost;
this.elements = deps.elements;
this.search = deps.search;
this.deviceProfile = deps.deviceProfile;
this.readGameplayView = deps.gameplayView;
this.readShowing = deps.showing;
this.readLaunching = deps.launching;
}
/** The play (`createPlayScene`), whichever family draws it: loading behind a decide screen, or on the stage. */
private get gameplayView(): PlayScene | undefined {
return this.readGameplayView();
}
/** Last state {@link syncGameplayShellState} applied, so the per-frame check only touches the DOM on a change. */
private shellGameplayState = '';
/** The last applied `inGameplay`, which holds between scenes (see `resolveShellState`). */
private wasInGameplay = false;
/**
* The on-screen pause / quit buttons phones and tablets get during play (they have no Space or Escape key). Pause
* toggles; Quit, shown while paused, leaves the play the way Escape does.
*/
public wireTouchControls(): void {
const pause = document.querySelector<HTMLButtonElement>('#touch-pause');
const quit = document.querySelector<HTMLButtonElement>('#touch-quit');
pause?.addEventListener('click', () => this.gameplayView?.requestTogglePause());
quit?.addEventListener('click', () => {
const view = this.gameplayView;
if (!view) return;
// Resume first so the exit fade (which runs on the play clock) plays out instead of freezing on the pause.
if (view.isPaused()) view.requestTogglePause();
view.requestExit();
});
}
/**
* Marks the shell `in-gameplay` while a play screen is up, so the select-screen overlays (the floating Help button and
* the search box) step aside instead of covering the skin's track plate. Checked every frame because the gameplay
* views are created and torn down along many paths; a play being prepared behind a decide splash counts already.
* Off the select screen (a result, say) the shell is `off-select`, which hides the search field for every family.
*/
public readonly syncGameplayShellState = (): void => {
const view = this.gameplayView;
const resolved = resolveShellState({
playFamily: view?.identity.family,
paused: view?.isPaused() === true,
showing: this.readShowing(),
launching: this.readLaunching?.() === true,
wasInGameplay: this.wasInGameplay,
// A skin drawing on its portrait stage needs no rotate prompt on a phone held upright.
portraitStage: this.sceneHost.getStageOrientation() === 'portrait',
skinHasBox: this.search.skinHasBox,
});
this.elements.shell.classList.toggle('native-search', this.search.ownsNativeInput === true);
const state = shellStateKey(resolved);
if (state === this.shellGameplayState) return;
this.shellGameplayState = state;
const { inGameplay, touchPlay, paused, portraitStage, searchInSkin, offSelect } = resolved;
this.wasInGameplay = inGameplay;
const { shell } = this.elements;
shell.classList.toggle('in-gameplay', inGameplay);
for (const element of shell.querySelectorAll<HTMLElement>('.player-service-header, .service-page-footer')) {
element.inert = inGameplay;
}
// A skin with its own search box hides the field at the foot of the screen; the box opens it in place.
shell.classList.toggle('search-in-skin', searchInSkin);
shell.classList.toggle('off-select', offSelect);
// The built-in / LR2 play views take touch input and expose pause / exit for the touch controls.
shell.classList.toggle('touch-play', touchPlay);
shell.classList.toggle('play-paused', paused);
document.documentElement.classList.toggle('portrait-stage', portraitStage);
// No pinch zoom while playing: a stray two-finger touch on the lanes would otherwise zoom the page.
document
.querySelector('meta[name="viewport"]')
?.setAttribute(
'content',
inGameplay
? 'width=device-width, initial-scale=1.0, viewport-fit=cover, maximum-scale=1.0, user-scalable=no'
: 'width=device-width, initial-scale=1.0, viewport-fit=cover',
);
};
/**
* Double-click the stage (the Pixi canvas, or the background of a native screen) to toggle the player shell into (and
* out of) fullscreen.
* Escape while fullscreen is consumed here so the active scene's Esc handler — quit play,
* dismiss result, close a select folder — does not fire on the same keypress that leaves
* fullscreen. Where the browser has Keyboard Lock (Chrome / Edge), Escape is locked while
* fullscreen instead: a short press reaches the scene as it does windowed and the browser
* leaves fullscreen on a long press, so nothing is consumed.
*/
public wireCanvasFullscreen(): void {
const canvas = this.sceneHost.app.canvas;
// The stage, not the canvas: the host takes the canvas out while a native screen is showing.
const stage = this.sceneHost.container;
const isStageBackground = (target: EventTarget | null): boolean =>
target === canvas ||
target === stage ||
(target instanceof HTMLElement && target.dataset.beMusicScreen !== undefined);
let lastPointerType = 'mouse';
let previousPressHandled = false;
let lastPressHandled = false;
let autoFullscreenTried = false;
stage.addEventListener('pointerdown', (event) => {
lastPointerType = event.pointerType;
if (event.isPrimary && event.button === 0) {
previousPressHandled = lastPressHandled;
lastPressHandled = event.defaultPrevented;
}
// A phone gives the stage the whole screen on the first tap: the browser bars eat a large share of a landscape
// phone's height, and the stage is fitted to that height. The play surface (`#main`) goes fullscreen. It holds
// the Options dialog, and the site header and the site footer sit outside it. Once only, so leaving fullscreen
// sticks. iPhone Safari has no element fullscreen and simply stays windowed.
if (this.deviceProfile.phone && event.pointerType === 'touch' && !autoFullscreenTried) {
autoFullscreenTried = true;
void enterTouchFullscreen(playerFullscreenElement(document));
}
});
stage.addEventListener('dblclick', (event) => {
if (!isStageBackground(event.target) || previousPressHandled || lastPressHandled) return;
event.preventDefault();
// Double taps are lane presses during play, not a fullscreen toggle.
if (lastPointerType !== 'mouse' && this.elements.shell.classList.contains('in-gameplay')) return;
void toggleFullscreen(playerFullscreenElement(document));
});
// Every way in or out of fullscreen (double-click, the Options dialog toggle, the phone's first tap, the browser's own
// exit) fires `fullscreenchange`, so the lock follows it from here.
const escapeLock = new FullscreenEscapeLock(navigator as KeyboardLockNavigatorLike, (error) =>
fullscreenLog.debug('Escape key lock refused', error),
);
const syncEscapeLock = (): void => void escapeLock.sync(getFullscreenElement() !== null);
document.addEventListener('fullscreenchange', syncEscapeLock);
document.addEventListener('webkitfullscreenchange', syncEscapeLock);
window.addEventListener(
'keydown',
(event) => {
if (!shouldCaptureFullscreenEscape(event, document, escapeLock.isLocked())) return;
event.preventDefault();
event.stopImmediatePropagation();
void exitFullscreen();
},
true,
);
}
}
|