-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathRunPage.js
233 lines (214 loc) · 6.46 KB
/
RunPage.js
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
import React, { Component } from "react";
import { Button, Progress } from "reactstrap";
import { Link } from "react-router-dom";
import config from "./config";
import ControlsModal from "./ControlsModal";
import Emulator from "./Emulator";
import RomLibrary from "./RomLibrary";
import "./RunPage.css";
function loadBinary(path, callback, handleProgress) {
var req = new XMLHttpRequest();
req.open("GET", path);
req.overrideMimeType("text/plain; charset=x-user-defined");
req.onload = function() {
if (this.status === 200) {
if (req.responseText.match(/^<!doctype html>/i)) {
// Got HTML back, so it is probably falling back to index.html due to 404
return callback(new Error("Page not found"));
}
callback(null, this.responseText);
} else if (this.status === 0) {
// Aborted, so ignore error
} else {
callback(new Error(req.statusText));
}
};
req.onerror = function() {
callback(new Error(req.statusText));
};
req.onprogress = handleProgress;
req.send();
return req;
}
/*
* The UI for the emulator. Also responsible for loading ROM from URL or file.
*/
class RunPage extends Component {
constructor(props) {
super(props);
this.state = {
romName: null,
romData: null,
running: false,
paused: false,
controlsModalOpen: false,
loading: true,
loadedPercent: 3,
error: null
};
}
render() {
return (
<div className="RunPage">
<nav
className="navbar navbar-expand"
ref={el => {
this.navbar = el;
}}
>
<ul className="navbar-nav" style={{ width: "200px" }}>
<li className="navitem">
<Link to="/" className="nav-link">
‹ Back
</Link>
</li>
</ul>
<ul className="navbar-nav ml-auto mr-auto">
<li className="navitem">
<span className="navbar-text mr-3">{this.state.romName}</span>
</li>
</ul>
<ul className="navbar-nav" style={{ width: "200px" }}>
<li className="navitem">
<Button
outline
color="primary"
onClick={this.toggleControlsModal}
className="mr-3"
>
Controls
</Button>
<Button
outline
color="primary"
onClick={this.handlePauseResume}
disabled={!this.state.running}
>
{this.state.paused ? "Resume" : "Pause"}
</Button>
</li>
</ul>
</nav>
{this.state.error ? (
this.state.error
) : (
<div
className="screen-container"
ref={el => {
this.screenContainer = el;
}}
>
{this.state.loading ? (
<Progress
value={this.state.loadedPercent}
style={{
position: "absolute",
width: "70%",
left: "15%",
top: "48%"
}}
/>
) : this.state.romData ? (
<Emulator
romData={this.state.romData}
paused={this.state.paused}
ref={emulator => {
this.emulator = emulator;
}}
/>
) : null}
{/* TODO: lift keyboard and gamepad state up */}
{this.state.controlsModalOpen && (
<ControlsModal
isOpen={this.state.controlsModalOpen}
toggle={this.toggleControlsModal}
keys={this.emulator.keyboardController.keys}
setKeys={this.emulator.keyboardController.setKeys}
promptButton={this.emulator.gamepadController.promptButton}
gamepadConfig={this.emulator.gamepadController.gamepadConfig}
setGamepadConfig={
this.emulator.gamepadController.setGamepadConfig
}
/>
)}
</div>
)}
</div>
);
}
componentDidMount() {
window.addEventListener("resize", this.layout);
this.layout();
this.load();
}
componentWillUnmount() {
window.removeEventListener("resize", this.layout);
if (this.currentRequest) {
this.currentRequest.abort();
}
}
load = () => {
if (this.props.match.params.slug) {
const slug = this.props.match.params.slug;
const isLocalROM = /^local-/.test(slug);
const romHash = slug.split("-")[1];
const romInfo = isLocalROM
? RomLibrary.getRomInfoByHash(romHash)
: config.ROMS[slug];
if (!romInfo) {
this.setState({ error: `No such ROM: ${slug}` });
return;
}
if (isLocalROM) {
this.setState({ romName: romInfo.name });
const localROMData = localStorage.getItem("blob-" + romHash);
this.handleLoaded(localROMData);
} else {
this.setState({ romName: romInfo.description });
this.currentRequest = loadBinary(
romInfo.url,
(err, data) => {
if (err) {
this.setState({ error: `Error loading ROM: ${err.message}` });
} else {
this.handleLoaded(data);
}
},
this.handleProgress
);
}
} else if (this.props.location.state && this.props.location.state.file) {
let reader = new FileReader();
reader.readAsBinaryString(this.props.location.state.file);
reader.onload = e => {
this.currentRequest = null;
this.handleLoaded(reader.result);
};
} else {
this.setState({ error: "No ROM provided" });
}
};
handleProgress = e => {
if (e.lengthComputable) {
this.setState({ loadedPercent: (e.loaded / e.total) * 100 });
}
};
handleLoaded = data => {
this.setState({ running: true, loading: false, romData: data });
};
handlePauseResume = () => {
this.setState({ paused: !this.state.paused });
};
layout = () => {
let navbarHeight = parseFloat(window.getComputedStyle(this.navbar).height);
this.screenContainer.style.height = `${window.innerHeight -
navbarHeight}px`;
if (this.emulator) {
this.emulator.fitInParent();
}
};
toggleControlsModal = () => {
this.setState({ controlsModalOpen: !this.state.controlsModalOpen });
};
}
export default RunPage;