From 89dee6841a641e8ea3f7a545dc0c2f8d0d1d6ad2 Mon Sep 17 00:00:00 2001 From: moonleay Date: Sun, 5 May 2024 20:03:29 +0200 Subject: [PATCH] feat: finished HUD pos conf system Signed-off-by: moonleay --- .../gimbal/client/config/GimbalGuiSettings.kt | 5 +- .../{AnchorPoint.kt => HorizontalAnchor.kt} | 10 ++-- .../gimbal/client/config/VerticalAnchor.kt | 28 ++++++++++ .../gimbal/client/screen/GimbalEditHudGui.kt | 53 ++++++++++++------- .../gimbal/client/screen/GimbalSettingsGui.kt | 5 +- .../gimbal/client/util/screen/ScreenUtil.kt | 38 ++++++++----- .../net/moonleay/gimbal/mixin/HudMixin.java | 44 +++++++++++++-- 7 files changed, 136 insertions(+), 47 deletions(-) rename src/main/java/net/moonleay/gimbal/client/config/{AnchorPoint.kt => HorizontalAnchor.kt} (88%) create mode 100644 src/main/java/net/moonleay/gimbal/client/config/VerticalAnchor.kt diff --git a/src/main/java/net/moonleay/gimbal/client/config/GimbalGuiSettings.kt b/src/main/java/net/moonleay/gimbal/client/config/GimbalGuiSettings.kt index 2a274b0..7c24608 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/GimbalGuiSettings.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/GimbalGuiSettings.kt @@ -23,6 +23,7 @@ import kotlinx.serialization.Serializable @Serializable data class GimbalGuiSettings( val showHud: Boolean = true, - val scaledRes: ScaledRes = ScaledRes(4.0, 4.0), - val anchorPoint: AnchorPoint = AnchorPoint.TOP_LEFT, + val offset: ScaledRes = ScaledRes(4.0, 4.0), + val horizontalAnchor: HorizontalAnchor = HorizontalAnchor.LEFT, + val verticalAnchor: VerticalAnchor = VerticalAnchor.TOP, ) diff --git a/src/main/java/net/moonleay/gimbal/client/config/AnchorPoint.kt b/src/main/java/net/moonleay/gimbal/client/config/HorizontalAnchor.kt similarity index 88% rename from src/main/java/net/moonleay/gimbal/client/config/AnchorPoint.kt rename to src/main/java/net/moonleay/gimbal/client/config/HorizontalAnchor.kt index 29f34a2..fa4dc4f 100644 --- a/src/main/java/net/moonleay/gimbal/client/config/AnchorPoint.kt +++ b/src/main/java/net/moonleay/gimbal/client/config/HorizontalAnchor.kt @@ -21,10 +21,8 @@ package net.moonleay.gimbal.client.config import kotlinx.serialization.Serializable @Serializable -enum class AnchorPoint { - TOP_LEFT, - TOP_RIGHT, - BOTTOM_RIGHT, - BOTTOM_LEFT, - CENTER +enum class HorizontalAnchor { + LEFT, + CENTER, + RIGHT, } diff --git a/src/main/java/net/moonleay/gimbal/client/config/VerticalAnchor.kt b/src/main/java/net/moonleay/gimbal/client/config/VerticalAnchor.kt new file mode 100644 index 0000000..7fd660a --- /dev/null +++ b/src/main/java/net/moonleay/gimbal/client/config/VerticalAnchor.kt @@ -0,0 +1,28 @@ +/* + * Gimbal + * Copyright (C) 2024 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.gimbal.client.config + +import kotlinx.serialization.Serializable + +@Serializable +enum class VerticalAnchor { + TOP, + CENTER, + BOTTOM, +} diff --git a/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt b/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt index 0b7fd61..6af484e 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/GimbalEditHudGui.kt @@ -24,7 +24,10 @@ import net.minecraft.client.util.math.MatrixStack import net.minecraft.screen.ScreenTexts import net.minecraft.text.Text import net.moonleay.gimbal.build.BuildConstants -import net.moonleay.gimbal.client.config.* +import net.moonleay.gimbal.client.config.ClientConfigHolder +import net.moonleay.gimbal.client.config.GimbalClientConfig +import net.moonleay.gimbal.client.config.GimbalGuiSettings +import net.moonleay.gimbal.client.config.ScaledRes import net.moonleay.gimbal.client.util.screen.ScreenUtil import org.apache.logging.log4j.LogManager @@ -37,8 +40,8 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig private var shouldFollow = false init { - this.tempXOffset = cfg.config.guiSettings.scaledRes.scaledX - this.tempYOffset = cfg.config.guiSettings.scaledRes.scaledY + this.tempXOffset = cfg.config.guiSettings.offset.scaledX + this.tempYOffset = cfg.config.guiSettings.offset.scaledY } override fun init() { @@ -69,14 +72,18 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig } LOGGER.info("Saving Pos...") + val anchor = ScreenUtil.getAnchor(this.tempXOffset, this.tempYOffset) + val newConf = GimbalClientConfig( showToasts = oldConf.showToasts, guiSettings = GimbalGuiSettings( showHud = oldConf.guiSettings.showHud, - scaledRes = ScaledRes( + offset = ScaledRes( scaledX = this.tempXOffset, scaledY = this.tempYOffset, - ) + ), + horizontalAnchor = anchor.first, + verticalAnchor = anchor.second ) ) @@ -92,12 +99,14 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig super.tick() if (this.shouldFollow) { val mouse = this.client?.mouse!! - val scaleFactor = this.client!!.window.calculateScaleFactor( - this.client!!.options.guiScale.value, - this.client!!.forcesUnicodeFont() - ) - this.tempXOffset = ScreenUtil.getScaled(this.width, mouse.x.toFloat() / scaleFactor) - this.tempYOffset = ScreenUtil.getScaled(this.height, mouse.y.toFloat() / scaleFactor) + val wantedX = ScreenUtil.getScaled(this.client!!.window.width, mouse.x.toFloat()) + val wantedY = ScreenUtil.getScaled(this.client!!.window.height, mouse.y.toFloat()) + if (!ScreenUtil.isPositionOutOfBounds(wantedX, wantedY)) { + this.tempXOffset = wantedX + this.tempYOffset = wantedY + } else { + LOGGER.info("Text is oob. wantedX: ${wantedX}, wantedY: ${wantedY}, width: ${this.client!!.window.width}, height: ${this.client!!.window.height}") + } } } @@ -114,22 +123,30 @@ class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfig override fun render(matrices: MatrixStack?, mouseX: Int, mouseY: Int, delta: Float) { this.renderBackground(matrices) drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 15, 16777215) + val scaleFactor = this.client!!.window.calculateScaleFactor( + this.client!!.options.guiScale.value, + this.client!!.forcesUnicodeFont() + ) + val anchor = ScreenUtil.getAnchor(this.tempXOffset, this.tempYOffset) this.textRenderer.drawWithShadow( matrices, Text.translatable("gimbal.gui.examplehud"), - ScreenUtil.getXForRender( - ScreenUtil.getReal(this.width, this.tempXOffset), - AnchorPoint.CENTER, + ScreenUtil.getXForAnchor( + ScreenUtil.getReal(this.client!!.window.width, this.tempXOffset) / scaleFactor, + anchor.first, this.textRenderer.getWidth(Text.translatable("gimbal.gui.examplehud")) ), - ScreenUtil.getYForRender( - ScreenUtil.getReal(this.height, this.tempYOffset), - AnchorPoint.CENTER, + ScreenUtil.getYForAnchor( + ScreenUtil.getReal(this.client!!.window.height, this.tempYOffset) / scaleFactor, + anchor.second, this.textRenderer.fontHeight ), 0xFFFFFF ) // TODO: improve placement when changing the scaling -// LOGGER.info("width: ${this.width}, height: ${this.height}, posX: ${ScreenUtil.getReal(this.width, this.tempXOffset)}, posY: ${ScreenUtil.getReal(this.height, this.tempYOffset)}, scaleX: ${this.tempXOffset}, scaleY: ${this.tempYOffset}") +// LOGGER.info("width: ${this.width}, height: ${this.height}," + +// " posX: ${ScreenUtil.getReal(this.width, this.tempXOffset)}," + +// " posY: ${ScreenUtil.getReal(this.height, this.tempYOffset)}," + +// " scaleX: ${this.tempXOffset}, scaleY: ${this.tempYOffset}") super.render(matrices, mouseX, mouseY, delta) } } diff --git a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt index 99cedbb..0885102 100644 --- a/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt +++ b/src/main/java/net/moonleay/gimbal/client/screen/GimbalSettingsGui.kt @@ -46,8 +46,9 @@ class GimbalSettingsGui(private val parent: Screen, private val cfg: ClientConfi val oldGui = cfg.config.guiSettings val newGui = GimbalGuiSettings( showHud = isEnabled ?: true, - scaledRes = oldGui.scaledRes, - anchorPoint = oldGui.anchorPoint + offset = oldGui.offset, + horizontalAnchor = oldGui.horizontalAnchor, + verticalAnchor = oldGui.verticalAnchor, ) val newConf = GimbalClientConfig( guiSettings = newGui, diff --git a/src/main/java/net/moonleay/gimbal/client/util/screen/ScreenUtil.kt b/src/main/java/net/moonleay/gimbal/client/util/screen/ScreenUtil.kt index c9166cb..9c0c130 100644 --- a/src/main/java/net/moonleay/gimbal/client/util/screen/ScreenUtil.kt +++ b/src/main/java/net/moonleay/gimbal/client/util/screen/ScreenUtil.kt @@ -18,27 +18,37 @@ package net.moonleay.gimbal.client.util.screen -import net.moonleay.gimbal.client.config.AnchorPoint +import net.moonleay.gimbal.client.config.HorizontalAnchor +import net.moonleay.gimbal.client.config.VerticalAnchor object ScreenUtil { - fun getXForRender(x: Float, anchor: AnchorPoint, textWidth: Int): Float { - return when (anchor) { - AnchorPoint.TOP_LEFT -> x - AnchorPoint.BOTTOM_LEFT -> x - AnchorPoint.TOP_RIGHT -> x - textWidth - AnchorPoint.BOTTOM_RIGHT -> x - textWidth - AnchorPoint.CENTER -> x - textWidth / 2 + fun getAnchor(x: Double, y: Double): Pair { + return when (x) { + in 0.0..<34.0 -> HorizontalAnchor.LEFT + in 34.0..<67.0 -> HorizontalAnchor.CENTER + else -> HorizontalAnchor.RIGHT + } to when (y) { + in 0.0..<34.0 -> VerticalAnchor.TOP + in 34.0..<67.0 -> VerticalAnchor.CENTER + else -> VerticalAnchor.BOTTOM } } - fun getYForRender(y: Float, anchor: AnchorPoint, textHeight: Int): Float { + fun getXForAnchor(x: Float, anchor: HorizontalAnchor, textWidth: Int): Float { return when (anchor) { - AnchorPoint.TOP_LEFT -> y - AnchorPoint.TOP_RIGHT -> y - AnchorPoint.BOTTOM_RIGHT -> y - textHeight - AnchorPoint.BOTTOM_LEFT -> y - textHeight - AnchorPoint.CENTER -> y - textHeight / 2 + HorizontalAnchor.LEFT -> x + HorizontalAnchor.CENTER -> x - (textWidth / 2) + HorizontalAnchor.RIGHT -> x - textWidth + } + } + + + fun getYForAnchor(y: Float, anchor: VerticalAnchor, textHeight: Int): Float { + return when (anchor) { + VerticalAnchor.TOP -> y + VerticalAnchor.CENTER -> y - (textHeight / 2) + VerticalAnchor.BOTTOM -> y - textHeight } } diff --git a/src/main/java/net/moonleay/gimbal/mixin/HudMixin.java b/src/main/java/net/moonleay/gimbal/mixin/HudMixin.java index fab79b6..f9a3d89 100644 --- a/src/main/java/net/moonleay/gimbal/mixin/HudMixin.java +++ b/src/main/java/net/moonleay/gimbal/mixin/HudMixin.java @@ -22,7 +22,11 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.hud.InGameHud; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import net.moonleay.gimbal.client.ClientMain; +import net.moonleay.gimbal.client.config.GimbalGuiSettings; import net.moonleay.gimbal.client.editor.ClientEditor; +import net.moonleay.gimbal.client.util.screen.ScreenUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -31,16 +35,46 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(InGameHud.class) -public class HudMixin { +public abstract class HudMixin { @Shadow @Final private MinecraftClient client; + @Shadow + private int scaledHeight; + + @Shadow + public abstract void clear(); + @Inject(method = "renderStatusEffectOverlay", at = @At("HEAD")) private void render(MatrixStack matrices, CallbackInfo ci) { TextRenderer tr = this.client.textRenderer; - tr.drawWithShadow(matrices, - ClientEditor.INSTANCE.getModeDisplayText(), - 4, 4, - ClientEditor.INSTANCE.getCurrentColor()); + GimbalGuiSettings conf = ClientMain.CONFIG.getConfig().getGuiSettings(); + if (!conf.getShowHud()) + return; + int scaleFactor = this.client.getWindow() + .calculateScaleFactor( + this.client.options.getGuiScale().getValue(), + this.client.forcesUnicodeFont() + ); + Text displayText = ClientEditor.INSTANCE.getModeDisplayText(); + tr.drawWithShadow( + matrices, + displayText, + ScreenUtil.INSTANCE.getXForAnchor( + ScreenUtil.INSTANCE.getReal( + this.client.getWindow().getWidth(), + conf.getOffset().getScaledX() / scaleFactor), + conf.getHorizontalAnchor(), + this.client.textRenderer.getWidth(displayText) + ), + ScreenUtil.INSTANCE.getYForAnchor( + ScreenUtil.INSTANCE.getReal( + this.client.getWindow().getHeight(), + conf.getOffset().getScaledY() / scaleFactor), + conf.getVerticalAnchor(), + this.client.textRenderer.fontHeight + ), + ClientEditor.INSTANCE.getCurrentColor() + ); } }