package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.config.core.config.Position
import at.hannibal2.skyhanni.data.GuiEditManager
import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsX
import at.hannibal2.skyhanni.data.GuiEditManager.Companion.getAbsY
import at.hannibal2.skyhanni.utils.renderables.Renderable
import io.github.moulberry.moulconfig.internal.TextRenderUtils
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.FontRenderer
import net.minecraft.client.gui.Gui
import net.minecraft.client.renderer.GlStateManager
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.renderer.vertex.DefaultVertexFormats
import net.minecraft.entity.Entity
import net.minecraft.inventory.Slot
import net.minecraft.util.AxisAlignedBB
import net.minecraft.util.MathHelper
import net.minecraft.util.ResourceLocation
import net.minecraftforge.client.event.RenderWorldLastEvent
import org.lwjgl.opengl.GL11
import java.awt.Color
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt
import kotlin.time.Duration
import kotlin.time.DurationUnit
object RenderUtils {
val beaconBeam = ResourceLocation("textures/entity/beacon_beam.png")
infix fun Slot.highlight(color: LorenzColor) {
highlight(color.toColor())
}
infix fun Slot.highlight(color: Color) {
val lightingState = GL11.glIsEnabled(GL11.GL_LIGHTING)
GlStateManager.disableLighting()
GlStateManager.color(1f, 1f, 1f, 1f)
GlStateManager.pushMatrix()
// TODO don't use z
GlStateManager.translate(0f, 0f, 110 + Minecraft.getMinecraft().renderItem.zLevel)
Gui.drawRect(
this.xDisplayPosition,
this.yDisplayPosition,
this.xDisplayPosition + 16,
this.yDisplayPosition + 16,
color.rgb
)
GlStateManager.popMatrix()
if (lightingState) GlStateManager.enableLighting()
}
fun RenderWorldLastEvent.drawColor(
location: LorenzVec,
color: LorenzColor,
beacon: Boolean = false,
alpha: Float = -1f,
) {
val (viewerX, viewerY, viewerZ) = getViewerPos(partialTicks)
val x = location.x - viewerX
val y = location.y - viewerY
val z = location.z - viewerZ
val distSq = x * x + y * y + z * z
val realAlpha = if (alpha == -1f) {
(0.1f + 0.005f * distSq.toFloat()).coerceAtLeast(0.2f)
} else {
alpha
}
GlStateManager.disableDepth()
GlStateManager.disableCull()
drawFilledBoundingBox(
AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1).expandBlock(),
color.toColor(),
realAlpha
)
GlStateManager.disableTexture2D()
if (distSq > 5 * 5 && beacon) renderBeaconBeam(x, y + 1, z, color.toColor().rgb, 1.0f, partialTicks)
GlStateManager.disableLighting()
GlStateManager.enableTexture2D()
GlStateManager.enableDepth()
GlStateManager.enableCull()
}
fun getViewerPos(partialTicks: Float) = exactLocation(Minecraft.getMinecraft().renderViewEntity, partialTicks)
/**
* Taken from NotEnoughUpdates under Creative Commons Attribution-NonCommercial 3.0
* https://github.com/Moulberry/NotEnoughUpdates/blob/master/LICENSE
* @author Moulberry
* @author Mojang
*/
fun drawFilledBoundingBox(
aabb: AxisAlignedBB,
c: Color,
alphaMultiplier: Float = 1f,
/**
* If set to `true`, renders the box relative to the camera instead of relative to the world.
* If set to `false`, will be relativized to [RenderUtils.getViewerPos]. Setting this to `false` requires
* specifying [partialTicks]]
*/
renderRelativeToCamera: Boolean = true,
drawVerticalBarriers: Boolean = true,
partialTicks: Float = 0F,
) {
GlStateManager.enableBlend()
GlStateManager.disableLighting()
GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0)
GlStateManager.disableTexture2D()
GlStateManager.disableCull()
val effectiveAABB = if (!renderRelativeToCamera) {
val vp = getViewerPos(partialTicks)
AxisAlignedBB(
aabb.minX - vp.x, aabb.minY - vp.y, aabb.minZ - vp.z,
aabb.maxX - vp.x, aabb.maxY - vp.y, aabb.maxZ - vp.z,
)
} else {
aabb
}
val tessellator = Tessellator.getInstance()
val worldRenderer = tessellator.worldRenderer
//vertical
if (drawVerticalBarriers) {
GlStateManager.color(c.red / 255f, c.green / 255f, c.blue / 255f, c.alpha / 255f * alphaMultiplier)
worldRenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION)
with(effectiveAABB)