chore: Moved TranslatedSelectBox to its own file in ui.components - see #10031

This commit is contained in:
Yair Morgenstern 2023-09-04 14:40:59 +03:00
parent d19281e0e6
commit 743c90d454
11 changed files with 36 additions and 29 deletions

View File

@ -2,7 +2,7 @@ package com.unciv.models.metadata
import com.badlogic.gdx.Gdx
import com.unciv.json.json
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.screens.pickerscreens.Github

View File

@ -0,0 +1,27 @@
package com.unciv.ui.components
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.utils.Array
import com.unciv.models.translations.tr
class TranslatedSelectBox(values : Collection<String>, default:String, skin: Skin) : SelectBox<TranslatedSelectBox.TranslatedString>(skin) {
class TranslatedString(val value: String) {
val translation = value.tr()
override fun toString() = translation
// Equality contract needs to be implemented else TranslatedSelectBox.setSelected won't work properly
override fun equals(other: Any?): Boolean = other is TranslatedString && value == other.value
override fun hashCode() = value.hashCode()
}
init {
val array = Array<TranslatedString>()
values.forEach { array.add(TranslatedString(it)) }
items = array
selected = array.firstOrNull { it.value == default } ?: array.first()
}
fun setSelected(newValue: String) {
selected = items.firstOrNull { it == TranslatedString(newValue) } ?: return
}
}

View File

@ -23,7 +23,7 @@ import com.unciv.ui.components.extensions.toTextButton
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popups.ConfirmPopup
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.screens.worldscreen.NotificationsScroll
import com.unciv.utils.Display
import com.unciv.utils.ScreenMode

View File

@ -23,7 +23,7 @@ import com.unciv.ui.components.input.onClick
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.popups.ToastPopup
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.utils.Concurrency
import com.unciv.utils.Log
import com.unciv.utils.debug

View File

@ -10,7 +10,7 @@ import com.unciv.models.ruleset.RulesetCache
import com.unciv.ui.screens.mapeditorscreen.MapEditorScreen
import com.unciv.ui.screens.mapeditorscreen.TileInfoNormalizer
import com.unciv.ui.screens.newgamescreen.ModCheckboxTable
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.popups.Popup
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.components.TabbedPager

View File

@ -17,7 +17,7 @@ import com.unciv.ui.components.input.onActivation
import com.unciv.ui.components.input.onChange
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import kotlin.math.sign
/**

View File

@ -19,6 +19,7 @@ import com.unciv.ui.audio.MusicMood
import com.unciv.ui.audio.MusicTrackChooserFlags
import com.unciv.ui.components.AutoScrollPane
import com.unciv.ui.components.ExpanderTab
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.components.UncivSlider
import com.unciv.ui.components.extensions.pad
import com.unciv.ui.components.extensions.toCheckBox

View File

@ -2,6 +2,7 @@ package com.unciv.ui.screens.newgamescreen
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.logic.map.MapGeneratedMainType
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.components.extensions.toLabel
import com.unciv.ui.components.input.onChange
import com.unciv.ui.screens.basescreen.BaseScreen

View File

@ -15,6 +15,7 @@ import com.unciv.logic.map.MapSize
import com.unciv.logic.map.MapSizeNew
import com.unciv.logic.map.MapType
import com.unciv.ui.components.ExpanderTab
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.components.UncivSlider
import com.unciv.ui.components.UncivTextField
import com.unciv.ui.components.WrappableLabel

View File

@ -2,10 +2,7 @@ package com.unciv.ui.screens.newgamescreen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup
import com.badlogic.gdx.utils.Array
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.logic.GameInfo
@ -411,23 +408,3 @@ class NewGameScreen(
override fun recreate(): BaseScreen = NewGameScreen(gameSetupInfo)
}
class TranslatedSelectBox(values : Collection<String>, default:String, skin: Skin) : SelectBox<TranslatedSelectBox.TranslatedString>(skin) {
class TranslatedString(val value: String) {
val translation = value.tr()
override fun toString() = translation
// Equality contract needs to be implemented else TranslatedSelectBox.setSelected won't work properly
override fun equals(other: Any?): Boolean = other is TranslatedString && value == other.value
override fun hashCode() = value.hashCode()
}
init {
val array = Array<TranslatedString>()
values.forEach { array.add(TranslatedString(it)) }
items = array
selected = array.firstOrNull { it.value == default } ?: array.first()
}
fun setSelected(newValue: String) {
selected = items.firstOrNull { it == TranslatedString(newValue) } ?: return
}
}

View File

@ -14,7 +14,7 @@ import com.unciv.ui.components.extensions.packIfNeeded
import com.unciv.ui.components.input.OnClickListener
import com.unciv.ui.images.ImageGetter
import com.unciv.ui.screens.basescreen.BaseScreen
import com.unciv.ui.screens.newgamescreen.TranslatedSelectBox
import com.unciv.ui.components.TranslatedSelectBox
import com.unciv.ui.screens.victoryscreen.VictoryScreenCivGroup.DefeatedPlayerStyle
import com.unciv.ui.screens.worldscreen.WorldScreen