Nicer multiplayer settings (#7198)

* Nicer multiplayer settings

* Refactor: use shortcut method
This commit is contained in:
Timo T 2022-06-19 20:47:36 +02:00 committed by GitHub
parent c2c991f8b8
commit 6d6b390ef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 22 deletions

View File

@ -12,8 +12,11 @@ import com.unciv.models.metadata.GameSetting
import com.unciv.models.metadata.GameSettings
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.RulesetCache
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popup.Popup
import com.unciv.ui.utils.BaseScreen
import com.unciv.ui.utils.extensions.addSeparator
import com.unciv.ui.utils.extensions.brighten
import com.unciv.ui.utils.extensions.format
import com.unciv.ui.utils.extensions.isEnabled
import com.unciv.ui.utils.extensions.onChange
@ -40,6 +43,8 @@ fun multiplayerTab(
settings.multiplayer::statusButtonInSinglePlayer, updateWorld = true
)
addSeparator(tab)
val curRefreshSelect = RefreshSelect(
"Update status of currently played game every:",
createRefreshOptions(ChronoUnit.SECONDS, 3, 5),
@ -47,7 +52,7 @@ fun multiplayerTab(
GameSetting.MULTIPLAYER_CURRENT_GAME_REFRESH_DELAY,
settings
)
curRefreshSelect.addTo(tab)
addSelectAsSeparateTable(tab, curRefreshSelect)
val allRefreshSelect = RefreshSelect(
"In-game, update status of all games every:",
@ -56,21 +61,27 @@ fun multiplayerTab(
GameSetting.MULTIPLAYER_ALL_GAME_REFRESH_DELAY,
settings
)
allRefreshSelect.addTo(tab)
addSelectAsSeparateTable(tab, allRefreshSelect)
addSeparator(tab)
val turnCheckerSelect = addTurnCheckerOptions(tab, optionsPopup)
SettingsSelect("Sound notification for when it's your turn in your currently open game:",
addSeparator(tab)
addSelectAsSeparateTable(tab, SettingsSelect("Sound notification for when it's your turn in your currently open game:",
createNotificationSoundOptions(),
GameSetting.MULTIPLAYER_CURRENT_GAME_TURN_NOTIFICATION_SOUND,
settings
).addTo(tab)
))
SettingsSelect("Sound notification for when it's your turn in any other game:",
addSelectAsSeparateTable(tab, SettingsSelect("Sound notification for when it's your turn in any other game:",
createNotificationSoundOptions(),
GameSetting.MULTIPLAYER_OTHER_GAME_TURN_NOTIFICATION_SOUND,
settings
).addTo(tab)
))
addSeparator(tab)
addMultiplayerServerOptions(tab, optionsPopup, listOf(curRefreshSelect, allRefreshSelect, turnCheckerSelect).filterNotNull())
@ -127,7 +138,7 @@ private fun addMultiplayerServerOptions(
serverIpTable.add("Server address".toLabel().onClick {
multiplayerServerTextField.text = Gdx.app.clipboard.contents
}).row()
}).row()
multiplayerServerTextField.onChange {
val isCustomServer = OnlineMultiplayer.usesCustomServer()
connectionToServerButton.isEnabled = isCustomServer
@ -186,7 +197,7 @@ private fun addTurnCheckerOptions(
GameSetting.MULTIPLAYER_TURN_CHECKER_DELAY,
settings
)
turnCheckerSelect.addTo(tab)
addSelectAsSeparateTable(tab, turnCheckerSelect)
optionsPopup.addCheckbox(
@ -213,25 +224,23 @@ private class RefreshSelect(
dropboxOptions: List<SelectItem<Duration>>,
setting: GameSetting,
settings: GameSettings
) {
) : SettingsSelect<Duration>(labelText, getInitialOptions(extraCustomServerOptions, dropboxOptions), setting, settings) {
private val customServerItems = (extraCustomServerOptions + dropboxOptions).toGdxArray()
private val dropboxItems = dropboxOptions.toGdxArray()
private val settingsSelect: SettingsSelect<Duration>
init {
val initialOptions = if (OnlineMultiplayer.usesCustomServer()) customServerItems else dropboxItems
settingsSelect = SettingsSelect(labelText, initialOptions, setting, settings)
}
fun update(isCustomServer: Boolean) {
if (isCustomServer && settingsSelect.items.size != customServerItems.size) {
settingsSelect.replaceItems(customServerItems)
} else if (!isCustomServer && settingsSelect.items.size != dropboxItems.size) {
settingsSelect.replaceItems(dropboxItems)
if (isCustomServer && items.size != customServerItems.size) {
replaceItems(customServerItems)
} else if (!isCustomServer && items.size != dropboxItems.size) {
replaceItems(dropboxItems)
}
}
}
fun addTo(tab: Table) = settingsSelect.addTo(tab)
private fun getInitialOptions(extraCustomServerOptions: List<SelectItem<Duration>>, dropboxOptions: List<SelectItem<Duration>>): Iterable<SelectItem<Duration>> {
val customServerItems = (extraCustomServerOptions + dropboxOptions).toGdxArray()
val dropboxItems = dropboxOptions.toGdxArray()
return if (OnlineMultiplayer.usesCustomServer()) customServerItems else dropboxItems
}
private fun fixTextFieldUrlOnType(TextField: TextField) {
@ -260,3 +269,13 @@ private fun createRefreshOptions(unit: ChronoUnit, vararg options: Long): List<S
SelectItem(duration.format(), duration)
}
}
private fun addSelectAsSeparateTable(tab: Table, settingsSelect: SettingsSelect<*>) {
val table = Table()
settingsSelect.addTo(table)
tab.add(table).growX().fillX().row()
}
private fun addSeparator(tab: Table) {
tab.addSeparator(ImageGetter.getBlue().brighten(0.1f))
}

View File

@ -3,6 +3,7 @@ package com.unciv.ui.options
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
@ -191,10 +192,16 @@ open class SettingsSelect<T : Any>(
settings: GameSettings
) {
private val settingsProperty: KMutableProperty0<T> = setting.getProperty(settings)
private val label = labelText.toLabel()
private val label = createLabel(labelText)
protected val refreshSelectBox = createSelectBox(items.toGdxArray(), settings)
val items by refreshSelectBox::items
private fun createLabel(labelText: String): Label {
val selectLabel = labelText.toLabel()
selectLabel.wrap = true
return selectLabel
}
private fun createSelectBox(initialItems: Array<SelectItem<T>>, settings: GameSettings): SelectBox<SelectItem<T>> {
val selectBox = SelectBox<SelectItem<T>>(BaseScreen.skin)
selectBox.items = initialItems
@ -215,7 +222,7 @@ open class SettingsSelect<T : Any>(
}
fun addTo(table: Table) {
table.add(label).left()
table.add(label).growX().left()
table.add(refreshSelectBox).row()
}