diff options
Diffstat (limited to 'src/main/kotlin/com/dulkirfabric/util/render/HudRenderUtil.kt')
-rw-r--r-- | src/main/kotlin/com/dulkirfabric/util/render/HudRenderUtil.kt | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main/kotlin/com/dulkirfabric/util/render/HudRenderUtil.kt b/src/main/kotlin/com/dulkirfabric/util/render/HudRenderUtil.kt new file mode 100644 index 0000000..e055b60 --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/util/render/HudRenderUtil.kt @@ -0,0 +1,41 @@ +package com.dulkirfabric.util.render + +import com.dulkirfabric.DulkirModFabric.mc +import com.dulkirfabric.events.HudRenderEvent +import meteordevelopment.orbit.EventHandler +import net.minecraft.client.gui.DrawContext +import net.minecraft.text.Text +import java.time.Duration + +object HudRenderUtil { + + private var curTitle: Text? = null + private var clearTime: Long = -1 + + private fun drawTitle(context: DrawContext, content: Text) { + val matrices = context.matrices + val tr = mc.textRenderer + val w = tr.getWidth(content) + val sf: Float = mc.window.scaledWidth / w.toFloat() / 3 + matrices.push() + matrices.translate(mc.window.scaledWidth / 3f, mc.window.scaledHeight / 2f, 0f) + matrices.scale(sf, sf, 1f) + context.drawText(tr, content, 0, -tr.fontHeight / 2, -1, true) + matrices.pop() + } + + fun drawTitle(content: Text, duration: Duration) { + curTitle = content + clearTime = System.currentTimeMillis() + duration.toMillis() + } + + @EventHandler + fun onHudRender(event: HudRenderEvent) { + val content = curTitle ?: return + if (System.currentTimeMillis() >= clearTime) { + curTitle = null + return + } + drawTitle(event.context, content) + } +}
\ No newline at end of file |