Fix bugs in buttons settings menu

This commit is contained in:
Tissevert 2018-12-04 08:27:32 +01:00
parent 9bd0f127ff
commit 6038d3bdbc
2 changed files with 38 additions and 14 deletions

View file

@ -1,6 +1,16 @@
function Buttons(session) { function Buttons(session) {
var mapping; var mapping;
var mappingsStack = []; var mappingsStack = [];
var buttons = {
'A': 0,
'B': 1,
'Left': 2,
'Right': 3,
'Up': 4,
'Down': 5,
'Start': 6,
'Select': 7
};
document.addEventListener('keydown', function(event) { document.addEventListener('keydown', function(event) {
var button = session.getOptions().layout[event.key]; var button = session.getOptions().layout[event.key];
@ -11,19 +21,28 @@ function Buttons(session) {
}); });
return { return {
findKey: findKey, assign: assign,
code: code,
map: map, map: map,
pop: pop, pop: pop,
push: push push: push
}; };
function findKey(button) { function assign(key, button) {
var layoutDiff = {};
var layout = session.getOptions().layout; var layout = session.getOptions().layout;
for(var key in layout) { for(var currentKey in layout) {
if(layout[key] == button) { if(layout[currentKey] == button) {
return key; layoutDiff[currentKey] = undefined;
break;
} }
} }
layoutDiff[key] = button;
return {layoutDiff: layoutDiff, button: layout[key] != button ? layout[key] : null};
}
function code(button) {
return buttons[button];
} }
function map(newMapping) { function map(newMapping) {

View file

@ -204,22 +204,22 @@ function UI(async, buttons, dom, screen, session) {
function setLayout() { function setLayout() {
var layout = session.getOptions().layout var layout = session.getOptions().layout
var buttons = { var buttonsMenu = {
cancel: function() {}, cancel: function() {},
entries: [], entries: [],
name: "layout" name: "layout"
}; };
for(var key in layout) { for(var key in layout) {
buttons.entries.push({ buttonsMenu.entries[buttons.code(layout[key])] = {
label: keyPromptLabel(key, layout[key]), label: keyPromptLabel(key, layout[key]),
action: setKey(layout[key]) action: setKey(layout[key])
}); };
} }
menu(buttons); menu(buttonsMenu);
} }
function setKey(button) { function setKey(button) {
return function() { return function(f) {
buttons.push({}); buttons.push({});
var elem = dom.make('div', { var elem = dom.make('div', {
class: ['framed', 'center'], class: ['framed', 'center'],
@ -227,14 +227,19 @@ function UI(async, buttons, dom, screen, session) {
}); });
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(null, button); screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(null, button);
var promptKey = function(event) { var promptKey = function(event) {
var layout = {}; var assignment = buttons.assign(event.key, button);
layout[event.key] = button; session.setOptions({layout: assignment.layoutDiff});
layout[buttons.findKey(button)] = undefined;
session.setOptions({layout: layout});
screen.clear(elem); screen.clear(elem);
screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(event.key, button); screen.currentMenu().getElementsByClassName('selected')[0].textContent = keyPromptLabel(event.key, button);
document.removeEventListener('keydown', promptKey); document.removeEventListener('keydown', promptKey);
buttons.pop(); buttons.pop();
if(assignment.button != undefined) {
screen.select(buttons.code(assignment.button));
return setKey(assignment.button)(function () {
screen.select(buttons.code(button));
});
}
return f();
}; };
screen.show(elem); screen.show(elem);
document.addEventListener('keydown', promptKey); document.addEventListener('keydown', promptKey);