From 40b94aeb5d2d44988e89c98a89befe72bb12a55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Dole=C5=BEal?= Date: Sat, 26 Dec 2020 21:49:28 +0000 Subject: [PATCH] Cycle to previous building when nothing selected This allows to quickly select previous building with the cycle keybinding after placing a building (and removing the selection), instead of always jump to the next one. --- src/js/game/hud/parts/base_toolbar.js | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/js/game/hud/parts/base_toolbar.js b/src/js/game/hud/parts/base_toolbar.js index b3f5abfc..4534eab6 100644 --- a/src/js/game/hud/parts/base_toolbar.js +++ b/src/js/game/hud/parts/base_toolbar.js @@ -150,6 +150,10 @@ export class HUDBaseToolbar extends BaseHUDPart { } } + isBuildingSelected() { + return Object.entries(this.buildingHandles).some(([_, handle]) => handle.selected) + } + /** * Cycles through all buildings */ @@ -161,22 +165,25 @@ export class HUDBaseToolbar extends BaseHUDPart { let newBuildingFound = false; let newIndex = this.lastSelectedIndex; - const direction = this.root.keyMapper.getBinding(KEYMAPPINGS.placement.rotateInverseModifier).pressed - ? -1 - : 1; + if (this.isBuildingSelected()) { + const direction = this.root.keyMapper.getBinding(KEYMAPPINGS.placement.rotateInverseModifier).pressed + ? -1 + : 1; - for (let i = 0; i <= this.primaryBuildings.length; ++i) { - newIndex = safeModulo(newIndex + direction, this.primaryBuildings.length); - const metaBuilding = gMetaBuildingRegistry.findByClass(this.primaryBuildings[newIndex]); - const handle = this.buildingHandles[metaBuilding.id]; - if (!handle.selected && handle.unlocked) { - newBuildingFound = true; - break; + for (let i = 0; i <= this.primaryBuildings.length; ++i) { + newIndex = safeModulo(newIndex + direction, this.primaryBuildings.length); + const metaBuilding = gMetaBuildingRegistry.findByClass(this.primaryBuildings[newIndex]); + const handle = this.buildingHandles[metaBuilding.id]; + if (!handle.selected && handle.unlocked) { + newBuildingFound = true; + break; + } + } + if (!newBuildingFound) { + return; } } - if (!newBuildingFound) { - return; - } + const metaBuildingClass = this.primaryBuildings[newIndex]; const metaBuilding = gMetaBuildingRegistry.findByClass(metaBuildingClass); this.selectBuildingForPlacement(metaBuilding);