Fixes to city button - now allows for units to be selected in the city (#771)

This commit is contained in:
Yair Morgenstern 2019-05-19 22:58:24 +03:00
parent 5c4be33418
commit 4a97d3ea18
3 changed files with 20 additions and 46 deletions

View File

@ -2,7 +2,6 @@ package com.unciv.ui.tilegroups
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.math.Interpolation import com.badlogic.gdx.math.Interpolation
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Group import com.badlogic.gdx.scenes.scene2d.Group
import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.actions.Actions import com.badlogic.gdx.scenes.scene2d.actions.Actions
@ -25,8 +24,7 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski
touchable= Touchable.disabled touchable= Touchable.disabled
} }
var buttonDownClickArea: Actor? = null var isButtonMoved=false
fun isButtonMoved() = buttonDownClickArea != null
fun update(isCityViewable:Boolean) { fun update(isCityViewable:Boolean) {
val cityButtonText = city.population.population.toString() + " | " + city.name val cityButtonText = city.population.population.toString() + " | " + city.name
@ -43,26 +41,24 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski
touchable = Touchable.enabled touchable = Touchable.enabled
label.touchable = Touchable.enabled label.touchable = Touchable.enabled
// clicking anywhere else on the button opens the city screen immediately // clicking on the button swings the button a little down to allow selection of units there.
onClickEvent { _, x, y -> // this also allows to target selected units to move to the city tile from elsewhere.
if (!isButtonMoved()) { // second tap on the button will go to the city screen
if (hit(x, y, true) == label) { onClick {
// clicking on the label swings that label a little down to allow selection of units there. if (!isButtonMoved) {
// this also allows to target selected units to move to the city tile from elsewhere. moveButtonDown()
// second tap on the label will go to the city screen if (unitTable.selectedUnit == null || unitTable.selectedUnit!!.currentMovement == 0f)
moveButtonDown() tileGroup.selectCity(city)
if (unitTable.selectedUnit == null || unitTable.selectedUnit!!.currentMovement==0f)
tileGroup.selectCity(city) } else {
} else { UnCivGame.Current.screen = CityScreen(city)
UnCivGame.Current.screen = CityScreen(city)
}
} }
} }
} }
// when deselected, move city button to its original position // when deselected, move city button to its original position
if (isButtonMoved() if (isButtonMoved
&& unitTable.selectedCity != city && unitTable.selectedCity != city
&& unitTable.selectedUnit?.currentTile != city.ccenterTile) { && unitTable.selectedUnit?.currentTile != city.ccenterTile) {
@ -107,44 +103,22 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski
setOrigin(Align.center) setOrigin(Align.center)
centerX(tileGroup) centerX(tileGroup)
touchable = Touchable.enabled touchable = Touchable.enabled
updateClickArea()
} }
private fun moveButtonDown() { private fun moveButtonDown() {
val floatAction = Actions.sequence( val moveButtonAction = Actions.sequence(
Actions.moveBy(0f, -height, 0.4f, Interpolation.swingOut), Actions.moveBy(0f, -height, 0.4f, Interpolation.swingOut),
Actions.run { Actions.run { isButtonMoved=true }
buttonDownClickArea = Actor().onClick {
UnCivGame.Current.screen = CityScreen(city)
}
tileGroup.cityButtonLayerGroup.addActor(buttonDownClickArea)
updateClickArea()
}
) )
tileGroup.addAction(floatAction) parent.addAction(moveButtonAction) // Move the whole cityButtonLayerGroup down, so the citybutton remains clickable
} }
private fun moveButtonUp() { private fun moveButtonUp() {
val floatAction = Actions.sequence( val floatAction = Actions.sequence(
Actions.moveBy(0f, height, 0.4f, Interpolation.sine), Actions.moveBy(0f, height, 0.4f, Interpolation.sine),
Actions.run { Actions.run {isButtonMoved=false}
buttonDownClickArea?.remove()
buttonDownClickArea = null
}
) )
tileGroup.addAction(floatAction) parent.addAction(floatAction)
}
private fun updateClickArea() {
buttonDownClickArea?.let { clickArea ->
clickArea.setSize(width, height)
clickArea.setScale(scaleX, scaleY)
clickArea.setOrigin(Align.center)
clickArea.centerX(tileGroup.cityButtonLayerGroup)
clickArea.y = y-height
clickArea.touchable = Touchable.enabled
}
} }
private fun getConstructionGroup(cityConstructions: CityConstructions): Group { private fun getConstructionGroup(cityConstructions: CityConstructions): Group {

View File

@ -67,7 +67,7 @@ class WorldTileGroup(internal val worldScreen: WorldScreen, tileInfo: TileInfo)
if (city != null && tileInfo.isCityCenter()) { if (city != null && tileInfo.isCityCenter()) {
if (cityButton == null) { if (cityButton == null) {
cityButton = CityButton(city, this, CameraStageBaseScreen.skin) cityButton = CityButton(city, this, CameraStageBaseScreen.skin)
addActor(cityButton) cityButtonLayerGroup.addActor(cityButton)
} }
cityButton!!.update(viewable) cityButton!!.update(viewable)

View File

@ -48,8 +48,8 @@ class TileGroupMap<T: TileGroup>(val tileGroups:Collection<T>, padding:Float): G
for(group in miscLayers) addActor(group) for(group in miscLayers) addActor(group)
for(group in circleCrosshairFogLayers) addActor(group) for(group in circleCrosshairFogLayers) addActor(group)
for(group in tileGroups) addActor(group) // The above layers are for the visual layers, this is for the clickability for(group in tileGroups) addActor(group) // The above layers are for the visual layers, this is for the clickability
for(group in cityButtonLayers) addActor(group) // city buttons clickability
for(group in unitLayers) addActor(group) // Aaand units above everything else. for(group in unitLayers) addActor(group) // Aaand units above everything else.
for(group in cityButtonLayers) addActor(group) // city buttons clickability
// there are tiles "below the zero", // there are tiles "below the zero",