Cacao: Misc changes

This commit is contained in:
Shadowfacts 2021-02-27 13:24:17 -05:00
parent c1ecdacc06
commit d20ba7460e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
7 changed files with 68 additions and 49 deletions

View File

@ -8,6 +8,7 @@ import net.minecraft.client.render.*
import net.minecraft.client.sound.PositionedSoundInstance import net.minecraft.client.sound.PositionedSoundInstance
import net.minecraft.client.util.math.MatrixStack import net.minecraft.client.util.math.MatrixStack
import net.minecraft.sound.SoundEvent import net.minecraft.sound.SoundEvent
import net.minecraft.util.math.Matrix4f
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.texture.Texture import net.shadowfacts.cacao.util.texture.Texture
@ -40,11 +41,11 @@ object RenderHelper {
/** /**
* Binds and draws the given [texture] filling the [rect]. * Binds and draws the given [texture] filling the [rect].
*/ */
fun draw(rect: Rect, texture: Texture) { fun draw(matrixStack: MatrixStack, rect: Rect, texture: Texture) {
if (disabled) return if (disabled) return
color(1f, 1f, 1f, 1f) color(1f, 1f, 1f, 1f)
MinecraftClient.getInstance().textureManager.bindTexture(texture.location) MinecraftClient.getInstance().textureManager.bindTexture(texture.location)
draw(rect.left, rect.top, texture.u, texture.v, rect.width, rect.height, texture.width, texture.height) draw(matrixStack, rect.left, rect.top, texture.u, texture.v, rect.width, rect.height, texture.width, texture.height)
} }
fun drawLine(start: Point, end: Point, z: Double, width: Float, color: Color) { fun drawLine(start: Point, end: Point, z: Double, width: Float, color: Color) {
@ -62,27 +63,29 @@ object RenderHelper {
/** /**
* Draws the bound texture with the given screen and texture position and size. * 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) { fun draw(matrixStack: MatrixStack, x: Double, y: Double, u: Int, v: Int, width: Double, height: Double, textureWidth: Int, textureHeight: Int) {
if (disabled) return if (disabled) return
val uStart = u.toFloat() / textureWidth val uStart = u.toFloat() / textureWidth
val uEnd = (u + width).toFloat() / textureWidth val uEnd = (u + width).toFloat() / textureWidth
val vStart = v.toFloat() / textureHeight val vStart = v.toFloat() / textureHeight
val vEnd = (v + height).toFloat() / textureHeight val vEnd = (v + height).toFloat() / textureHeight
drawTexturedQuad(x, x + width, y, y + height, 0.0, uStart, uEnd, vStart, vEnd) drawTexturedQuad(matrixStack.peek().model, x, x + width, y, y + height, 0.0, uStart, uEnd, vStart, vEnd)
} }
// Copied from net.minecraft.client.gui.DrawableHelper // Copied from net.minecraft.client.gui.DrawableHelper
private fun drawTexturedQuad(xStart: Double, xEnd: Double, yStart: Double, yEnd: Double, z: Double, uStart: Float, uEnd: Float, vStart: Float, vEnd: Float) { private fun drawTexturedQuad(matrix: Matrix4f, x0: Double, x1: Double, y0: Double, y1: Double, z: Double, u0: Float, u1: Float, v0: Float, v1: Float) {
val tessellator = Tessellator.getInstance() val bufferBuilder = Tessellator.getInstance().buffer
val buffer = tessellator.buffer bufferBuilder.begin(GL11.GL_QUADS, VertexFormats.POSITION_TEXTURE)
buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_TEXTURE) bufferBuilder.vertex(matrix, x0.toFloat(), y1.toFloat(), z.toFloat()).texture(u0, v1).next()
buffer.vertex(xStart, yEnd, z).texture(uStart, vEnd).next() bufferBuilder.vertex(matrix, x1.toFloat(), y1.toFloat(), z.toFloat()).texture(u1, v1).next()
buffer.vertex(xEnd, yEnd, z).texture(uEnd, vEnd).next() bufferBuilder.vertex(matrix, x1.toFloat(), y0.toFloat(), z.toFloat()).texture(u1, v0).next()
buffer.vertex(xEnd, yStart, z).texture(uEnd, vStart).next() bufferBuilder.vertex(matrix, x0.toFloat(), y0.toFloat(), z.toFloat()).texture(u0, v0).next()
buffer.vertex(xStart, yStart, z).texture(uStart, vStart).next() bufferBuilder.end()
tessellator.draw() RenderSystem.enableAlphaTest()
BufferRenderer.draw(bufferBuilder)
} }
/** /**
* @see org.lwjgl.opengl.GL11.glPushMatrix * @see org.lwjgl.opengl.GL11.glPushMatrix
*/ */
@ -127,4 +130,4 @@ object RenderHelper {
return color(color.red, color.green, color.blue, color.alpha) return color(color.red, color.green, color.blue, color.alpha)
} }
} }

View File

@ -3,6 +3,7 @@ package net.shadowfacts.cacao.view
import net.minecraft.client.MinecraftClient import net.minecraft.client.MinecraftClient
import net.minecraft.client.font.TextRenderer import net.minecraft.client.font.TextRenderer
import net.minecraft.client.util.math.MatrixStack import net.minecraft.client.util.math.MatrixStack
import net.minecraft.text.LiteralText
import net.minecraft.text.OrderedText import net.minecraft.text.OrderedText
import net.minecraft.text.Text import net.minecraft.text.Text
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
@ -44,6 +45,14 @@ class Label(
LEFT, CENTER, RIGHT LEFT, CENTER, RIGHT
} }
constructor(
text: String,
shadow: Boolean = true,
maxLines: Int = 0,
wrappingMode: WrappingMode = WrappingMode.WRAP,
textAlignment: TextAlignment = TextAlignment.LEFT,
): this(LiteralText(text), shadow, maxLines, wrappingMode, textAlignment)
/** /**
* The text of this label. Mutating this field will update the intrinsic content size and trigger a layout. * The text of this label. Mutating this field will update the intrinsic content size and trigger a layout.
*/ */

View File

@ -77,60 +77,60 @@ open class NinePatchView(val ninePatch: NinePatchTexture): View() {
} }
override fun drawContent(matrixStack: MatrixStack, mouse: Point, delta: Float) { override fun drawContent(matrixStack: MatrixStack, mouse: Point, delta: Float) {
drawCorners() drawCorners(matrixStack)
drawEdges() drawEdges(matrixStack)
drawCenter() drawCenter(matrixStack)
} }
private fun drawCorners() { private fun drawCorners(matrixStack: MatrixStack) {
RenderHelper.draw(topLeft, ninePatch.topLeft) RenderHelper.draw(matrixStack, topLeft, ninePatch.topLeft)
RenderHelper.draw(topRight, ninePatch.topRight) RenderHelper.draw(matrixStack, topRight, ninePatch.topRight)
RenderHelper.draw(bottomLeft, ninePatch.bottomLeft) RenderHelper.draw(matrixStack, bottomLeft, ninePatch.bottomLeft)
RenderHelper.draw(bottomRight, ninePatch.bottomRight) RenderHelper.draw(matrixStack, bottomRight, ninePatch.bottomRight)
} }
private fun drawEdges() { private fun drawEdges(matrixStack: MatrixStack) {
// Horizontal // Horizontal
for (i in 0 until (topMiddle.width.toInt() / ninePatch.centerWidth)) { for (i in 0 until (topMiddle.width.toInt() / ninePatch.centerWidth)) {
RenderHelper.draw(topMiddle.left + i * ninePatch.centerWidth, topMiddle.top, ninePatch.topMiddle.u, ninePatch.topMiddle.v, ninePatch.centerWidth.toDouble(), topMiddle.height, ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, topMiddle.left + i * ninePatch.centerWidth, topMiddle.top, ninePatch.topMiddle.u, ninePatch.topMiddle.v, ninePatch.centerWidth.toDouble(), topMiddle.height, ninePatch.texture.width, ninePatch.texture.height)
RenderHelper.draw(bottomMiddle.left + i * ninePatch.centerWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, ninePatch.centerWidth.toDouble(), bottomMiddle.height, ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, bottomMiddle.left + i * ninePatch.centerWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, ninePatch.centerWidth.toDouble(), bottomMiddle.height, ninePatch.texture.width, ninePatch.texture.height)
} }
val remWidth = topMiddle.width.toInt() % ninePatch.centerWidth val remWidth = topMiddle.width.toInt() % ninePatch.centerWidth
if (remWidth > 0) { if (remWidth > 0) {
RenderHelper.draw(topMiddle.right - remWidth, topMiddle.top, ninePatch.topMiddle.u, ninePatch.topMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, topMiddle.right - remWidth, topMiddle.top, ninePatch.topMiddle.u, ninePatch.topMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
RenderHelper.draw(bottomMiddle.right - remWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, bottomMiddle.right - remWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
} }
// Vertical // Vertical
for (i in 0 until (leftMiddle.height.toInt() / ninePatch.centerHeight)) { for (i in 0 until (leftMiddle.height.toInt() / ninePatch.centerHeight)) {
RenderHelper.draw(leftMiddle.left, leftMiddle.top + i * ninePatch.centerHeight, ninePatch.leftMiddle.u, ninePatch.leftMiddle.v, ninePatch.cornerWidth.toDouble(), ninePatch.centerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, leftMiddle.left, leftMiddle.top + i * ninePatch.centerHeight, ninePatch.leftMiddle.u, ninePatch.leftMiddle.v, ninePatch.cornerWidth.toDouble(), ninePatch.centerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
RenderHelper.draw(rightMiddle.left, rightMiddle.top + i * ninePatch.centerHeight, ninePatch.rightMiddle.u, ninePatch.rightMiddle.v, ninePatch.cornerWidth.toDouble(), ninePatch.centerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, rightMiddle.left, rightMiddle.top + i * ninePatch.centerHeight, ninePatch.rightMiddle.u, ninePatch.rightMiddle.v, ninePatch.cornerWidth.toDouble(), ninePatch.centerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
} }
val remHeight = leftMiddle.height.toInt() % ninePatch.centerHeight val remHeight = leftMiddle.height.toInt() % ninePatch.centerHeight
if (remHeight > 0) { if (remHeight > 0) {
RenderHelper.draw(leftMiddle.left, leftMiddle.bottom - remHeight, ninePatch.leftMiddle.u, ninePatch.leftMiddle.v, ninePatch.cornerWidth.toDouble(), remHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, leftMiddle.left, leftMiddle.bottom - remHeight, ninePatch.leftMiddle.u, ninePatch.leftMiddle.v, ninePatch.cornerWidth.toDouble(), remHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
RenderHelper.draw(rightMiddle.left, rightMiddle.bottom - remHeight, ninePatch.rightMiddle.u, ninePatch.rightMiddle.v, ninePatch.cornerWidth.toDouble(), remHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, rightMiddle.left, rightMiddle.bottom - remHeight, ninePatch.rightMiddle.u, ninePatch.rightMiddle.v, ninePatch.cornerWidth.toDouble(), remHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
} }
} }
private fun drawCenter() { private fun drawCenter(matrixStack: MatrixStack) {
for (i in 0 until (center.height.toInt() / ninePatch.centerHeight)) { for (i in 0 until (center.height.toInt() / ninePatch.centerHeight)) {
drawCenterRow(center.top + i * ninePatch.centerHeight.toDouble(), ninePatch.centerHeight.toDouble()) drawCenterRow(matrixStack, center.top + i * ninePatch.centerHeight.toDouble(), ninePatch.centerHeight.toDouble())
} }
val remHeight = center.height.toInt() % ninePatch.centerHeight val remHeight = center.height.toInt() % ninePatch.centerHeight
if (remHeight > 0) { if (remHeight > 0) {
drawCenterRow(center.bottom - remHeight, remHeight.toDouble()) drawCenterRow(matrixStack, center.bottom - remHeight, remHeight.toDouble())
} }
} }
private fun drawCenterRow(y: Double, height: Double) { private fun drawCenterRow(matrixStack: MatrixStack, y: Double, height: Double) {
for (i in 0 until (center.width.toInt() / ninePatch.centerWidth)) { for (i in 0 until (center.width.toInt() / ninePatch.centerWidth)) {
RenderHelper.draw(center.left + i * ninePatch.centerWidth, y, ninePatch.center.u, ninePatch.center.v, ninePatch.centerWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, center.left + i * ninePatch.centerWidth, y, ninePatch.center.u, ninePatch.center.v, ninePatch.centerWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height)
} }
val remWidth = center.width.toInt() % ninePatch.centerWidth val remWidth = center.width.toInt() % ninePatch.centerWidth
if (remWidth > 0) { if (remWidth > 0) {
RenderHelper.draw(center.right - remWidth, y, ninePatch.center.u, ninePatch.center.v, remWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height) RenderHelper.draw(matrixStack, center.right - remWidth, y, ninePatch.center.u, ninePatch.center.v, remWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height)
} }
} }
} }

View File

@ -14,7 +14,7 @@ import net.shadowfacts.cacao.util.texture.Texture
class TextureView(var texture: Texture): View() { class TextureView(var texture: Texture): View() {
override fun drawContent(matrixStack: MatrixStack, mouse: Point, delta: Float) { override fun drawContent(matrixStack: MatrixStack, mouse: Point, delta: Float) {
RenderHelper.draw(bounds, texture) RenderHelper.draw(matrixStack, bounds, texture)
} }
} }

View File

@ -262,8 +262,8 @@ open class View() {
} }
if (new != null) { if (new != null) {
solver.dsl { solver.dsl {
this@View.intrinsicContentSizeWidthConstraint = (widthAnchor.equalTo(new.width, strength = WEAK)) this@View.intrinsicContentSizeWidthConstraint = (widthAnchor.equalTo(new.width, strength = MEDIUM))
this@View.intrinsicContentSizeHeightConstraint = (heightAnchor.equalTo(new.height, strength = WEAK)) this@View.intrinsicContentSizeHeightConstraint = (heightAnchor.equalTo(new.height, strength = MEDIUM))
} }
} }
} }
@ -388,4 +388,4 @@ open class View() {
return Rect(convert(rect.origin, to), rect.size) return Rect(convert(rect.origin, to), rect.size)
} }
} }

View File

@ -60,10 +60,13 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
override fun wasAdded() { override fun wasAdded() {
solver.dsl { solver.dsl {
addSubview(content) addSubview(content)
content.leftAnchor equalTo (leftAnchor + padding) content.centerXAnchor equalTo centerXAnchor
content.rightAnchor equalTo (rightAnchor - padding) content.centerYAnchor equalTo centerYAnchor
content.topAnchor equalTo (topAnchor + padding)
content.bottomAnchor equalTo (bottomAnchor - padding) content.leftAnchor.lessThanOrEqualTo((leftAnchor + padding), WEAK)
content.rightAnchor.greaterThanOrEqualTo(rightAnchor - padding, WEAK)
content.topAnchor.lessThanOrEqualTo(topAnchor + padding, WEAK)
content.bottomAnchor.greaterThanOrEqualTo(bottomAnchor - padding, WEAK)
listOfNotNull(background, hoveredBackground, disabledBackground).forEach { listOfNotNull(background, hoveredBackground, disabledBackground).forEach {
addSubview(it) addSubview(it)
@ -114,4 +117,4 @@ abstract class AbstractButton<Impl: AbstractButton<Impl>>(val content: View, val
return true return true
} }
} }

View File

@ -9,4 +9,8 @@ import net.shadowfacts.cacao.view.View
* @param content The [View] that provides the content of this button. * @param content The [View] that provides the content of this button.
* @param padding The padding between the [content] and the edges of the button. * @param padding The padding between the [content] and the edges of the button.
*/ */
class Button(content: View, padding: Double = 4.0): AbstractButton<Button>(content, padding) class Button(content: View, padding: Double = 4.0): AbstractButton<Button>(content, padding) {
constructor(content: View, padding: Double = 4.0, handler: (Button) -> Unit): this(content, padding) {
this.handler = handler
}
}