-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputDiagnostic.js
More file actions
215 lines (179 loc) · 7.15 KB
/
Copy pathinputDiagnostic.js
File metadata and controls
215 lines (179 loc) · 7.15 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
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
/**
* Input Diagnostic Tool
* This script helps diagnose input-related issues by monitoring key events and displaying diagnostic information
*/
class InputDiagnostic {
constructor() {
this.active = false;
this.keyStates = {};
this.diagnosticElement = null;
// Bind methods to this instance
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleKeyUp = this.handleKeyUp.bind(this);
this.update = this.update.bind(this);
}
/**
* Start monitoring input events
*/
start() {
if (this.active) return;
console.log('Starting input diagnostics');
this.active = true;
// Create diagnostic overlay if it doesn't exist
if (!this.diagnosticElement) {
this.createDiagnosticOverlay();
}
// Attach event listeners
window.addEventListener('keydown', this.handleKeyDown);
window.addEventListener('keyup', this.handleKeyUp);
// Start update loop
this.updateInterval = setInterval(this.update, 100);
// Force display
this.diagnosticElement.style.display = 'block';
// Log input module status
if (window.input) {
console.log('Input module state:', JSON.stringify(window.input.keys));
} else {
console.log('Input module not available');
}
}
/**
* Stop monitoring input events
*/
stop() {
if (!this.active) return;
console.log('Stopping input diagnostics');
this.active = false;
// Remove event listeners
window.removeEventListener('keydown', this.handleKeyDown);
window.removeEventListener('keyup', this.handleKeyUp);
// Stop update loop
clearInterval(this.updateInterval);
// Hide overlay
if (this.diagnosticElement) {
this.diagnosticElement.style.display = 'none';
}
}
/**
* Handle key down events
*/
handleKeyDown(event) {
// Record key state
this.keyStates[event.key] = true;
// Log to console
console.log(`[Diagnostic] Key down: ${event.key}`);
}
/**
* Handle key up events
*/
handleKeyUp(event) {
// Record key state
this.keyStates[event.key] = false;
// Log to console
console.log(`[Diagnostic] Key up: ${event.key}`);
}
/**
* Update diagnostic display
*/
update() {
if (!this.active || !this.diagnosticElement) return;
// Check if input module exists and compare states
let inputModuleStatus = 'Not Available';
if (window.input) {
// Compare our observed states with input module states
const inputKeys = window.input.keys;
const importantKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'w', 'a', 's', 'd', 'W', 'A', 'S', 'D'];
let differences = [];
for (const key of importantKeys) {
if (this.keyStates[key] !== inputKeys[key]) {
differences.push(`${key}: diagnostic=${!!this.keyStates[key]}, input=${!!inputKeys[key]}`);
}
}
if (differences.length > 0) {
inputModuleStatus = `Differences: ${differences.join(', ')}`;
} else {
inputModuleStatus = 'In Sync';
}
}
// Update display
this.diagnosticElement.innerHTML = `
<div style="font-weight: bold; margin-bottom: 5px;">Input Diagnostic</div>
<div>Input Module: ${inputModuleStatus}</div>
<div>Arrow Keys: ↑:${!!this.keyStates['ArrowUp']} ↓:${!!this.keyStates['ArrowDown']} ←:${!!this.keyStates['ArrowLeft']} →:${!!this.keyStates['ArrowRight']}</div>
<div>WASD: W:${!!this.keyStates['W'] || !!this.keyStates['w']} A:${!!this.keyStates['A'] || !!this.keyStates['a']} S:${!!this.keyStates['S'] || !!this.keyStates['s']} D:${!!this.keyStates['D'] || !!this.keyStates['d']}</div>
<div>Last Updated: ${new Date().toLocaleTimeString()}</div>
<div><button id="force-input-reset" style="margin-top: 5px; padding: 2px 5px; font-size: 10px;">Reset Input</button></div>
`;
// Add event listener to reset button
const resetButton = document.getElementById('force-input-reset');
if (resetButton) {
resetButton.onclick = this.forceInputReset.bind(this);
}
}
/**
* Create diagnostic overlay
*/
createDiagnosticOverlay() {
this.diagnosticElement = document.createElement('div');
this.diagnosticElement.id = 'input-diagnostic';
this.diagnosticElement.style.position = 'fixed';
this.diagnosticElement.style.bottom = '10px';
this.diagnosticElement.style.left = '10px';
this.diagnosticElement.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
this.diagnosticElement.style.color = '#fff';
this.diagnosticElement.style.padding = '10px';
this.diagnosticElement.style.borderRadius = '5px';
this.diagnosticElement.style.fontFamily = 'monospace';
this.diagnosticElement.style.fontSize = '12px';
this.diagnosticElement.style.zIndex = '9999';
this.diagnosticElement.style.maxWidth = '300px';
document.body.appendChild(this.diagnosticElement);
}
/**
* Force input system reset
*/
forceInputReset() {
console.log('Forcing input system reset');
// Reset our own tracking
this.keyStates = {};
// Reset the game's input system if available
if (window.input && typeof window.input.reset === 'function') {
window.input.reset();
}
// Dispatch input reset event
document.dispatchEvent(new CustomEvent('inputReset'));
// Force release all keys
const keys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'w', 'a', 's', 'd', 'W', 'A', 'S', 'D'];
for (const key of keys) {
const keyupEvent = new KeyboardEvent('keyup', {
key: key,
bubbles: true,
cancelable: true
});
document.dispatchEvent(keyupEvent);
}
// Update display
this.update();
}
}
// Create global instance
const inputDiagnostic = new InputDiagnostic();
// Auto-start diagnostics when the game is loaded
document.addEventListener('DOMContentLoaded', () => {
// Wait a moment to ensure the game has loaded
setTimeout(() => {
inputDiagnostic.start();
}, 1000);
});
// Also set up a listener for when loading completes
window.addEventListener('loadingComplete', () => {
console.log('Loading complete, starting input diagnostics');
// Start with a delay to ensure everything is initialized
setTimeout(() => {
inputDiagnostic.start();
}, 500);
});
// Export the diagnostic tool
export { inputDiagnostic };