reading text from JInputField to support multilingual input

Former-commit-id: 4dc1228ba000f88d3af29569aa3954cb8acc4d90
Former-commit-id: 7fd7f8bd4f35e3be30cd9fa0da96684a48eedad3
This commit is contained in:
Song Minjae
2017-02-05 22:04:33 +09:00
parent ee3b72949e
commit 9e5dcb7b3f
4 changed files with 130 additions and 3 deletions

View File

@@ -43,8 +43,7 @@ import java.util.concurrent.locks.ReentrantLock
/**
* Created by minjaesong on 15-12-30.
*/
class StateInGame @Throws(SlickException::class)
constructor() : BasicGameState() {
class StateInGame : BasicGameState() {
private val ACTOR_UPDATE_RANGE = 4096
internal var game_mode = 0

View File

@@ -53,7 +53,8 @@ object CommandDict {
Pair("spawnball", SpawnPhysTestBall),
Pair("spawntorch", SpawnTikiTorch),
Pair("musictest", MusicTest),
Pair("spawntapestry", SpawnTapestry)
Pair("spawntapestry", SpawnTapestry),
Pair("imtest", JavaIMTest)
)
operator fun get(commandName: String): ConsoleCommand {

View File

@@ -0,0 +1,24 @@
package net.torvald.terrarum.console
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.swingapp.IMStringReader
import javax.swing.JFrame
/**
* Created by SKYHi14 on 2017-02-05.
*/
object JavaIMTest : ConsoleCommand {
override fun execute(args: Array<String>) {
IMStringReader(
Terrarum.appgc,
{ Echo("[JavaIMTest -> IMStringReader] $it") }, // send input to Echo
"JavaIMTest"
)
}
override fun printUsage() {
Echo("Tests Swing input window to get non-English text input")
}
}

View File

@@ -0,0 +1,103 @@
package net.torvald.terrarum.swingapp
import net.torvald.terrarum.langpack.Lang
import org.newdawn.slick.GameContainer
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.event.*
import javax.swing.*
/**
* Multilingual string input
*
* This kind of hack was often used in retro games that does not support multilingual input per se,
* so it would give you a pop-up with text field to get the string input from user, using resources
* provided by the OS.
*
* This hack is still alive, for example: "Princess Maker 2 Refine" on Steam, when installed in
* Chinese/Japanese/Korean Language. Although the game was released in 2016 via Steam (hence the "Refine"),
* the original game were released on 1995.
*
* Although admittedly, Korean input does not require this hack, you can just write the Input Method
* out of Java/Kotlin as the language does not need conversion (jp. Henkan) exists in Chinese and Japanese.
*
* Created by SKYHi14 on 2017-02-05.
*/
class IMStringReader(gc: GameContainer, feedInput: (String) -> Unit, message: String? = null) : JFrame() {
private val inputArea = JTextField()
private val buttonOkay = JButton(Lang["MENU_LABEL_OK"])
private val buttonCancel = JButton(Lang["MENU_LABEL_CANCEL"])
private val labelTitle = message ?: "Enter some text"
var userInput: String = ""//null
private set
init {
this.title = labelTitle
defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE
gc.pause()
buttonOkay.addMouseListener(object : MouseListener {
override fun mouseEntered(e: MouseEvent?) { }
override fun mouseClicked(e: MouseEvent?) { }
override fun mouseReleased(e: MouseEvent?) { }
override fun mouseExited(e: MouseEvent?) { }
override fun mousePressed(e: MouseEvent?) {
userInput = inputArea.text
isVisible = false
gc.isPaused = false
feedInput(userInput)
dispose()
}
})
buttonCancel.addMouseListener(object : MouseListener {
override fun mouseEntered(e: MouseEvent?) { }
override fun mouseClicked(e: MouseEvent?) { }
override fun mouseReleased(e: MouseEvent?) { }
override fun mouseExited(e: MouseEvent?) { }
override fun mousePressed(e: MouseEvent?) {
userInput = ""//null
isVisible = false
gc.isPaused = false
dispose()
}
})
this.addKeyListener(object : KeyListener {
override fun keyTyped(e: KeyEvent?) { }
override fun keyReleased(e: KeyEvent?) { }
override fun keyPressed(e: KeyEvent?) {
userInput = inputArea.text
isVisible = false
gc.isPaused = false
feedInput(userInput)
dispose()
}
})
val buttonsArea = JPanel()
buttonsArea.layout = FlowLayout()
buttonsArea.add(buttonOkay)
buttonsArea.add(buttonCancel)
this.layout = BorderLayout(2, 2)
this.add(JLabel(labelTitle), BorderLayout.PAGE_START)
this.add(inputArea, BorderLayout.CENTER)
this.add(buttonsArea, BorderLayout.PAGE_END)
this.isVisible = true
this.setSize(240, 118)
}
}