/* * 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.screen import net.minecraft.client.gui.screen.Screen import net.minecraft.client.gui.widget.ButtonWidget 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.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 class GimbalEditHudGui(private val parent: Screen, private val cfg: ClientConfigHolder) : Screen(Text.translatable("gimbal.gui.edithud")) { private val LOGGER = LogManager.getLogger(BuildConstants.modName) private var tempXOffset = 4.0 private var tempYOffset = 4.0 private var shouldFollow = false init { this.tempXOffset = cfg.config.guiSettings.offset.scaledX this.tempYOffset = cfg.config.guiSettings.offset.scaledY } override fun init() { super.init() this.addDrawableChild(ButtonWidget( this.width / 2 + 10, this.height - 27, 90, 20, Text.translatable("gimbal.gui.resethud") ) { _: ButtonWidget? -> tempXOffset = 4.0 tempYOffset = 4.0 shouldFollow = false }) this.addDrawableChild(ButtonWidget( this.width / 2 - 100, this.height - 27, 90, 20, ScreenTexts.DONE ) { _: ButtonWidget? -> this.save() this.client!!.setScreen( this.parent ) }) } private fun save() { val oldConf = cfg.config if (ScreenUtil.isPositionOutOfBounds(this.tempXOffset, this.tempYOffset)) { LOGGER.info("Text is not in bounds. This will not be saved in order to keep the user from making the hud inaccessible.") return } LOGGER.info("Saving Pos...") val anchor = ScreenUtil.getAnchor(this.tempXOffset, this.tempYOffset) val newConf = GimbalClientConfig( showToasts = oldConf.showToasts, guiSettings = GimbalGuiSettings( showHud = oldConf.guiSettings.showHud, offset = ScaledRes( scaledX = this.tempXOffset, scaledY = this.tempYOffset, ), horizontalAnchor = anchor.first, verticalAnchor = anchor.second ) ) cfg.updateConfig(newConf) } override fun close() { this.save() this.client!!.setScreen(this.parent) } override fun tick() { super.tick() if (this.shouldFollow) { val mouse = this.client?.mouse!! 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}") } } } override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean { this.shouldFollow = true return super.mouseClicked(mouseX, mouseY, button) } override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean { this.shouldFollow = false return super.mouseReleased(mouseX, mouseY, button) } 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.getXForAnchor( ScreenUtil.getReal(this.client!!.window.width, this.tempXOffset) / scaleFactor, anchor.first, this.textRenderer.getWidth(Text.translatable("gimbal.gui.examplehud")) ), 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}") super.render(matrices, mouseX, mouseY, delta) } }