forked from EXT/VR180-Web-Player
Folder organisation
This commit is contained in:
111
src/vr180player/xr/vr-panel-visibility.ts
Normal file
111
src/vr180player/xr/vr-panel-visibility.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import {
|
||||
hideVrPanelImmediately,
|
||||
setVrPanelOpacity,
|
||||
type VrControlPanel
|
||||
} from './vr-control-panel.js';
|
||||
|
||||
const FADE_DURATION_MS = 200;
|
||||
const AUTO_HIDE_DELAY_MS = 10000;
|
||||
|
||||
export class VrPanelVisibility {
|
||||
private hideTimeout: number | undefined;
|
||||
private lastFadeTimestamp = 0;
|
||||
private opacity = 0;
|
||||
private panel: VrControlPanel | undefined;
|
||||
private targetOpacity = 0;
|
||||
private fading = false;
|
||||
|
||||
get isFading(): boolean {
|
||||
return this.fading;
|
||||
}
|
||||
|
||||
get isVisible(): boolean {
|
||||
return !!(this.panel?.group.visible && this.opacity > 0.01);
|
||||
}
|
||||
|
||||
setPanel(panel: VrControlPanel): void {
|
||||
this.panel = panel;
|
||||
this.hideImmediately();
|
||||
}
|
||||
|
||||
show(): void {
|
||||
if (this.panel) this.panel.group.visible = true;
|
||||
this.clearHideTimeout();
|
||||
|
||||
if (this.targetOpacity !== 1.0 || this.opacity < 1.0) {
|
||||
this.targetOpacity = 1.0;
|
||||
this.startFade();
|
||||
}
|
||||
|
||||
this.hideTimeout = window.setTimeout(() => this.hide(), AUTO_HIDE_DELAY_MS);
|
||||
}
|
||||
|
||||
hide(): void {
|
||||
this.clearHideTimeout();
|
||||
|
||||
if (this.targetOpacity !== 0.0 || this.opacity > 0.0) {
|
||||
this.targetOpacity = 0.0;
|
||||
this.startFade();
|
||||
}
|
||||
}
|
||||
|
||||
hideImmediately(): void {
|
||||
this.clearHideTimeout();
|
||||
this.targetOpacity = 0;
|
||||
this.opacity = 0;
|
||||
this.fading = false;
|
||||
hideVrPanelImmediately(this.panel);
|
||||
}
|
||||
|
||||
updateFade(timestamp: number): void {
|
||||
if (!this.panel) return;
|
||||
if (this.lastFadeTimestamp === 0) this.lastFadeTimestamp = timestamp;
|
||||
|
||||
const deltaTime = (timestamp - this.lastFadeTimestamp) / 1000;
|
||||
this.lastFadeTimestamp = timestamp;
|
||||
const fadeSpeed = 1 / (FADE_DURATION_MS / 1000);
|
||||
let opacityChanged = false;
|
||||
|
||||
if (this.opacity < this.targetOpacity) {
|
||||
this.opacity += fadeSpeed * deltaTime;
|
||||
if (this.opacity >= this.targetOpacity) {
|
||||
this.opacity = this.targetOpacity;
|
||||
this.fading = false;
|
||||
}
|
||||
opacityChanged = true;
|
||||
} else if (this.opacity > this.targetOpacity) {
|
||||
this.opacity -= fadeSpeed * deltaTime;
|
||||
if (this.opacity <= this.targetOpacity) {
|
||||
this.opacity = this.targetOpacity;
|
||||
this.fading = false;
|
||||
if (this.opacity === 0) this.panel.group.visible = false;
|
||||
}
|
||||
opacityChanged = true;
|
||||
} else {
|
||||
this.fading = false;
|
||||
}
|
||||
|
||||
if (opacityChanged) {
|
||||
setVrPanelOpacity(this.panel, this.opacity);
|
||||
}
|
||||
|
||||
if (this.fading) {
|
||||
requestAnimationFrame((nextTimestamp) => this.updateFade(nextTimestamp));
|
||||
}
|
||||
}
|
||||
|
||||
private clearHideTimeout(): void {
|
||||
if (this.hideTimeout !== undefined) {
|
||||
clearTimeout(this.hideTimeout);
|
||||
this.hideTimeout = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private startFade(): void {
|
||||
if (!this.fading) {
|
||||
this.fading = true;
|
||||
this.lastFadeTimestamp = 0;
|
||||
requestAnimationFrame((timestamp) => this.updateFade(timestamp));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user