Added backing field selectedUnits:ArrayList<MapUnit> for selectedUnit in UnitTable - will allow multi-select in the future, no functional change for now

This commit is contained in:
Yair Morgenstern 2020-09-11 12:04:43 +03:00
parent 7d32412646
commit 395292e2f3
6 changed files with 39 additions and 38 deletions

View file

@ -213,7 +213,7 @@ class CityScreen(internal val city: CityInfo): CameraStageBaseScreen() {
stage.removeListener(keyListener)
game.setWorldScreen()
game.worldScreen.mapHolder.setCenterPosition(city.location)
game.worldScreen.bottomUnitTable.selectedUnit=null
game.worldScreen.bottomUnitTable.selectUnits()
}
fun page(delta: Int) {
val civInfo = city.civInfo

View file

@ -135,7 +135,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
Sounds.play(UncivSound.Whoosh)
if (selectedUnit.currentTile != targetTile)
selectedUnit.action = "moveTo " + targetTile.position.x.toInt() + "," + targetTile.position.y.toInt()
if (selectedUnit.currentMovement > 0) worldScreen.bottomUnitTable.selectedUnit = selectedUnit
if (selectedUnit.currentMovement > 0) worldScreen.bottomUnitTable.selectUnits(selectedUnit)
worldScreen.shouldUpdate = true
unitActionOverlay?.remove()
@ -164,7 +164,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
if(UncivGame.Current.settings.singleTapMove && turnsToGetThere==1) {
// single turn instant move
selectedUnit.movement.headTowards(tileInfo)
worldScreen.bottomUnitTable.selectedUnit = selectedUnit // keep moved unit selected
worldScreen.bottomUnitTable.selectUnits(selectedUnit) // keep moved unit selected
} else {
// add "move to" button if there is a path to tileInfo
val moveHereButtonDto = if (turnsToGetThere != 0) MoveHereButtonDto(selectedUnit, tileInfo, turnsToGetThere)
@ -197,8 +197,7 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
if (unit.currentMovement == 0f) unitGroup.color.a = 0.5f
unitGroup.touchable = Touchable.enabled
unitGroup.onClick {
worldScreen.bottomUnitTable.selectedUnit = unit
worldScreen.bottomUnitTable.selectedCity = null
worldScreen.bottomUnitTable.selectUnits(unit)
worldScreen.shouldUpdate = true
unitActionOverlay?.remove()
}

View file

@ -593,8 +593,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
val nextDueUnit = viewingCiv.getNextDueUnit()
if (nextDueUnit != null) {
mapHolder.setCenterPosition(nextDueUnit.currentTile.position, false, false)
bottomUnitTable.selectedCity = null
bottomUnitTable.selectedUnit = nextDueUnit
bottomUnitTable.selectUnits(nextDueUnit)
shouldUpdate = true
}
}
@ -687,7 +686,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
// Deselect Unit
if (bottomUnitTable.selectedUnit != null) {
bottomUnitTable.selectedUnit = null
bottomUnitTable.selectUnits()
bottomUnitTable.isVisible = false
shouldUpdate = true
return

View file

@ -31,14 +31,14 @@ class IdleUnitButton (
onClick {
val idleUnits = unitTable.worldScreen.viewingCiv.getIdleUnits()
if(idleUnits.none()) return@onClick
if (idleUnits.none()) return@onClick
val unitToSelect: MapUnit
if (unitTable.selectedUnit==null || !idleUnits.contains(unitTable.selectedUnit!!))
if (unitTable.selectedUnit == null || !idleUnits.contains(unitTable.selectedUnit!!))
unitToSelect = idleUnits.first()
else {
var index = idleUnits.indexOf(unitTable.selectedUnit!!)
if(previous) index-- else index++
if (previous) index-- else index++
index += idleUnits.count()
index %= idleUnits.count() // for looping
unitToSelect = idleUnits.elementAt(index)
@ -46,10 +46,8 @@ class IdleUnitButton (
unitToSelect.due = false
tileMapHolder.setCenterPosition(unitToSelect.currentTile.position)
unitTable.selectedCity = null // need to deselect city, so that units on cities show their tiles
unitTable.selectedUnit = unitToSelect
unitTable.worldScreen.shouldUpdate=true
unitTable.selectUnits(unitToSelect)
unitTable.worldScreen.shouldUpdate = true
}
}

View file

@ -14,8 +14,6 @@ import com.unciv.models.UncivSound
import com.unciv.models.UnitAction
import com.unciv.models.UnitActionType
import com.unciv.models.ruleset.Building
import com.unciv.models.translations.equalsPlaceholderText
import com.unciv.models.translations.getPlaceholderParameters
import com.unciv.models.translations.tr
import com.unciv.ui.pickerscreens.ImprovementPickerScreen
import com.unciv.ui.pickerscreens.PromotionPickerScreen
@ -287,7 +285,7 @@ object UnitActions {
type = UnitActionType.ConstructImprovement,
isCurrentAction = unit.currentTile.hasImprovementInProgress(),
action = {
worldScreen.game.setScreen(ImprovementPickerScreen(tile) { unitTable.selectedUnit = null })
worldScreen.game.setScreen(ImprovementPickerScreen(tile) { unitTable.selectUnits() })
}.takeIf { canConstruct })
}
@ -445,7 +443,7 @@ object UnitActions {
uncivSound = UncivSound.Fortify,
action = {
unit.fortify()
unitTable.selectedUnit = null
unitTable.selectUnits()
}.takeIf { unit.currentMovement > 0 })
if (unit.health < 100) {
@ -454,7 +452,7 @@ object UnitActions {
title = UnitActionType.FortifyUntilHealed.value,
action = {
unit.fortifyUntilHealed()
unitTable.selectedUnit = null
unitTable.selectUnits()
}.takeIf { unit.currentMovement > 0 })
actionList += actionForWounded
}
@ -471,7 +469,7 @@ object UnitActions {
isCurrentAction = isSleeping,
action = {
unit.action = Constants.unitActionSleep
unitTable.selectedUnit = null
unitTable.selectUnits()
}.takeIf { !isSleeping })
if (unit.health < 100 && !isSleeping) {
@ -480,7 +478,7 @@ object UnitActions {
title = UnitActionType.SleepUntilHealed.value,
action = {
unit.action = Constants.unitActionSleepUntilHealed
unitTable.selectedUnit = null
unitTable.selectUnits()
})
actionList += actionForWounded
}

View file

@ -22,10 +22,21 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
private val unitNameLabel = "".toLabel()
private val promotionsTable = Table()
private val unitDescriptionTable = Table(CameraStageBaseScreen.skin)
var selectedUnit : MapUnit? = null
val selectedUnit : MapUnit?
get() = selectedUnits.firstOrNull()
/** This is in preparation for multi-select and multi-move */
val selectedUnits = ArrayList<MapUnit>()
/** Sending no units clears the selected units entirely */
fun selectUnits(vararg units:MapUnit) {
selectedUnits.clear()
selectedCity = null
for (unit in units) selectedUnits.add(unit)
}
var selectedCity : CityInfo? = null
val deselectUnitButton = Table()
val helpUnitButton = Table()
// This is so that not on every update(), we will update the unit table.
// Most of the time it's the same unit with the same stats so why waste precious time?
@ -45,7 +56,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
deselectUnitButton.add(ImageGetter.getImage("OtherIcons/Close")).size(20f).pad(10f)
deselectUnitButton.pack()
deselectUnitButton.touchable = Touchable.enabled
deselectUnitButton.onClick { selectedUnit=null; selectedCity=null; worldScreen.shouldUpdate=true;this@UnitTable.isVisible=false }
deselectUnitButton.onClick { selectUnits(); worldScreen.shouldUpdate=true; this@UnitTable.isVisible=false }
addActor(deselectUnitButton)
}).left()
@ -75,12 +86,10 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
if(selectedUnit!=null) {
isVisible=true
if (selectedUnit!!.civInfo != worldScreen.viewingCiv && !worldScreen.viewingCiv.isSpectator()) { // The unit that was selected, was captured. It exists but is no longer ours.
selectedUnit = null
selectedCity = null
selectUnits()
selectedUnitHasChanged = true
} else if (selectedUnit!! !in selectedUnit!!.getTile().getUnits()) { // The unit that was there no longer exists}
selectedUnit = null
selectedCity = null
selectUnits()
selectedUnitHasChanged = true
}
}
@ -183,9 +192,9 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
}
fun citySelected(cityInfo: CityInfo) : Boolean {
selectUnits()
if (cityInfo == selectedCity) return false
selectedCity = cityInfo
selectedUnit = null
selectedUnitHasChanged = true
worldScreen.shouldUpdate = true
return true
@ -202,20 +211,18 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
else if(selectedTile.militaryUnit!=null
&& (selectedTile.militaryUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator())
&& selectedUnit!=selectedTile.militaryUnit
&& (selectedTile.civilianUnit==null || selectedUnit!=selectedTile.civilianUnit)){
selectedUnit = selectedTile.militaryUnit
selectedCity = null
&& (selectedTile.civilianUnit==null || selectedUnit!=selectedTile.civilianUnit)) {
selectUnits(selectedTile.militaryUnit!!)
}
else if (selectedTile.civilianUnit!=null
&& (selectedTile.civilianUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator())
&& selectedUnit!=selectedTile.civilianUnit){
selectedUnit = selectedTile.civilianUnit
selectedCity = null
&& selectedUnit!=selectedTile.civilianUnit) {
selectUnits(selectedTile.civilianUnit!!)
} else if(selectedTile == previouslySelectedUnit?.currentTile) {
// tapping the same tile again will deselect a unit.
// important for single-tap-move to abort moving easily
selectedUnit = null
isVisible=false
selectUnits()
isVisible = false
}
if(selectedUnit != previouslySelectedUnit)