Move from translated select box to regular one for unique builder

This commit is contained in:
yairm210 2024-07-13 23:40:40 +03:00
parent ee1663c51d
commit 7a57a6d28c
4 changed files with 29 additions and 22 deletions

View File

@ -1386,6 +1386,8 @@ enum class UniqueType(
fun canAcceptUniqueTarget(uniqueTarget: UniqueTarget) =
targetTypes.any { uniqueTarget.canAcceptUniqueTarget(it) }
override fun toString() = text
companion object {
val uniqueTypeMap: Map<String, UniqueType> = entries.associateBy { it.placeholderText }
}

View File

@ -20,11 +20,13 @@ import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup
import com.badlogic.gdx.utils.Align
import com.badlogic.gdx.utils.Array
import com.unciv.Constants
import com.unciv.models.translations.tr
import com.unciv.ui.components.extensions.GdxKeyCodeFixes.DEL
@ -234,6 +236,12 @@ fun <T : Actor> Cell<T>.pad(vertical: Float, horizontal: Float): Cell<T> {
return pad(vertical, horizontal, vertical, horizontal)
}
fun <T> SelectBox<T>.setItems(newItems: Collection<T>){
val array = Array<T>()
newItems.forEach { array.add(it) }
items = array
}
/** Sets both the width and height to [size] */
fun Image.setSize(size: Float) {
setSize(size, size)

View File

@ -1,8 +1,8 @@
package com.unciv.ui.components.widgets
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.utils.Array
import com.unciv.models.translations.tr
import com.unciv.ui.components.extensions.setItems
import com.unciv.ui.screens.basescreen.BaseScreen
class TranslatedSelectBox(values: Collection<String>, default: String) : SelectBox<TranslatedSelectBox.TranslatedString>(BaseScreen.skin) {
@ -15,10 +15,8 @@ class TranslatedSelectBox(values: Collection<String>, default: String) : SelectB
}
init {
val array = Array<TranslatedString>()
values.forEach { array.add(TranslatedString(it)) }
items = array
selected = array.firstOrNull { it.value == default } ?: array.first()
setItems(values.map { TranslatedString(it) })
selected = items.firstOrNull { it.value == default } ?: items.first()
}
fun setSelected(newValue: String) {

View File

@ -3,6 +3,7 @@ package com.unciv.ui.screens
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextField
import com.unciv.models.ruleset.Ruleset
@ -12,14 +13,11 @@ import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.ruleset.validation.UniqueValidator
import com.unciv.models.translations.fillPlaceholders
import com.unciv.models.translations.getPlaceholderParameters
import com.unciv.ui.components.extensions.enable
import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.extensions.toTextButton
import com.unciv.ui.components.extensions.*
import com.unciv.ui.components.input.onChange
import com.unciv.ui.components.input.onClick
import com.unciv.ui.components.widgets.LanguageTable
import com.unciv.ui.components.widgets.LanguageTable.Companion.addLanguageTables
import com.unciv.ui.components.widgets.TranslatedSelectBox
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popups.options.OptionsPopup
import com.unciv.ui.screens.basescreen.BaseScreen
@ -74,23 +72,23 @@ class UniqueTable(isMainUnique: Boolean, val ruleset: Ruleset, stage: Stage,
private val uniqueErrorTable = Table().apply { defaults().pad(5f) }
val uniqueTextField = TextField("Unique", BaseScreen.skin)
private var uniqueTargetsSelectBox: TranslatedSelectBox
private var uniqueTargetsSelectBox: SelectBox<UniqueTarget>
init {
this.stage = stage // required for width
// Main unique should be non-modifier
val uniqueTargets = if (isMainUnique) UniqueTarget.entries
.filter { it.modifierType == UniqueTarget.ModifierType.None }
.map { it.name }
// Additional ones should be modifiers
else UniqueTarget.entries
.filter { it.modifierType != UniqueTarget.ModifierType.None }
.map { it.name }
defaults().pad(10f)
background = ImageGetter.getWhiteDotDrawable().tint(Color.BLACK.cpy().apply { a=0.3f })
val uniqueTargetSelectBoxTable = Table().apply { defaults().pad(5f) }
uniqueTargetsSelectBox = TranslatedSelectBox(uniqueTargets, UniqueTarget.Global.name)
uniqueTargetsSelectBox = SelectBox<UniqueTarget>(BaseScreen.skin)
uniqueTargetsSelectBox.setItems(uniqueTargets)
uniqueTargetsSelectBox.selected = UniqueTarget.Global
uniqueTargetSelectBoxTable.add(uniqueTargetsSelectBox)
uniqueTargetSelectBoxTable.add(uniqueSelectBoxTable).row()
@ -116,28 +114,28 @@ class UniqueTable(isMainUnique: Boolean, val ruleset: Ruleset, stage: Stage,
}
private fun onUniqueTargetChange(
uniqueTargetsSelectBox: TranslatedSelectBox,
uniqueTargetsSelectBox: SelectBox<UniqueTarget>,
ruleset: Ruleset,
) {
val selected = UniqueTarget.entries.first { it.name == uniqueTargetsSelectBox.selected.value }
val selected = uniqueTargetsSelectBox.selected
val uniquesForTarget = UniqueType.entries.filter { it.canAcceptUniqueTarget(selected) }
val uniqueSelectBox = TranslatedSelectBox(uniquesForTarget.filter { it.getDeprecationAnnotation() == null }
.map { it.name }, uniquesForTarget.first().name)
val uniqueSelectBox = SelectBox<UniqueType>(BaseScreen.skin)
uniqueSelectBox.setItems(uniquesForTarget.filter { it.getDeprecationAnnotation() == null })
uniqueSelectBox.onChange {
onUniqueSelected(uniqueSelectBox, uniqueTextField, ruleset, parameterSelectBoxTable)
}
onUniqueSelected(uniqueSelectBox, uniqueTextField, ruleset, parameterSelectBoxTable)
uniqueSelectBoxTable.clear()
uniqueSelectBoxTable.add(uniqueSelectBox)
uniqueSelectBoxTable.add(uniqueSelectBox).width(stage.width * 0.5f)
}
private fun onUniqueSelected(
uniqueSelectBox: TranslatedSelectBox,
uniqueSelectBox: SelectBox<UniqueType>,
uniqueTextField: TextField,
ruleset: Ruleset,
parameterSelectBoxTable: Table
) {
val uniqueType = UniqueType.entries.first { it.name == uniqueSelectBox.selected.value }
val uniqueType = uniqueSelectBox.selected
uniqueTextField.text = uniqueType.text
updateUnique(ruleset, uniqueTextField)
@ -150,10 +148,11 @@ class UniqueTable(isMainUnique: Boolean, val ruleset: Ruleset, stage: Stage,
.flatMap { it.getKnownValuesForAutocomplete(ruleset) }.toSet()
if (knownParamValues.isNotEmpty()) {
val paramSelectBox = TranslatedSelectBox(knownParamValues.toList(), knownParamValues.first())
val paramSelectBox = SelectBox<String>(BaseScreen.skin)
paramSelectBox.setItems(knownParamValues)
paramSelectBox.onChange {
val currentParams = uniqueTextField.text.getPlaceholderParameters().toMutableList()
currentParams[index] = paramSelectBox.selected.value
currentParams[index] = paramSelectBox.selected
val newText = uniqueType.text.fillPlaceholders(*currentParams.toTypedArray())
uniqueTextField.text = newText
updateUnique(ruleset, uniqueTextField)