2019-06-22 19:02:17 +00:00
|
|
|
package net.shadowfacts.cacao.util
|
2019-06-21 20:22:23 +00:00
|
|
|
|
2019-06-23 15:41:32 +00:00
|
|
|
import com.mojang.blaze3d.platform.GlStateManager
|
2019-06-23 22:23:59 +00:00
|
|
|
import net.minecraft.client.MinecraftClient
|
2019-06-21 20:22:23 +00:00
|
|
|
import net.minecraft.client.gui.DrawableHelper
|
2019-06-26 01:52:17 +00:00
|
|
|
import net.minecraft.client.render.Tessellator
|
|
|
|
import net.minecraft.client.render.VertexFormats
|
2019-06-24 01:22:54 +00:00
|
|
|
import net.minecraft.client.sound.PositionedSoundInstance
|
|
|
|
import net.minecraft.sound.SoundEvent
|
2019-06-22 19:02:17 +00:00
|
|
|
import net.shadowfacts.cacao.geometry.Rect
|
2019-06-26 01:52:17 +00:00
|
|
|
import org.lwjgl.opengl.GL11
|
2019-06-21 20:22:23 +00:00
|
|
|
|
2019-06-22 14:59:18 +00:00
|
|
|
/**
|
2019-06-22 20:08:00 +00:00
|
|
|
* Helper methods for rendering using Minecraft's utilities from Cacao views.
|
2019-06-23 15:41:32 +00:00
|
|
|
* For unit testing, all drawing and OpenGL interaction can be disabled by setting the `cacao.drawing.disabled` JVM property to `true`.
|
2019-06-22 20:08:00 +00:00
|
|
|
*
|
2019-06-22 14:59:18 +00:00
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
2019-06-21 20:22:23 +00:00
|
|
|
object RenderHelper {
|
|
|
|
|
2019-06-24 01:55:42 +00:00
|
|
|
val disabled = (System.getProperty("cacao.drawing.disabled") ?: "false").toBoolean()
|
2019-06-23 15:41:32 +00:00
|
|
|
|
2019-06-24 01:22:54 +00:00
|
|
|
// TODO: find a better place for this
|
|
|
|
fun playSound(event: SoundEvent) {
|
|
|
|
if (disabled) return
|
|
|
|
MinecraftClient.getInstance().soundManager.play(PositionedSoundInstance.master(event, 1f))
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:08:00 +00:00
|
|
|
/**
|
|
|
|
* Draws a solid [rect] filled with the given [color].
|
|
|
|
*/
|
2019-06-21 23:02:08 +00:00
|
|
|
fun fill(rect: Rect, color: Color) {
|
2019-06-23 15:41:32 +00:00
|
|
|
if (disabled) return
|
2019-06-21 20:22:23 +00:00
|
|
|
DrawableHelper.fill(rect.left.toInt(), rect.top.toInt(), rect.right.toInt(), rect.bottom.toInt(), color.argb)
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:23:59 +00:00
|
|
|
/**
|
2019-06-23 23:58:06 +00:00
|
|
|
* Binds and draws the given [texture] filling the [rect].
|
2019-06-23 22:23:59 +00:00
|
|
|
*/
|
|
|
|
fun draw(rect: Rect, texture: Texture) {
|
|
|
|
if (disabled) return
|
|
|
|
color(1f, 1f, 1f, 1f)
|
|
|
|
MinecraftClient.getInstance().textureManager.bindTexture(texture.location)
|
2019-06-23 23:58:06 +00:00
|
|
|
draw(rect.left, rect.top, texture.u, texture.v, rect.width, rect.height, texture.width, texture.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the bound texture with the given screen and texture position and size.
|
|
|
|
*/
|
|
|
|
fun draw(x: Double, y: Double, u: Int, v: Int, width: Double, height: Double, textureWidth: Int, textureHeight: Int) {
|
|
|
|
if (disabled) return
|
2019-06-26 01:52:17 +00:00
|
|
|
val uStart = u.toDouble() / textureWidth
|
|
|
|
val uEnd = (u + width) / textureWidth
|
|
|
|
val vStart = v.toDouble() / textureHeight
|
|
|
|
val vEnd = (v + height) / textureHeight
|
|
|
|
innerBlit(x, x + width, y, y + height, 0.0, uStart, uEnd, vStart, vEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copied from net.minecraft.client.gui.DrawableHelper
|
|
|
|
private fun innerBlit(xStart: Double, xEnd: Double, yStart: Double, yEnd: Double, z: Double, uStart: Double, uEnd: Double, vStart: Double, vEnd: Double) {
|
|
|
|
val tessellator = Tessellator.getInstance()
|
|
|
|
val buffer = tessellator.bufferBuilder
|
|
|
|
buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_UV)
|
|
|
|
buffer.vertex(xStart, yEnd, z).texture(uStart, vEnd).next()
|
|
|
|
buffer.vertex(xEnd, yEnd, z).texture(uEnd, vEnd).next()
|
|
|
|
buffer.vertex(xEnd, yStart, z).texture(uEnd, vStart).next()
|
|
|
|
buffer.vertex(xStart, yStart, z).texture(uStart, vStart).next()
|
|
|
|
tessellator.draw()
|
2019-06-23 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 15:41:32 +00:00
|
|
|
/**
|
|
|
|
* @see org.lwjgl.opengl.GL11.glPushMatrix
|
|
|
|
*/
|
|
|
|
fun pushMatrix() {
|
|
|
|
if (disabled) return
|
|
|
|
GlStateManager.pushMatrix()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see org.lwjgl.opengl.GL11.glPopMatrix
|
|
|
|
*/
|
|
|
|
fun popMatrix() {
|
|
|
|
if (disabled) return
|
|
|
|
GlStateManager.popMatrix()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see org.lwjgl.opengl.GL11.glTranslated
|
|
|
|
*/
|
|
|
|
fun translate(x: Double, y: Double, z: Double = 0.0) {
|
|
|
|
if (disabled) return
|
|
|
|
GlStateManager.translated(x, y, z)
|
|
|
|
}
|
|
|
|
|
2019-06-24 01:22:54 +00:00
|
|
|
/**
|
|
|
|
* @see org.lwjgl.opengl.GL11.glColor4f
|
|
|
|
*/
|
2019-06-23 22:23:59 +00:00
|
|
|
fun color(r: Float, g: Float, b: Float, alpha: Float) {
|
|
|
|
if (disabled) return
|
|
|
|
GlStateManager.color4f(r, g, b, alpha)
|
|
|
|
}
|
|
|
|
|
2019-06-21 20:22:23 +00:00
|
|
|
}
|