2021-02-19 04:12:43 +00:00
|
|
|
package net.shadowfacts.cacao.view
|
|
|
|
|
|
|
|
import net.minecraft.client.util.math.MatrixStack
|
|
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
|
|
import net.shadowfacts.cacao.geometry.Rect
|
|
|
|
import net.shadowfacts.cacao.util.texture.NinePatchTexture
|
|
|
|
import net.shadowfacts.cacao.util.RenderHelper
|
|
|
|
import net.shadowfacts.cacao.util.properties.ResettableLazyProperty
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class for drawing a [NinePatchTexture] in a view.
|
|
|
|
* `NinePatchView` will draw the given nine patch texture filling its bounds.
|
|
|
|
*
|
|
|
|
* This class and the region properties are left open for internal framework use, overriding them is not recommended.
|
|
|
|
*
|
|
|
|
* @author shadowfacts
|
|
|
|
* @param ninePatch The nine patch texture that this view will draw.
|
|
|
|
*/
|
|
|
|
open class NinePatchView(val ninePatch: NinePatchTexture): View() {
|
|
|
|
|
|
|
|
// Corners
|
|
|
|
private val topLeftDelegate = ResettableLazyProperty {
|
|
|
|
Rect(0.0, 0.0, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble())
|
|
|
|
}
|
|
|
|
protected open val topLeft by topLeftDelegate
|
|
|
|
|
|
|
|
private val topRightDelegate = ResettableLazyProperty {
|
|
|
|
Rect(bounds.width - ninePatch.cornerWidth, 0.0, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble())
|
|
|
|
}
|
|
|
|
protected open val topRight by topRightDelegate
|
|
|
|
|
|
|
|
private val bottomLeftDelegate = ResettableLazyProperty {
|
|
|
|
Rect(0.0, bounds.height - ninePatch.cornerHeight, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble())
|
|
|
|
}
|
|
|
|
protected open val bottomLeft by bottomLeftDelegate
|
|
|
|
|
|
|
|
private val bottomRightDelegate = ResettableLazyProperty {
|
|
|
|
Rect(bounds.width - ninePatch.cornerWidth, bounds.height - ninePatch.cornerHeight, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble())
|
|
|
|
}
|
|
|
|
protected open val bottomRight by bottomRightDelegate
|
|
|
|
|
|
|
|
|
|
|
|
// Edges
|
|
|
|
private val topMiddleDelegate = ResettableLazyProperty {
|
|
|
|
Rect(ninePatch.cornerWidth.toDouble(), topLeft.top, bounds.width - 2 * ninePatch.cornerWidth, ninePatch.cornerHeight.toDouble())
|
|
|
|
}
|
|
|
|
protected open val topMiddle by topMiddleDelegate
|
|
|
|
|
|
|
|
private val bottomMiddleDelegate = ResettableLazyProperty {
|
|
|
|
Rect(topMiddle.left, bottomLeft.top, topMiddle.width, topMiddle.height)
|
|
|
|
}
|
|
|
|
protected open val bottomMiddle by bottomMiddleDelegate
|
|
|
|
|
|
|
|
private val leftMiddleDelegate = ResettableLazyProperty {
|
|
|
|
Rect(topLeft.left, ninePatch.cornerHeight.toDouble(), ninePatch.cornerWidth.toDouble(), bounds.height - 2 * ninePatch.cornerHeight)
|
|
|
|
}
|
|
|
|
protected open val leftMiddle by leftMiddleDelegate
|
|
|
|
|
|
|
|
private val rightMiddleDelegate = ResettableLazyProperty {
|
|
|
|
Rect(topRight.left, leftMiddle.top, leftMiddle.width, leftMiddle.height)
|
|
|
|
}
|
|
|
|
protected open val rightMiddle by rightMiddleDelegate
|
|
|
|
|
|
|
|
|
|
|
|
// Center
|
|
|
|
private val centerDelegate = ResettableLazyProperty {
|
|
|
|
Rect(topLeft.right, topLeft.bottom, topMiddle.width, leftMiddle.height)
|
|
|
|
}
|
|
|
|
protected open val center by centerDelegate
|
|
|
|
|
|
|
|
private val delegates = listOf(topLeftDelegate, topRightDelegate, bottomLeftDelegate, bottomRightDelegate, topMiddleDelegate, bottomMiddleDelegate, leftMiddleDelegate, rightMiddleDelegate, centerDelegate)
|
|
|
|
|
|
|
|
override fun didLayout() {
|
|
|
|
super.didLayout()
|
|
|
|
|
|
|
|
delegates.forEach(ResettableLazyProperty<Rect>::reset)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun drawContent(matrixStack: MatrixStack, mouse: Point, delta: Float) {
|
2021-02-27 18:24:17 +00:00
|
|
|
drawCorners(matrixStack)
|
|
|
|
drawEdges(matrixStack)
|
|
|
|
drawCenter(matrixStack)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 18:24:17 +00:00
|
|
|
private fun drawCorners(matrixStack: MatrixStack) {
|
|
|
|
RenderHelper.draw(matrixStack, topLeft, ninePatch.topLeft)
|
|
|
|
RenderHelper.draw(matrixStack, topRight, ninePatch.topRight)
|
|
|
|
RenderHelper.draw(matrixStack, bottomLeft, ninePatch.bottomLeft)
|
|
|
|
RenderHelper.draw(matrixStack, bottomRight, ninePatch.bottomRight)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 18:24:17 +00:00
|
|
|
private fun drawEdges(matrixStack: MatrixStack) {
|
2021-02-19 04:12:43 +00:00
|
|
|
// Horizontal
|
|
|
|
for (i in 0 until (topMiddle.width.toInt() / ninePatch.centerWidth)) {
|
2021-02-27 18:24:17 +00:00
|
|
|
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(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)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
val remWidth = topMiddle.width.toInt() % ninePatch.centerWidth
|
|
|
|
if (remWidth > 0) {
|
2021-02-27 18:24:17 +00:00
|
|
|
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(matrixStack, bottomMiddle.right - remWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical
|
|
|
|
for (i in 0 until (leftMiddle.height.toInt() / ninePatch.centerHeight)) {
|
2021-02-27 18:24:17 +00:00
|
|
|
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(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)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
val remHeight = leftMiddle.height.toInt() % ninePatch.centerHeight
|
|
|
|
if (remHeight > 0) {
|
2021-02-27 18:24:17 +00:00
|
|
|
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(matrixStack, rightMiddle.left, rightMiddle.bottom - remHeight, ninePatch.rightMiddle.u, ninePatch.rightMiddle.v, ninePatch.cornerWidth.toDouble(), remHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:24:17 +00:00
|
|
|
private fun drawCenter(matrixStack: MatrixStack) {
|
2021-02-19 04:12:43 +00:00
|
|
|
for (i in 0 until (center.height.toInt() / ninePatch.centerHeight)) {
|
2021-02-27 18:24:17 +00:00
|
|
|
drawCenterRow(matrixStack, center.top + i * ninePatch.centerHeight.toDouble(), ninePatch.centerHeight.toDouble())
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
val remHeight = center.height.toInt() % ninePatch.centerHeight
|
|
|
|
if (remHeight > 0) {
|
2021-02-27 18:24:17 +00:00
|
|
|
drawCenterRow(matrixStack, center.bottom - remHeight, remHeight.toDouble())
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:24:17 +00:00
|
|
|
private fun drawCenterRow(matrixStack: MatrixStack, y: Double, height: Double) {
|
2021-02-19 04:12:43 +00:00
|
|
|
for (i in 0 until (center.width.toInt() / ninePatch.centerWidth)) {
|
2021-02-27 18:24:17 +00:00
|
|
|
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)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
val remWidth = center.width.toInt() % ninePatch.centerWidth
|
|
|
|
if (remWidth > 0) {
|
2021-02-27 18:24:17 +00:00
|
|
|
RenderHelper.draw(matrixStack, center.right - remWidth, y, ninePatch.center.u, ninePatch.center.v, remWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height)
|
2021-02-19 04:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:24:17 +00:00
|
|
|
}
|