From 362f71b14f864c6d86e7c805e51cd64a8194d9fb Mon Sep 17 00:00:00 2001 From: inglettronald Date: Sun, 23 Jul 2023 08:59:42 -0500 Subject: improved usability of Title Util --- .../kotlin/com/dulkirfabric/features/RenderTest.kt | 6 ------ .../kotlin/com/dulkirfabric/util/HudRenderUtil.kt | 24 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/com/dulkirfabric/features/RenderTest.kt b/src/main/kotlin/com/dulkirfabric/features/RenderTest.kt index d747b93..ac34ede 100644 --- a/src/main/kotlin/com/dulkirfabric/features/RenderTest.kt +++ b/src/main/kotlin/com/dulkirfabric/features/RenderTest.kt @@ -1,7 +1,6 @@ package com.dulkirfabric.features import com.dulkirfabric.events.EntityLoadEvent -import com.dulkirfabric.events.HudRenderEvent import com.dulkirfabric.events.WorldRenderLastEvent import com.dulkirfabric.util.WorldRenderUtils import meteordevelopment.orbit.EventHandler @@ -31,9 +30,4 @@ object RenderTest { //if (event.entity !is GlowingEntityInterface) return //event.entity.setDulkirEntityGlow(true, Color(0, 0, 255, 255),false) } - - @EventHandler - fun onHudRender(event: HudRenderEvent) { - // HudRenderUtil.drawTitle(event.context, Text.literal("Hello World!")) - } } \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/util/HudRenderUtil.kt b/src/main/kotlin/com/dulkirfabric/util/HudRenderUtil.kt index 02b79f7..327afdd 100644 --- a/src/main/kotlin/com/dulkirfabric/util/HudRenderUtil.kt +++ b/src/main/kotlin/com/dulkirfabric/util/HudRenderUtil.kt @@ -1,11 +1,18 @@ package com.dulkirfabric.util 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 { - fun drawTitle(context: DrawContext, content: Text) { + + 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) @@ -16,4 +23,19 @@ object HudRenderUtil { 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 -- cgit