diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Ext.kt index 712269a62..b68e339f9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/render/Ext.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.client.render import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.Tesselator import com.mojang.blaze3d.vertex.VertexConsumer +import com.mojang.blaze3d.vertex.VertexSorting import net.minecraft.client.gui.Font import net.minecraft.client.gui.GuiGraphics import net.minecraft.client.renderer.MultiBufferSource @@ -104,8 +105,12 @@ private fun Font.drawScaledDuckTyped(poseStack: PoseStack, buffer: MultiBufferSo return size } +private val buffer = DynamicBufferSource(vertexSorting = VertexSorting.ORTHOGRAPHIC_Z) + private fun Font.drawDuckTyped(poseStack: PoseStack, text: Any, x: Float, y: Float, color: Int): Int { - return drawDuckTyped(poseStack, DynamicBufferSource.GUI, text, x, y, color) + val width = drawDuckTyped(poseStack, buffer, text, x, y, color) + buffer.endBatch() + return width } private fun Font.drawDuckTyped( @@ -191,6 +196,33 @@ private fun Font.drawAlignedDuckTyped( } } +private fun GuiGraphics.drawAlignedDuckTyped( + font: Font, + text: Any, + align: TextAlign, + x: Float, + y: Float, + color: Int, + drawShadow: Boolean = false, + displayMode: Font.DisplayMode = Font.DisplayMode.NORMAL, + packedLightCoords: Int = 15728880, + effectColor: Int = 0 +): Int { + return when (align) { + TextAlign.TOP_LEFT -> font.drawDuckTyped(pose(), bufferSource(), text, x, y, color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.TOP_CENTER -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text) / 2f), y, color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.TOP_RIGHT -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text)), y, color, drawShadow, displayMode, packedLightCoords, effectColor) + + TextAlign.CENTER_LEFT -> font.drawDuckTyped(pose(), bufferSource(), text, x, (y - font.lineHeight / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.CENTER_CENTER -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text) / 2f), (y - font.lineHeight / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.CENTER_RIGHT -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text)), (y - font.lineHeight / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) + + TextAlign.BOTTOM_LEFT -> font.drawDuckTyped(pose(), bufferSource(), text, x, (y - font.lineHeight), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.BOTTOM_CENTER -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text) / 2f), (y - font.lineHeight), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.BOTTOM_RIGHT -> font.drawDuckTyped(pose(), bufferSource(), text, (x - font.widthDuckTyped(text)), (y - font.lineHeight), color, drawShadow, displayMode, packedLightCoords, effectColor) + } +} + private fun Font.drawScaledAlignedDuckTyped(poseStack: PoseStack, text: Any, scale: Float, align: TextAlign, x: Float, y: Float, color: Int): Int { return when (align) { TextAlign.TOP_LEFT -> drawScaledDuckTyped(poseStack, text, scale, x, y, color) @@ -237,8 +269,7 @@ private fun Font.drawScaledAlignedDuckTyped( } private fun GuiGraphics.drawScaledAlignedDuckTyped( - poseStack: PoseStack, - buffer: MultiBufferSource, + font: Font, text: Any, scale: Float, align: TextAlign, @@ -251,17 +282,17 @@ private fun GuiGraphics.drawScaledAlignedDuckTyped( effectColor: Int = 0 ): Int { return when (align) { - TextAlign.TOP_LEFT -> drawScaledDuckTyped(poseStack, buffer, text, scale, x, y, color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.TOP_CENTER -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale / 2f), y, color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.TOP_RIGHT -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale), y, color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.TOP_LEFT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, x, y, color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.TOP_CENTER -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale / 2f), y, color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.TOP_RIGHT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale), y, color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.CENTER_LEFT -> drawScaledDuckTyped(poseStack, buffer, text, scale, x, (y - lineHeight / 2f * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.CENTER_CENTER -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale / 2f), (y - lineHeight * scale / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.CENTER_RIGHT -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale), (y - lineHeight * scale / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.CENTER_LEFT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, x, (y - font.lineHeight / 2f * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.CENTER_CENTER -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale / 2f), (y - font.lineHeight * scale / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.CENTER_RIGHT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale), (y - font.lineHeight * scale / 2f), color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.BOTTOM_LEFT -> drawScaledDuckTyped(poseStack, buffer, text, scale, x, (y - lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.BOTTOM_CENTER -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale / 2f), (y - lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) - TextAlign.BOTTOM_RIGHT -> drawScaledDuckTyped(poseStack, buffer, text, scale, (x - widthDuckTyped(text) * scale), (y - lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.BOTTOM_LEFT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, x, (y - font.lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.BOTTOM_CENTER -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale / 2f), (y - font.lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) + TextAlign.BOTTOM_RIGHT -> font.drawScaledDuckTyped(pose(), bufferSource(), text, scale, (x - font.widthDuckTyped(text) * scale), (y - font.lineHeight * scale), color, drawShadow, displayMode, packedLightCoords, effectColor) } } @@ -297,18 +328,18 @@ fun Font.drawScaledAligned(poseStack: PoseStack, buffer: MultiBufferSource, text fun Font.drawScaledAligned(poseStack: PoseStack, buffer: MultiBufferSource, text: Component, scale: Float, align: TextAlign, x: Float, y: Float, drawShadow: Boolean = false, displayMode: Font.DisplayMode = Font.DisplayMode.NORMAL, packedLightCoords: Int = 15728880, effectColor: Int = 0, color: RGBAColor) = drawScaledAligned(poseStack, buffer, text, scale, align, x, y, color.toInt(), drawShadow, displayMode, packedLightCoords, effectColor) fun Font.drawScaledAligned(poseStack: PoseStack, buffer: MultiBufferSource, text: FormattedCharSequence, scale: Float, align: TextAlign, x: Float, y: Float, drawShadow: Boolean = false, displayMode: Font.DisplayMode = Font.DisplayMode.NORMAL, packedLightCoords: Int = 15728880, effectColor: Int = 0, color: RGBAColor) = drawScaledAligned(poseStack, buffer, text, scale, align, x, y, color.toInt(), drawShadow, displayMode, packedLightCoords, effectColor) -fun GuiGraphics.drawAligned(font: Font, text: String, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(poseStack, text, align, x, y, color) -fun GuiGraphics.drawAligned(font: Font, text: Component, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(poseStack, text, align, x, y, color) -fun GuiGraphics.drawAligned(font: Font, text: FormattedCharSequence, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(poseStack, text, align, x, y, color) +fun GuiGraphics.drawAligned(font: Font, text: String, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(font, text, align, x, y, color) +fun GuiGraphics.drawAligned(font: Font, text: Component, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(font, text, align, x, y, color) +fun GuiGraphics.drawAligned(font: Font, text: FormattedCharSequence, align: TextAlign, x: Float, y: Float, color: Int) = drawAlignedDuckTyped(font, text, align, x, y, color) -fun GuiGraphics.drawAligned(font: Font, text: String, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(poseStack, text, align, x, y, color.toInt()) -fun GuiGraphics.drawAligned(font: Font, text: Component, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(poseStack, text, align, x, y, color.toInt()) -fun GuiGraphics.drawAligned(font: Font, text: FormattedCharSequence, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(poseStack, text, align, x, y, color.toInt()) +fun GuiGraphics.drawAligned(font: Font, text: String, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(font, text, align, x, y, color.toInt()) +fun GuiGraphics.drawAligned(font: Font, text: Component, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(font, text, align, x, y, color.toInt()) +fun GuiGraphics.drawAligned(font: Font, text: FormattedCharSequence, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawAligned(font, text, align, x, y, color.toInt()) -fun GuiGraphics.drawScaledAligned(font: Font, text: String, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(poseStack, text, scale, align, x, y, color) -fun GuiGraphics.drawScaledAligned(font: Font, text: Component, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(poseStack, text, scale, align, x, y, color) -fun GuiGraphics.drawScaledAligned(font: Font, text: FormattedCharSequence, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(poseStack, text, scale, align, x, y, color) +fun GuiGraphics.drawScaledAligned(font: Font, text: String, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(font, text, scale, align, x, y, color) +fun GuiGraphics.drawScaledAligned(font: Font, text: Component, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(font, text, scale, align, x, y, color) +fun GuiGraphics.drawScaledAligned(font: Font, text: FormattedCharSequence, scale: Float, align: TextAlign, x: Float, y: Float, color: Int) = drawScaledAlignedDuckTyped(font, text, scale, align, x, y, color) -fun GuiGraphics.drawScaledAligned(font: Font, text: String, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(poseStack, text, scale, align, x, y, color.toInt()) -fun GuiGraphics.drawScaledAligned(font: Font, text: Component, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(poseStack, text, scale, align, x, y, color.toInt()) -fun GuiGraphics.drawScaledAligned(font: Font, text: FormattedCharSequence, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(poseStack, text, scale, align, x, y, color.toInt()) +fun GuiGraphics.drawScaledAligned(font: Font, text: String, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(font, text, scale, align, x, y, color.toInt()) +fun GuiGraphics.drawScaledAligned(font: Font, text: Component, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(font, text, scale, align, x, y, color.toInt()) +fun GuiGraphics.drawScaledAligned(font: Font, text: FormattedCharSequence, scale: Float, align: TextAlign, x: Float, y: Float, color: RGBAColor) = drawScaledAligned(font, text, scale, align, x, y, color.toInt())