From ef0c0b940746b45bfce38627eb3af0889a34c514 Mon Sep 17 00:00:00 2001 From: "k.h.lai" Date: Tue, 26 May 2020 02:17:02 +0800 Subject: [PATCH] Improve keyboard shortcut (#2663) * Use keyDown instead of keyTyped to handle arrow keys * Rename getIconAnKeyForUnitAction to getIconAndKeyForUnitAction Remove unnecessary dependencies in UnitActionsTable.kt * Use backButtonAndESCHandler to deselect unit and city --- core/src/com/unciv/ui/cityscreen/CityScreen.kt | 15 +++++++++------ core/src/com/unciv/ui/worldscreen/WorldScreen.kt | 16 ++++++++++++++++ .../ui/worldscreen/unit/UnitActionsTable.kt | 7 ++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/core/src/com/unciv/ui/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/cityscreen/CityScreen.kt index 1704677aca..e6b5e161f4 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreen.kt @@ -221,11 +221,14 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() { game.setScreen(CityScreen(civInfo.cities[indexOfNextCity])) } - private fun getKeyboardListener(): InputListener = object : InputListener() { - override fun keyTyped(event: InputEvent?, character: Char): Boolean { - if (character != 0.toChar() || event == null) return super.keyTyped(event, character) - if (event.keyCode == Input.Keys.LEFT) page(-1) - if (event.keyCode == Input.Keys.RIGHT) page(1) + private fun getKeyboardListener(): InputListener = object: InputListener() { + override fun keyDown(event: InputEvent?, keyCode: Int): Boolean { + if (event == null) return super.keyDown(event, keyCode) + when(event.keyCode) { + Input.Keys.LEFT -> page(-1) + Input.Keys.RIGHT -> page(1) + else -> return super.keyDown(event, keyCode) + } return true } } @@ -233,4 +236,4 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() { fun updateExitCityButton(){ } -} \ No newline at end of file +} diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 7f709da1bd..fd35b1c840 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -634,6 +634,22 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() { return } + // Deselect Unit + if (bottomUnitTable.selectedUnit != null) { + bottomUnitTable.selectedUnit = null + bottomUnitTable.isVisible = false + shouldUpdate = true + return + } + + // Deselect city + if (bottomUnitTable.selectedCity != null) { + bottomUnitTable.selectedCity = null + bottomUnitTable.isVisible = false + shouldUpdate = true + return + } + // don't show a dialog, if it can't exit the game if (game.exitEvent == null) { return diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitActionsTable.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitActionsTable.kt index d138a3262a..a284d98ded 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitActionsTable.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitActionsTable.kt @@ -2,12 +2,9 @@ package com.unciv.ui.worldscreen.unit import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Actor -import com.badlogic.gdx.scenes.scene2d.InputEvent import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.ui.Button -import com.badlogic.gdx.scenes.scene2d.ui.Label import com.badlogic.gdx.scenes.scene2d.ui.Table -import com.badlogic.gdx.scenes.scene2d.utils.ClickListener import com.unciv.Constants import com.unciv.UncivGame import com.unciv.logic.map.MapUnit @@ -24,7 +21,7 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() { touchable = Touchable.enabled } - private fun getIconAnKeyForUnitAction(unitAction: String): UnitIconAndKey { + private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey { when { unitAction.startsWith("Upgrade to") -> { // Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ]. @@ -74,7 +71,7 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() { private fun getUnitActionButton(unitAction: UnitAction): Button { - val iconAndKey = getIconAnKeyForUnitAction(unitAction.title) + val iconAndKey = getIconAndKeyForUnitAction(unitAction.title) val actionButton = Button(CameraStageBaseScreen.skin) actionButton.add(iconAndKey.Icon).size(20f).pad(5f) val fontColor = if (unitAction.isCurrentAction) Color.YELLOW else Color.WHITE