mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-24 03:53:12 -04:00
Fix number popup with commas in the number (#13495)
* Fix trade gold popup with locale code * Be verbose and rename the function to toIntOrNullTranslated()
This commit is contained in:
parent
50de8e65c6
commit
d18b78d5f4
@ -12,6 +12,7 @@ import com.unciv.ui.components.fonts.DiacriticSupport
|
||||
import com.unciv.ui.components.fonts.FontRulesetIcons
|
||||
import com.unciv.utils.Log
|
||||
import com.unciv.utils.debug
|
||||
import java.text.ParseException
|
||||
import java.util.Locale
|
||||
import org.jetbrains.annotations.VisibleForTesting
|
||||
|
||||
@ -548,7 +549,39 @@ fun Number.tr(): String {
|
||||
return UncivGame.Current.settings.getCurrentNumberFormat().format(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the string as an integer using the current number format.
|
||||
*
|
||||
* Empty strings result in 0.
|
||||
*
|
||||
* @return The integer value, or null if parsing fails.
|
||||
*/
|
||||
fun String.toIntOrNullTranslated(): Int? {
|
||||
if (isEmpty()) return 0
|
||||
return try {
|
||||
UncivGame.Current.settings.getCurrentNumberFormat().parse(this).toInt()
|
||||
} catch (_: ParseException) {
|
||||
this.toIntOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
// formats number according to given language
|
||||
fun Number.tr(language: String): String {
|
||||
return LocaleCode.getNumberFormatFromLanguage(language).format(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the string as an integer using the current number format.
|
||||
*
|
||||
* Empty strings result in 0.
|
||||
*
|
||||
* @return The integer value, or null if parsing fails.
|
||||
*/
|
||||
fun String.toIntOrNullTranslated(language: String): Int? {
|
||||
if (isEmpty()) return 0
|
||||
return try {
|
||||
LocaleCode.getNumberFormatFromLanguage(language).parse(this).toInt()
|
||||
} catch (_: ParseException) {
|
||||
this.toIntOrNull()
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Button
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextField
|
||||
import com.unciv.models.translations.toIntOrNullTranslated
|
||||
import com.unciv.models.translations.tr
|
||||
import com.unciv.ui.components.widgets.UncivTextField
|
||||
import com.unciv.ui.components.input.onChange
|
||||
@ -66,13 +67,11 @@ class AskNumberPopup(
|
||||
val nameField = UncivTextField(label, defaultValue)
|
||||
nameField.textFieldFilter = TextField.TextFieldFilter { _, char -> char.isDigit() || char == '-' }
|
||||
|
||||
fun isValidInt(input: String): Boolean {
|
||||
return input.toIntOrNull() != null
|
||||
}
|
||||
|
||||
fun isValidInt(input: String): Boolean = input.toIntOrNullTranslated() != null
|
||||
fun getInt(input: String): Int? = input.toIntOrNullTranslated()
|
||||
|
||||
fun clampInBounds(input: String): String {
|
||||
val int = input.toIntOrNull() ?: return input
|
||||
val int = getInt(input) ?: return input
|
||||
|
||||
if (bounds.first > int) {
|
||||
return bounds.first.tr()
|
||||
@ -97,7 +96,7 @@ class AskNumberPopup(
|
||||
).apply {
|
||||
onClick {
|
||||
if (isValidInt(nameField.text))
|
||||
nameField.text = clampInBounds((nameField.text.toInt() + value).tr())
|
||||
nameField.text = clampInBounds((getInt(nameField.text)!! + value).tr())
|
||||
}
|
||||
}
|
||||
).pad(5f)
|
||||
@ -121,12 +120,12 @@ class AskNumberPopup(
|
||||
addCloseButton()
|
||||
addOKButton(
|
||||
validate = {
|
||||
val errorFound = !isValidInt(nameField.text) || !validate(nameField.text.toInt())
|
||||
val errorFound = getInt(nameField.text)?.let { validate(it) } != true
|
||||
if (errorFound) add(errorLabel).colspan(2).center()
|
||||
!errorFound
|
||||
}
|
||||
) {
|
||||
actionOnOk(nameField.text.toInt())
|
||||
actionOnOk(getInt(nameField.text)!!)
|
||||
}
|
||||
equalizeLastTwoButtonWidths()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user