-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpresets.js
359 lines (330 loc) · 11 KB
/
presets.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
import { brUuid, maxMainInput } from './utils/constants.js';
import { getLatestRelease } from './utils/getLatestRelease.js';
import { getAppVersion } from './utils/getAppVersion.js';
import { getBdAddr } from './utils/getBdAddr.js';
import { savePresetInput } from './utils/savePresetInput.js';
import { getGameId } from './utils/getGameId.js';
import { getGameName } from './utils/getGameName.js';
import { getCfgSrc } from './utils/getCfgSrc.js';
import { setDefaultCfg } from './utils/setDefaultCfg.js';
import { setGameIdCfg } from './utils/setGameIdCfg.js';
var presets = new Array();
var bluetoothDevice;
let brService = null;
var pageInit = 0;
var consoles = []
var bdaddr = '';
var app_ver = '';
var latest_ver = '';
var name = '';
var gameid = '';
var gamename = '';
var current_cfg = 0;
function initInputSelect() {
//push all console names from JSON files
for (var i = 0; i < presets.length; i++) {
consoles.push(presets[i].console)
}
//filter out non-unique items
consoles = consoles.filter(onlyUnique)
//set placeholder description
document.getElementById("desc").textContent = "Select a system and then preset";
var div = document.createElement("outputandconsole");
var main = document.createElement("select");
for (var i = 0; i < maxMainInput; i++) {
var option = document.createElement("option");
option.value = i;
option.text = "Output " + (i + 1);
main.add(option);
}
main.id = "inputSelect";
div.appendChild(main);
var main = document.createElement("select");
//add placeholder option
var option = document.createElement("option");
option.value = -1;
option.text = "All";
main.add(option);
//add console filter options
for (var i = 0; i < consoles.length; i++) {
var option = document.createElement("option");
option.value = i;
option.text = consoles[i];
main.add(option);
}
main.id = "consoleName";
main.addEventListener("change", chooseConsole);
div.appendChild(main);
//add preset drop down menu
var main = document.createElement("select");
main.id = "presetsName";
main.addEventListener("change", selectInput);
div.appendChild(main);
var divInputCfg = document.getElementById("divInputCfg");
divInputCfg.appendChild(div);
//populate preset list with all options by default
populateConsolePresets(undefined);
}
function initOutputMapping() {
/* Save */
var divSave = document.createElement("saveButton");
var btn = document.createElement("button");
btn.id = "inputSave";
btn.innerText = 'Save';
btn.addEventListener("click", saveInput);
divSave.appendChild(btn);
divSave.setAttribute("style", "margin-top:1em;");
var div = document.createElement("div");
div.id = "inputSaveText";
div.setAttribute("style", "display:none;margin-top:1em;");
var p = document.createElement("p");
p.setAttribute("style", "font-style:italic;font-size:small;color:green;");
p.innerText = "Config saved, mapping changes take effect immediately.";
div.appendChild(p);
divSave.appendChild(div);
/* Append first cfg */
var divMappingGrp = document.createElement("save");
var divInputCfg = document.getElementById("divInputCfg");
divMappingGrp.appendChild(divSave);
divInputCfg.appendChild(divMappingGrp);
}
function fetchMap(presets, files, idx) {
return new Promise(function(resolve, reject) {
fetch("map/" + files[idx].name)
.then(rsp => {
return rsp.json();
})
.then(data => {
presets.push(data);
if (++idx < files.length) {
resolve(fetchMap(presets, files, idx));
}
else {
resolve(presets);
}
})
.catch(error => {
reject(error);
});
});
}
function getMapList(url) {
return new Promise(function(resolve, reject) {
fetch(url)
.then(rsp => {
return rsp.json();
})
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
}
//standard function to filter out non-unique items from an array
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function initBlueRetroCfg() {
getMapList('https://api.github.com/repos/darthcloud/BlueRetroWebCfg/contents/map/')
.then(files => {
return fetchMap(presets, files, 0);
})
.then(_ => {
initInputSelect();
initOutputMapping();
pageInit = 1;
})
.catch(error => {
log('Argh! ' + error);
});
}
function saveInput() {
document.getElementById("inputSaveText").style.display = 'none';
//get preset index
var preset_idx = Number(document.getElementById("presetsName").value);
//make sure preset is not placeholder before we do anything
if (preset_idx != -1) {
var preset = presets[preset_idx];
var cfgId = Number(document.getElementById("inputSelect").value);
document.getElementById("inputSaveText").style.display = 'none';
savePresetInput(preset, brService, cfgId)
.then(_ => {
document.getElementById("inputSaveText").style.display = 'block';
});
}
}
function onDisconnected() {
log('> Bluetooth Device disconnected');
document.getElementById("divBtConn").style.display = 'block';
document.getElementById("divInfo").style.display = 'none';
document.getElementById("divCfgSel").style.display = 'none';
document.getElementById("divInputCfg").style.display = 'none';
}
function swGameIdCfg() {
setGameIdCfg(brService)
.then(_ => {
return getCfgSrc(brService);
})
.then(value => {
current_cfg = value;
initCfgSelection();
})
}
function swDefaultCfg() {
setDefaultCfg(brService)
.then(_ => {
return getCfgSrc(brService);
})
.then(value => {
current_cfg = value;
initCfgSelection();
})
}
function initCfgSelection() {
let divCfgSel = document.getElementById("divCfgSel");
let cfgSw = document.createElement("div");
let cfgBtn = document.createElement("button");
divCfgSel.innerHTML = '';
let header = document.createElement("h2");
header.style.margin = 0;
header.innerText = 'Config Selection';
divCfgSel.appendChild(header);
cfgBtn.id = "cfgSw";
if (current_cfg == 0) {
cfgBtn.innerText += 'Switch to GameID';
cfgBtn.addEventListener("click", swGameIdCfg);
divCfgSel.innerHTML += 'Current config: Global';
if (gameid.length) {
cfgSw.appendChild(cfgBtn);
}
}
else {
cfgBtn.innerText = 'Switch to Global';
cfgBtn.addEventListener("click", swDefaultCfg);
divCfgSel.innerHTML += 'Current config: GameID';
cfgSw.appendChild(cfgBtn);
}
cfgSw.setAttribute("style", "margin-top:1em;");
divCfgSel.append(cfgSw);
}
export function btConn() {
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: [{namePrefix: 'BlueRetro'}],
optionalServices: [brUuid[0]]})
.then(device => {
log('Connecting to GATT Server...');
name = device.name;
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
return bluetoothDevice.gatt.connect();
})
.then(server => {
log('Getting BlueRetro Service...');
return server.getPrimaryService(brUuid[0]);
})
.catch(error => {
log(error.name);
throw 'Couldn\'t connect to BlueRetro';
})
.then(service => {
log('Init Cfg DOM...');
brService = service;
if (!pageInit) {
initBlueRetroCfg();
}
return getBdAddr(brService);
})
.then(value => {
bdaddr = value;
return getLatestRelease();
})
.then(value => {
latest_ver = value;
return getAppVersion(brService);
})
.then(value => {
app_ver = value;
return getGameId(brService);
})
.then(value => {
gameid = value;
return getGameName(gameid);
})
.then(value => {
gamename = value;
return getCfgSrc(brService);
})
.catch(error => {
if (error.name == 'NotFoundError'
|| error.name == 'NotSupportedError') {
return 0;
}
throw error;
})
.then(value => {
current_cfg = value;
document.getElementById("divInfo").innerHTML = 'Connected to: ' + name + ' (' + bdaddr + ') [' + app_ver
+ ']<br> Current Game: ' + gamename + ' (' + gameid + ')';
try {
if (app_ver.indexOf(latest_ver) == -1) {
document.getElementById("divInfo").innerHTML += '<br><br>Download latest FW ' + latest_ver + ' from <a href=\'https://darthcloud.itch.io/blueretro\'>itch.io</a>';
}
}
catch (e) {
// Just move on
}
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divCfgSel").style.display = 'block';
document.getElementById("divInputCfg").style.display = 'block';
initCfgSelection();
})
.catch(error => {
log('Argh! ' + error);
});
}
function selectInput() {
//only change the description if selected input is not the placeholder
if (Number(document.getElementById("presetsName").value) == -1) {
document.getElementById("desc").textContent = "Select a console and preset!";
}
else {
document.getElementById("desc").textContent = presets[Number(this.value)].desc;
}
}
function clearConsolePresets() {
//clear the presets list then change the description to "select a console and preset!"
var presetsList = document.getElementById("presetsName");
var presetsListLength = presetsList.length;
for (var i = 0; i < presetsListLength; i++) {
presetsList.remove(0);
}
document.getElementById("desc").textContent = "Select a console and preset!";
}
function populateConsolePresets(selectedConsole) {
//add "select preset first as -1 to prevent trying to save the placeholder"
var list = document.getElementById("presetsName")
list.add(new Option("Select preset", -1));
//add presets to the list that match the selected console type
if (selectedConsole != undefined) {
for (var i = 0; i < presets.length; i++) {
if (presets[i].console === selectedConsole) {
list.add(new Option(presets[i].name, i));
}
}
}
//no filter selected, show whole preset list
else {
for (var i = 0; i < presets.length; i++) {
list.add(new Option(presets[i].name, i));
}
}
}
function chooseConsole(e) {
//when changing consoles we clear the presets list and populate it with presets from the newly selected system
clearConsolePresets()
populateConsolePresets(consoles[e.target.value])
}