diff options
author | HiZe_ <superhize@hotmail.com> | 2023-08-10 12:23:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-10 12:23:02 +0200 |
commit | 1efe50bff3fbb0e6a782aaf5284fab3fd60ec637 (patch) | |
tree | 8ba814aee575609da3461d2be0107b8fc46f2f8b /src/main/java/at/hannibal2/skyhanni/utils/renderables | |
parent | d0bbd687ca9d33cc7bd8f53e3103ecc92905f8dc (diff) | |
download | skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.tar.gz skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.tar.bz2 skyhanni-1efe50bff3fbb0e6a782aaf5284fab3fd60ec637.zip |
Merge pull request #348
* Chest Value
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/renderables')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt | 217 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt | 63 |
2 files changed, 229 insertions, 51 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt new file mode 100644 index 000000000..2cf20ee72 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderLineTooltips.kt @@ -0,0 +1,217 @@ +package at.hannibal2.skyhanni.utils.renderables + +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import io.github.moulberry.notenoughupdates.util.Utils +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.ScaledResolution +import net.minecraft.client.renderer.GlStateManager +import net.minecraft.client.renderer.RenderHelper +import net.minecraft.client.renderer.Tessellator +import net.minecraft.client.renderer.vertex.DefaultVertexFormats +import net.minecraft.item.ItemStack +import java.awt.Color + +object RenderLineTooltips { + + fun drawHoveringText(posX: Int, posY: Int, tips: List<String?>, stack: ItemStack? = null) { + if (tips.isNotEmpty()) { + var textLines = tips + val x = Utils.getMouseX() + 12 - posX + val y = Utils.getMouseY() - 10 - posY + val color: Char = stack?.getLore()?.lastOrNull()?.take(4)?.get(1) + ?: Utils.getPrimaryColourCode(textLines[0]) + val colourInt = Minecraft.getMinecraft().fontRendererObj.getColorCode(color) + val borderColorStart = Color(colourInt).darker().rgb and 0x00FFFFFF or (200 shl 24) + val font = Minecraft.getMinecraft().fontRendererObj + val scaled = ScaledResolution(Minecraft.getMinecraft()) + GlStateManager.disableRescaleNormal() + RenderHelper.disableStandardItemLighting() + GlStateManager.disableLighting() + GlStateManager.enableDepth() + var tooltipTextWidth = 0 + for (textLine in textLines) { + val textLineWidth = font.getStringWidth(textLine) + if (textLineWidth > tooltipTextWidth) { + tooltipTextWidth = textLineWidth + } + } + var needsWrap = false + var titleLinesCount = 1 + var tooltipX = x + if (tooltipX + tooltipTextWidth + 4 > scaled.scaledWidth) { + tooltipX = x - 16 - tooltipTextWidth + if (tooltipX < 4) { + tooltipTextWidth = if (x > scaled.scaledWidth / 2) { + x - 12 - 8 + } else { + scaled.scaledWidth - 16 - x + } + needsWrap = true + } + } + if (needsWrap) { + var wrappedTooltipWidth = 0 + val wrappedTextLines: MutableList<String?> = ArrayList() + for (i in textLines.indices) { + val textLine = textLines[i] + val wrappedLine = font.listFormattedStringToWidth(textLine, tooltipTextWidth) + if (i == 0) { + titleLinesCount = wrappedLine.size + } + for (line in wrappedLine) { + val lineWidth = font.getStringWidth(line) + if (lineWidth > wrappedTooltipWidth) { + wrappedTooltipWidth = lineWidth + } + wrappedTextLines.add(line) + } + } + tooltipTextWidth = wrappedTooltipWidth + textLines = wrappedTextLines.toList() + tooltipX = if (x > scaled.scaledWidth / 2) { + x - 16 - tooltipTextWidth + } else { + x + 12 + } + } + var tooltipY = y - 12 + var tooltipHeight = 8 + if (textLines.size > 1) { + tooltipHeight += (textLines.size - 1) * 10 + if (textLines.size > titleLinesCount) { + tooltipHeight += 2 + } + } + + if (tooltipY + tooltipHeight + 6 > scaled.scaledHeight) { + tooltipY = scaled.scaledHeight - tooltipHeight - 6 + } + val zLevel = 300 + val backgroundColor = -0xfeffff0 + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY - 4, + tooltipX + tooltipTextWidth + 3, + tooltipY - 3, + backgroundColor, + backgroundColor + ) + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY + tooltipHeight + 3, + tooltipX + tooltipTextWidth + 3, + tooltipY + tooltipHeight + 4, + backgroundColor, + backgroundColor + ) + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY - 3, + tooltipX + tooltipTextWidth + 3, + tooltipY + tooltipHeight + 3, + backgroundColor, + backgroundColor + ) + drawGradientRect( + zLevel, + tooltipX - 4, + tooltipY - 3, + tooltipX - 3, + tooltipY + tooltipHeight + 3, + backgroundColor, + backgroundColor + ) + drawGradientRect( + zLevel, + tooltipX + tooltipTextWidth + 3, + tooltipY - 3, + tooltipX + tooltipTextWidth + 4, + tooltipY + tooltipHeight + 3, + backgroundColor, + backgroundColor + ) + val borderColorEnd = borderColorStart and 0xFEFEFE shr 1 or (borderColorStart and -0x1000000) + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY - 3 + 1, + tooltipX - 3 + 1, + tooltipY + tooltipHeight + 3 - 1, + borderColorStart, + borderColorEnd + ) + drawGradientRect( + zLevel, + tooltipX + tooltipTextWidth + 2, + tooltipY - 3 + 1, + tooltipX + tooltipTextWidth + 3, + tooltipY + tooltipHeight + 3 - 1, + borderColorStart, + borderColorEnd + ) + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY - 3, + tooltipX + tooltipTextWidth + 3, + tooltipY - 3 + 1, + borderColorStart, + borderColorStart + ) + drawGradientRect( + zLevel, + tooltipX - 3, + tooltipY + tooltipHeight + 2, + tooltipX + tooltipTextWidth + 3, + tooltipY + tooltipHeight + 3, + borderColorEnd, + borderColorEnd + ) + GlStateManager.disableDepth() + for (lineNumber in textLines.indices) { + val line = textLines[lineNumber] + font.drawStringWithShadow(line, 1f + tooltipX.toFloat(), 1f + tooltipY.toFloat(), -1) + if (lineNumber + 1 == titleLinesCount) { + tooltipY += 2 + } + tooltipY += 10 + } + GlStateManager.enableLighting() + GlStateManager.enableDepth() + RenderHelper.enableStandardItemLighting() + GlStateManager.enableRescaleNormal() + } + GlStateManager.disableLighting() + } + + private fun drawGradientRect(zLevel: Int, left: Int, top: Int, right: Int, bottom: Int, startColor: Int, endColor: Int) { + val startAlpha = (startColor shr 24 and 255).toFloat() / 255.0f + val startRed = (startColor shr 16 and 255).toFloat() / 255.0f + val startGreen = (startColor shr 8 and 255).toFloat() / 255.0f + val startBlue = (startColor and 255).toFloat() / 255.0f + val endAlpha = (endColor shr 24 and 255).toFloat() / 255.0f + val endRed = (endColor shr 16 and 255).toFloat() / 255.0f + val endGreen = (endColor shr 8 and 255).toFloat() / 255.0f + val endBlue = (endColor and 255).toFloat() / 255.0f + GlStateManager.disableTexture2D() + GlStateManager.enableBlend() + GlStateManager.disableAlpha() + GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0) + GlStateManager.shadeModel(7425) + val tessellator = Tessellator.getInstance() + val worldrenderer = tessellator.worldRenderer + worldrenderer.begin(7, DefaultVertexFormats.POSITION_COLOR) + worldrenderer.pos(right.toDouble(), top.toDouble(), zLevel.toDouble()).color(startRed, startGreen, startBlue, startAlpha).endVertex() + worldrenderer.pos(left.toDouble(), top.toDouble(), zLevel.toDouble()).color(startRed, startGreen, startBlue, startAlpha).endVertex() + worldrenderer.pos(left.toDouble(), bottom.toDouble(), zLevel.toDouble()).color(endRed, endGreen, endBlue, endAlpha).endVertex() + worldrenderer.pos(right.toDouble(), bottom.toDouble(), zLevel.toDouble()).color(endRed, endGreen, endBlue, endAlpha).endVertex() + tessellator.draw() + GlStateManager.shadeModel(7424) + GlStateManager.disableBlend() + GlStateManager.enableAlpha() + GlStateManager.enableTexture2D() + } +}
\ No newline at end of file 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 c554b8bde..dabf428d4 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -2,14 +2,12 @@ package at.hannibal2.skyhanni.utils.renderables import at.hannibal2.skyhanni.config.core.config.gui.GuiPositionEditor import at.hannibal2.skyhanni.data.ToolTipData -import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper import io.github.moulberry.notenoughupdates.util.Utils import net.minecraft.client.Minecraft import net.minecraft.client.gui.Gui -import net.minecraft.client.gui.GuiScreen import net.minecraft.client.gui.inventory.GuiEditSign import net.minecraft.client.renderer.GlStateManager import net.minecraft.item.ItemStack @@ -21,7 +19,7 @@ interface Renderable { 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? + && Utils.getMouseY() in (posY..posY + height) // TODO: adjust for variable height? /** * N.B.: the offset is absolute, not relative to the position and shouldn't be used for rendering @@ -31,6 +29,7 @@ interface Renderable { companion object { val logger = LorenzLogger("debug/renderable") + val list = mutableMapOf<Pair<Int, Int>, List<Int>>() fun fromAny(any: Any?, itemScale: Double = 1.0): Renderable? = when (any) { null -> placeholder(12) @@ -101,12 +100,8 @@ interface Renderable { } } - fun hoverTips( - text: String, - tips: List<String>, - bypassChecks: Boolean = false, - condition: () -> Boolean = { true } - ): Renderable { + fun hoverTips(text: String, tips: List<String>, indexes: List<Int> = listOf(), stack: ItemStack? = null, bypassChecks: Boolean = false, condition: () -> Boolean = { true }): Renderable { + val render = string(text) return object : Renderable { override val width: Int @@ -117,53 +112,18 @@ interface Renderable { render.render(posX, posY) if (isHovered(posX, posY)) { if (condition() && shouldAllowLink(true, bypassChecks)) { - renderToolTips(posX, posY, tips) + list[Pair(posX, posY)] = indexes + RenderLineTooltips.drawHoveringText(posX, posY, tips, stack) + } + } else { + if (list.contains(Pair(posX, posY))) { + list.remove(Pair(posX, posY)) } } } } } - private fun renderToolTips(posX: Int, posY: Int, tips: List<String>, border: Int = 1) { - GlStateManager.pushMatrix() -// GlStateManager.translate(0f, 0f, 2f) -// GuiRenderUtils.drawTooltip(tips, posX, posY, 0) -// GlStateManager.translate(0f, 0f, -2f) - - val x = Utils.getMouseX() - posX + 10 - val startY = Utils.getMouseY() - posY - 10 - var maxX = 0 - var y = startY - val renderer = Minecraft.getMinecraft().fontRendererObj - - GlStateManager.translate(0f, 0f, 2f) - for (line in tips) { - renderer.drawStringWithShadow( - "§f$line", - 1f + x, - 1f + y, - 0 - ) - val currentX = renderer.getStringWidth(line) - if (currentX > maxX) { - maxX = currentX - } - y += 10 - } - GlStateManager.translate(0f, 0f, -1f) - - GuiScreen.drawRect( - x - border, - startY - border, - x + maxX + 10 + border, - y + border, - LorenzColor.DARK_GRAY.toColor().rgb - ) - GlStateManager.translate(0f, 0f, -1f) - - GlStateManager.popMatrix() - } - private fun shouldAllowLink(debug: Boolean = false, bypassChecks: Boolean): Boolean { val isGuiScreen = Minecraft.getMinecraft().currentScreen != null if (bypassChecks) { @@ -252,4 +212,5 @@ interface Renderable { } } } -}
\ No newline at end of file +} + |