From bfd926d7fe3e7a9a6cb2fcf8e50d8e3cd85d6f54 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 25 Aug 2023 17:38:15 +0900 Subject: [PATCH] avatar import error handling --- assets/locales/en/terrarum_sentences.json | 1 + assets/locales/koKR/terrarum_sentences.json | 1 + .../mods/basegame/locales/en/sentences.json | 3 +- .../mods/basegame/locales/koKR/sentences.json | 3 +- .../modulebasegame/ui/UIImportAvatar.kt | 32 ++++++++++++++++--- .../terrarum/ui/UIItemTextLineInput.kt | 4 +++ 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/assets/locales/en/terrarum_sentences.json b/assets/locales/en/terrarum_sentences.json index e1d4c0945..8022748b8 100644 --- a/assets/locales/en/terrarum_sentences.json +++ b/assets/locales/en/terrarum_sentences.json @@ -1,4 +1,5 @@ { + "ERROR_FILE_NOT_FOUND": "File not found.", "GAME_32BIT_WARNING1": "It looks like you’re running a 32-Bit version of Java.", "GAME_32BIT_WARNING2": "Please download and install the latest 64-Bit Java at:", "GAME_32BIT_WARNING3": "https://www.java.com/en/download/", diff --git a/assets/locales/koKR/terrarum_sentences.json b/assets/locales/koKR/terrarum_sentences.json index bb2980034..544fc2500 100644 --- a/assets/locales/koKR/terrarum_sentences.json +++ b/assets/locales/koKR/terrarum_sentences.json @@ -1,4 +1,5 @@ { + "ERROR_FILE_NOT_FOUND": "파일을 찾을 수 없습니다.", "GAME_32BIT_WARNING1": "32비트 버전의 Java를 사용중인 것 같습니다.", "GAME_32BIT_WARNING2": "아래 링크에서 최신 64비트 Java를 내려받아 설치해주세요.", "GAME_32BIT_WARNING3": "https://www.java.com/ko/download/", diff --git a/assets/mods/basegame/locales/en/sentences.json b/assets/mods/basegame/locales/en/sentences.json index de775ae98..b57964c3c 100644 --- a/assets/mods/basegame/locales/en/sentences.json +++ b/assets/mods/basegame/locales/en/sentences.json @@ -2,5 +2,6 @@ "CONTEXT_THIS_IS_A_WORLD_CURRENTLY_PLAYING": "This is a world currently playing.", "CONTEXT_IMPORT_AVATAR_INSTRUCTION_1": "1. Place the Avatar file into the following directory:", "CONTEXT_IMPORT_AVATAR_INSTRUCTION_2": "", - "CONTEXT_IMPORT_AVATAR_INSTRUCTION_3": "2. Enter the name of the file below, then press Import" + "CONTEXT_IMPORT_AVATAR_INSTRUCTION_3": "2. Enter the name of the file below, then press Import", + "ERROR_AVATAR_ALREADY_EXISTS": "The Avatar already exists." } \ No newline at end of file diff --git a/assets/mods/basegame/locales/koKR/sentences.json b/assets/mods/basegame/locales/koKR/sentences.json index 5b11d6022..dc35da24e 100644 --- a/assets/mods/basegame/locales/koKR/sentences.json +++ b/assets/mods/basegame/locales/koKR/sentences.json @@ -1,5 +1,6 @@ { "CONTEXT_THIS_IS_A_WORLD_CURRENTLY_PLAYING": "현재 플레이 중인 월드입니다.", "CONTEXT_IMPORT_AVATAR_INSTRUCTION_1": "1. 아바타 파일을 다음 폴더에 넣어주세요", - "CONTEXT_IMPORT_AVATAR_INSTRUCTION_3": "2. 아바타 파일 이름을 아래에 입력하고 가져오기를 눌러주세요" + "CONTEXT_IMPORT_AVATAR_INSTRUCTION_3": "2. 아바타 파일 이름을 아래에 입력하고 가져오기를 눌러주세요", + "ERROR_AVATAR_ALREADY_EXISTS": "이미 존재하는 아바타입니다." } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIImportAvatar.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIImportAvatar.kt index b1bca8749..318436a3b 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIImportAvatar.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIImportAvatar.kt @@ -61,6 +61,8 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { private val goButton = UIItemTextButton(this, { Lang["MENU_IO_IMPORT"] }, drawX + width/2 + (width/2 - goButtonWidth) / 2, drawY + height - 24, goButtonWidth, alignment = UIItemTextButton.Companion.Alignment.CENTRE, hasBorder = true) + private var importReturnCode = 0 + init { // addUIitem(codeBox) // addUIitem(clearButton) @@ -79,8 +81,15 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { remoCon.openUI(UILoadSavegame(remoCon)) } goButton.clickOnceListener = { _,_ -> - val returnCode = doImport() - if (returnCode == 0) remoCon.openUI(UILoadSavegame(remoCon)) + if (filenameInput.getText().isNotBlank()) { + importReturnCode = doImport() + if (importReturnCode == 0) remoCon.openUI(UILoadSavegame(remoCon)) + } + } + + // reset importReturnCode if the text input has changed + filenameInput.onKeyDown = { _ -> + importReturnCode = 0 } } @@ -92,7 +101,6 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { override fun updateUI(delta: Float) { uiItems.forEach { it.update(delta) } - pathW = App.fontGame.getWidth(App.importDir) val textX = (Toolkit.drawWidth - pathW) / 2 textY = (App.scr.height - height) / 2 + descStartY + (1) * lh @@ -102,10 +110,18 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { if (mouseOnLink && Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) { OpenFile(File(App.importDir)) } + } private val textboxIndices = (1..3) + private val errorMessages = listOf( + Lang["ERROR_GENERIC_TEXT"], // -1 + "", // 0 + Lang["ERROR_FILE_NOT_FOUND"], // 1 + Lang["ERROR_AVATAR_ALREADY_EXISTS"], // 2 + ) + override fun renderUI(batch: SpriteBatch, camera: Camera) { batch.color = Color.WHITE val textboxWidth = textboxIndices.maxOf { App.fontGame.getWidth(Lang["CONTEXT_IMPORT_AVATAR_INSTRUCTION_$it"]) } @@ -120,6 +136,14 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { uiItems.forEach { it.render(batch, camera) } + + + if (importReturnCode != 0) { + batch.color = Toolkit.Theme.COL_RED + val tby = filenameInput.posY + val btny = backButton.posY + Toolkit.drawTextCentered(batch, App.fontGame, errorMessages[importReturnCode + 1], Toolkit.drawWidth, 0, (tby + btny) / 2) + } } override fun dispose() { @@ -129,7 +153,7 @@ class UIImportAvatar(val remoCon: UIRemoCon) : Advanceable() { } private fun doImport(): Int { - val file = File("${App.importDir}/${filenameInput.getText()}") + val file = File("${App.importDir}/${filenameInput.getText().trim()}") // check file's existence if (!file.exists()) { diff --git a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt index 5e576cc7b..d20a83782 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextLineInput.kt @@ -73,6 +73,8 @@ class UIItemTextLineInput( init { } + var onKeyDown: (TerrarumKeyboardEvent) -> Unit = {} + private val labels = CommonResourcePool.getAsTextureRegionPack("inventory_category") override val height = 24 @@ -394,6 +396,8 @@ class UIItemTextLineInput( if (textbuf.size == 0) { currentPlaceholderText = CodepointSequence(placeholder().toCodePoints()) } + + onKeyDown(e) } } else if (oldActive) { // just became deactivated