diff options
author | Linnea Gräf <nea@nea.moe> | 2023-09-15 09:55:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-15 09:55:26 +0200 |
commit | f5e2667e1ee0f8e663ae32f34f76e1a2bc0b6ada (patch) | |
tree | 6fb997272eab7c9d7e026bc865e873afdec4341e /src/main | |
parent | 54a5b0c591470cf0ee247977ecef7cbdcddc5172 (diff) | |
download | skyhanni-f5e2667e1ee0f8e663ae32f34f76e1a2bc0b6ada.tar.gz skyhanni-f5e2667e1ee0f8e663ae32f34f76e1a2bc0b6ada.tar.bz2 skyhanni-f5e2667e1ee0f8e663ae32f34f76e1a2bc0b6ada.zip |
Feature: gui scaling (#464)
Add gui scaling #464
Diffstat (limited to 'src/main')
7 files changed, 112 insertions, 22 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java b/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java index 5ce2cafce..f63192cde 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java +++ b/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java @@ -19,6 +19,7 @@ package at.hannibal2.skyhanni.config.core.config; +import at.hannibal2.skyhanni.SkyHanniMod; import com.google.gson.annotations.Expose; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.ScaledResolution; @@ -29,6 +30,9 @@ public class Position { @Expose private int y; @Expose + private float scale = 1F; + + @Expose private boolean centerX; @Expose private boolean centerY; @@ -52,6 +56,19 @@ public class Position { this.y = other.y; this.centerX = other.centerX; this.centerY = other.centerY; + this.scale = other.scale; + } + + public float getEffectiveScale() { + return Math.max(Math.min(scale * SkyHanniMod.getFeature().gui.globalScale, 10F), 0.1F); + } + + public float getScale() { + return scale; + } + + public void setScale(float newScale) { + scale = Math.max(Math.min(10F, newScale), 0.1f); } public int getRawX() { diff --git a/src/main/java/at/hannibal2/skyhanni/config/core/config/gui/GuiPositionEditor.kt b/src/main/java/at/hannibal2/skyhanni/config/core/config/gui/GuiPositionEditor.kt index e3b828407..8dff4260b 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/core/config/gui/GuiPositionEditor.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/core/config/gui/GuiPositionEditor.kt @@ -212,4 +212,25 @@ class GuiPositionEditor(private val positions: List<Position>, private val borde grabbedY += position.moveY(mouseY - grabbedY, elementHeight) } } + + override fun handleMouseInput() { + super.handleMouseInput() + val mw = Mouse.getEventDWheel() + if (mw == 0) return + val mx = Mouse.getEventX() * width / mc.displayWidth + val my = height - Mouse.getEventY() * height / mc.displayHeight - 1 + val hovered = positions.firstOrNull { it.clicked } + ?: positions.lastOrNull { + val size = it.getDummySize() + GuiRenderUtils.isPointInRect( + mx, my, + it.getAbsX() - border, it.getAbsY() - border, + size.x + border * 2, size.y + border * 2 + ) + } ?: return + if (mw < 0) + hovered.scale -= .1F + else + hovered.scale += .1F + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/GUIConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/GUIConfig.java index 2e02156e3..cf9f0cc39 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/GUIConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/GUIConfig.java @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.data.GuiEditManager; import com.google.gson.annotations.Expose; import io.github.moulberry.moulconfig.annotations.ConfigEditorButton; import io.github.moulberry.moulconfig.annotations.ConfigEditorKeybind; +import io.github.moulberry.moulconfig.annotations.ConfigEditorSlider; import io.github.moulberry.moulconfig.annotations.ConfigOption; import org.lwjgl.input.Keyboard; @@ -17,4 +18,9 @@ public class GUIConfig { @ConfigOption(name = "Open Hotkey", desc = "Press this key to open the GUI Editor.") @ConfigEditorKeybind(defaultKey = Keyboard.KEY_NONE) public int keyBindOpen = Keyboard.KEY_NONE; + + @Expose + @ConfigOption(name = "Global GUI scale", desc = "Globally scale all SkyHanni GUIs") + @ConfigEditorSlider(minValue = 0.1F, maxValue = 10, minStep = 0.05F) + public float globalScale = 1F; } diff --git a/src/main/java/at/hannibal2/skyhanni/data/GuiEditManager.kt b/src/main/java/at/hannibal2/skyhanni/data/GuiEditManager.kt index be373f62f..16b41f6e6 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/GuiEditManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/GuiEditManager.kt @@ -95,7 +95,7 @@ class GuiEditManager { return Vector2i(5, 5) } else { val (x, y) = currentBorderSize[internalName] ?: return Vector2i(1, 1) - return Vector2i(x, y) + return Vector2i((x * effectiveScale).toInt(), (y * effectiveScale).toInt()) } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index 760c4b946..57dfd0dc0 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsY import at.hannibal2.skyhanni.events.GuiRenderItemEvent import at.hannibal2.skyhanni.utils.renderables.Renderable import io.github.moulberry.moulconfig.internal.TextRenderUtils +import io.github.moulberry.notenoughupdates.util.Utils import net.minecraft.client.Minecraft import net.minecraft.client.gui.FontRenderer import net.minecraft.client.gui.Gui @@ -354,6 +355,15 @@ object RenderUtils { return lastValue + (currentValue - lastValue) * multiplier } + + fun Position.transform(): Pair<Int, Int> { + GlStateManager.translate(getAbsX().toFloat(), getAbsY().toFloat(), 0F) + GlStateManager.scale(effectiveScale, effectiveScale, 1F) + val x = ((Utils.getMouseX() - getAbsX()) / effectiveScale).toInt() + val y = ((Utils.getMouseY() - getAbsY()) / effectiveScale).toInt() + return x to y + } + fun Position.renderString(string: String?, offsetX: Int = 0, offsetY: Int = 0, posLabel: String) { if (string == null) return if (string == "") return @@ -364,12 +374,12 @@ object RenderUtils { private fun Position.renderString0(string: String?, offsetX: Int = 0, offsetY: Int = 0): Int { val display = "§f$string" GlStateManager.pushMatrix() - + transform() val minecraft = Minecraft.getMinecraft() val renderer = minecraft.renderManager.fontRenderer - val x = getAbsX() + offsetX - val y = getAbsY() + offsetY + val x = offsetX + val y = offsetY GlStateManager.translate(x + 1.0, y + 1.0, 0.0) renderer.drawStringWithShadow(display, 0f, 0f, 0) @@ -443,15 +453,17 @@ object RenderUtils { private fun Position.renderLine(line: List<Any?>, offsetY: Int, itemScale: Double = 1.0): Int { GlStateManager.pushMatrix() - GlStateManager.translate(getAbsX().toFloat(), (getAbsY() + offsetY).toFloat(), 0F) + val mp = transform() + GlStateManager.translate(0f, offsetY.toFloat(), 0F) var offsetX = 0 - for (any in line) { - 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) + Renderable.withMousePosition(mp.first, mp.second) { + for (any in line) { + val renderable = Renderable.fromAny(any, itemScale = itemScale) + ?: throw RuntimeException("Unknown render object: $any") + renderable.render(offsetX, offsetY) + offsetX += renderable.width + GlStateManager.translate(renderable.width.toFloat(), 0F, 0F) + } } GlStateManager.popMatrix() return offsetX @@ -972,7 +984,12 @@ object RenderUtils { ) } - fun GuiRenderItemEvent.RenderOverlayEvent.GuiRenderItemPost.drawSlotText(xPos: Int, yPos: Int, text: String, scale: Float) { + fun GuiRenderItemEvent.RenderOverlayEvent.GuiRenderItemPost.drawSlotText( + xPos: Int, + yPos: Int, + text: String, + scale: Float + ) { val fontRenderer = Minecraft.getMinecraft().fontRendererObj GlStateManager.disableLighting() 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<String?>, stack: ItemStack? = null) { + fun drawHoveringText(posX: Int, posY: Int, tips: List<String?>, 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<Pair<Int, Int>, List<Int>>() + var currentRenderPassMousePosition: Pair<Int, Int>? = null + private set + + fun <T> 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<String>, indexes: List<Int> = listOf(), stack: ItemStack? = null, 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 { @@ -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 { |