-
Notifications
You must be signed in to change notification settings - Fork 22
/
ui.js
479 lines (417 loc) · 15.2 KB
/
ui.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/**
* jsSMS - A Sega Master System/Game Gear emulator in JavaScript
* Copyright (C) 2012 Guillaume Marty (https://github.com/gmarty)
* Based on JavaGear Copyright (c) 2002-2008 Chris White
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
/**
* Default implementation of UI. Could as well be defined as an interface, to
* make sure we don't forget anything when implementing it.
*
* @constructor
* @param {JSSMS} sms
*/
JSSMS.DummyUI = function(sms) {
this.main = sms;
this.reset = function() {
};
this.updateStatus = function() {
};
this.writeFrame = function() {
};
this.canvasImageData = {
data: []
};
};
if (window['$']) {
/**
* @constructor
* @param {Object.<string, Object.<string, string>>} roms A list of rom files.
*/
$.fn['JSSMSUI'] = function(roms) {
var parent = /** HTMLElement **/ (this);
var UI = function(sms) {
this.main = sms;
// Exit if ran from Opera Mini.
if (Object.prototype.toString.call(window['operamini']) == '[object OperaMini]') {
$(parent).html('<div class="alert alert-error"><strong>Oh no!</strong> Your browser can\'t run this emulator. Try the latest version of Firefox, Google Chrome, Opera or Safari!</div>');
return;
}
var self = this;
// Create UI
var root = $('<div></div>');
var screenContainer = $('<div id="screen"></div>');
var gamepadContainer = $('<div class="gamepad"><div class="direction"><div class="up"></div><div class="right"></div><div class="left"></div><div class="down"></div></div><div class="buttons"><div class="start"></div><div class="fire1"></div><div class="fire2"></div></div></div>');
var controls = $('<div id="controls"></div>');
// General settings
/**
* Contains the fullscreen API prefix or false if not supported.
* @type {string|boolean}
*/
var fullscreenSupport = JSSMS.Utils.getPrefix(['fullscreenEnabled', 'mozFullScreenEnabled', 'webkitCancelFullScreen']);
var requestAnimationFramePrefix = JSSMS.Utils.getPrefix(['requestAnimationFrame', 'msRequestAnimationFrame', 'mozRequestAnimationFrame', 'webkitRequestAnimationFrame'], window);
var i;
if (requestAnimationFramePrefix) {
this.requestAnimationFrame = window[requestAnimationFramePrefix].bind(window);
} else {
var lastTime = 0;
this.requestAnimationFrame = function(callback) {
var currTime = JSSMS.Utils.getTimestamp();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
window.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
};
}
// Screen
this.screen = $('<canvas width=' + SMS_WIDTH + ' height=' + SMS_HEIGHT + ' moz-opaque></canvas>');
this.canvasContext = this.screen[0].getContext('2d');
// Nearest-neighbour rendering for scaling pixel-art.
this.canvasContext['webkitImageSmoothingEnabled'] = false;
this.canvasContext['mozImageSmoothingEnabled'] = false;
this.canvasContext['imageSmoothingEnabled'] = false;
if (!this.canvasContext.getImageData) {
$(parent).html('<div class="alert alert-error"><strong>Oh no!</strong> Your browser doesn\'t support writing pixels directly to the <code><canvas></code> tag. Try the latest version of Firefox, Google Chrome, Opera or Safari!</div>');
return;
}
this.canvasImageData = this.canvasContext.getImageData(0, 0, SMS_WIDTH, SMS_HEIGHT);
// Gamepad
this.gamepad = {
u: {
e: $('.up', gamepadContainer),
k: KEY_UP
},
r: {
e: $('.right', gamepadContainer),
k: KEY_RIGHT
},
d: {
e: $('.down', gamepadContainer),
k: KEY_DOWN
},
l: {
e: $('.left', gamepadContainer),
k: KEY_LEFT
},
1: {
e: $('.fire1', gamepadContainer),
k: KEY_FIRE1
},
2: {
e: $('.fire2', gamepadContainer),
k: KEY_FIRE2
}
};
var startButton = $('.start', gamepadContainer);
// Rom selector
this.romContainer = $('<div id="romSelector"></div>');
this.romSelect = $('<select></select>')
.change(function() {
self.loadROM();
});
// Buttons
this.buttons = Object.create(null);
this.buttons.start = $('<input type="button" value="Start" class="btn btn-primary" disabled="disabled">')
.click(function() {
if (!self.main.isRunning) {
self.main.start();
self.buttons.start.attr('value', 'Pause');
} else {
self.main.stop();
self.updateStatus('Paused');
self.buttons.start.attr('value', 'Start');
}
});
this.buttons.reset = $('<input type="button" value="Reset" class="btn" disabled="disabled">')
.click(function() {
if (!self.main.reloadRom()) {
$(this).attr('disabled', 'disabled');
return;
}
self.main.reset();
self.main.vdp.forceFullRedraw();
self.main.start();
});
if (ENABLE_DEBUGGER) {
this.dissambler = $('<div id="dissambler"></div>');
$(parent).after(this.dissambler);
this.buttons.nextStep = $('<input type="button" value="Next step" class="btn" disabled="disabled">')
.click(function() {
self.main.nextStep();
});
}
if (this.main.soundEnabled) {
this.buttons.sound = $('<input type="button" value="Enable sound" class="btn" disabled="disabled">')
.click(function() {
if (self.main.soundEnabled) {
self.main.soundEnabled = false;
self.buttons.sound.attr('value', 'Enable sound');
} else {
self.main.soundEnabled = true;
self.buttons.sound.attr('value', 'Disable sound');
}
});
}
if (fullscreenSupport) {
// @todo Add an exit fullScreen button.
this.buttons.fullscreen = $('<input type="button" value="Go fullscreen" class="btn">')
.click(function() {
var screen = /** @type {HTMLDivElement} */ (screenContainer[0]);
if (screen.requestFullscreen) {
screen.requestFullscreen();
} else if (screen.mozRequestFullScreen) {
screen.mozRequestFullScreen();
} else {
screen.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}
});
} else {
this.zoomed = false;
this.buttons.zoom = $('<input type="button" value="Zoom in" class="btn hidden-phone">')
.click(function() {
if (self.zoomed) {
self.screen.animate({
width: SMS_WIDTH + 'px',
height: SMS_HEIGHT + 'px'
}, function() {
$(this).removeAttr('style');
});
self.buttons.zoom.attr('value', 'Zoom in');
} else {
self.screen.animate({
width: (SMS_WIDTH * 2) + 'px',
height: (SMS_HEIGHT * 2) + 'px'
});
self.buttons.zoom.attr('value', 'Zoom out');
}
self.zoomed = !self.zoomed;
});
}
// Append buttons to controls div.
for (i in this.buttons) {
this.buttons[i].appendTo(controls);
}
this.log = $('<div id="status"></div>');
this.screen.appendTo(screenContainer);
gamepadContainer.appendTo(screenContainer);
screenContainer.appendTo(root);
this.romContainer.appendTo(root);
controls.appendTo(root);
this.log.appendTo(root);
root.appendTo($(parent));
if (roms != undefined) {
this.setRoms(roms);
}
// Keyboard
$(document)
.bind('keydown', function(evt) {
self.main.keyboard.keydown(evt);
//console.log(self.main.keyboard.controller1, self.main.keyboard.ggstart);
})
.bind('keyup', function(evt) {
self.main.keyboard.keyup(evt);
//console.log(self.main.keyboard.controller1, self.main.keyboard.ggstart);
});
for (i in this.gamepad) {
this.gamepad[i].e
.on('mousedown touchstart', function(key) {
return function(evt) {
self.main.keyboard.controller1 &= ~key;
evt.preventDefault();
};
}(this.gamepad[i].k))
.on('mouseup touchend', function(key) {
return function(evt) {
self.main.keyboard.controller1 |= key;
evt.preventDefault();
};
}(this.gamepad[i].k));
}
startButton
.on('mousedown touchstart', function(evt) {
if (self.main.is_sms)
self.main.pause_button = true; // Pause
else
self.main.keyboard.ggstart &= ~0x80; // Start
evt.preventDefault();
})
.on('mouseup touchend', function(evt) {
if (!self.main.is_sms)
self.main.keyboard.ggstart |= 0x80; // Start
evt.preventDefault();
});
};
UI.prototype = {
reset: function() {
this.screen[0].width = SMS_WIDTH;
this.screen[0].height = SMS_HEIGHT;
this.log.empty();
if (ENABLE_DEBUGGER) {
this.dissambler.empty();
}
},
/**
* Given an map of roms, build a <select> tag to allow game selection.
*
* @param {Object.<Array.<string>>} roms The list of roms.
*/
setRoms: function(roms) {
var groupName,
optgroup,
length,
i,
count = 0;
this.romSelect.children().remove();
$('<option>Select a ROM...</option>').appendTo(this.romSelect);
for (groupName in roms) {
if (roms.hasOwnProperty(groupName)) {
optgroup = $('<optgroup></optgroup>').attr('label', groupName);
length = roms[groupName].length;
i = 0;
for (; i < length; i++) {
$('<option>' + roms[groupName][i][0] + '</option>')
.attr('value', roms[groupName][i][1])
.appendTo(optgroup);
}
optgroup.appendTo(this.romSelect);
}
count++;
}
if (count) {
this.romSelect.appendTo(this.romContainer);
}
},
loadROM: function() {
var self = this;
this.updateStatus('Downloading...');
$.ajax({
url: escape(this.romSelect.val()),
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if (xhr.overrideMimeType != undefined) {
// Download as binary
xhr.overrideMimeType('text/plain; charset=x-user-defined');
}
self.xhr = xhr;
return xhr;
},
complete: function(xhr, status) {
var data;
if (status == 'error') {
self.updateStatus('The selected ROM file could not be loaded.');
return;
}
/*if (JSSMS.Utils.isIE()) {
var charCodes = JSNESBinaryToArray(xhr.responseBody).toArray();
data = String.fromCharCode.apply(undefined, charCodes);
} else {*/
data = xhr.responseText;
//}
self.main.stop();
self.main.readRomDirectly(data, self.romSelect.val());
self.main.reset();
self.main.vdp.forceFullRedraw();
self.enable();
}
});
},
/**
* Enable and reset UI elements.
*/
enable: function() {
/*this.buttons.pause.removeAttr('disabled');
if (this.main.isRunning) {
this.buttons.pause.attr('value', 'pause');
} else {
this.buttons.pause.attr('value', 'resume');
}*/
this.buttons.start.removeAttr('disabled');
this.buttons.start.attr('value', 'Start');
this.buttons.reset.removeAttr('disabled');
if (ENABLE_DEBUGGER) {
this.buttons.nextStep.removeAttr('disabled');
}
if (this.main.soundEnabled) {
if (this.buttons.sound) {
this.buttons.sound.attr('value', 'Disable sound');
} else {
this.buttons.sound.attr('value', 'Enable sound');
}
}
},
/**
* Update the message. Used mainly for displaying frame rate.
*
* @param {string} s The message to display.
*/
updateStatus: function(s) {
this.log.text(s);
},
/**
* @param {Array.<number>} buffer
*/
writeAudio: function(buffer) {
},
/**
* Update the canvas screen. ATM, prevBuffer is not used. See JSNES for
* an implementation of differential update.
*/
writeFrame: function() {
/**
* Contains the visibility API prefix or false if not supported.
* @type {string|boolean}
*/
var hiddenPrefix = JSSMS.Utils.getPrefix(['hidden', 'mozHidden', 'webkitHidden', 'msHidden']);
if (hiddenPrefix) {
// If browser supports visibility API and this page is hidden, we exit.
return function() {
if (document[hiddenPrefix]) {
return;
}
this.canvasContext.putImageData(this.canvasImageData, 0, 0);
}
} else {
return function() {
this.canvasContext.putImageData(this.canvasImageData, 0, 0);
}
}
}(),
/**
* A function called at each cpu instruction and display information relative to debug.
* \@todo If currentAddress is not in this.main.cpu.instructions, then start parsing from it.
*/
updateDisassembly: function(currentAddress) {
var startAddress = currentAddress < 8 ? 0 : currentAddress - 8;
var instructions = this.main.cpu.instructions;
var length = instructions.length;
var html = '';
var i = startAddress;
var num = 0;
for (; num < 16 && i <= length; i++) {
if (instructions[i]) {
html += '<div' + (instructions[i].address == currentAddress ? ' class="current"' : '') + '>' + instructions[i].hexAddress +
(instructions[i].isJumpTarget ? ':' : ' ') +
'<code>' + instructions[i].inst + '</code></div>';
num++;
}
}
this.dissambler.html(html);
}
};
return UI;
};
}