Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Special case handler (multiple key binds)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 committed May 14, 2024
1 parent cbe6a4d commit d720bb8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
4 changes: 2 additions & 2 deletions webroot/img/dualshock4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 54 additions & 11 deletions webroot/js/settings/controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(() => {
const trAPI = window.opener.trAPI;
const ctl_modified = {};
const _keybinds = window._keybinds;
const _isSimilar = window._isSimilar;
Expand Down Expand Up @@ -36,20 +37,62 @@
overlay.appendChild(htbtn);
}

window.warnAPI.callback = (data) => {
switch (data.event) {
case 'click':
if (data.resp === 0) window.warnAPI.send({ hidden: true, id: data.id });
break;
case 'key':
console.log(data);
break;
}
const special_cases = {
'controller.ls': ['controller.lx-', 'controller.lx+', 'controller.ly-', 'controller.ly+'],
'controller.rs': ['controller.rx-', 'controller.rx+', 'controller.ry-', 'controller.ry+'],
'controller.lb': ['controller.l1', 'controller.l2'],
'controller.rb': ['controller.r1', 'controller.r2']
};

overlay.on('click', ({ target }) => {
if (target.tagName !== 'BUTTON') return;
window.warnAPI.send({ id: target.dataset.cfgbtn, text: '{$tr:settings.emulator.wkeybind.bindmessage}', trparams: { action: target.dataset.cfgbtn }, hidden: false, buttons: ['Close'] })
const actionid = target.dataset.cfgbtn;

const createButtonMsg = (actionid) => (new Promise((resolve, reject) => {
window.warnAPI.callback = (data) => {
window.warnAPI.send({ hidden: true, id: data.id });

switch (data.event) {
case 'click':
if (data.resp === 0) reject('cancelled');
break;
case 'key':
resolve({ action: data.id, key: data.key });
break;
}
};

window.warnAPI.send({
id: actionid, text: '{$tr:settings.emulator.wkeybind.bindmessage}',
trparams: { action: actionid }, hidden: false, buttons: ['{$tr:buttons.ca}']
});
}));

const spechandler = (sc, idx = 0, collected = []) => {
if (idx === 0) alert(trAPI.get('settings.emulator.wkeybind.alerts.multikey', { count: sc.length }));
if (!sc[idx]) return { multiple: true, keys: collected };
return createButtonMsg(sc[idx]).then((data) => {
collected.push(data);
return spechandler(sc, idx + 1, collected);
});
};

(special_cases[actionid] ? spechandler(special_cases[actionid]) : createButtonMsg(actionid)).then((data) => {
alert('Binding is not actually implemented yet');
if (data.multiple) {
const keys = data.keys;
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
ctl_modified[key.action] = resolveSDLKey(key.code);
}

return;
}

ctl_modified[key.action] = resolveSDLKey(key.code);
}).catch((reason) => {
if (reason !== 'cancelled') throw reason;
});
}, true);
};
doc.data = './img/dualshock4.svg';
Expand All @@ -62,5 +105,5 @@
* elements inside child window. We should explicitly pass each element from child
* window to the parent to get it translated.
*/
document.querySelectorAll('[eo-translator]').forEach((el) => window.opener.trAPI.translateElement(el));
document.querySelectorAll('[eo-translator]').forEach((el) => trAPI.translateElement(el));
})();
3 changes: 3 additions & 0 deletions webroot/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"inbackends": "Gamepad backend configuration",
"wkeybind": {
"bindmessage": "Press key for {action}...",
"alerts": {
"multikey": "Please note that you're about to see {count} button assignment messages, which will follow one after another. If you press the \"Cancel\" button then all prveious binds in line will be cancelled too."
},
"buttons": {
"save": "Save and close",
"reset": "Reset changes",
Expand Down

0 comments on commit d720bb8

Please sign in to comment.