Skip to content

Commit

Permalink
wyvest moment
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jun 25, 2024
1 parent 4db46aa commit 9e7fca9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 99 deletions.
17 changes: 9 additions & 8 deletions src/main/kotlin/org/polyfrost/crosshair/config/CrosshairEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ 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,
var scale = 100

@Slider(name = "Rotation", min = -180f, max = 180f)
var rotation: Int = 0,
var rotation = 0

@Slider(name = "X Offset", min = -1920f, max = 1920f)
var offsetX: Int = 0,
var offsetX = 0

@Slider(name = "Y Offset", min = -1080f, max = 1080f)
var offsetY: Int = 0
) {
var offsetY = 0

@Button(name = "Transform", text = "Reset", size = 2)
var transformReset = Runnable {
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/org/polyfrost/crosshair/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ import java.util.stream.Collectors

object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD, "/${PolyCrosshair.MODID}.svg"), "${PolyCrosshair.MODID}/config.json") {

@Exclude
var mode = false

@Exclude
var drawer = HashMap<Int, Int>()

var currentCrosshair = ""

@DualOption(
name = "Mode",
left = "Vanilla",
right = "Custom",
size = 2
)
var mode = false

@CustomOption
var crosshairs = ArrayList<String>()

Expand Down Expand Up @@ -59,13 +64,15 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD, "/${PolyCrosshair
newCrosshairs.addAll(crosshairs.stream().map { CrosshairEntry(it) }.collect(Collectors.toList()))
crosshairs.clear()
}
val options = listOf("hostile", "passive", "player", "hostileColor", "passiveColor", "playerColor", "dynamicOpacity")
var options = listOf("hostile", "passive", "player", "hostileColor", "passiveColor", "playerColor", "dynamicOpacity")
for (i in options) {
hideIf(i) { !renderConfig.dynamicColor }
}
addDependency(options[3], options[0])
addDependency(options[4], options[1])
addDependency(options[5], options[2])
options = listOf("mirror", "canvaSize")
options.forEach { hideIf(it) { !mode } }
addListener("canvaSize") {
for (i in drawer) {
val pos = Utils.indexToPos(i.key)
Expand Down Expand Up @@ -109,6 +116,7 @@ object ModConfig : Config(Mod(PolyCrosshair.NAME, ModType.HUD, "/${PolyCrosshair
mod: Mod,
migrate: Boolean,
): BasicOption? {
Drawer.addHideCondition { !mode }
ConfigUtils.getSubCategory(page, "General", "").options.add(Drawer)
return null
}
Expand Down
119 changes: 32 additions & 87 deletions src/main/kotlin/org/polyfrost/crosshair/render/CrosshairRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ import cc.polyfrost.oneconfig.libs.universal.UResolution
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.GlStateManager as GL
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.image.BufferedImage
import kotlin.math.roundToInt

object CrosshairRenderer {
private var texture = DynamicTexture(15, 15)
Expand All @@ -33,19 +28,15 @@ object CrosshairRenderer {
private var whiteTextureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", whiteTexture)

fun updateTexture(image: OneImage) {
val paddedImage = if (ModConfig.canvaSize % 2 == 1 && ModConfig.newCurrentCrosshair.padToEvenSize) {
addPixel(image.image)
} else {
image.image
}
texture = DynamicTexture(paddedImage)
val bufferedImage = image.image
texture = DynamicTexture(bufferedImage)
textureLocation = mc.textureManager.getDynamicTextureLocation("polycrosshair", texture)
whiteTexture = DynamicTexture(paddedImage.width, paddedImage.height)
for (posY in 0..<paddedImage.height) {
for (posX in 0..<paddedImage.width) {
val color = paddedImage.getRGB(posX, posY)
whiteTexture = DynamicTexture(bufferedImage.width, bufferedImage.height)
for (posY in 0..<bufferedImage.height) {
for (posX in 0..<bufferedImage.width) {
val color = bufferedImage.getRGB(posX, posY)
if (color shr 24 == 0) continue
whiteTexture.textureData[posX + posY * paddedImage.width] = -1
whiteTexture.textureData[posX + posY * bufferedImage.width] = -1
}
}
whiteTexture.updateDynamicTexture()
Expand All @@ -55,7 +46,7 @@ object CrosshairRenderer {
@SubscribeEvent
fun cancel(event: RenderGameOverlayEvent.Pre) {
if (event.type != RenderGameOverlayEvent.ElementType.CROSSHAIRS || !ModConfig.enabled) return
GlStateManager.enableAlpha()
GL.enableAlpha()
event.isCanceled = true
}

Expand All @@ -64,53 +55,50 @@ object CrosshairRenderer {
if ((mc.ingameGUI as? GuiIngameAccessor)?.shouldShowCrosshair() == false) return

entityRenderer.setupOverlayRendering()
GlStateManager.pushMatrix()
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0)
GlStateManager.enableBlend()
GL.pushMatrix()
GL.tryBlendFuncSeparate(770, 771, 1, 0)
GL.enableBlend()
val renderConfig = ModConfig.renderConfig
if (renderConfig.invertColor) {
GlStateManager.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
GL.tryBlendFuncSeparate(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR, 1, 0)
}
GlStateManager.enableAlpha()
GL.enableAlpha()

GL11.glColor4f(1f, 1f, 1f, 1f)

mc.textureManager.bindTexture(textureLocation)
(if (ModConfig.mode) textureLocation else Gui.icons).let { mc.textureManager.bindTexture(it) }
val mcScale = UResolution.scaleFactor.toFloat()
GlStateManager.scale(1 / mcScale, 1 / mcScale, 1f)
GL.scale(1 / mcScale, 1 / mcScale, 1f)
val crosshair = ModConfig.newCurrentCrosshair
GlStateManager.translate(crosshair.offsetX.toFloat(), crosshair.offsetY.toFloat(), 0f)
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)
val padded = ModConfig.canvaSize % 2 == 1 && crosshair.padToEvenSize
val size = ModConfig.canvaSize + if (padded) 1 else 0
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())
GL.translate(crosshair.offsetX / mcScale, crosshair.offsetY / mcScale, 0f)
GL.scale(mcScale, mcScale, 1f)
GL.translate((UResolution.scaledWidth / 2).toDouble(), (UResolution.scaledHeight / 2).toDouble(), 0.0)
GL.rotate(crosshair.rotation.toFloat(), 0f, 0f, 1f)
val size = if (ModConfig.mode) ModConfig.canvaSize else 15
val textureSize = if (ModConfig.mode) size.toFloat() else 256f
val scale = (crosshair.scale + crosshair.scale % 2) / 100f
GL.scale(scale, scale, 1f)
GL.translate(-size / 2f, -size / 2f, 0f)
Gui.drawModalRectWithCustomSizedTexture(0, 0, 0f, 0f, size, size, textureSize, textureSize)
val c = getColor()
if (c.rgb != -1) {
mc.textureManager.bindTexture(whiteTextureLocation)
if (ModConfig.mode) mc.textureManager.bindTexture(whiteTextureLocation)
GL11.glColor4f(c.red / 255f, c.green / 255f, c.blue / 255f, renderConfig.dynamicOpacity / 100f)
drawScaledCustomSizeModalRect(x + size * (1 - scale) / 2, y + size * (1 - scale) / 2, 0f, 0f, size, size, scaledSize, scaledSize, size.toFloat(), size.toFloat())
Gui.drawModalRectWithCustomSizedTexture(0, 0, 0f, 0f, size, size, textureSize, textureSize)
}
if (renderConfig.invertColor) {
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
GL.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
}
GL11.glColor4f(1f, 1f, 1f, 1f)
GlStateManager.disableBlend()
GlStateManager.popMatrix()
GL.disableBlend()
GL.popMatrix()
}

val WHITE = OneColor(-1)

private fun getColor(): OneColor {
with(ModConfig.renderConfig) {
if (mc.objectMouseOver == null) return WHITE
if (mc.objectMouseOver.typeOfHit != MovingObjectPosition.MovingObjectType.ENTITY) return WHITE
val entity = mc.objectMouseOver.entityHit ?: 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 @@ -121,47 +109,4 @@ object CrosshairRenderer {
return WHITE
}

private fun addPixel(original: BufferedImage): BufferedImage {
// Create a new BufferedImage with 1 pixel added to width and height
val newWidth = original.width + 1
val newHeight = original.height + 1
val resizedImage = BufferedImage(newWidth, newHeight, original.type)

// Get the Graphics2D object to draw on the new BufferedImage
val g2d = resizedImage.createGraphics()

// Draw the original image onto the new image
g2d.drawImage(original, 0, 0, null)

// Dispose of the Graphics2D object to release resources
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 9e7fca9

Please sign in to comment.