From f5e2667e1ee0f8e663ae32f34f76e1a2bc0b6ada Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 15 Sep 2023 09:55:26 +0200 Subject: Feature: gui scaling (#464) Add gui scaling #464 --- .../utils/renderables/RenderLineTooltips.kt | 8 +++-- .../skyhanni/utils/renderables/Renderable.kt | 39 ++++++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils/renderables') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt index 2cf20ee72..22675039e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt @@ -13,11 +13,13 @@ import java.awt.Color object RenderLineTooltips { - fun drawHoveringText(posX: Int, posY: Int, tips: List, stack: ItemStack? = null) { + fun drawHoveringText(posX: Int, posY: Int, tips: List, stack: ItemStack? = null, + mouseX: Int = Utils.getMouseX(), + mouseY: Int = Utils.getMouseY()) { if (tips.isNotEmpty()) { var textLines = tips - val x = Utils.getMouseX() + 12 - posX - val y = Utils.getMouseY() - 10 - posY + val x = mouseX + 12 - posX + val y = mouseY - 10 - posY val color: Char = stack?.getLore()?.lastOrNull()?.take(4)?.get(1) ?: Utils.getPrimaryColourCode(textLines[0]) val colourInt = Minecraft.getMinecraft().fontRendererObj.getColorCode(color) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt index 81ddaabde..ed3aa9cfd 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -18,12 +18,13 @@ import kotlin.math.max interface Renderable { val width: Int val height: Int - fun isHovered(posX: Int, posY: Int) = - Utils.getMouseX() in (posX..posX + width) - && Utils.getMouseY() in (posY..posY + height) // TODO: adjust for variable height? + fun isHovered(posX: Int, posY: Int) = currentRenderPassMousePosition?.let { mp -> + mp.first in (posX..posX + width) + && mp.second in (posY..posY + height) // TODO: adjust for variable height? + } ?: false /** - * N.B.: the offset is absolute, not relative to the position and shouldn't be used for rendering + * Pos x and pos y are relative to the mouse position. * (the GL matrix stack should already be pre transformed) */ fun render(posX: Int, posY: Int) @@ -32,6 +33,19 @@ interface Renderable { val logger = LorenzLogger("debug/renderable") val list = mutableMapOf, List>() + var currentRenderPassMousePosition: Pair? = null + private set + + fun withMousePosition(posX: Int, posY: Int, block: () -> T): T { + val last = currentRenderPassMousePosition + try { + currentRenderPassMousePosition = Pair(posX, posY) + return block() + } finally { + currentRenderPassMousePosition = last + } + } + fun fromAny(any: Any?, itemScale: Double = 1.0): Renderable? = when (any) { null -> placeholder(12) is Renderable -> any @@ -101,7 +115,14 @@ interface Renderable { } } - fun hoverTips(text: String, tips: List, indexes: List = listOf(), stack: ItemStack? = null, bypassChecks: Boolean = false, condition: () -> Boolean = { true }): Renderable { + fun hoverTips( + text: String, + tips: List, + indexes: List = listOf(), + stack: ItemStack? = null, + bypassChecks: Boolean = false, + condition: () -> Boolean = { true } + ): Renderable { val render = string(text) return object : Renderable { @@ -116,7 +137,13 @@ interface Renderable { list[Pair(posX, posY)] = indexes GlStateManager.pushMatrix() GlStateManager.translate(0F, 0F, 400F) - RenderLineTooltips.drawHoveringText(posX, posY, tips, stack) + + RenderLineTooltips.drawHoveringText( + posX, posY, tips, + stack, + currentRenderPassMousePosition?.first ?: Utils.getMouseX(), + currentRenderPassMousePosition?.second ?: Utils.getMouseY(), + ) GlStateManager.popMatrix() } } else { -- cgit