Unciv/core/src/com/unciv/ui/LanguagePickerScreen.kt

115 lines
4.1 KiB
Kotlin
Raw Normal View History

2018-07-26 20:46:14 +00:00
package com.unciv.ui
import com.badlogic.gdx.Gdx
2018-07-26 20:46:14 +00:00
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Label
2018-07-26 20:46:14 +00:00
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.UnCivGame
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.tr
2018-07-26 20:46:14 +00:00
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.*
import com.unciv.ui.worldscreen.optionstable.PopupTable
import com.unciv.ui.worldscreen.optionstable.YesNoPopupTable
2018-07-26 20:46:14 +00:00
class LanguageTable(val language:String,skin: Skin):Table(skin){
private val blue = ImageGetter.getBlue()
private val darkBlue = blue.cpy().lerp(Color.BLACK,0.5f)!!
val percentComplete: Int
2018-07-26 20:46:14 +00:00
init{
pad(10f)
defaults().pad(10f)
if(ImageGetter.imageExists("FlagIcons/$language"))
add(ImageGetter.getImage("FlagIcons/$language")).size(40f)
val availableTranslations = GameBasics.Translations.filter { it.value.containsKey(language) }
if(language=="English") percentComplete = 100
else percentComplete = (availableTranslations.size*100 / GameBasics.Translations.size) - 5
add("$language ($percentComplete%)")
update("")
touchable = Touchable.enabled // so click listener is activated when any part is clicked, not only children
2018-07-26 20:46:14 +00:00
pack()
}
2018-07-26 20:46:14 +00:00
fun update(chosenLanguage:String){
background = ImageGetter.getBackground( if(chosenLanguage==language) blue else darkBlue)
}
2018-07-26 20:46:14 +00:00
}
class LanguagePickerScreen: PickerScreen(){
var chosenLanguage = "English"
private val languageTables = ArrayList<LanguageTable>()
fun update(){
languageTables.forEach { it.update(chosenLanguage) }
}
init {
closeButton.isVisible = false
topTable.add(Label(
"Please note that translations are a " +
"community-based work in progress and are INCOMPLETE! \n" +
"The percentage shown is how much of the language is translated in-game.\n" +
"If you want to help translating the game " +
"into your language, send me an email to yairm210@hotmail.com!",skin)).pad(10f).row()
languageTables.addAll(GameBasics.Translations.getLanguages().map { LanguageTable(it,skin) }
.sortedByDescending { it.percentComplete } )
languageTables.forEach {
2018-09-23 07:39:56 +00:00
it.onClick {
chosenLanguage = it.language
2018-07-26 20:46:14 +00:00
rightSideButton.enable()
update()
}
topTable.add(it).pad(10f).row()
2018-07-26 20:46:14 +00:00
}
rightSideButton.setText("Pick language".tr())
2018-09-23 07:39:56 +00:00
rightSideButton.onClick {
if (Fonts().containsFont(Fonts().getFontForLanguage(chosenLanguage)))
pickLanguage()
else {
YesNoPopupTable("This language requires you to download fonts.\n" +
"Do you want to download fonts for $chosenLanguage?",
{
val downloading = PopupTable()
downloading.add(Label("Downloading...",skin))
downloading.pack()
downloading.center(stage)
stage.addActor(downloading)
Gdx.input.inputProcessor = null // no interaction until download is over
kotlin.concurrent.thread {
Fonts().downloadFontForLanguage(chosenLanguage)
shouldPickLanguage = true
}
},this)
}
2018-07-26 20:46:14 +00:00
}
}
fun pickLanguage(){
UnCivGame.Current.settings.language = chosenLanguage
UnCivGame.Current.settings.save()
CameraStageBaseScreen.resetFonts()
UnCivGame.Current.startNewGame()
dispose()
}
var shouldPickLanguage=false
override fun render(delta: Float) {
if(shouldPickLanguage)
pickLanguage()
super.render(delta)
}
2018-07-26 20:46:14 +00:00
}