From 4db46aae75fd2b23e28be871581a4a45594e2641 Mon Sep 17 00:00:00 2001 From: ev chang Date: Mon, 24 Jun 2024 02:48:52 +0700 Subject: [PATCH] Pad to even size toggle, improve scaling, and fix dynamic color detection --- .../crosshair/config/CrosshairEntry.kt | 3 ++ .../crosshair/render/CrosshairRenderer.kt | 49 ++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/polyfrost/crosshair/config/CrosshairEntry.kt b/src/main/kotlin/org/polyfrost/crosshair/config/CrosshairEntry.kt index 8fcc1ff..3ccc9f2 100644 --- a/src/main/kotlin/org/polyfrost/crosshair/config/CrosshairEntry.kt +++ b/src/main/kotlin/org/polyfrost/crosshair/config/CrosshairEntry.kt @@ -2,12 +2,15 @@ package org.polyfrost.crosshair.config import cc.polyfrost.oneconfig.config.annotations.Button import cc.polyfrost.oneconfig.config.annotations.Slider +import cc.polyfrost.oneconfig.config.annotations.Switch import cc.polyfrost.oneconfig.config.core.ConfigUtils import cc.polyfrost.oneconfig.utils.dsl.runAsync import org.polyfrost.crosshair.utils.Utils class CrosshairEntry( var img: String = "iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAGUlEQVR42mNgGH7gPxAMd83/iQCjATYkAQAXJEO9Ljp2dQAAAABJRU5ErkJggg\u003d\u003d", + @Switch(name = "Pad to Even Size", description = "By default, PolyCrosshair will pad the image when rendering to ensure the size is even. However, sometimes this can cause issues with certain images. If you're experiencing issues, try disabling this.", size = 2) + var padToEvenSize: Boolean = true, @Slider(name = "Scale %", min = 0f, max = 200f) var scale: Int = 100, @Slider(name = "Rotation", min = -180f, max = 180f) diff --git a/src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt b/src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt index 3416907..dfa6737 100644 --- a/src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt +++ b/src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt @@ -8,20 +8,23 @@ import cc.polyfrost.oneconfig.utils.dsl.mc import net.minecraft.client.gui.Gui import net.minecraft.client.renderer.EntityRenderer import net.minecraft.client.renderer.GlStateManager +import net.minecraft.client.renderer.Tessellator import net.minecraft.client.renderer.texture.DynamicTexture +import net.minecraft.client.renderer.vertex.DefaultVertexFormats import net.minecraft.entity.monster.IMob import net.minecraft.entity.passive.EntityAmbientCreature import net.minecraft.entity.passive.EntityAnimal import net.minecraft.entity.passive.EntityVillager import net.minecraft.entity.passive.EntityWaterMob import net.minecraft.entity.player.EntityPlayer +import net.minecraft.util.MovingObjectPosition import net.minecraftforge.client.event.RenderGameOverlayEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.lwjgl.opengl.GL11 import org.polyfrost.crosshair.config.ModConfig import org.polyfrost.crosshair.mixin.GuiIngameAccessor -import java.awt.Color import java.awt.image.BufferedImage +import kotlin.math.roundToInt object CrosshairRenderer { private var texture = DynamicTexture(15, 15) @@ -30,7 +33,7 @@ object CrosshairRenderer { private var whiteTextureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture) fun updateTexture(image: OneImage) { - val paddedImage = if (ModConfig.canvaSize % 2 == 1) { + val paddedImage = if (ModConfig.canvaSize % 2 == 1 && ModConfig.newCurrentCrosshair.padToEvenSize) { addPixel(image.image) } else { image.image @@ -80,15 +83,18 @@ object CrosshairRenderer { GlStateManager.scale(mcScale, mcScale, 1f) GlStateManager.translate((UResolution.scaledWidth / 2).toDouble(), (UResolution.scaledHeight / 2).toDouble(), 0.0) GlStateManager.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f) - GlStateManager.scale(crosshair.scale / 100f, crosshair.scale / 100f, 1f) - val padded = ModConfig.canvaSize % 2 == 1 + val padded = ModConfig.canvaSize % 2 == 1 && crosshair.padToEvenSize val size = ModConfig.canvaSize + if (padded) 1 else 0 - Gui.drawModalRectWithCustomSizedTexture(0 - 7 - (ModConfig.canvaSize - 15) / 2, 0 - 7 - (ModConfig.canvaSize - 15) / 2, 0f, 0f, size, size, size.toFloat(), size.toFloat()) + val x = 0 - 7 - (((ModConfig.canvaSize / 2f).roundToInt() - 8) / 2f) + val y = 0 - 7 - (((ModConfig.canvaSize / 2f).roundToInt() - 8) / 2f) + val scale = crosshair.scale / 100f + val scaledSize = size.toFloat() * scale + drawScaledCustomSizeModalRect(x + size * (1 - scale) / 2, y + size * (1 - scale) / 2, 0f, 0f, size, size, scaledSize, scaledSize, size.toFloat(), size.toFloat()) val c = getColor() if (c.rgb != -1) { mc.textureManager.bindTexture(whiteTextureLocation) GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f) - Gui.drawModalRectWithCustomSizedTexture(0 - 7 - (ModConfig.canvaSize - 15) / 2, 0 - 7 - (ModConfig.canvaSize - 15) / 2, 0f, 0f, size, size, size.toFloat(), size.toFloat()) + drawScaledCustomSizeModalRect(x + size * (1 - scale) / 2, y + size * (1 - scale) / 2, 0f, 0f, size, size, scaledSize, scaledSize, size.toFloat(), size.toFloat()) } if (renderConfig.invertColor) { GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0) @@ -102,7 +108,9 @@ object CrosshairRenderer { private fun getColor(): OneColor { with(ModConfig.renderConfig) { - val entity = mc.pointedEntity ?: return WHITE + if (mc.objectMouseOver == null) return WHITE + if (mc.objectMouseOver.typeOfHit != MovingObjectPosition.MovingObjectType.ENTITY) return WHITE + val entity = mc.objectMouseOver.entityHit ?: return WHITE if (entity.isInvisible) return WHITE if (dynamicColor) { if (hostile && entity is IMob) return hostileColor @@ -129,4 +137,31 @@ object CrosshairRenderer { g2d.dispose() return resizedImage } + + fun drawScaledCustomSizeModalRect( + x: Float, + y: Float, + u: Float, + v: Float, + uWidth: Int, + vHeight: Int, + width: Float, + height: Float, + tileWidth: Float, + tileHeight: Float + ) { + val f = 1.0f / tileWidth + val g = 1.0f / tileHeight + val tessellator = Tessellator.getInstance() + val worldRenderer = tessellator.worldRenderer + worldRenderer.begin(7, DefaultVertexFormats.POSITION_TEX) + worldRenderer.pos(x.toDouble(), (y + height).toDouble(), 0.0) + .tex((u * f).toDouble(), ((v + vHeight.toFloat()) * g).toDouble()).endVertex() + worldRenderer.pos((x + width).toDouble(), (y + height).toDouble(), 0.0) + .tex(((u + uWidth.toFloat()) * f).toDouble(), ((v + vHeight.toFloat()) * g).toDouble()).endVertex() + worldRenderer.pos((x + width).toDouble(), y.toDouble(), 0.0) + .tex(((u + uWidth.toFloat()) * f).toDouble(), (v * g).toDouble()).endVertex() + worldRenderer.pos(x.toDouble(), y.toDouble(), 0.0).tex((u * f).toDouble(), (v * g).toDouble()).endVertex() + tessellator.draw() + } } \ No newline at end of file