Skip to content

Commit

Permalink
Pad to even size toggle, improve scaling, and fix dynamic color detec…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Wyvest committed Jun 23, 2024
1 parent f9807c7 commit 4db46aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()
}
}

0 comments on commit 4db46aa

Please sign in to comment.