fix: text cursor darting around everywhere

This commit is contained in:
minjaesong
2024-03-22 22:15:27 +09:00
parent 6d2764933f
commit c87bd182b0

View File

@@ -1,7 +1,6 @@
package net.torvald.terrarum.ui package net.torvald.terrarum.ui
import com.badlogic.gdx.Input import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Pixmap import com.badlogic.gdx.graphics.Pixmap
@@ -194,7 +193,7 @@ class UIItemTextLineInput(
cursorOn = true cursorOn = true
} }
private fun moveCursorBack(delta: Int) { private fun __moveCursorBackward(delta: Int) {
cursorDrawX -= delta cursorDrawX -= delta
if (cursorDrawX < 0) { if (cursorDrawX < 0) {
val stride = -cursorDrawX + min(256, fbo.width * 40 / 100) // + lookbehind term val stride = -cursorDrawX + min(256, fbo.width * 40 / 100) // + lookbehind term
@@ -203,12 +202,15 @@ class UIItemTextLineInput(
} }
// make sure to not scroll past the line head // make sure to not scroll past the line head
if (cursorDrawScroll < 0) { if (cursorDrawScroll < 0) {
cursorDrawX += cursorDrawScroll cursorDrawX = 0
cursorDrawScroll = 0 cursorDrawScroll = 0
} }
else if (cursorDrawScroll == 0 && cursorDrawX < 0) {
cursorDrawX = 0
}
} }
private fun moveCursorForward(delta: Int) { private fun __moveCursorForward(delta: Int) {
cursorDrawX += delta cursorDrawX += delta
if (cursorDrawX > fbo.width) { if (cursorDrawX > fbo.width) {
val stride = cursorDrawX - fbo.width val stride = cursorDrawX - fbo.width
@@ -217,11 +219,16 @@ class UIItemTextLineInput(
} }
} }
private fun moveCursorBy(delta: Int) {
if (delta > 0) __moveCursorForward(delta)
else if (delta < 0) __moveCursorBackward(-delta)
}
/*private fun moveCursorToEnd(stride: Int) { /*private fun moveCursorToEnd(stride: Int) {
try { try {
cursorX += stride cursorX += stride
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
catch (e: Throwable) {} catch (e: Throwable) {}
@@ -241,7 +248,7 @@ class UIItemTextLineInput(
} }
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorBack(oldTextLenPx - currentTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
} }
@@ -300,7 +307,7 @@ class UIItemTextLineInput(
textbuf.addAll(cursorX, codepoints) textbuf.addAll(cursorX, codepoints)
cursorX += codepoints.size cursorX += codepoints.size
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
} }
@@ -322,7 +329,7 @@ class UIItemTextLineInput(
if (cursorX > 0) { if (cursorX > 0) {
cursorX -= 1 cursorX -= 1
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorBack(oldTextLenPx - currentTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
} }
@@ -332,12 +339,13 @@ class UIItemTextLineInput(
if (cursorX < textbuf.size) { if (cursorX < textbuf.size) {
cursorX += 1 cursorX += 1
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
} }
else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) { else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) {
endComposing() endComposing()
oldTextLenPx = 0
// println("END COMPOSING!!") // println("END COMPOSING!!")
} }
@@ -380,7 +388,7 @@ class UIItemTextLineInput(
textbuf.addAll(cursorX, codepoints) textbuf.addAll(cursorX, codepoints)
cursorX += codepoints.size cursorX += codepoints.size
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
} }
@@ -505,7 +513,7 @@ class UIItemTextLineInput(
textbuf.addAll(actuallyInserted) textbuf.addAll(actuallyInserted)
cursorX += codepoints.size cursorX += codepoints.size
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, minOf(cursorX, textbuf.size)))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, minOf(cursorX, textbuf.size))))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
fboUpdateLatch = true fboUpdateLatch = true
@@ -602,6 +610,7 @@ class UIItemTextLineInput(
// draw text cursor // draw text cursor
val cursorXOnScreen = inputPosX + cursorDrawX + 2 + textDrawOffset val cursorXOnScreen = inputPosX + cursorDrawX + 2 + textDrawOffset
// printdbg(this, "inputPosX=$inputPosX, cursorDrawX=$cursorDrawX, cursorDrawScroll=$cursorDrawScroll")
if (isEnabled && cursorOn) { if (isEnabled && cursorOn) {
val baseCol = if (maxLen.exceeds(textbuf, listOf(32))) TEXTINPUT_COL_TEXT_NOMORE else TEXTINPUT_COL_TEXT val baseCol = if (maxLen.exceeds(textbuf, listOf(32))) TEXTINPUT_COL_TEXT_NOMORE else TEXTINPUT_COL_TEXT
@@ -695,8 +704,8 @@ class UIItemTextLineInput(
resetIME() resetIME()
textbuf.clear() textbuf.clear()
cursorX = 0 cursorX = 0
cursorDrawScroll = 0
cursorDrawX = 0 cursorDrawX = 0
cursorDrawScroll = 0
} }
fun setText(s: String) { fun setText(s: String) {
clearText() clearText()
@@ -707,7 +716,7 @@ class UIItemTextLineInput(
textbuf.addAll(codepoints) textbuf.addAll(codepoints)
cursorX += codepoints.size cursorX += codepoints.size
currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX))) currentTextLenPx = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
moveCursorForward(currentTextLenPx - oldTextLenPx) moveCursorBy(currentTextLenPx - oldTextLenPx)
oldTextLenPx = currentTextLenPx oldTextLenPx = currentTextLenPx
} }
override fun dispose() { override fun dispose() {