Add Texture and TextureView

This commit is contained in:
Shadowfacts 2019-06-23 18:23:59 -04:00
parent 62fbc10aa3
commit 6485fd1035
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 59 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package net.shadowfacts.asmr
import net.minecraft.util.Identifier
import net.shadowfacts.kiwidsl.dsl
import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.cacao.view.View
@ -7,8 +8,10 @@ import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.Color
import net.shadowfacts.cacao.util.Texture
import net.shadowfacts.cacao.view.Label
import net.shadowfacts.cacao.view.StackView
import net.shadowfacts.cacao.view.TextureView
import net.shadowfacts.cacao.view.button.Button
/**
@ -21,9 +24,8 @@ class TestCacaoScreen: CacaoScreen() {
val stack = addView(StackView(Axis.VERTICAL, StackView.Distribution.CENTER).apply {
backgroundColor = Color.WHITE
})
val red = stack.addArrangedSubview(View().apply {
val red = stack.addArrangedSubview(TextureView(Texture(Identifier("textures/block/birch_log_top.png"), 0, 0, 16, 16)).apply {
intrinsicContentSize = Size(50.0, 50.0)
backgroundColor = Color(0xff0000)
})
val green = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(75.0, 100.0)

View File

@ -35,6 +35,8 @@ open class CacaoScreen: Screen(TextComponent("CacaoScreen")) {
}
override fun render(mouseX: Int, mouseY: Int, delta: Float) {
renderBackground()
val mouse = Point(mouseX, mouseY)
windows.forEach {
it.draw(mouse, delta)

View File

@ -1,7 +1,9 @@
package net.shadowfacts.cacao.util
import com.mojang.blaze3d.platform.GlStateManager
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawableHelper
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
/**
@ -22,6 +24,18 @@ object RenderHelper {
DrawableHelper.fill(rect.left.toInt(), rect.top.toInt(), rect.right.toInt(), rect.bottom.toInt(), color.argb)
}
/**
* Draws the given [texture] filling the [rect].
*/
fun draw(rect: Rect, texture: Texture) {
if (disabled) return
color(1f, 1f, 1f, 1f)
MinecraftClient.getInstance().textureManager.bindTexture(texture.location)
val u = texture.u / texture.width.toFloat()
val v = texture.v / texture.height.toFloat()
DrawableHelper.blit(rect.left.toInt(), rect.top.toInt(), u, v, rect.width.toInt(), rect.height.toInt(), texture.width, texture.height)
}
/**
* @see org.lwjgl.opengl.GL11.glPushMatrix
*/
@ -46,4 +60,9 @@ object RenderHelper {
GlStateManager.translated(x, y, z)
}
fun color(r: Float, g: Float, b: Float, alpha: Float) {
if (disabled) return
GlStateManager.color4f(r, g, b, alpha)
}
}

View File

@ -0,0 +1,15 @@
package net.shadowfacts.cacao.util
import net.minecraft.util.Identifier
/**
* A helper class that represents a texture.
*
* @author shadowfacts
* @param location The identifier representing the resource-pack location of the texture image.
* @param u The X coordinate in pixels of where the texture starts within the image.
* @param v The Y coordinate in pixels of where the texture starts within the image.
* @param width The width in pixels of the entire image.
* @param height The height in pixels of the entire image.
*/
data class Texture(val location: Identifier, val u: Int, val v: Int, val width: Int = 256, val height: Int = 256)

View File

@ -0,0 +1,19 @@
package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.util.RenderHelper
import net.shadowfacts.cacao.util.Texture
/**
* A helper class for drawing a [Texture] in a view.
* `TextureView` will draw the given texture filling the bounds of the view.
*
* @author shadowfacts
*/
class TextureView(val texture: Texture): View() {
override fun drawContent(mouse: Point, delta: Float) {
RenderHelper.draw(bounds, texture)
}
}