diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt | 29 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt | 101 |
2 files changed, 110 insertions, 20 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index 491b73d31..df5567415 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -4,7 +4,7 @@ import at.hannibal2.skyhanni.config.core.config.Position import at.hannibal2.skyhanni.data.GuiEditManager import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsX import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsY -import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen +import at.hannibal2.skyhanni.utils.renderables.Renderable import io.github.moulberry.moulconfig.internal.TextRenderUtils import net.minecraft.client.Minecraft import net.minecraft.client.gui.FontRenderer @@ -14,7 +14,6 @@ import net.minecraft.client.renderer.Tessellator import net.minecraft.client.renderer.vertex.DefaultVertexFormats import net.minecraft.entity.Entity import net.minecraft.inventory.Slot -import net.minecraft.item.ItemStack import net.minecraft.util.AxisAlignedBB import net.minecraft.util.MathHelper import net.minecraft.util.ResourceLocation @@ -517,27 +516,17 @@ object RenderUtils { } private fun Position.renderLine(line: List<Any?>, offsetY: Int, itemScale: Double = 1.0): Int { - val renderer = Minecraft.getMinecraft().fontRendererObj + GlStateManager.pushMatrix() + GlStateManager.translate(getAbsX().toFloat(), (getAbsY() + offsetY).toFloat(), 0F) var offsetX = 0 for (any in line) { - if (any == null) { - offsetX += 12 - continue - } - if (any is String) { - renderString0(any, offsetX, offsetY) - val width = renderer.getStringWidth(any) - offsetX += width - } else if (any is ItemStack) { - val isX = getAbsX() + offsetX - val isY = getAbsY() + offsetY - - any.renderOnScreen(isX.toFloat(), isY.toFloat(), itemScale) - offsetX += 12 - } else { - throw RuntimeException("Unknown render object: $any") - } + val renderable = Renderable.fromAny(any, itemScale = itemScale) ?: throw RuntimeException("Unknown render object: ${any}") + + renderable.render(getAbsX() + offsetX, getAbsY() + offsetY) + offsetX += renderable.width + GlStateManager.translate(renderable.width.toFloat(), 0F, 0F) } + GlStateManager.popMatrix() return offsetX } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt new file mode 100644 index 000000000..73bba5686 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -0,0 +1,101 @@ +package at.hannibal2.skyhanni.utils.renderables + +import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen +import io.github.moulberry.notenoughupdates.util.Utils +import net.minecraft.client.Minecraft +import net.minecraft.item.ItemStack +import org.lwjgl.input.Mouse +import kotlin.math.max + +interface Renderable { + val width: Int + fun isHovered(posX: Int, posY: Int) = + Utils.getMouseX() in (posX..posX + width) + && Utils.getMouseY() in (posY..posY + 10) // TODO: adjust for variable height? + + /** + * N.B.: the offset is absolute, not relative to the position and should not be used for rendering + * (the GL matrix stack should already be pre transformed) + */ + fun render(posX: Int, posY: Int) + + companion object { + fun fromAny(any: Any?, itemScale: Double = 1.0): Renderable? { + return when (any) { + null -> placeholder(12) + is Renderable -> any + is String -> string(any) + is ItemStack -> itemStack(any, itemScale) + else -> null + } + } + + fun link(text: String, onClick: () -> Unit): Renderable { + return clickable(hoverable(string("§n$text"), string(text)), onClick) + } + + fun clickable(render: Renderable, onClick: () -> Unit, button: Int = 0): Renderable { + return object : Renderable { + override val width: Int + get() = render.width + + var wasDown = false + + override fun render(posX: Int, posY: Int) { + val isDown = Mouse.isButtonDown(button) + if (isDown > wasDown && isHovered(posX, posY)) { + onClick() + } + wasDown = isDown + render.render(posX, posY) + } + + } + } + + fun hoverable(hovered: Renderable, unhovered: Renderable): Renderable { + return object : Renderable { + override val width: Int + get() = max(hovered.width, unhovered.width) + + override fun render(posX: Int, posY: Int) { + if (isHovered(posX, posY)) + hovered.render(posX, posY) + else + unhovered.render(posX, posY) + } + } + } + + fun itemStack(any: ItemStack, scale: Double = 1.0): Renderable { + return object : Renderable { + override val width: Int + get() = 12 + + override fun render(posX: Int, posY: Int) { + any.renderOnScreen(0F, 0F, scaleMultiplier = scale) + } + } + } + + fun string(string: String): Renderable { + return object : Renderable { + override val width: Int + get() = Minecraft.getMinecraft().fontRendererObj.getStringWidth(string) + + override fun render(posX: Int, posY: Int) { + Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow("§f$string", 1f, 1f, 0) + } + } + } + + fun placeholder(width: Int): Renderable { + return object : Renderable { + override val width: Int = width + + override fun render(posX: Int, posY: Int) { + } + } + } + } +}
\ No newline at end of file |