Round aligned text coordinates

because it's draw code is stupid and can't properly sample at half pixels
This commit is contained in:
DBotThePony 2022-09-09 15:17:42 +07:00
parent fc096c28aa
commit 66576fee94
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -10,6 +10,7 @@ import net.minecraft.core.Vec3i
import net.minecraft.network.chat.Component
import net.minecraft.util.FormattedCharSequence
import ru.dbotthepony.mc.otm.core.*
import kotlin.math.roundToInt
val tesselator: Tesselator get() = Tesselator.getInstance()
@ -66,48 +67,48 @@ enum class TextAlign {
fun Font.drawAligned(poseStack: PoseStack, text: String, align: TextAlign, x: Float, y: Float, color: Int): Int {
return when (align) {
TextAlign.TOP_LEFT -> draw(poseStack, text, x, y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, x - width(text) / 2f, y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, x - width(text), y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), y, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, y - lineHeight / 2f, color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight / 2f, color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight / 2f, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, y - lineHeight, color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight, color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight, color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
}
}
fun Font.drawAligned(poseStack: PoseStack, text: Component, align: TextAlign, x: Float, y: Float, color: Int): Int {
return when (align) {
TextAlign.TOP_LEFT -> draw(poseStack, text, x, y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, x - width(text) / 2f, y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, x - width(text), y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), y, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, y - lineHeight / 2f, color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight / 2f, color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight / 2f, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, y - lineHeight, color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight, color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight, color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
}
}
fun Font.drawAligned(poseStack: PoseStack, text: FormattedCharSequence, align: TextAlign, x: Float, y: Float, color: Int): Int {
return when (align) {
TextAlign.TOP_LEFT -> draw(poseStack, text, x, y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, x - width(text) / 2f, y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, x - width(text), y, color)
TextAlign.TOP_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), y, color)
TextAlign.TOP_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), y, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, y - lineHeight / 2f, color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight / 2f, color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight / 2f, color)
TextAlign.CENTER_LEFT -> draw(poseStack, text, x, (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.CENTER_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight / 2f).roundToInt().toFloat(), color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, y - lineHeight, color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, x - width(text) / 2f, y - lineHeight, color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, x - width(text), y - lineHeight, color)
TextAlign.BOTTOM_LEFT -> draw(poseStack, text, x, (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_CENTER -> draw(poseStack, text, (x - width(text) / 2f).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
TextAlign.BOTTOM_RIGHT -> draw(poseStack, text, (x - width(text)).roundToInt().toFloat(), (y - lineHeight).roundToInt().toFloat(), color)
}
}