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
This commit is contained in:
k.h.lai
2020-05-26 02:17:02 +08:00
committed by GitHub
parent 29d17ead9b
commit ef0c0b9407
3 changed files with 27 additions and 11 deletions

View File

@ -221,11 +221,14 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() {
game.setScreen(CityScreen(civInfo.cities[indexOfNextCity])) game.setScreen(CityScreen(civInfo.cities[indexOfNextCity]))
} }
private fun getKeyboardListener(): InputListener = object : InputListener() { private fun getKeyboardListener(): InputListener = object: InputListener() {
override fun keyTyped(event: InputEvent?, character: Char): Boolean { override fun keyDown(event: InputEvent?, keyCode: Int): Boolean {
if (character != 0.toChar() || event == null) return super.keyTyped(event, character) if (event == null) return super.keyDown(event, keyCode)
if (event.keyCode == Input.Keys.LEFT) page(-1) when(event.keyCode) {
if (event.keyCode == Input.Keys.RIGHT) page(1) Input.Keys.LEFT -> page(-1)
Input.Keys.RIGHT -> page(1)
else -> return super.keyDown(event, keyCode)
}
return true return true
} }
} }
@ -233,4 +236,4 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() {
fun updateExitCityButton(){ fun updateExitCityButton(){
} }
} }

View File

@ -634,6 +634,22 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
return 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 // don't show a dialog, if it can't exit the game
if (game.exitEvent == null) { if (game.exitEvent == null) {
return return

View File

@ -2,12 +2,9 @@ package com.unciv.ui.worldscreen.unit
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Actor 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.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Button 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.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.unciv.Constants import com.unciv.Constants
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.logic.map.MapUnit import com.unciv.logic.map.MapUnit
@ -24,7 +21,7 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() {
touchable = Touchable.enabled touchable = Touchable.enabled
} }
private fun getIconAnKeyForUnitAction(unitAction: String): UnitIconAndKey { private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
when { when {
unitAction.startsWith("Upgrade to") -> { unitAction.startsWith("Upgrade to") -> {
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ]. // 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 { private fun getUnitActionButton(unitAction: UnitAction): Button {
val iconAndKey = getIconAnKeyForUnitAction(unitAction.title) val iconAndKey = getIconAndKeyForUnitAction(unitAction.title)
val actionButton = Button(CameraStageBaseScreen.skin) val actionButton = Button(CameraStageBaseScreen.skin)
actionButton.add(iconAndKey.Icon).size(20f).pad(5f) actionButton.add(iconAndKey.Icon).size(20f).pad(5f)
val fontColor = if (unitAction.isCurrentAction) Color.YELLOW else Color.WHITE val fontColor = if (unitAction.isCurrentAction) Color.YELLOW else Color.WHITE