-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhud.js
More file actions
149 lines (128 loc) · 4.01 KB
/
hud.js
File metadata and controls
149 lines (128 loc) · 4.01 KB
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
// HUD data structure
export class HUDData {
constructor() {
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
this.fps = 0;
}
}
let hudElement = null;
let hudVisible = false;
const hudData = new HUDData();
// Listeners that get called when HUD is toggled
const hudToggleListeners = new Set();
// Register a listener to be called when HUD is toggled
// Callback receives (isVisible: boolean)
export function onHudToggle(callback) {
hudToggleListeners.add(callback);
}
// Unregister a HUD toggle listener
export function offHudToggle(callback) {
hudToggleListeners.delete(callback);
}
// Notify all listeners of HUD toggle
function notifyHudToggleListeners() {
hudToggleListeners.forEach(callback => callback(hudVisible));
}
// Check if HUD is enabled/visible
export function isHudEnabled() {
return hudVisible;
}
// FPS tracking variables
const FPS_UPDATE_INTERVAL = 500;
let lastTime = performance.now();
let frameCount = 0;
let lastFpsUpdate = performance.now();
// Sync HUD toggle button icon
function syncHUDToggle() {
const hudToggleButton = document.getElementById("hud-toggle");
if (!hudToggleButton) return;
hudToggleButton.innerHTML = hudVisible ? '<i data-lucide="panel-top"></i>' : '<i data-lucide="eye-off"></i>';
hudToggleButton.setAttribute("aria-label", hudVisible ? "Hide HUD" : "Show HUD");
// Re-initialize icons after dynamically setting innerHTML
if (window.lucide) {
window.lucide.createIcons();
}
}
// Initialize HUD element
export function initializeHUD() {
// Check if HUD element already exists
hudElement = document.getElementById('hud');
if (!hudElement) {
// Create HUD element if it doesn't exist
hudElement = document.createElement('div');
hudElement.id = 'hud';
hudElement.style.cssText = `
position: fixed;
top: 10px;
right: 10px;
background: rgba(0, 0, 0, 0.7);
color: #0f0;
padding: 10px;
border-radius: 5px;
font-size: 12px;
line-height: 1.6;
z-index: 1000;
pointer-events: none;
min-width: 200px;
font-family: monospace;
display: none;
`;
document.body.appendChild(hudElement);
} else {
// Ensure existing HUD element respects visibility state
hudElement.style.display = hudVisible ? 'block' : 'none';
}
// Initialize FPS tracking
lastTime = performance.now();
frameCount = 0;
lastFpsUpdate = performance.now();
// Setup HUD toggle button
const hudToggleButton = document.getElementById("hud-toggle");
if (hudToggleButton) {
hudToggleButton.addEventListener("click", () => {
toggleHUD();
});
syncHUDToggle();
}
updateHUD();
}
// Update HUD display with camera position and FPS
export function updateHUD(cameraPosition) {
if (!hudElement) return;
if (!hudVisible) return;
const currentTime = performance.now();
frameCount++;
// Update FPS
if (currentTime - lastFpsUpdate >= FPS_UPDATE_INTERVAL) {
hudData.fps = Math.round((frameCount * 1000) / (currentTime - lastFpsUpdate));
frameCount = 0;
lastFpsUpdate = currentTime;
}
// Update position if camera position is provided
if (cameraPosition) {
hudData.xpos = cameraPosition.x;
hudData.ypos = cameraPosition.y;
hudData.zpos = cameraPosition.z;
}
hudElement.innerHTML = `
<div><span style="color: #0ff;">X:</span> <span style="color: #0f0;">${hudData.xpos.toFixed(2)}</span></div>
<div><span style="color: #0ff;">Y:</span> <span style="color: #0f0;">${hudData.ypos.toFixed(2)}</span></div>
<div><span style="color: #0ff;">Z:</span> <span style="color: #0f0;">${hudData.zpos.toFixed(2)}</span></div>
<div><span style="color: #0ff;">FPS:</span> <span style="color: #0f0;">${hudData.fps}</span></div>
`;
}
// Toggle HUD visibility
export function toggleHUD() {
hudVisible = !hudVisible;
if (hudElement) {
hudElement.style.display = hudVisible ? 'block' : 'none';
}
syncHUDToggle();
notifyHudToggleListeners();
}
// Get HUD data object for updating values
export function getHUDData() {
return hudData;
}