From 95024302808ef323b8f417bddb9ffac36bdcad24 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Thu, 24 Jun 2021 19:06:53 -0500 Subject: [PATCH] Implemented hotkeys --- src/store/layers.js | 13 ++++++++++--- src/util/layers.js | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/store/layers.js b/src/store/layers.js index 3758181..fa268d6 100644 --- a/src/store/layers.js +++ b/src/store/layers.js @@ -391,8 +391,15 @@ export function addLayer(layer, player = null) { } if (layer.hotkeys) { for (let id in layer.hotkeys) { - if (layer.hotkeys[id].onPress) { - layer.hotkeys[id].onPress.forceCached = false; + if (isPlainObject(layer.hotkeys[id])) { + if (layer.hotkeys[id].onPress) { + layer.hotkeys[id].onPress.forceCached = false; + } + if (layer.hotkeys[id].unlocked == undefined) { + layer.hotkeys[id].unlocked = function() { + return layer.unlocked; + } + } } } } @@ -407,7 +414,7 @@ export function addLayer(layer, player = null) { // Register hotkeys if (layer.hotkeys) { for (let id in layer.hotkeys) { - hotkeys[id] = layer.hotkeys[id]; + hotkeys[layer.hotkeys[id].key] = layer.hotkeys[id]; } } } diff --git a/src/util/layers.js b/src/util/layers.js index c5c1c09..f94fca2 100644 --- a/src/util/layers.js +++ b/src/util/layers.js @@ -1,6 +1,6 @@ import Decimal from './bignum'; import { isPlainObject } from './common'; -import { layers } from '../store/layers'; +import { layers, hotkeys } from '../store/layers'; import { player } from '../store/proxies'; export function resetLayer(layer, force = false) { @@ -292,3 +292,22 @@ export const defaultLayerProperties = { } } }; + +document.onkeydown = function(e) { + if (player.hasWon && !player.keepGoing) { + return; + } + let key = e.key; + if (e.shiftKey) { + key = "shift+" + key; + } + if (e.ctrlKey) { + key = "ctrl+" + key; + } + if (hotkeys[key]) { + e.preventDefault(); + if (hotkeys[key].unlocked) { + hotkeys[key].onPress?.(); + } + } +}