Fix unit promotions shortcut in UnitTable (#4335)

This commit is contained in:
SomeTroglodyte 2021-07-02 09:07:20 +02:00 committed by GitHub
parent 43ff2ea5f9
commit 4e36773cf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,8 +22,9 @@ import com.unciv.ui.worldscreen.WorldScreen
class UnitTable(val worldScreen: WorldScreen) : Table(){ class UnitTable(val worldScreen: WorldScreen) : Table(){
private val prevIdleUnitButton = IdleUnitButton(this,worldScreen.mapHolder,true) private val prevIdleUnitButton = IdleUnitButton(this,worldScreen.mapHolder,true)
private val nextIdleUnitButton = IdleUnitButton(this,worldScreen.mapHolder,false) private val nextIdleUnitButton = IdleUnitButton(this,worldScreen.mapHolder,false)
private val unitIconHolder=Table() private val unitIconHolder = Table()
private val unitNameLabel = "".toLabel() private val unitNameLabel = "".toLabel()
private val unitIconNameGroup = Table()
private val promotionsTable = Table() private val promotionsTable = Table()
private val unitDescriptionTable = Table(CameraStageBaseScreen.skin) private val unitDescriptionTable = Table(CameraStageBaseScreen.skin)
@ -71,8 +72,10 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
add(Table().apply { add(Table().apply {
val moveBetweenUnitsTable = Table().apply { val moveBetweenUnitsTable = Table().apply {
add(prevIdleUnitButton) add(prevIdleUnitButton)
add(unitIconHolder) unitIconNameGroup.add(unitIconHolder)
add(unitNameLabel).pad(5f) unitIconNameGroup.add(unitNameLabel).pad(5f)
unitIconNameGroup.touchable = Touchable.enabled
add(unitIconNameGroup)
add(nextIdleUnitButton) add(nextIdleUnitButton)
} }
add(moveBetweenUnitsTable).colspan(2).fill().row() add(moveBetweenUnitsTable).colspan(2).fill().row()
@ -82,19 +85,18 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
add(unitDescriptionTable) add(unitDescriptionTable)
touchable = Touchable.enabled touchable = Touchable.enabled
onClick { onClick {
selectedUnit?.currentTile?.position?.let { val position = selectedUnit?.currentTile?.position
if ( !worldScreen.mapHolder.setCenterPosition(it, false, false) && selectedUnit != null ) { ?: selectedCity?.location
worldScreen.game.setScreen(CivilopediaScreen(worldScreen.gameInfo.ruleSet, CivilopediaCategories.Unit, selectedUnit!!.name)) if (position != null)
} worldScreen.mapHolder.setCenterPosition(position, false, false)
}
} }
}).expand() }).expand()
} }
fun update() { fun update() {
if(selectedUnit!=null) { if (selectedUnit != null) {
isVisible=true isVisible = true
if (selectedUnit!!.civInfo != worldScreen.viewingCiv && !worldScreen.viewingCiv.isSpectator()) { // The unit that was selected, was captured. It exists but is no longer ours. if (selectedUnit!!.civInfo != worldScreen.viewingCiv && !worldScreen.viewingCiv.isSpectator()) { // The unit that was selected, was captured. It exists but is no longer ours.
selectUnit() selectUnit()
selectedUnitHasChanged = true selectedUnitHasChanged = true
@ -104,17 +106,16 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
} }
} }
if(prevIdleUnitButton.hasIdleUnits()) { // more efficient to do this check once for both if (prevIdleUnitButton.hasIdleUnits()) { // more efficient to do this check once for both
prevIdleUnitButton.enable() prevIdleUnitButton.enable()
nextIdleUnitButton.enable() nextIdleUnitButton.enable()
} } else {
else{
prevIdleUnitButton.disable() prevIdleUnitButton.disable()
nextIdleUnitButton.disable() nextIdleUnitButton.disable()
} }
if(selectedUnit!=null) { // set texts - this is valid even when it's the same unit, because movement points and health change if (selectedUnit != null) { // set texts - this is valid even when it's the same unit, because movement points and health change
if(selectedUnits.size==1) { //single selected unit if (selectedUnits.size == 1) { //single selected unit
separator.isVisible = true separator.isVisible = true
val unit = selectedUnit!! val unit = selectedUnit!!
var nameLabelText = unit.displayName().tr() var nameLabelText = unit.displayName().tr()
@ -123,6 +124,10 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
unitNameLabel.setText(nameLabelText) unitNameLabel.setText(nameLabelText)
selectedUnitHasChanged = true // We need to reload the health bar of the unit in the icon - happens e.g. when picking the Heal Instantly promotion selectedUnitHasChanged = true // We need to reload the health bar of the unit in the icon - happens e.g. when picking the Heal Instantly promotion
} }
unitIconNameGroup.clearListeners()
unitIconNameGroup.onClick {
worldScreen.game.setScreen(CivilopediaScreen(worldScreen.gameInfo.ruleSet, CivilopediaCategories.Unit, unit.name))
}
unitDescriptionTable.clear() unitDescriptionTable.clear()
unitDescriptionTable.defaults().pad(2f) unitDescriptionTable.defaults().pad(2f)
@ -157,8 +162,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
if (unit.promotions.promotions.size != promotionsTable.children.size) // The unit has been promoted! Reload promotions! if (unit.promotions.promotions.size != promotionsTable.children.size) // The unit has been promoted! Reload promotions!
selectedUnitHasChanged = true selectedUnitHasChanged = true
} } else { // multiple selected units
else { // multiple selected units
unitNameLabel.setText("") unitNameLabel.setText("")
unitDescriptionTable.clear() unitDescriptionTable.clear()
} }
@ -179,20 +183,20 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
unitDescriptionTable.add(CityCombatant(city).getAttackingStrength().toString()).row() unitDescriptionTable.add(CityCombatant(city).getAttackingStrength().toString()).row()
selectedUnitHasChanged = true selectedUnitHasChanged = true
} } else {
else {
isVisible = false isVisible = false
} }
if(!selectedUnitHasChanged) return if (!selectedUnitHasChanged) return
unitIconHolder.clear() unitIconHolder.clear()
promotionsTable.clear() promotionsTable.clear()
unitDescriptionTable.clearListeners() unitDescriptionTable.clearListeners()
if(selectedUnit!=null) { if (selectedUnit != null) {
if(selectedUnits.size==1) { // single selected unit if (selectedUnits.size == 1) { // single selected unit
unitIconHolder.add(UnitGroup(selectedUnit!!, 30f)).pad(5f) unitIconHolder.add(UnitGroup(selectedUnit!!, 30f)).pad(5f)
for (promotion in selectedUnit!!.promotions.promotions.sorted()) for (promotion in selectedUnit!!.promotions.promotions.sorted())
promotionsTable.add(ImageGetter.getPromotionIcon(promotion)) promotionsTable.add(ImageGetter.getPromotionIcon(promotion))
@ -201,8 +205,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
if (selectedUnit == null || selectedUnit!!.promotions.promotions.isEmpty()) return@onClick if (selectedUnit == null || selectedUnit!!.promotions.promotions.isEmpty()) return@onClick
UncivGame.Current.setScreen(PromotionPickerScreen(selectedUnit!!)) UncivGame.Current.setScreen(PromotionPickerScreen(selectedUnit!!))
} }
} } else { // multiple selected units
else { // multiple selected units
for (unit in selectedUnits) for (unit in selectedUnits)
unitIconHolder.add(UnitGroup(unit, 30f)).pad(5f) unitIconHolder.add(UnitGroup(unit, 30f)).pad(5f)
} }