mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
feat(ui): render the FPS readout in the title bar from the status store
createFrameRateMonitor() keeps the key, frame count and interval and publishes the readout to the gev-status store; the scene chrome renders it after the subtitle with the same class, hidden state and title. A page without the title bar still gets no monitor, and a monitor only updates or withdraws the readout the store shows.
This commit is contained in:
1 parent
e0abf74308
commit
f9bf782b9c
8 files changed
+161
-48
No files matched your search
+36
-22
@@ -1,17 +1,29 @@
|
||||
import { t } from '../i18n/index.js';
|
||||
/** Count rendered globe frames while the optional readout is visible. */
|
||||
export function createFrameRateMonitor({ viewer, documentRef = document }) {
|
||||
const host = documentRef.getElementById('title-bar');
|
||||
import { useStatusStore } from './stores/status.js';
|
||||
import { applicationPinia } from './vueRuntime.js';
|
||||
|
||||
/**
|
||||
* Count rendered globe frames while the optional readout is visible. The
|
||||
* readout is presented to the status store, which the title bar renders.
|
||||
*/
|
||||
export function createFrameRateMonitor({
|
||||
viewer,
|
||||
documentRef = document,
|
||||
store = useStatusStore(applicationPinia()),
|
||||
}) {
|
||||
const frameEvent = viewer?.scene?.postRender;
|
||||
if (!host || !frameEvent) return { destroy() {} };
|
||||
// A page without the scene chrome has no title bar to show it in.
|
||||
if (!documentRef.getElementById('title-bar') || !frameEvent)
|
||||
return { destroy() {} };
|
||||
|
||||
const readout = documentRef.createElement('div');
|
||||
readout.className = 'frame-rate-readout scene-text-holdout';
|
||||
readout.hidden = true;
|
||||
// i18n-ignore: FPS is a unit code, the same in every language
|
||||
readout.textContent = 'FPS —';
|
||||
readout.title = t('Rendered globe frames per second · toggle with `');
|
||||
host.appendChild(readout);
|
||||
let readout = { visible: false, text: 'FPS —' };
|
||||
// Each monitor shows its readout until a newer one takes the title bar.
|
||||
store.publishFps(readout);
|
||||
const present = (patch) => {
|
||||
const shown = store.fps === readout;
|
||||
readout = { ...readout, ...patch };
|
||||
if (shown) store.publishFps(readout);
|
||||
};
|
||||
let removeFrameListener = null;
|
||||
let timer = null;
|
||||
let frames = 0;
|
||||
@@ -19,7 +31,7 @@ export function createFrameRateMonitor({ viewer, documentRef = document }) {
|
||||
let destroyed = false;
|
||||
|
||||
function hide() {
|
||||
readout.hidden = true;
|
||||
present({ visible: false });
|
||||
removeFrameListener?.();
|
||||
removeFrameListener = null;
|
||||
clearInterval(timer);
|
||||
@@ -30,19 +42,20 @@ export function createFrameRateMonitor({ viewer, documentRef = document }) {
|
||||
frames = 0;
|
||||
startedAt = performance.now();
|
||||
// i18n-ignore: FPS is a unit code, the same in every language
|
||||
readout.textContent = 'FPS —';
|
||||
readout.hidden = false;
|
||||
present({ visible: true, text: 'FPS —' });
|
||||
removeFrameListener = frameEvent.addEventListener(() => {
|
||||
frames++;
|
||||
});
|
||||
timer = setInterval(() => {
|
||||
const now = performance.now();
|
||||
const elapsed = now - startedAt;
|
||||
readout.textContent =
|
||||
// i18n-ignore: FPS is a unit code, the same in every language
|
||||
documentRef.hidden || elapsed <= 0
|
||||
? 'FPS —'
|
||||
: `FPS ${Math.round((frames * 1000) / elapsed)}`;
|
||||
present({
|
||||
text:
|
||||
// i18n-ignore: FPS is a unit code, the same in every language
|
||||
documentRef.hidden || elapsed <= 0
|
||||
? 'FPS —'
|
||||
: `FPS ${Math.round((frames * 1000) / elapsed)}`,
|
||||
});
|
||||
frames = 0;
|
||||
startedAt = now;
|
||||
}, 1000);
|
||||
@@ -68,8 +81,8 @@ export function createFrameRateMonitor({ viewer, documentRef = document }) {
|
||||
)
|
||||
return;
|
||||
event.preventDefault();
|
||||
if (readout.hidden) show();
|
||||
else hide();
|
||||
if (readout.visible) hide();
|
||||
else show();
|
||||
}
|
||||
|
||||
documentRef.addEventListener('keydown', onKeyDown);
|
||||
@@ -79,7 +92,8 @@ export function createFrameRateMonitor({ viewer, documentRef = document }) {
|
||||
destroyed = true;
|
||||
hide();
|
||||
documentRef.removeEventListener('keydown', onKeyDown);
|
||||
readout.remove();
|
||||
// A newer monitor's readout stays.
|
||||
if (store.fps === readout) store.publishFps(null);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user