Newgame screens and mods (#2250)

* Recreated diff for #2231

* Refactor for code readability. 4 loops instead of 2, but they hardly hurt
This commit is contained in:
proteus-anguinus 2020-03-25 10:14:53 +01:00 committed by GitHub
parent f22a5723f8
commit 8ebbd881d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 8 deletions

View file

@ -30,7 +30,7 @@ class NewGameScreen: PickerScreen(){
scrollPane.setScrollingDisabled(true,true)
val playerPickerTable = PlayerPickerTable(this, newGameParameters)
val newGameScreenOptionsTable = NewGameScreenOptionsTable(this) { playerPickerTable.update() }
val newGameScreenOptionsTable = NewGameScreenOptionsTable(this) { desiredCiv: String -> playerPickerTable.update(desiredCiv) }
topTable.add(ScrollPane(newGameScreenOptionsTable).apply{setOverscroll(false,false)}).height(topTable.parent.height)
topTable.add(playerPickerTable).height(topTable.parent.height)
topTable.pack()

View file

@ -18,7 +18,7 @@ import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.toLabel
class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlayerPickerTable:()->Unit)
class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlayerPickerTable:(desiredCiv:String)->Unit)
: Table(CameraStageBaseScreen.skin) {
val newGameParameters = newGameScreen.newGameParameters
val mapParameters = newGameScreen.mapParameters
@ -134,7 +134,7 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay
isOnlineMultiplayerCheckbox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
newGameParameters.isOnlineMultiplayer = isOnlineMultiplayerCheckbox.isChecked
updatePlayerPickerTable()
updatePlayerPickerTable("")
}
})
add(isOnlineMultiplayerCheckbox).colspan(2).row()
@ -233,16 +233,24 @@ class NewGameScreenOptionsTable(val newGameScreen: NewGameScreen, val updatePlay
ImageGetter.setTextureRegionDrawables()
}
add("{Mods}:".tr()).colspan(2).row()
add("{Mods}:".tr().toLabel(fontSize = 24)).padTop(16f).colspan(2).row()
val modCheckboxTable = Table().apply { defaults().pad(5f) }
for(mod in modRulesets){
val checkBox = CheckBox(mod.name,CameraStageBaseScreen.skin)
if (mod.name in newGameParameters.mods) checkBox.isChecked = true
checkBox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
if(checkBox.isChecked) newGameParameters.mods.add(mod.name)
else newGameParameters.mods.remove(mod.name)
reloadMods()
updatePlayerPickerTable()
var desiredCiv = ""
if (checkBox.isChecked){
val modNations = RulesetCache[mod.name]?.nations
if (modNations != null && modNations.size > 0) {
desiredCiv = modNations.keys.first()
}
}
updatePlayerPickerTable(desiredCiv)
}
})
modCheckboxTable.add(checkBox).row()

View file

@ -28,12 +28,14 @@ class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters:
update()
}
fun update() {
fun update(desiredCiv: String = "") {
playerListTable.clear()
val gameBasics = newGameScreen.ruleset // the mod picking changes this ruleset
reassignRemovedModReferences()
if (desiredCiv.isNotEmpty()) assignDesiredCiv(desiredCiv)
for (player in newGameParameters.players) {
if(!newGameScreen.ruleset.nations.containsKey(player.chosenCiv)) // this was in a mod we disabled
player.chosenCiv="Random"
playerListTable.add(getPlayerTable(player, gameBasics)).pad(10f).row()
}
if(newGameParameters.players.count() < gameBasics.nations.values.count { it.isMajorCiv() }) {
@ -43,6 +45,20 @@ class PlayerPickerTable(val newGameScreen: NewGameScreen, val newGameParameters:
newGameScreen.setNewGameButtonEnabled(newGameParameters.players.size>1)
}
private fun reassignRemovedModReferences() {
for (player in newGameParameters.players) {
if (!newGameScreen.ruleset.nations.containsKey(player.chosenCiv))
player.chosenCiv = "Random"
}
}
private fun assignDesiredCiv(desiredCiv: String) {
// No auto-select if desiredCiv already used
if (newGameParameters.players.any {it.chosenCiv == desiredCiv}) return
// Do auto-select, silently no-op if no suitable slot (human with 'random' choice)
newGameParameters.players.firstOrNull { it.chosenCiv == "Random" && it.playerType==PlayerType.Human }?.chosenCiv = desiredCiv
}
fun getPlayerTable(player: Player, ruleset: Ruleset): Table {
val playerTable = Table()
playerTable.pad(5f)