1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-02-12 19:09:21 +00:00

change keyCode2 to keyCodes[]

This commit is contained in:
Dimava 2020-06-12 16:18:41 +03:00
parent a4c57fd67b
commit de71c39947

View File

@ -244,17 +244,21 @@ export class Keybinding {
* @param {KeyActionMapper} keyMapper * @param {KeyActionMapper} keyMapper
* @param {Application} app * @param {Application} app
* @param {object} param0 * @param {object} param0
* @param {number} param0.keyCode * @param {number} [param0.keyCode]
* @param {number} param0.keyCode2 * @param {number[]} [param0.keyCodes]
* @param {boolean=} param0.builtin * @param {boolean=} param0.builtin
* @param {boolean=} param0.repeated * @param {boolean=} param0.repeated
*/ */
constructor(keyMapper, app, { keyCode, keyCode2 = 0, builtin = false, repeated = false }) { constructor(keyMapper, app, { keyCode = 0, keyCodes = [], builtin = false, repeated = false }) {
assert(keyCode && Number.isInteger(keyCode), "Invalid key code: " + keyCode); if (keyCode && keyCode != keyCodes[0]) {
keyCodes.unshift(keyCode);
}
for (let keyCode of keyCodes) {
assert(Number.isInteger(keyCode), "Invalid key code: " + keyCode);
}
this.keyMapper = keyMapper; this.keyMapper = keyMapper;
this.app = app; this.app = app;
this.keyCode = keyCode; this.keyCodes = keyCodes;
this.keyCode2 = keyCode2;
this.builtin = builtin; this.builtin = builtin;
this.repeated = repeated; this.repeated = repeated;
@ -268,11 +272,13 @@ export class Keybinding {
*/ */
get pressed() { get pressed() {
// Check if the key is down // Check if the key is down
if (this.app.inputMgr.keysDown.has(this.keyCode) || this.app.inputMgr.keysDown.has(this.keyCode2)) { for (let keyCode of this.keyCodes) {
if (this.app.inputMgr.keysDown.has(keyCode)) {
// Check if it is the top reciever // Check if it is the top reciever
const reciever = this.keyMapper.inputReceiver; const reciever = this.keyMapper.inputReceiver;
return this.app.inputMgr.getTopReciever() === reciever; return this.app.inputMgr.getTopReciever() === reciever;
} }
}
return false; return false;
} }
@ -295,19 +301,17 @@ export class Keybinding {
} }
const spacer = document.createElement("code"); const spacer = document.createElement("code");
spacer.classList.add("keybinding"); spacer.classList.add("keybinding");
spacer.innerHTML = getStringForKeyCode(this.keyCode); spacer.innerHTML = getStringForKeyCode(this.keyCodes[0]);
elem.appendChild(spacer); elem.appendChild(spacer);
return spacer; return spacer;
} }
/** /**
* Returns the key code as a nice string * Returns the key code as a nice string
* @param {number} index
*/ */
getKeyCodeString() { getKeyCodeString(index = 0) {
return getStringForKeyCode(this.keyCode); return getStringForKeyCode(this.keyCodes[index] || 0);
}
getKeyCodeString2() {
return getStringForKeyCode(this.keyCode2);
} }
/** /**
@ -342,8 +346,11 @@ export class KeyActionMapper {
if (overrides[key]) { if (overrides[key]) {
payload.keyCode = overrides[key]; payload.keyCode = overrides[key];
} }
if (overrides[key + "_2"]) { payload.keyCodes = payload.keyCodes || [];
payload.keyCode2 = overrides[key + "_2"]; let index = 1;
while (overrides[`${key}_${index + 1}`]) {
payload.keyCodes.push(overrides[`${key}_${index + 1}`]);
index++;
} }
this.keybindings[key] = new Keybinding(this, this.root.app, payload); this.keybindings[key] = new Keybinding(this, this.root.app, payload);
@ -412,7 +419,7 @@ export class KeyActionMapper {
/** @type {Keybinding} */ /** @type {Keybinding} */
const binding = this.keybindings[key]; const binding = this.keybindings[key];
if ( if (
(binding.keyCode === keyCode || binding.keyCode2 == keyCode) && binding.keyCodes.includes(keyCode) &&
(initial || binding.repeated) (initial || binding.repeated)
) { ) {
/** @type {Signal} */ /** @type {Signal} */