From fce4b41aaaef5c7dff682cffc6b582213748071e Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Fri, 17 Apr 2020 09:14:13 +0200 Subject: [PATCH] Keyboard: Left/Right arrows work in city screen (#2445) --- .../src/com/unciv/ui/cityscreen/CityScreen.kt | 32 +++++++++++++++++++ .../cityscreen/CityScreenCityPickerTable.kt | 19 ++--------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/core/src/com/unciv/ui/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/cityscreen/CityScreen.kt index 063d9474..cc795252 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreen.kt @@ -1,7 +1,10 @@ package com.unciv.ui.cityscreen +import com.badlogic.gdx.Input import com.unciv.ui.utils.AutoScrollPane as ScrollPane import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.scenes.scene2d.InputEvent +import com.badlogic.gdx.scenes.scene2d.InputListener import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.badlogic.gdx.utils.Align @@ -20,6 +23,7 @@ import java.util.* class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() { var selectedTile: TileInfo? = null var selectedConstruction: IConstruction? = null + var keyListener: InputListener? = null /** Toggle between Constructions and cityInfo (buildings, specialists etc. */ var showConstructionsTable = true @@ -64,6 +68,9 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() { stage.addActor(cityPickerTable) stage.addActor(cityInfoTable) update() + + keyListener = getKeyboardListener() + stage.addListener(keyListener) } internal fun update() { @@ -190,4 +197,29 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() { scrollPane.scrollPercentY=0.5f scrollPane.updateVisualScroll() } + + fun exit() { + stage.removeListener(keyListener) + game.setWorldScreen() + game.worldScreen.mapHolder.setCenterPosition(city.location) + game.worldScreen.bottomUnitTable.selectedUnit=null + } + fun page(delta: Int) { + val civInfo = city.civInfo + val numCities = civInfo.cities.size + if (numCities == 0) return + val indexOfCity = civInfo.cities.indexOf(city) + val indexOfNextCity = (indexOfCity + delta + numCities) % numCities + stage.removeListener(keyListener) + 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) + return true + } + } } \ No newline at end of file diff --git a/core/src/com/unciv/ui/cityscreen/CityScreenCityPickerTable.kt b/core/src/com/unciv/ui/cityscreen/CityScreenCityPickerTable.kt index 9357a4ce..afd60f2c 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreenCityPickerTable.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreenCityPickerTable.kt @@ -21,11 +21,7 @@ class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){ if (civInfo.cities.size > 1) { val prevCityButton = TextButton("<", CameraStageBaseScreen.skin) - prevCityButton.onClick { - val indexOfCity = civInfo.cities.indexOf(city) - val indexOfNextCity = if (indexOfCity == 0) civInfo.cities.size - 1 else indexOfCity - 1 - cityScreen.game.setScreen(CityScreen(civInfo.cities[indexOfNextCity])) - } + prevCityButton.onClick { cityScreen.page(-1) } add(prevCityButton).pad(20f) } else add() @@ -72,11 +68,7 @@ class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){ if (civInfo.cities.size > 1) { val nextCityButton = TextButton(">", CameraStageBaseScreen.skin) - nextCityButton.onClick { - val indexOfCity = civInfo.cities.indexOf(city) - val indexOfNextCity = if (indexOfCity == civInfo.cities.size - 1) 0 else indexOfCity + 1 - cityScreen.game.setScreen(CityScreen(civInfo.cities[indexOfNextCity])) - } + nextCityButton.onClick { cityScreen.page(1) } add(nextCityButton).pad(20f) } else add() row() @@ -84,12 +76,7 @@ class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){ val exitCityButton = TextButton("Exit city".tr(), CameraStageBaseScreen.skin) exitCityButton.labelCell.pad(10f) - exitCityButton.onClick { - val game = cityScreen.game - game.setWorldScreen() - game.worldScreen.mapHolder.setCenterPosition(city.location) - game.worldScreen.bottomUnitTable.selectedUnit=null - } + exitCityButton.onClick { cityScreen.exit() } add(exitCityButton).pad(10f).colspan(columns)