From 8bd15ec4a82aa7364cc26dc4109e0ad144cde873 Mon Sep 17 00:00:00 2001 From: David Cole <40234707+DavidArthurCole@users.noreply.github.com> Date: Sat, 14 Dec 2024 04:51:20 -0500 Subject: [PATCH 1/2] Done --- .../at/hannibal2/skyhanni/utils/ColorUtils.kt | 13 +- .../skyhanni/utils/GuiRenderUtils.kt | 228 ++---------------- .../hannibal2/skyhanni/utils/RenderUtils.kt | 42 +--- .../utils/renderables/RenderableTooltips.kt | 20 +- 4 files changed, 40 insertions(+), 263 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt index 1d61741b4cd9..871f9314f604 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt @@ -14,13 +14,24 @@ object ColorUtils { fun String.getFirstColorCode() = takeIf { it.firstOrNull() == '§' }?.getOrNull(1) + fun getAlpha(color: Int) = color shr 24 and 0xFF + fun getRed(color: Int) = color shr 16 and 0xFF fun getGreen(color: Int) = color shr 8 and 0xFF fun getBlue(color: Int) = color and 0xFF - fun getAlpha(color: Int) = color shr 24 and 0xFF + /** + * Returns a quad of the color's alpha, red, green, and blue values, in that order. + */ + fun getQuad(color: Int): Quad = + Quad( + getAlpha(color) / 255f, + getRed(color) / 255f, + getGreen(color) / 255f, + getBlue(color) / 255f + ) fun blendRGB(start: Color, end: Color, percent: Double) = Color( (start.red * (1 - percent) + end.red * percent).toInt(), diff --git a/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt index 68ffd74bcca8..f3799815e625 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/GuiRenderUtils.kt @@ -1,8 +1,5 @@ package at.hannibal2.skyhanni.utils -import at.hannibal2.skyhanni.config.features.skillprogress.SkillProgressBarConfig -import at.hannibal2.skyhanni.features.chroma.ChromaShaderManager -import at.hannibal2.skyhanni.features.chroma.ChromaType import at.hannibal2.skyhanni.utils.NumberUtil.fractionOf import at.hannibal2.skyhanni.utils.NumberUtil.roundTo import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment @@ -20,16 +17,13 @@ import org.lwjgl.opengl.GL11 import org.lwjgl.opengl.GL14 import java.awt.Color import java.text.DecimalFormat -import kotlin.math.ceil -import kotlin.math.min /** * Some functions taken from NotEnoughUpdates */ -// TODO cleanup of redundant functions object GuiRenderUtils { - fun drawStringCentered(str: String?, fr: FontRenderer, x: Float, y: Float, shadow: Boolean, color: Int) { + private fun drawStringCentered(str: String?, fr: FontRenderer, x: Float, y: Float, shadow: Boolean, color: Int) { val strLen = fr.getStringWidth(str) val x2 = x - strLen / 2f val y2 = y - fr.FONT_HEIGHT / 2f @@ -46,45 +40,13 @@ object GuiRenderUtils { Minecraft.getMinecraft().fontRendererObj.drawString(str, x.toFloat(), y.toFloat(), 0xffffff, true) } - fun drawTwoLineString(str: String, x: Float, y: Float) { - val desiredSplitIndex = str.length / 2 - var splitIndex = -1 - var lastColorCode = "" - - for (i in desiredSplitIndex downTo 0) { - if (str[i] == ' ') { - splitIndex = i - break - } - } - - if (splitIndex == -1) { - splitIndex = desiredSplitIndex - } - for (i in 0 until desiredSplitIndex) { - if (str[i] == '§' && i + 1 < str.length) { - lastColorCode = str.substring(i, i + 2) - } - } - - val firstString = str.substring(0, splitIndex).trim() - val secondString = lastColorCode + str.substring(splitIndex).trim() - - Minecraft.getMinecraft().fontRendererObj.drawString(firstString, x, y - 5, 0xffffff, true) - Minecraft.getMinecraft().fontRendererObj.drawString(secondString, x, y + 5, 0xffffff, true) - } - fun drawStringCentered(str: String?, x: Int, y: Int) { drawStringCentered( str, Minecraft.getMinecraft().fontRendererObj, x.toFloat(), y.toFloat(), true, 0xffffff, ) } - fun drawStringCentered(str: String?, x: Float, y: Float) { - drawStringCentered(str, x.toInt(), y.toInt()) - } - - fun renderItemStack(item: ItemStack, x: Int, y: Int) { + private fun renderItemStack(item: ItemStack, x: Int, y: Int) { val itemRender = Minecraft.getMinecraft().renderItem RenderHelper.enableGUIStandardItemLighting() itemRender.zLevel = -145f @@ -93,87 +55,6 @@ object GuiRenderUtils { RenderHelper.disableStandardItemLighting() } - // Code taken and edited from NEU - private fun drawTooltip( - textLines: List, - mouseX: Int, - mouseY: Int, - screenHeight: Int, - fr: FontRenderer, - ) { - if (textLines.isNotEmpty()) { - val borderColor = StringUtils.getColor(textLines[0], 0x505000FF) - - GlStateManager.disableRescaleNormal() - RenderHelper.disableStandardItemLighting() - GlStateManager.disableLighting() - GlStateManager.enableDepth() - var tooltipTextWidth = 0 - - for (textLine in textLines) { - val textLineWidth: Int = fr.getStringWidth(textLine) - if (textLineWidth > tooltipTextWidth) { - tooltipTextWidth = textLineWidth - } - } - - val tooltipX = mouseX + 12 - var tooltipY = mouseY - 12 - var tooltipHeight = 8 - - if (textLines.size > 1) tooltipHeight += (textLines.size - 1) * 10 + 2 - GlStateManager.translate(0f, 0f, 100f) - if (tooltipY + tooltipHeight + 6 > screenHeight) tooltipY = screenHeight - tooltipHeight - 6 - // main background - GuiScreen.drawRect( - tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, -0xfeffff0, - ) - - // borders - GuiScreen.drawRect( - tooltipX - 3, tooltipY - 3 + 1, tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColor, - ) - - GuiScreen.drawRect( - tooltipX + tooltipTextWidth + 2, - tooltipY - 3 + 1, - tooltipX + tooltipTextWidth + 3, - tooltipY + tooltipHeight + 3 - 1, - borderColor, - ) - - GuiScreen.drawRect( - tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColor, - ) - - GuiScreen.drawRect( - tooltipX - 3, - tooltipY + tooltipHeight + 2, - tooltipX + tooltipTextWidth + 3, - tooltipY + tooltipHeight + 3, - borderColor, - ) - GlStateManager.translate(0f, 0f, -100f) - GlStateManager.disableDepth() - - for (line in textLines) { - fr.drawString(line, tooltipX.toFloat(), tooltipY.toFloat(), 0xffffff, true) - - tooltipY += if (line == textLines[0]) 12 else 10 - } - - GlStateManager.enableDepth() - GlStateManager.enableLighting() - GlStateManager.enableRescaleNormal() - RenderHelper.enableStandardItemLighting() - } - GlStateManager.disableLighting() - } - - fun drawTooltip(textLines: List, mouseX: Int, mouseY: Int, screenHeight: Int) { - drawTooltip(textLines, mouseX, mouseY, screenHeight, Minecraft.getMinecraft().fontRendererObj) - } - fun isPointInRect(x: Int, y: Int, left: Int, top: Int, width: Int, height: Int) = left <= x && x < left + width && top <= y && y < top + height @@ -217,12 +98,6 @@ object GuiRenderUtils { ) } - private fun barColorGradient(double: Double): Int { - var newDouble = (double - .5) * 2 - if (newDouble < 0) newDouble = 0.0 - return Color((255 * (1 - newDouble)).toInt(), (255 * newDouble).toInt(), 0).rgb - } - fun Int.darkenColor(): Int { val color = Color(this) return Color(color.red / 5, color.green / 5, color.blue / 5).rgb @@ -243,91 +118,18 @@ object GuiRenderUtils { GuiScreen.drawRect(x, y, x + 16, y + 16, color) } - // Taken and edited from NEU <- it's broken - fun renderTexturedBar( - x: Float, - y: Float, - xSize: Float, - completed: Float, - color: Color, - useChroma: Boolean, - texture: SkillProgressBarConfig.TexturedBar.UsedTexture, - height: Float, - ) { - GlStateManager.pushMatrix() - GlStateManager.translate(x, y, 0f) - val w = xSize.toInt() - val w_2 = w / 2 - val k = min(w.toDouble(), ceil((completed * w).toDouble())).toInt() - val vanilla = texture == SkillProgressBarConfig.TexturedBar.UsedTexture.MATCH_PACK - val vMinEmpty = if (vanilla) 64 / 256f else 0f - val vMaxEmpty = if (vanilla) 69 / 256f else .5f - val vMinFilled = if (vanilla) 69 / 256f else .5f - val vMaxFilled = if (vanilla) 74 / 256f else 1f - - if (useChroma) { - ChromaShaderManager.begin(ChromaType.TEXTURED) - GlStateManager.color( - Color.LIGHT_GRAY.darker().red / 255f, - Color.LIGHT_GRAY.darker().green / 255f, - Color.LIGHT_GRAY.darker().blue / 255f, - 1f, - ) - } else { - GlStateManager.color(color.darker().red / 255f, color.darker().green / 255f, color.darker().blue / 255f, 1f) - } - - drawTexturedRect(x, y, w_2.toFloat(), height, 0f, w_2 / xSize, vMinEmpty, vMaxEmpty, GL11.GL_NEAREST) - drawTexturedRect(x + w_2, y, w_2.toFloat(), height, 1 - w_2 / xSize, 1f, vMinEmpty, vMaxEmpty, GL11.GL_NEAREST) - - if (useChroma) { - GlStateManager.color(Color.WHITE.red / 255f, Color.WHITE.green / 255f, Color.WHITE.blue / 255f, 1f) - } else { - GlStateManager.color(color.red / 255f, color.green / 255f, color.blue / 255f, 1f) - } - - if (k > 0) { - val uMax = w_2.toDouble().coerceAtMost(k.toDouble() / xSize).toFloat() - val width = w_2.coerceAtMost(k).toFloat() - drawTexturedRect(x, y, width, height, 0f, uMax, vMinFilled, vMaxFilled, GL11.GL_NEAREST) - if (completed > 0.5f) { - drawTexturedRect( - x + w_2, - y, - (k - w_2).toFloat(), - height, - 1 - w_2 / xSize, - 1 + (k - w) / xSize, - vMinFilled, - vMaxFilled, - GL11.GL_NEAREST, - ) - } - } - if (useChroma) { - ChromaShaderManager.end() - } - GlStateManager.popMatrix() - } - - /**@Mojang */ + /** @Mojang */ fun drawGradientRect( left: Int, top: Int, right: Int, bottom: Int, - startColor: Int, - endColor: Int, - zLevel: Double, + startColor: Int = -0xfeffff0, + endColor: Int = -0xfeffff0, + zLevel: Double = 0.0, ) { - val f = (startColor shr 24 and 255).toFloat() / 255.0f - val g = (startColor shr 16 and 255).toFloat() / 255.0f - val h = (startColor shr 8 and 255).toFloat() / 255.0f - val i = (startColor and 255).toFloat() / 255.0f - val j = (endColor shr 24 and 255).toFloat() / 255.0f - val k = (endColor shr 16 and 255).toFloat() / 255.0f - val l = (endColor shr 8 and 255).toFloat() / 255.0f - val m = (endColor and 255).toFloat() / 255.0f + val (startAlpha, startRed, startGreen, startBlue) = ColorUtils.getQuad(startColor) + val (endAlpha, endRed, endGreen, endBlue) = ColorUtils.getQuad(endColor) GlStateManager.disableTexture2D() GlStateManager.enableBlend() GlStateManager.disableAlpha() @@ -336,10 +138,14 @@ object GuiRenderUtils { val tessellator = Tessellator.getInstance() val worldRenderer = tessellator.worldRenderer worldRenderer.begin(7, DefaultVertexFormats.POSITION_COLOR) - worldRenderer.pos(right.toDouble(), top.toDouble(), zLevel).color(g, h, i, f).endVertex() - worldRenderer.pos(left.toDouble(), top.toDouble(), zLevel).color(g, h, i, f).endVertex() - worldRenderer.pos(left.toDouble(), bottom.toDouble(), zLevel).color(k, l, m, j).endVertex() - worldRenderer.pos(right.toDouble(), bottom.toDouble(), zLevel).color(k, l, m, j).endVertex() + worldRenderer.pos(right.toDouble(), top.toDouble(), zLevel) + .color(startRed, startGreen, startBlue, startAlpha).endVertex() + worldRenderer.pos(left.toDouble(), top.toDouble(), zLevel) + .color(startRed, startGreen, startBlue, startAlpha).endVertex() + worldRenderer.pos(left.toDouble(), bottom.toDouble(), zLevel) + .color(endRed, endGreen, endBlue, endAlpha).endVertex() + worldRenderer.pos(right.toDouble(), bottom.toDouble(), zLevel) + .color(endRed, endGreen, endBlue, endAlpha).endVertex() tessellator.draw() GlStateManager.shadeModel(7424) GlStateManager.disableBlend() @@ -368,7 +174,7 @@ object GuiRenderUtils { } // Taken from NEU - fun drawTexturedRect( + private fun drawTexturedRect( x: Float, y: Float, width: Float, diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index a6f92fb10932..2652afcef2e8 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -1936,7 +1936,7 @@ object RenderUtils { GlStateManager.pushMatrix() ShaderManager.enableShader(ShaderManager.Shaders.ROUNDED_RECT_OUTLINE) - drawGradientRect( + GuiRenderUtils.drawGradientRect( x - borderAdjustment, y - borderAdjustment, x + width + borderAdjustment, @@ -1949,46 +1949,6 @@ object RenderUtils { GlStateManager.popMatrix() } - // todo merge with the one in GuiRenderUtils - fun drawGradientRect( - left: Int, - top: Int, - right: Int, - bottom: Int, - startColor: Int = -0xfeffff0, - endColor: Int = -0xfeffff0, - ) { - 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(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 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(), 0.0) - .color(startRed, startGreen, startBlue, startAlpha).endVertex() - worldrenderer.pos(left.toDouble(), top.toDouble(), 0.0) - .color(startRed, startGreen, startBlue, startAlpha).endVertex() - worldrenderer.pos(left.toDouble(), bottom.toDouble(), 0.0) - .color(endRed, endGreen, endBlue, endAlpha).endVertex() - worldrenderer.pos(right.toDouble(), bottom.toDouble(), 0.0) - .color(endRed, endGreen, endBlue, endAlpha).endVertex() - tessellator.draw() - GlStateManager.shadeModel(7424) - GlStateManager.disableBlend() - GlStateManager.enableAlpha() - GlStateManager.enableTexture2D() - } - fun getAlpha(): Float { colorBuffer.clear() GlStateManager.getFloat(GL11.GL_CURRENT_COLOR, colorBuffer) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt index 906371bf4300..210cdd8d8a6a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt @@ -1,9 +1,9 @@ package at.hannibal2.skyhanni.utils.renderables import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.GuiRenderUtils.drawGradientRect import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzColor -import at.hannibal2.skyhanni.utils.RenderUtils import at.hannibal2.skyhanni.utils.compat.GuiScreenUtils import at.hannibal2.skyhanni.utils.renderables.RenderableUtils.renderXAligned import net.minecraft.client.Minecraft @@ -77,38 +77,38 @@ object RenderableTooltips { val zLevel = 400f GlStateManager.translate(tooltipX.toFloat(), tooltipY.toFloat(), zLevel) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = -4, right = tooltipTextWidth + 2, bottom = -3, ) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = tooltipHeight + 3, right = tooltipTextWidth + 2, bottom = tooltipHeight + 4, ) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = -3, right = tooltipTextWidth + 2, bottom = tooltipHeight + 3, ) - RenderUtils.drawGradientRect( + drawGradientRect( left = -4, top = -3, right = -3, bottom = tooltipHeight + 3, ) - RenderUtils.drawGradientRect( + drawGradientRect( left = tooltipTextWidth + 2, top = -3, right = tooltipTextWidth + 3, bottom = tooltipHeight + 3, ) val borderColorEnd = borderColorStart and 0xFEFEFE shr 1 or (borderColorStart and -0x1000000) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = -3 + 1, right = -3 + 1, @@ -116,7 +116,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorEnd ) - RenderUtils.drawGradientRect( + drawGradientRect( left = tooltipTextWidth + 1, top = -3 + 1, right = tooltipTextWidth + 2, @@ -124,7 +124,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorEnd ) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = -3, right = tooltipTextWidth + 2, @@ -132,7 +132,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorStart ) - RenderUtils.drawGradientRect( + drawGradientRect( left = -3, top = tooltipHeight + 2, right = tooltipTextWidth + 2, From ff06b69c2ec70bfd55c5a74662ce946707b689f1 Mon Sep 17 00:00:00 2001 From: David Cole <40234707+DavidArthurCole@users.noreply.github.com> Date: Sat, 14 Dec 2024 04:54:39 -0500 Subject: [PATCH 2/2] Fix direct import --- .../utils/renderables/RenderableTooltips.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt index 210cdd8d8a6a..d1270b3c545c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableTooltips.kt @@ -1,7 +1,7 @@ package at.hannibal2.skyhanni.utils.renderables import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule -import at.hannibal2.skyhanni.utils.GuiRenderUtils.drawGradientRect +import at.hannibal2.skyhanni.utils.GuiRenderUtils import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzColor import at.hannibal2.skyhanni.utils.compat.GuiScreenUtils @@ -77,38 +77,38 @@ object RenderableTooltips { val zLevel = 400f GlStateManager.translate(tooltipX.toFloat(), tooltipY.toFloat(), zLevel) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = -4, right = tooltipTextWidth + 2, bottom = -3, ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = tooltipHeight + 3, right = tooltipTextWidth + 2, bottom = tooltipHeight + 4, ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = -3, right = tooltipTextWidth + 2, bottom = tooltipHeight + 3, ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -4, top = -3, right = -3, bottom = tooltipHeight + 3, ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = tooltipTextWidth + 2, top = -3, right = tooltipTextWidth + 3, bottom = tooltipHeight + 3, ) val borderColorEnd = borderColorStart and 0xFEFEFE shr 1 or (borderColorStart and -0x1000000) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = -3 + 1, right = -3 + 1, @@ -116,7 +116,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorEnd ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = tooltipTextWidth + 1, top = -3 + 1, right = tooltipTextWidth + 2, @@ -124,7 +124,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorEnd ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = -3, right = tooltipTextWidth + 2, @@ -132,7 +132,7 @@ object RenderableTooltips { startColor = borderColorStart, endColor = borderColorStart ) - drawGradientRect( + GuiRenderUtils.drawGradientRect( left = -3, top = tooltipHeight + 2, right = tooltipTextWidth + 2,