/
controls.html
346 lines (289 loc) · 14.1 KB
/
controls.html
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<html>
<head>
<script src="cloudpilot-emu.js"></script>
<style>
body {
background-color: #eee;
}
.container {
width: 100vw;
overflow: hidden;
height: calc(100vh - 8em);
margin-bottom: 1em;
display: flex;
flex-direction: row;
}
#canvas-wrapper {
flex-grow: 1;
max-width: 60em;
}
canvas {
width: 100%;
height: 100%;
}
canvas:focus {
border: none;
outline: none;
}
#control-container {
flex-grow: 0;
flex-shrink: 0;
flex-basis: 30em;
text-align: left;
margin-left: 0.5em;
}
.hidden {
visibility: hidden;
}
body {
text-align: center;
}
button {
min-width: 10em;
margin: 0.5em;
margin-left: 0;
}
input {
width: 15em;
margin-right: 0.5em;
}
label {
display: block;
}
#ui-status {
display: inline-block;
margin-left: 1em;
}
#statistics {
margin-top: 0.5em;
}
a {
margin: 0 0.5em;
}
</style>
</head>
<body>
<h1>Emulator controls</h1>
<div class="container">
<div id="canvas-wrapper"><canvas tabindex="1"></canvas></div>
<div id="control-container" class="hidden">
<div><button type="button" id="power-button"></button><span id="ui-status"></span></div>
<div><button type="button" id="pause-resume-button"></button></div>
<div>
<button type="button" id="reset-button">Reset</button>
<button type="button" id="reset-noext-button">Reset noext</button>
<button type="button" id="reset-hard-button">Reset hard</button>
</div>
<div>
<button type="button" id="orientation-button"></button>
</div>
<div>
<button type="button" id="run-hidden-button"></button>
</div>
<div id="hotsync-name-container"></div>
<div id="hotsync-name-controls">
<label for="hotsync-name-input">New name:</label>
<input maxlength="40" type="text" id="hotsync-name-field"></input>
<button type="button" id="apply-hotsync-name-button">apply</button>
</div>
<div>
<button type="button" id="game-mode-button"></button>
<button type="button" id="game-mode-hotkey-button"></button>
<button type="button" id="game-mode-indicator-button"></button>
</div>
<div id="speed-controls">
<label for="speed-input" id="speed-label">Speed:</label>
<input type="range" min="10" max="500" step="10" id="speed-input">
</div>
<div id="audio-controls">
<button type="button" id="enable-audio-button">Enable audio</button>
<div id="volume-input-container" style="display: none">
<label for="volume-input" id="volume-label">Volume:</label>
<input type="range" min="0" max="100" step="5" id="volume-input">
</div>
</div>
<div id="statistics" style="display: none">
<div id="host-speed"></div>
<div id="emulation-speed"></div>
<div id="fps"></div>
</div>
</div>
</div>
<a href="index.html">...back to index</a>
|
<a href="https://github.com/cloudpilot-emu/cloudpilot-emu/blob/master/web/embedded/public/controls.html">
source on github
</a>
<script>
(async function () {
// Convert orientation into a human readable string
function orientationToString(orientation) {
switch (orientation) {
case cloudpilot.DeviceOrientation.portrait:
return '0°';
case cloudpilot.DeviceOrientation.landscape90:
return '90°';
case cloudpilot.DeviceOrientation.landscape270:
return '270°';
case cloudpilot.DeviceOrientation.portrait180:
return '180°';
}
}
// Switch to the next orientation: 0° -> 90° -> 180° -> 270° -> 0°
function nextOrientation(orientation) {
switch (orientation) {
case cloudpilot.DeviceOrientation.portrait:
return cloudpilot.DeviceOrientation.landscape90;
case cloudpilot.DeviceOrientation.landscape90:
return cloudpilot.DeviceOrientation.portrait180;
case cloudpilot.DeviceOrientation.portrait180:
return cloudpilot.DeviceOrientation.landscape270;
case cloudpilot.DeviceOrientation.landscape270:
return cloudpilot.DeviceOrientation.portrait;
}
}
// Fetch session image
const imageResponse = await fetch('session.img');
if (!imageResponse.ok) {
throw new Error('could not download session image');
}
const image = await imageResponse.arrayBuffer();
// Get handle to canvas and initialize emulator
const canvas = document.getElementsByTagName('canvas')[0];
const emulator = await cloudpilot.createEmulator();
// Load session and start emulator
emulator.
loadSession(new Uint8Array(image))
.setCanvas(canvas)
.bindInput(canvas)
.resume();
// Get handles to the UI elements
const pauseResumeButton = document.getElementById('pause-resume-button');
const powerButton = document.getElementById('power-button');
const resetButton = document.getElementById('reset-button');
const resetNoextButton = document.getElementById('reset-noext-button');
const resetHardButton = document.getElementById('reset-hard-button');
const controlContainer = document.getElementById('control-container');
const orientationButton = document.getElementById('orientation-button');
const uiStatusContainer = document.getElementById('ui-status');
const hotsyncNameContainer = document.getElementById('hotsync-name-container');
const applyHotsyncNameButton = document.getElementById('apply-hotsync-name-button');
const hotsyncNameInput = document.getElementById('hotsync-name-field');
const enableAudioButton = document.getElementById('enable-audio-button');
const volumeLabel = document.getElementById('volume-label');
const volumeInput = document.getElementById('volume-input');
const volumeInputContainer = document.getElementById('volume-input-container');
const statisticsContainer = document.getElementById('statistics');
const hostSpeedContainer = document.getElementById('host-speed');
const emulationSpeedContainer = document.getElementById('emulation-speed');
const fpsContainer = document.getElementById('fps');
const gameModeButton = document.getElementById('game-mode-button');
const gameModeHotkeyButton = document.getElementById('game-mode-hotkey-button');
const gameModeIndicatorButton = document.getElementById('game-mode-indicator-button');
const speedLabel = document.getElementById('speed-label');
const speedInput = document.getElementById('speed-input');
const runHiddenButton = document.getElementById('run-hidden-button');
// Update UI to reflect emulator state
function updateUi() {
pauseResumeButton.innerText = emulator.isRunning() ? 'Pause' : 'Resume';
powerButton.innerText = emulator.isPowerOff() ? 'Power on' : 'Power off';
uiStatusContainer.innerText = emulator.isUiInitialized() ? 'UI initialized' : 'UI not initialized';
orientationButton.innerText = 'Rotate to ' + orientationToString(nextOrientation(emulator.getOrientation()));
hotsyncNameContainer.innerText = `Hotsync name: ${emulator.getHotsyncName()}`;
applyHotsyncNameButton.disabled = emulator.isPowerOff() || !emulator.isUiInitialized();
enableAudioButton.style.display = emulator.isAudioInitialized() ? 'none' : undefined;
gameModeButton.innerText = emulator.isGameMode() ? 'Game mode off' : 'Game mode on';
gameModeHotkeyButton.innerText = emulator.isGameModeHotkeyEnabled() ? 'Game mode hotkey off' : 'Game mode hotkey on';
gameModeIndicatorButton.innerText = emulator.isGameModeIndicatorEnabled() ? 'Game mode indicator off' : 'Game mode hotkey on';
volumeInputContainer.style.display = emulator.isAudioInitialized() ? 'inline-block' : 'none';
volumeInput.value = (emulator.getVolume() * 100) + '';
volumeLabel.innerText = `Volume: ${Math.floor(emulator.getVolume() * 100)}`;
speedInput.value = (emulator.getSpeed() * 100) + '';
speedLabel.innerText = `Speed: ${emulator.getSpeed().toFixed(2)}x`;
runHiddenButton.innerText = emulator.getRunHidden() ? `pause if hidden` : `run while hidden`;
}
// Update statistics
function updateStatistics() {
const statistics = emulator.getStatistics();
statisticsContainer.style.display = 'block';
hostSpeedContainer.innerText = `host speed: ${statistics.hostSpeed.toFixed(2)} x`;
emulationSpeedContainer.innerText = `emulation speed: ${statistics.emulationSpeed.toFixed(2)} x`;
fpsContainer.innerText = `${statistics.averageFps.toFixed(2)} frames / second`;
}
// Update UI on changes that we didn't trigger directly
emulator.powerOffChangeEvent.addHandler(updateUi);
emulator.isUiInitializedChangeEvent.addHandler(updateUi);
emulator.hotsyncNameChangeEvent.addHandler(updateUi);
emulator.gameModeChangeEvent.addHandler(updateUi);
// Perform initial UI update
updateUi();
// Unhide controls
controlContainer.className = '';
// Pause / resume button
pauseResumeButton.addEventListener('click', () => {
if (emulator.isRunning()) emulator.pause();
else emulator.resume();
updateUi();
});
// Power button. The button is directly wired to the Pilot hard button. Note that
// powerOffChangeEvent will fire when the device powers down, so we don't have to call
// updateUi ourselves.
powerButton.addEventListener('mousedown', () => emulator.buttonDown(cloudpilot.Button.power));
powerButton.addEventListener('mouseup', () => emulator.buttonUp(cloudpilot.Button.power));
// Reset buttons
resetButton.addEventListener('click', () => emulator.reset());
resetNoextButton.addEventListener('click', () => emulator.resetNoExtensions());
resetHardButton.addEventListener('click', () => emulator.resetHard());
// The various game mode buttons
gameModeButton.addEventListener('click', () => emulator.setGameMode(!emulator.isGameMode()));
gameModeHotkeyButton.addEventListener('click', () => {
emulator.setGameModeHotkeyEnabled(!emulator.isGameModeHotkeyEnabled());
updateUi();
});
gameModeIndicatorButton.addEventListener('click', () => {
emulator.setGameModeIndicatorEnabled(!emulator.isGameModeIndicatorEnabled());
updateUi();
});
// Orientation button
orientationButton.addEventListener('click', () => {
emulator.setOrientation(nextOrientation(emulator.getOrientation()));
updateUi();
});
// Hotsync name UI. Note that hotsyncNameChangeEvent will fire when the hotsync name is changed, so
// we do not have to call updateUi ourselves.
applyHotsyncNameButton.addEventListener('click', () => emulator.setHotsyncName(hotsyncNameInput.value));
hotsyncNameInput.addEventListener('keydown', (e) => e.key === 'Enter' && emulator.setHotsyncName(hotsyncNameInput.value));
// Enable audio. This has to happen on a user interaction, so we do it here.
enableAudioButton.addEventListener('click', async () => {
// Make sure to await the result before checking whether audio is enabled (as is done
// by updateUi)
await emulator.initializeAudio();
updateUi();
});
// Volume slider.
volumeInput.addEventListener('input', () => {
const volume = parseInt(volumeInput.value);
if (!isNaN(volume)) {
emulator.setVolume(volume / 100);
updateUi();
}
});
// Speed slider
speedInput.addEventListener('input', () => {
const speed = parseInt(speedInput.value) / 100;
if (!isNaN(speed)) {
emulator.setSpeed(speed);
updateUi();
}
});
// Run-in-background button
runHiddenButton.addEventListener('click', () => {
emulator.setRunHidden(!emulator.getRunHidden());
updateUi();
});
// Update statistics every second
setInterval(updateStatistics, 1000);
})().catch(e => console.error(e));
</script>
</body>
</html>