mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-16 00:26:07 +09:00
properly supporting multibyte characters (i think...)
This commit is contained in:
Binary file not shown.
@@ -10,7 +10,9 @@ import com.badlogic.gdx.graphics.glutils.FrameBuffer
|
|||||||
import net.torvald.terrarum.*
|
import net.torvald.terrarum.*
|
||||||
import net.torvald.terrarum.gamecontroller.IngameController
|
import net.torvald.terrarum.gamecontroller.IngameController
|
||||||
import net.torvald.terrarum.utils.Clipboard
|
import net.torvald.terrarum.utils.Clipboard
|
||||||
|
import net.torvald.terrarumsansbitmap.gdx.CodepointSequence
|
||||||
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||||
|
import kotlin.streams.toList
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param width width of the text input where the text gets drawn, not the entire item
|
* @param width width of the text input where the text gets drawn, not the entire item
|
||||||
@@ -60,18 +62,16 @@ class UIItemTextLineInput(
|
|||||||
|
|
||||||
var isActive = true
|
var isActive = true
|
||||||
|
|
||||||
var cursorX = 0 // 1 per char (not codepoint)
|
var cursorX = 0
|
||||||
var cursorCodepoint = 0
|
|
||||||
var codepointCount = 0
|
|
||||||
var cursorDrawX = 0 // pixelwise point
|
var cursorDrawX = 0 // pixelwise point
|
||||||
var cursorBlinkCounter = 0f
|
var cursorBlinkCounter = 0f
|
||||||
var cursorOn = true
|
var cursorOn = true
|
||||||
|
|
||||||
private val textbuf = StringBuilder()
|
private val textbuf = CodepointSequence()
|
||||||
|
|
||||||
private var fboUpdateLatch = true
|
private var fboUpdateLatch = true
|
||||||
|
|
||||||
private var currentPlaceholderText = placeholder() // the placeholder text may change every time you call it
|
private var currentPlaceholderText = ArrayList<Int>(placeholder().toCodePoints()) // the placeholder text may change every time you call it
|
||||||
|
|
||||||
|
|
||||||
private val btn1PosX = posX + width - 2*WIDTH_ONEBUTTON - 3
|
private val btn1PosX = posX + width - 2*WIDTH_ONEBUTTON - 3
|
||||||
@@ -108,43 +108,33 @@ class UIItemTextLineInput(
|
|||||||
copyToClipboard()
|
copyToClipboard()
|
||||||
}
|
}
|
||||||
else if (cursorX > 0 && keycodes.contains(Input.Keys.BACKSPACE)) {
|
else if (cursorX > 0 && keycodes.contains(Input.Keys.BACKSPACE)) {
|
||||||
cursorCodepoint -= 1
|
cursorX -= 1
|
||||||
val lastCp = textbuf.codePointAt(cursorCodepoint)
|
textbuf.removeAt(cursorX)
|
||||||
val charCount = Character.charCount(lastCp)
|
cursorDrawX = App.fontGame.getWidth(textbuf.subList(0, cursorX))
|
||||||
cursorX -= charCount
|
|
||||||
textbuf.delete(cursorX, cursorX + charCount)
|
|
||||||
|
|
||||||
cursorDrawX -= App.fontGame.getWidth(String(Character.toChars(lastCp))) - 1
|
|
||||||
codepointCount -= 1
|
|
||||||
}
|
}
|
||||||
else if (cursorX > 0 && keycodes.contains(Input.Keys.LEFT)) {
|
else if (cursorX > 0 && keycodes.contains(Input.Keys.LEFT)) {
|
||||||
cursorCodepoint -= 1
|
cursorX -= 1
|
||||||
cursorX -= Character.charCount(textbuf.codePointAt(cursorCodepoint))
|
cursorDrawX = App.fontGame.getWidth(textbuf.subList(0, cursorX))
|
||||||
val lastCp = textbuf.codePointAt(cursorCodepoint)
|
|
||||||
cursorDrawX -= App.fontGame.getWidth(String(Character.toChars(lastCp))) - 1
|
|
||||||
if (cursorDrawX < 0) cursorDrawX = 0
|
if (cursorDrawX < 0) cursorDrawX = 0
|
||||||
}
|
}
|
||||||
else if (cursorX < codepointCount && keycodes.contains(Input.Keys.RIGHT)) {
|
else if (cursorX < textbuf.size && keycodes.contains(Input.Keys.RIGHT)) {
|
||||||
val lastCp = textbuf.codePointAt(cursorCodepoint)
|
cursorX += 1
|
||||||
cursorDrawX += App.fontGame.getWidth(String(Character.toChars(lastCp))) - 1
|
cursorDrawX = App.fontGame.getWidth(textbuf.subList(0, cursorX))
|
||||||
cursorX += Character.charCount(lastCp)
|
|
||||||
cursorCodepoint += 1
|
|
||||||
}
|
}
|
||||||
// accept:
|
// accept:
|
||||||
// - literal "<"
|
// - literal "<"
|
||||||
// - keysymbol that does not start with "<" (not always has length of 1 because UTF-16)
|
// - keysymbol that does not start with "<" (not always has length of 1 because UTF-16)
|
||||||
else if (char != null && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) {
|
else if (char != null && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) {
|
||||||
textbuf.insert(cursorX, char)
|
val codepoints = char.toCodePoints()
|
||||||
|
textbuf.addAll(cursorX, codepoints)
|
||||||
|
|
||||||
cursorDrawX += App.fontGame.getWidth(char) - 1
|
cursorX += codepoints.size
|
||||||
cursorX += char.length
|
cursorDrawX = App.fontGame.getWidth(textbuf.subList(0, cursorX))
|
||||||
codepointCount += 1
|
|
||||||
cursorCodepoint += 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cursorCodepoint == 0) {
|
if (textbuf.size == 0) {
|
||||||
currentPlaceholderText = placeholder()
|
currentPlaceholderText = ArrayList(placeholder().toCodePoints())
|
||||||
}
|
}
|
||||||
|
|
||||||
cursorBlinkCounter += delta
|
cursorBlinkCounter += delta
|
||||||
@@ -167,27 +157,29 @@ class UIItemTextLineInput(
|
|||||||
if (!mouseDown) mouseLatched = false
|
if (!mouseDown) mouseLatched = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun String.toCodePoints() = this.codePoints().toList()
|
||||||
|
|
||||||
private fun toggleIME() {
|
private fun toggleIME() {
|
||||||
imeOn = !imeOn
|
imeOn = !imeOn
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun paste() {
|
private fun paste() {
|
||||||
val str = Clipboard.fetch().substringBefore('\n').substringBefore('\t')
|
val codepoints = Clipboard.fetch().substringBefore('\n').substringBefore('\t').toCodePoints()
|
||||||
val strCodepointLen = str.codePoints().count().toInt()
|
|
||||||
|
|
||||||
textbuf.insert(cursorX, str)
|
textbuf.addAll(cursorX, codepoints)
|
||||||
|
|
||||||
cursorDrawX += App.fontGame.getWidth(str) - 1
|
cursorX += codepoints.size
|
||||||
cursorX += str.length
|
cursorDrawX = App.fontGame.getWidth(textbuf.subList(0, cursorX))
|
||||||
|
|
||||||
codepointCount += strCodepointLen
|
|
||||||
cursorCodepoint += strCodepointLen
|
|
||||||
|
|
||||||
fboUpdateLatch = true
|
fboUpdateLatch = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun copyToClipboard() {
|
private fun copyToClipboard() {
|
||||||
Clipboard.copy(textbuf.toString())
|
Clipboard.copy(textbufToString())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun textbufToString(): String {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun render(batch: SpriteBatch, camera: Camera) {
|
override fun render(batch: SpriteBatch, camera: Camera) {
|
||||||
@@ -200,7 +192,7 @@ class UIItemTextLineInput(
|
|||||||
gdxClearAndSetBlend(0f, 0f, 0f, 0f)
|
gdxClearAndSetBlend(0f, 0f, 0f, 0f)
|
||||||
|
|
||||||
it.color = Color.WHITE
|
it.color = Color.WHITE
|
||||||
App.fontGameFBO.draw(it, if (textbuf.isEmpty()) currentPlaceholderText else "$textbuf", 0f, 0f)
|
App.fontGameFBO.draw(it, if (textbuf.isEmpty()) currentPlaceholderText else textbuf, 0f, 0f)
|
||||||
} }
|
} }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,7 +276,7 @@ class UIItemTextLineInput(
|
|||||||
super.render(batch, camera)
|
super.render(batch, camera)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getText() = textbuf.toString()
|
fun getText() = textbufToString()
|
||||||
fun getTextOrPlaceholder() = if (textbuf.isEmpty()) currentPlaceholderText else getText()
|
fun getTextOrPlaceholder() = if (textbuf.isEmpty()) currentPlaceholderText else getText()
|
||||||
|
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
|
|||||||
Reference in New Issue
Block a user