diff options
Diffstat (limited to 'src/main/kotlin/dulkirmod/features/ScalableTooltips.kt')
-rw-r--r-- | src/main/kotlin/dulkirmod/features/ScalableTooltips.kt | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/main/kotlin/dulkirmod/features/ScalableTooltips.kt b/src/main/kotlin/dulkirmod/features/ScalableTooltips.kt new file mode 100644 index 0000000..ae53a3b --- /dev/null +++ b/src/main/kotlin/dulkirmod/features/ScalableTooltips.kt @@ -0,0 +1,146 @@ +package dulkirmod.features + +import dulkirmod.config.Config +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.FontRenderer +import net.minecraft.client.renderer.GlStateManager +import net.minecraft.client.renderer.RenderHelper +import net.minecraftforge.fml.client.config.GuiUtils +import org.lwjgl.input.Keyboard +import org.lwjgl.input.Mouse +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo + +object ScalableTooltips { + var scrollY: Int = 0 + var scrollX: Int = 0 + // Checks to see if large tooltips should be snapped (for larger than can fit on screen code) + var snapFlag: Boolean = true + + fun drawScaledHoveringText( + textLines: List<String>, + mouseX: Int, + mouseY: Int, + screenWidth: Int, + screenHeight: Int, + maxTextWidth: Int, + font: FontRenderer, + ci: CallbackInfo + ): Boolean { + if(!Config.scaledTooltips) return false + val scale = Config.tooltipSize + + // Calculate the width and height of the tooltip box + var width = 0 + for (textLine in textLines) { + val textWidth = font.getStringWidth(textLine) + if (textWidth > width) { + width = textWidth + } + } + val height = (textLines.size) * font.FONT_HEIGHT + + // Save the matrix state and scale it + GlStateManager.pushMatrix() + GlStateManager.scale(scale, scale, 1f) + GlStateManager.disableRescaleNormal() + RenderHelper.disableStandardItemLighting() + GlStateManager.disableLighting() + GlStateManager.disableDepth() + + // Calculate the amount of translation that should be applied based on how much the user has scrolled + val eventDWheel = Mouse.getDWheel() + if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) { + if (eventDWheel < 0) { + scrollX += Minecraft.getMinecraft().displayWidth/192 + } else if (eventDWheel > 0) { + //Scrolling to access higher stuff + scrollX -= Minecraft.getMinecraft().displayWidth/192 + } + } else { + if (eventDWheel < 0) { + scrollY -= Minecraft.getMinecraft().displayHeight/108 + } else if (eventDWheel > 0) { + //Scrolling to access higher stuff + scrollY += Minecraft.getMinecraft().displayHeight/108 + } + } + + // calculates where it wants to put the tooltip based on user input + var x = ((mouseX + 12 + scrollX) / scale).toInt() + var y = ((mouseY - 12 + scrollY) / scale).toInt() + + /** + * Extra code to account for larger tooltips: + * Tooltips should not initially render off the screen if they can fit, if too wide/too long for screen + * it will just default to normal. + */ + if ((x + width + 4 > screenWidth / scale) && (width + 4 <= screenWidth / scale)) { + scrollX = (screenWidth - mouseX - 12 - (width + 4)* scale).toInt() + } + + if ((y + height + 4 > screenHeight / scale) && (height + 4 <= screenHeight / scale)) { + scrollY = (screenHeight - mouseY + 12 - (height + 4)* scale).toInt() + } + + /** + * HAVE: default x and y + * NEED: modify scrollx and scroll why such that updates x and y to 0 later + */ + if (x < 0 && (width + 4 <= screenWidth / scale)) + scrollX = -mouseX - 12 + 4 + if (y < 0 && (height + 4 <= screenHeight / scale)) + scrollY = -mouseY + 12 + 4 + + + // if too large, then snap to top (if first time rendering tooltip) + if (snapFlag) { + if (width + 4 > screenWidth / scale) { + scrollX = -mouseX - 12 + 4 + } + if (height + 4 > screenHeight / scale) { + scrollY = -mouseY + 12 + 4 + } + snapFlag = false + } + + //updates the position of x and y if it has been modified. + x = ((mouseX + 12 + scrollX) / scale).toInt() + y = ((mouseY - 12 + scrollY) / scale).toInt() + + // Draw the background rectangle + val backgroundColor = -0xfeffff0 + val zLevel = 300 + + GuiUtils.drawGradientRect(zLevel, x - 3, y - 4, x + width + 3, y - 3, backgroundColor, backgroundColor) + GuiUtils.drawGradientRect(zLevel, x - 3, y + height + 3, x + width + 3, y + height + 4, backgroundColor, backgroundColor) + GuiUtils.drawGradientRect(zLevel, x - 3, y - 3, x + width + 3, y + height + 3, backgroundColor, backgroundColor) + GuiUtils.drawGradientRect(zLevel, x - 4, y - 3, x - 3, y + height + 3, backgroundColor, backgroundColor) + GuiUtils.drawGradientRect(zLevel, x + width + 3, y - 3, x + width + 4, y + height + 3, backgroundColor, backgroundColor) + val borderColorStart = 0x505000FF + val borderColorEnd = borderColorStart and 0xFEFEFE shr 1 or (borderColorStart and -0x1000000) + GuiUtils.drawGradientRect(zLevel, x - 3, y - 3 + 1, x - 3 + 1, y + height + 3 - 1, borderColorStart, borderColorEnd) + GuiUtils.drawGradientRect(zLevel, x + width + 2, y - 3 + 1, x + width + 3, y + height + 3 - 1, borderColorStart, borderColorEnd) + GuiUtils.drawGradientRect(zLevel, x - 3, y - 3, x + width + 3, y - 3 + 1, borderColorStart, borderColorStart) + GuiUtils.drawGradientRect(zLevel, x - 3, y + height + 2, x + width + 3, y + height + 3, borderColorEnd, borderColorEnd) + + // Render the tooltip text + var yStart = y + for (textLine in textLines) { + font.drawStringWithShadow(textLine, x.toFloat(), yStart.toFloat(), -1) + yStart += font.FONT_HEIGHT + } + // Reset matrix state + GlStateManager.enableLighting() + GlStateManager.enableDepth() + RenderHelper.enableStandardItemLighting() + GlStateManager.enableRescaleNormal() + GlStateManager.popMatrix() + return true + } + + fun resetPos() { + scrollX = 0 + scrollY = 0 + snapFlag = true + } +}
\ No newline at end of file |