From 6485fd1035e6969739e7455f6943ff5e290c4f3c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 23 Jun 2019 18:23:59 -0400 Subject: [PATCH] Add Texture and TextureView --- .../net/shadowfacts/asmr/TestCacaoScreen.kt | 6 ++++-- .../net/shadowfacts/cacao/CacaoScreen.kt | 2 ++ .../shadowfacts/cacao/util/RenderHelper.kt | 19 +++++++++++++++++++ .../net/shadowfacts/cacao/util/Texture.kt | 15 +++++++++++++++ .../net/shadowfacts/cacao/view/TextureView.kt | 19 +++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/net/shadowfacts/cacao/util/Texture.kt create mode 100644 src/main/kotlin/net/shadowfacts/cacao/view/TextureView.kt diff --git a/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt b/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt index 6c5e6d1..61ae588 100644 --- a/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/asmr/TestCacaoScreen.kt @@ -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) diff --git a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt index 6669a41..9d56402 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/CacaoScreen.kt @@ -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) diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt b/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt index e56dd76..17cb67d 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/util/RenderHelper.kt @@ -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) + } + } \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/Texture.kt b/src/main/kotlin/net/shadowfacts/cacao/util/Texture.kt new file mode 100644 index 0000000..b1fd37e --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/util/Texture.kt @@ -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) diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/TextureView.kt b/src/main/kotlin/net/shadowfacts/cacao/view/TextureView.kt new file mode 100644 index 0000000..21f114c --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/view/TextureView.kt @@ -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) + } + +} \ No newline at end of file