Files
Terrarum/src/net/torvald/terrarum/modulebasegame/ui/UILoadManage.kt
2023-08-25 16:47:00 +09:00

295 lines
12 KiB
Kotlin

package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.App
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.gamecontroller.TerrarumKeyboardEvent
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.serialise.LoadSavegame
import net.torvald.terrarum.savegame.VDFileID.PLAYER_SCREENSHOT
import net.torvald.terrarum.tryDispose
import net.torvald.terrarum.ui.Toolkit
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.ui.UIItemTextButton
import net.torvald.terrarum.ui.UIItemTextLineInput
/**
* Created by minjaesong on 2023-07-05.
*/
class UILoadManage(val full: UILoadSavegame) : UICanvas() {
override var width: Int = Toolkit.drawWidth
override var height: Int = App.scr.height
private val buttonHeight = full.buttonHeight
private val buttonGap = full.buttonGap
private val buttonWidth = full.buttonWidth
private val drawX = full.drawX
private val hx = Toolkit.hdrawWidth
private val buttonX1third = hx - (buttonWidth * 1.5).toInt() - buttonGap
private val buttonXcentre = hx - (buttonWidth / 2)
private val buttonX3third = hx + (buttonWidth / 2) + buttonGap
private val buttonXleft = drawX + (240 - buttonWidth) / 2
private val buttonXright = drawX + 240 + (240 - buttonWidth) / 2
private val buttonRowY = full.buttonRowY - buttonHeight
private val buttonRowY2 = buttonRowY - buttonHeight - buttonGap
private val mainGoButton = UIItemTextButton(this,
{ Lang["MENU_IO_LOAD_GAME"] }, buttonX1third, buttonRowY2, buttonWidth * 3 + buttonGap * 2, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
App.printdbg(this, "Load playerUUID: ${UILoadGovernor.playerUUID}, worldUUID: ${UILoadGovernor.worldUUID}")
/*if (full.loadables.moreRecentAutosaveAvailable()) {
full.bringAutosaveSelectorUp()
full.changePanelTo(2)
}
else */if (full.loadables.saveAvaliable()) {
if (full.loadables.newerSaveIsDamaged) {
UILoadGovernor.previousSaveWasLoaded = true
}
// full.takeAutosaveSelectorDown()
full.loadManageSelectedGame = full.loadables.getLoadableSave()!!
mode = MODE_LOAD
}
}
}
private val mainNoGoButton = UIItemTextButton(this,
{ Lang["ERROR_SAVE_CORRUPTED"].replace(".","") }, buttonX1third, buttonRowY2, buttonWidth * 3 + buttonGap * 2, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.isEnabled = false
}
private val mainImportedPlayerCreateNewWorldButton = UIItemTextButton(this,
{ Lang["CONTEXT_WORLD_NEW"].replace(".","") }, buttonX1third, buttonRowY2, buttonWidth * 3 + buttonGap * 2, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
val playerDisk = full.loadables.getImportedPlayer()!!
full.remoCon.openUI(UINewWorld(full.remoCon, playerDisk))
}
}
private val mainRenameButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_RENAME"] }, buttonX1third, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
mode = MODE_RENAME
}
}
private val mainBackButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_BACK"] }, buttonXcentre, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
full.resetScroll()
full.changePanelTo(0)
}
}
private val mainDeleteButton = UIItemTextButton(this,
{ Lang["CONTEXT_CHARACTER_DELETE"] }, buttonX3third, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true, inactiveCol = Toolkit.Theme.COL_RED, activeCol = Toolkit.Theme.COL_REDD).also {
it.clickOnceListener = { _,_ ->
mode = MODE_DELETE
}
}
private val confirmDeleteButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_DELETE"] }, buttonXleft, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true, inactiveCol = Toolkit.Theme.COL_RED, activeCol = Toolkit.Theme.COL_REDD).also {
it.clickOnceListener = { _,_ ->
val pu = full.playerButtonSelected!!.playerUUID
val wu = full.playerButtonSelected!!.worldUUID
App.savegamePlayers[pu]?.moveToRecycle(App.recycledPlayersDir)?.let {
App.sortedPlayers.remove(pu)
App.savegamePlayers.remove(pu)
App.savegamePlayersName.remove(pu)
}
// don't delete the world please
full.remoCon.openUI(UILoadSavegame(full.remoCon))
}
}
private val confirmCancelButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_CANCEL"] }, buttonXright, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
mode = MODE_INIT
}
}
private val renameInput = UIItemTextLineInput(this, buttonXleft, App.scr.halfh, 240 + buttonWidth)
private val renameCancelButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_CANCEL"] }, buttonXleft, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
mode = MODE_INIT
}
}
private val renameRenameButton = UIItemTextButton(this,
{ Lang["MENU_LABEL_RENAME"] }, buttonXright, buttonRowY, buttonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true).also {
it.clickOnceListener = { _,_ ->
val newName = renameInput.getText().trim()
if (newName.isNotBlank()) {
full.playerButtonSelected!!.playerUUID.let { uuid ->
App.savegamePlayersName[uuid] = newName
App.savegamePlayers[uuid]!!.renamePlayer(newName)
full.playerButtonSelected!!.playerName = newName
}
}
mode = MODE_INIT
}
}
private var mode = 0
private var mainButtons0 = listOf(mainGoButton, mainBackButton, mainRenameButton, mainDeleteButton)
private var mainButtons1 = listOf(mainNoGoButton, mainBackButton, mainRenameButton, mainDeleteButton)
private var mainButtons2 = listOf(mainImportedPlayerCreateNewWorldButton, mainBackButton, mainRenameButton, mainDeleteButton)
private var delButtons = listOf(confirmCancelButton, confirmDeleteButton)
private var renameButtons = listOf(renameRenameButton, renameCancelButton)
private val mainButtons: List<UIItemTextButton>
get() = if (full.loadables.saveAvaliable()) mainButtons0 else if (full.loadables.isImported) mainButtons2 else mainButtons1
private val MODE_INIT = 0
private val MODE_DELETE = 16 // are you sure?
private val MODE_RENAME = 32 // show rename dialogue
private val MODE_LOAD = 256 // is needed to make the static loading screen
init {
}
private var screencap: TextureRegion? = null
private val screencapW = SAVE_THUMBNAIL_MAIN_WIDTH
private val screencapH = SAVE_THUMBNAIL_MAIN_HEIGHT
override fun doOpening(delta: Float) {
full.playerButtonSelected?.forceUnhighlight = true
full.playerButtonSelected?.let { button ->
screencap?.texture?.tryDispose()
button.savegameThumbnailPixmap?.let {
Texture(it).also {
it.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
screencap = TextureRegion(it)
}
}
}
}
override fun doClosing(delta: Float) {
full.playerButtonSelected?.forceUnhighlight = false
}
override fun show() {
super.show()
}
override fun updateUI(delta: Float) {
when (mode) {
MODE_INIT -> {
mainButtons.forEach { it.update(delta) }
}
MODE_DELETE -> {
delButtons.forEach { it.update(delta) }
}
MODE_RENAME -> {
renameButtons.forEach { it.update(delta) }
renameInput.update(delta)
}
}
}
override fun inputStrobed(e: TerrarumKeyboardEvent) {
when (mode) {
MODE_RENAME -> {
renameInput.inputStrobed(e)
}
}
}
private var loadFiredFrameCounter = 0
override fun renderUI(batch: SpriteBatch, camera: Camera) {
val buttonYdelta = (full.titleTopGradEnd) - full.playerButtonSelected!!.posY
full.playerButtonSelected!!.render(batch, camera, 0, buttonYdelta)
when (mode) {
MODE_INIT -> {
mainButtons.forEach { it.render(batch, camera) }
// draw thumbnails of the most recent game
val tex = screencap ?: CommonResourcePool.getAsTextureRegion("terrarum-defaultsavegamethumb")
if (full.loadables.saveAvaliable()) {
val tx = (Toolkit.drawWidth - screencapW) / 2
val tys = full.titleTopGradEnd + SAVE_CELL_HEIGHT + buttonGap
val tye = buttonRowY2 - buttonGap
val ty = tys + (tye - tys - SAVE_THUMBNAIL_MAIN_HEIGHT) / 2
batch.color = Toolkit.Theme.COL_INACTIVE
Toolkit.drawBoxBorder(batch, tx - 1, ty - 1, screencapW + 2, screencapH + 2)
batch.color = UIInventoryFull.CELL_COL
Toolkit.fillArea(batch, tx, ty, screencapW, screencapH)
batch.color = Color.WHITE
batch.draw(tex, tx.toFloat(), ty.toFloat(), screencapW.toFloat(), screencapH.toFloat())
}
}
MODE_DELETE -> {
Toolkit.drawTextCentered(batch, App.fontGame, Lang["MENU_LABEL_SAVE_WILL_BE_DELETED"], Toolkit.drawWidth, 0, full.titleTopGradEnd + full.cellInterval + SAVE_CELL_HEIGHT + 36)
Toolkit.drawTextCentered(batch, App.fontGame, Lang["MENU_LABEL_ARE_YOU_SURE"], Toolkit.drawWidth, 0, full.titleTopGradEnd + full.cellInterval + SAVE_CELL_HEIGHT + 36 + 24)
delButtons.forEach { it.render(batch, camera) }
}
MODE_RENAME -> {
renameInput.render(batch, camera)
renameButtons.forEach { it.render(batch, camera) }
}
MODE_LOAD -> {
loadFiredFrameCounter += 1
StaticLoadScreenSubstitute(batch)
if (loadFiredFrameCounter == 2) LoadSavegame(full.loadManageSelectedGame)
}
}
}
override fun dispose() {
screencap?.texture?.tryDispose()
}
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
when (mode) {
MODE_INIT -> {
mainButtons.forEach { it.touchDown(screenX, screenY, pointer, button) }
}
MODE_DELETE -> {
delButtons.forEach { it.touchDown(screenX, screenY, pointer, button) }
}
MODE_RENAME -> {
renameInput.touchDown(screenX, screenY, pointer, button)
renameButtons.forEach { it.touchDown(screenX, screenY, pointer, button) }
}
}
return true
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
when (mode) {
MODE_INIT -> {
mainButtons.forEach { it.touchUp(screenX, screenY, pointer, button) }
}
MODE_DELETE -> {
delButtons.forEach { it.touchUp(screenX, screenY, pointer, button) }
}
MODE_RENAME -> {
renameInput.touchUp(screenX, screenY, pointer, button)
renameButtons.forEach { it.touchUp(screenX, screenY, pointer, button) }
}
}
return true
}
}