mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 10:25:06 -04:00
fix some text aligning bugs
This commit is contained in:
parent
53cab94924
commit
7ec41fc280
@ -325,7 +325,7 @@ class ChatComponentRendererTest {
|
||||
|
||||
fun `left alignment`() { // default
|
||||
val consumer = DummyComponentConsumer()
|
||||
render(TextComponent("bc\nde"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.LEFT, shadow = false))
|
||||
render(TextComponent("bc\nde\nbc"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.LEFT, shadow = false))
|
||||
|
||||
consumer.assert(
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(10, 10)),
|
||||
@ -333,12 +333,15 @@ class ChatComponentRendererTest {
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(10.0, 21)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12.5, 21)),
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(10, 32)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(11.5, 32)),
|
||||
)
|
||||
}
|
||||
|
||||
fun `center alignment`() {
|
||||
val consumer = DummyComponentConsumer()
|
||||
render(TextComponent("bc\nde"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.CENTER, shadow = false))
|
||||
render(TextComponent("bc\nde\nbc"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.CENTER, shadow = false))
|
||||
|
||||
consumer.assert(
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(11, 10)),
|
||||
@ -346,12 +349,15 @@ class ChatComponentRendererTest {
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(10.0, 21)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12.5, 21)),
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(11, 32)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12.5, 32)),
|
||||
)
|
||||
}
|
||||
|
||||
fun `right alignment`() {
|
||||
val consumer = DummyComponentConsumer()
|
||||
render(TextComponent("bc\nde"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.RIGHT, shadow = false))
|
||||
render(TextComponent("bc\nde\nbc"), fontManager = FontManager(consumer.Font()), consumer = consumer, properties = TextRenderProperties(alignment = HorizontalAlignments.RIGHT, shadow = false))
|
||||
|
||||
consumer.assert(
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12, 10)),
|
||||
@ -359,6 +365,9 @@ class ChatComponentRendererTest {
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(10.0, 21)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12.5, 21)),
|
||||
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(12, 32)),
|
||||
DummyComponentConsumer.RendererdCodePoint(Vec2(13.5, 32)),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -93,5 +93,5 @@ class TextElementTest {
|
||||
}
|
||||
|
||||
|
||||
// TODO: test on mouse (click/hover events), rendering
|
||||
// TODO: test on mouse (click/hover events), rendering (correct offset, background)
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ interface CodePointRenderer {
|
||||
private fun getVerticalSpacing(offset: TextOffset, properties: TextRenderProperties, info: TextRenderInfo, align: Boolean): Float {
|
||||
var lineStart = offset.initial.x
|
||||
if (align) {
|
||||
lineStart += properties.alignment.getOffset(info.lines[info.lineIndex].width, info.size.x)
|
||||
lineStart += properties.alignment.getOffset(info.size.x, info.lines[info.lineIndex].width)
|
||||
}
|
||||
if (offset.offset.x == lineStart) return 0.0f
|
||||
// not at line start
|
||||
@ -77,10 +77,6 @@ interface CodePointRenderer {
|
||||
|
||||
|
||||
if (consumer != null) {
|
||||
if (info.lineIndex == 0 && offset.offset.x == offset.initial.x) {
|
||||
// switched to consumer mode but offset was not updated yet
|
||||
offset.align(properties.alignment, info.lines.first().width, info.size)
|
||||
}
|
||||
render(offset.offset, color, properties.shadow, FormattingCodes.BOLD in formatting, FormattingCodes.ITALIC in formatting, properties.scale, consumer, options)
|
||||
} else {
|
||||
info.update(offset, properties, width, spacing) // info should only be updated when we determinate text properties, we know all that already when actually rendering it }
|
||||
|
@ -41,14 +41,14 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
|
||||
return properties.forcedColor ?: text.color ?: properties.fallbackColor
|
||||
}
|
||||
|
||||
private fun renderNewline(properties: TextRenderProperties, offset: TextOffset, info: TextRenderInfo, updateSize: Boolean, align: Boolean): Boolean {
|
||||
private fun renderNewline(properties: TextRenderProperties, offset: TextOffset, info: TextRenderInfo, consuming: Boolean): Boolean {
|
||||
val height = offset.getNextLineHeight(properties)
|
||||
if (!offset.addLine(properties, info, properties.lineHeight, height, align)) {
|
||||
if (!offset.addLine(properties, info, properties.lineHeight, height, consuming)) {
|
||||
info.cutOff = true
|
||||
return true
|
||||
}
|
||||
|
||||
if (updateSize) {
|
||||
if (!consuming) {
|
||||
info.size.y += height
|
||||
}
|
||||
|
||||
@ -67,6 +67,12 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
|
||||
override fun render(offset: TextOffset, fontManager: FontManager, properties: TextRenderProperties, info: TextRenderInfo, consumer: GUIVertexConsumer?, options: GUIVertexOptions?, text: TextComponent): Boolean {
|
||||
if (text.message.isEmpty()) return false
|
||||
|
||||
|
||||
if (consumer != null && info.lineIndex == 0 && offset.offset.x == offset.initial.x) {
|
||||
// switched to consumer mode but offset was not updated yet
|
||||
offset.align(properties.alignment, info.lines.first().width, info.size)
|
||||
}
|
||||
|
||||
val textFont = fontManager[text.font]
|
||||
val color = getColor(properties, text)
|
||||
val formatting = text.formatting
|
||||
@ -80,7 +86,7 @@ object TextComponentRenderer : ChatComponentRenderer<TextComponent> {
|
||||
val codePoint = stream.nextInt()
|
||||
if (codePoint == '\n'.code) {
|
||||
val lineIndex = info.lineIndex
|
||||
filled = renderNewline(properties, offset, info, consumer == null, consumer != null)
|
||||
filled = renderNewline(properties, offset, info, consumer != null)
|
||||
if (line.isNotEmpty()) {
|
||||
info.lines[lineIndex].push(text, line)
|
||||
}
|
||||
|
@ -26,9 +26,7 @@ class TextOffset(
|
||||
|
||||
|
||||
fun align(alignment: HorizontalAlignments, width: Float, size: Vec2) {
|
||||
this.offset.x = initial.x
|
||||
|
||||
this.offset.x += alignment.getOffset(width, size.x)
|
||||
this.offset.x = initial.x + alignment.getOffset(size.x, width)
|
||||
}
|
||||
|
||||
private fun fits(offset: Float, initial: Float, max: Float, value: Float): Boolean {
|
||||
@ -62,28 +60,31 @@ class TextOffset(
|
||||
return height
|
||||
}
|
||||
|
||||
fun addLine(properties: TextRenderProperties, info: TextRenderInfo, offset: Float, height: Float, align: Boolean): Boolean {
|
||||
fun addLine(properties: TextRenderProperties, info: TextRenderInfo, offset: Float, height: Float, consuming: Boolean): Boolean {
|
||||
if (!fitsY(info, offset, height)) return false
|
||||
|
||||
this.offset.y += height
|
||||
this.offset.x = initial.x
|
||||
if (align) {
|
||||
align(properties.alignment, info.lines[info.lineIndex].width, info.size)
|
||||
}
|
||||
info.lines += LineRenderInfo()
|
||||
info.lineIndex++
|
||||
|
||||
this.offset.y += height
|
||||
if (consuming) {
|
||||
align(properties.alignment, info.lines[info.lineIndex].width, info.size)
|
||||
} else {
|
||||
info.lines += LineRenderInfo()
|
||||
this.offset.x = this.initial.x
|
||||
}
|
||||
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
fun canAdd(properties: TextRenderProperties, info: TextRenderInfo, width: Float, height: Float, align: Boolean): CodePointAddResult {
|
||||
fun canAdd(properties: TextRenderProperties, info: TextRenderInfo, width: Float, height: Float, consuming: Boolean): CodePointAddResult {
|
||||
if (!canEverFit(info, width)) {
|
||||
info.cutOff = true
|
||||
return CodePointAddResult.BREAK
|
||||
}
|
||||
if (fitsInLine(properties, info, width)) return CodePointAddResult.FINE
|
||||
if (addLine(properties, info, 0.0f, height, align) && fitsInLine(properties, info, width)) return CodePointAddResult.NEW_LINE
|
||||
if (addLine(properties, info, 0.0f, height, consuming) && fitsInLine(properties, info, width)) return CodePointAddResult.NEW_LINE
|
||||
|
||||
info.cutOff = true
|
||||
return CodePointAddResult.BREAK
|
||||
|
@ -202,7 +202,7 @@ class DebugHUDElement(guiRenderer: GUIRenderer) : Element(guiRenderer), Layouted
|
||||
|
||||
layout += LineSpacerElement(guiRenderer)
|
||||
|
||||
layout += AutoTextElement(guiRenderer, 1) { "Allocation rate ${AllocationRate.allocationRate.formatBytes()}/s}" }
|
||||
layout += AutoTextElement(guiRenderer, 1) { "Allocation rate ${AllocationRate.allocationRate.formatBytes()}/s" }
|
||||
|
||||
SystemInformation.RUNTIME.apply {
|
||||
layout += AutoTextElement(guiRenderer, 1) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user