diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-02-05 21:07:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-05 21:07:27 +0100 |
commit | 9c1352f880dd626d766d1477da4962ae7da457a7 (patch) | |
tree | dad938054293378e9427a57c2c33e1c548af779b /src/main | |
parent | 9ff71b4b53f4c97933cf22c2d3425e3b208d44d5 (diff) | |
download | skyhanni-9c1352f880dd626d766d1477da4962ae7da457a7.tar.gz skyhanni-9c1352f880dd626d766d1477da4962ae7da457a7.tar.bz2 skyhanni-9c1352f880dd626d766d1477da4962ae7da457a7.zip |
Backend: Renderable added Alignment (#885)
Added alignment support to Renderable. #885
Diffstat (limited to 'src/main')
3 files changed, 94 insertions, 15 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt index b7bbf9487..68e20ac8b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/RenderUtils.kt @@ -34,6 +34,9 @@ import kotlin.time.DurationUnit object RenderUtils { + enum class HorizontalAlignment { LEFT, CENTER, RIGHT } + enum class VerticalAlignment { TOP, CENTER, BOTTOM } + private val beaconBeam = ResourceLocation("textures/entity/beacon_beam.png") infix fun Slot.highlight(color: LorenzColor) { 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 142fd8165..68b29e2fc 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -4,6 +4,8 @@ import at.hannibal2.skyhanni.config.core.config.gui.GuiPositionEditor import at.hannibal2.skyhanni.data.ToolTipData import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen +import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment +import at.hannibal2.skyhanni.utils.RenderUtils.VerticalAlignment import io.github.moulberry.moulconfig.gui.GuiScreenElementWrapper import io.github.moulberry.notenoughupdates.util.Utils import net.minecraft.client.Minecraft @@ -18,6 +20,9 @@ import kotlin.math.max interface Renderable { val width: Int val height: Int + + val horizontalAlign: HorizontalAlignment + val verticalAlign: VerticalAlignment fun isHovered(posX: Int, posY: Int) = currentRenderPassMousePosition?.let { (x, y) -> x in (posX..posX + width) && y in (posY..posY + height) // TODO: adjust for variable height? @@ -97,9 +102,10 @@ interface Renderable { condition: () -> Boolean = { true }, ) = object : Renderable { - override val width: Int - get() = render.width - override val height = 10 + override val width = render.width + override val height = render.height + override val horizontalAlign = render.horizontalAlign + override val verticalAlign = render.verticalAlign private var wasDown = false @@ -128,9 +134,10 @@ interface Renderable { val render = string(text) return object : Renderable { - override val width: Int - get() = render.width - override val height = 11 + override val width = render.width + override val height = render.height + override val horizontalAlign = render.horizontalAlign + override val verticalAlign = render.verticalAlign override fun render(posX: Int, posY: Int) { render.render(posX, posY) @@ -198,6 +205,8 @@ interface Renderable { override val width: Int get() = renderable.width override val height = 10 + override val horizontalAlign = renderable.horizontalAlign + override val verticalAlign = renderable.verticalAlign override fun render(posX: Int, posY: Int) { Gui.drawRect(0, 10, width, 11, 0xFFFFFFFF.toInt()) @@ -216,19 +225,34 @@ interface Renderable { override val width: Int get() = max(hovered.width, unhovered.width) override val height = 10 + override val horizontalAlign get() = if (isHovered) hovered.horizontalAlign else unhovered.horizontalAlign + override val verticalAlign get() = if (isHovered) hovered.verticalAlign else unhovered.verticalAlign + + var isHovered = false override fun render(posX: Int, posY: Int) { - if (isHovered(posX, posY) && condition() && shouldAllowLink(true, bypassChecks)) + if (isHovered(posX, posY) && condition() && shouldAllowLink(true, bypassChecks)) { hovered.render(posX, posY) - else + isHovered = true + } else { unhovered.render(posX, posY) + isHovered = false + } + } } - fun itemStack(any: ItemStack, scale: Double = 1.0) = object : Renderable { + fun itemStack( + any: ItemStack, + scale: Double = 1.0, + horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, + verticalAlign: VerticalAlignment = VerticalAlignment.TOP, + ) = object : Renderable { override val width: Int get() = 12 override val height = 10 + override val horizontalAlign = horizontalAlign + override val verticalAlign = verticalAlign override fun render(posX: Int, posY: Int) { GlStateManager.pushMatrix() @@ -241,19 +265,28 @@ interface Renderable { return Collections.singletonList(string(string)) } - fun string(string: String) = object : Renderable { + fun string( + text: String, + horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, + verticalAlign: VerticalAlignment = VerticalAlignment.TOP, + ) = object : Renderable { + override val width: Int - get() = Minecraft.getMinecraft().fontRendererObj.getStringWidth(string) + get() = Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) override val height = 10 + override val horizontalAlign = horizontalAlign + override val verticalAlign = verticalAlign override fun render(posX: Int, posY: Int) { - Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow("§f$string", 1f, 1f, 0) + Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow("§f$text", 1f, 1f, 0) } } - fun placeholder(width: Int) = object : Renderable { - override val width: Int = width - override val height = 10 + fun placeholder(width: Int, height: Int = 10) = object : Renderable { + override val width = width + override val height = height + override val horizontalAlign = HorizontalAlignment.LEFT + override val verticalAlign = VerticalAlignment.TOP override fun render(posX: Int, posY: Int) { } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt new file mode 100644 index 000000000..9c6b94a55 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt @@ -0,0 +1,43 @@ +package at.hannibal2.skyhanni.utils.renderables + +import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment +import at.hannibal2.skyhanni.utils.RenderUtils.VerticalAlignment +import net.minecraft.client.renderer.GlStateManager + +internal object RenderableUtils { + + private fun calculateAlignmentXOffset(renderable: Renderable, xSpace: Int) = when (renderable.horizontalAlign) { + HorizontalAlignment.LEFT -> 0 + HorizontalAlignment.CENTER -> (xSpace - renderable.width) / 2 + HorizontalAlignment.RIGHT -> xSpace - renderable.width + } + + private fun calculateAlignmentYOffset(renderable: Renderable, ySpace: Int) = when (renderable.verticalAlign) { + VerticalAlignment.TOP -> 0 + VerticalAlignment.CENTER -> (ySpace - renderable.height) / 2 + VerticalAlignment.BOTTOM -> ySpace - renderable.height + } + + fun Renderable.renderXYAligned(posX: Int, posY: Int, xSpace: Int, ySpace: Int) { + val xOffset = calculateAlignmentXOffset(this, xSpace) + val yOffset = calculateAlignmentYOffset(this, ySpace) + GlStateManager.translate(xOffset.toFloat(), yOffset.toFloat(), 0f) + this.render(posX + xOffset, posY + yOffset) + GlStateManager.translate(-xOffset.toFloat(), -yOffset.toFloat(), 0f) + } + + fun Renderable.renderXAligned(posX: Int, posY: Int, xSpace: Int) { + val xOffset = calculateAlignmentXOffset(this, xSpace) + GlStateManager.translate(xOffset.toFloat(), 0f, 0f) + this.render(posX + xOffset, posY) + GlStateManager.translate(-xOffset.toFloat(), 0f, 0f) + } + + fun Renderable.renderYAligned(posX: Int, posY: Int, ySpace: Int) { + val yOffset = calculateAlignmentYOffset(this, ySpace) + GlStateManager.translate(0f, yOffset.toFloat(), 0f) + this.render(posX, posY + yOffset) + GlStateManager.translate(0f, -yOffset.toFloat(), 0f) + } + +} |