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) stage.removeListener(keyListener)
game.setWorldScreen() game.setWorldScreen()
game.worldScreen.mapHolder.setCenterPosition(city.location) game.worldScreen.mapHolder.setCenterPosition(city.location)
game.worldScreen.bottomUnitTable.selectedUnit=null game.worldScreen.bottomUnitTable.selectUnits()
} }
fun page(delta: Int) { fun page(delta: Int) {
val civInfo = city.civInfo val civInfo = city.civInfo

View file

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

View file

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

View file

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

View file

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

View file

@ -22,10 +22,21 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
private val unitNameLabel = "".toLabel() private val unitNameLabel = "".toLabel()
private val promotionsTable = Table() private val promotionsTable = Table()
private val unitDescriptionTable = Table(CameraStageBaseScreen.skin) 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 var selectedCity : CityInfo? = null
val deselectUnitButton = Table() val deselectUnitButton = Table()
val helpUnitButton = Table()
// This is so that not on every update(), we will update the unit 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? // 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.add(ImageGetter.getImage("OtherIcons/Close")).size(20f).pad(10f)
deselectUnitButton.pack() deselectUnitButton.pack()
deselectUnitButton.touchable = Touchable.enabled 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) addActor(deselectUnitButton)
}).left() }).left()
@ -75,12 +86,10 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
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.
selectedUnit = null selectUnits()
selectedCity = null
selectedUnitHasChanged = true selectedUnitHasChanged = true
} else if (selectedUnit!! !in selectedUnit!!.getTile().getUnits()) { // The unit that was there no longer exists} } else if (selectedUnit!! !in selectedUnit!!.getTile().getUnits()) { // The unit that was there no longer exists}
selectedUnit = null selectUnits()
selectedCity = null
selectedUnitHasChanged = true selectedUnitHasChanged = true
} }
} }
@ -183,9 +192,9 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
} }
fun citySelected(cityInfo: CityInfo) : Boolean { fun citySelected(cityInfo: CityInfo) : Boolean {
selectUnits()
if (cityInfo == selectedCity) return false if (cityInfo == selectedCity) return false
selectedCity = cityInfo selectedCity = cityInfo
selectedUnit = null
selectedUnitHasChanged = true selectedUnitHasChanged = true
worldScreen.shouldUpdate = true worldScreen.shouldUpdate = true
return true return true
@ -202,20 +211,18 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
else if(selectedTile.militaryUnit!=null else if(selectedTile.militaryUnit!=null
&& (selectedTile.militaryUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator()) && (selectedTile.militaryUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator())
&& selectedUnit!=selectedTile.militaryUnit && selectedUnit!=selectedTile.militaryUnit
&& (selectedTile.civilianUnit==null || selectedUnit!=selectedTile.civilianUnit)){ && (selectedTile.civilianUnit==null || selectedUnit!=selectedTile.civilianUnit)) {
selectedUnit = selectedTile.militaryUnit selectUnits(selectedTile.militaryUnit!!)
selectedCity = null
} }
else if (selectedTile.civilianUnit!=null else if (selectedTile.civilianUnit!=null
&& (selectedTile.civilianUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator()) && (selectedTile.civilianUnit!!.civInfo == worldScreen.viewingCiv || worldScreen.viewingCiv.isSpectator())
&& selectedUnit!=selectedTile.civilianUnit){ && selectedUnit!=selectedTile.civilianUnit) {
selectedUnit = selectedTile.civilianUnit selectUnits(selectedTile.civilianUnit!!)
selectedCity = null
} else if(selectedTile == previouslySelectedUnit?.currentTile) { } else if(selectedTile == previouslySelectedUnit?.currentTile) {
// tapping the same tile again will deselect a unit. // tapping the same tile again will deselect a unit.
// important for single-tap-move to abort moving easily // important for single-tap-move to abort moving easily
selectedUnit = null selectUnits()
isVisible=false isVisible = false
} }
if(selectedUnit != previouslySelectedUnit) if(selectedUnit != previouslySelectedUnit)