uiitemtextlineinput: text cursor follows drawn text when text is centre-aligned

This commit is contained in:
minjaesong
2022-08-08 23:05:30 +09:00
parent 2a78646a02
commit 6e0f9a5924

View File

@@ -471,12 +471,15 @@ class UIItemTextLineInput(
private fun String.toUnicodeNFC() = Normalizer2.getNFCInstance().normalize(this) private fun String.toUnicodeNFC() = Normalizer2.getNFCInstance().normalize(this)
private var textDrawOffset = 0
override fun render(batch: SpriteBatch, camera: Camera) { override fun render(batch: SpriteBatch, camera: Camera) {
val ime = getIME(true) val ime = getIME(true)
batch.end() batch.end()
if (true || fboUpdateLatch) { if (true || fboUpdateLatch) {
fboUpdateLatch = false fboUpdateLatch = false
fbo.inAction(camera as OrthographicCamera, batch) { batch.inUse { fbo.inAction(camera as OrthographicCamera, batch) { batch.inUse {
@@ -486,9 +489,13 @@ class UIItemTextLineInput(
val text = if (textbuf.isEmpty()) currentPlaceholderText else textbuf val text = if (textbuf.isEmpty()) currentPlaceholderText else textbuf
val tw = App.fontGameFBO.getWidth(text) val tw = App.fontGameFBO.getWidth(text)
val offset = ((fbo.width - tw) / 2).coerceAtLeast(0) textDrawOffset = if (alignment == UIItemTextButton.Companion.Alignment.CENTRE)
((fbo.width - tw) / 2).coerceAtLeast(0)
App.fontGameFBO.draw(it, text, -1f*cursorDrawScroll + offset, 0f) else
0
// TODO support alignment-right
App.fontGameFBO.draw(it, text, -1f*cursorDrawScroll + textDrawOffset, 0f)
} } } }
textCommitListener(getTextOrPlaceholder()) textCommitListener(getTextOrPlaceholder())
} }
@@ -536,7 +543,7 @@ class UIItemTextLineInput(
batch.draw(fbo.colorBufferTexture, inputPosX + 2f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat()) batch.draw(fbo.colorBufferTexture, inputPosX + 2f, posY + 2f, fbo.width.toFloat(), fbo.height.toFloat())
// draw text cursor // draw text cursor
val cursorXOnScreen = inputPosX + cursorDrawX + 2 val cursorXOnScreen = inputPosX + cursorDrawX + 2 + textDrawOffset
if (isActive && cursorOn) { if (isActive && 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