text renderer: fix text in line when line cut off

This commit is contained in:
Bixilon 2022-02-18 21:49:55 +01:00
parent 870a691c7e
commit f9536f7f32
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 16 additions and 6 deletions

View File

@ -48,11 +48,15 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
return true
}
fun pushLine() {
if (consumer != null || currentLineText.isEmpty()) {
fun pushLine(index: Int = -1) {
var pushText: String = currentLineText
if (index > 0) {
pushText = currentLineText.substring(0, index)
}
if (consumer != null || pushText.isEmpty()) {
return
}
renderInfo.currentLine.text += text.copy(message = currentLineText)
renderInfo.currentLine.text += text.copy(message = pushText)
currentLineText = ""
}
@ -124,11 +128,11 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
}
applyOffset()
for (charCode in text.message.codePoints().toArray()) {
for ((index, charCode) in text.message.codePoints().toArray().withIndex()) {
val char = charCode.toChar()
if (char == '\n') {
if (wrap()) {
pushLine(index)
return true
}
continue
@ -157,6 +161,7 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
val previousY = offset.y
if (addX(width)) {
pushLine(index)
return true
}

View File

@ -219,9 +219,14 @@ open class TextElement(
textElement._prefMaxSize = Vec2i(offset.x, charHeight)
textElement.forceSilentApply()
offset.x += fontAlignment.getOffset(size.x, line.width)
return Pair(line.text.getTextAt(textElement.renderInfo.lines.getOrNull(0)?.text?.message?.length ?: return null), offset)
val line0 = textElement.renderInfo.lines.getOrNull(0) ?: return null
val text = line.text.getTextAt(line0.text.message.length)
offset.x -= line0.width // ToDo: Not 100% correct
return Pair(text, offset)
}
override fun toString(): String {