136 lines
6.6 KiB
Kotlin
136 lines
6.6 KiB
Kotlin
|
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) {
|
||
|
drawCorners()
|
||
|
drawEdges()
|
||
|
drawCenter()
|
||
|
}
|
||
|
|
||
|
private fun drawCorners() {
|
||
|
RenderHelper.draw(topLeft, ninePatch.topLeft)
|
||
|
RenderHelper.draw(topRight, ninePatch.topRight)
|
||
|
RenderHelper.draw(bottomLeft, ninePatch.bottomLeft)
|
||
|
RenderHelper.draw(bottomRight, ninePatch.bottomRight)
|
||
|
}
|
||
|
|
||
|
private fun drawEdges() {
|
||
|
// Horizontal
|
||
|
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(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
|
||
|
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(bottomMiddle.right - remWidth, bottomMiddle.top, ninePatch.bottomMiddle.u, ninePatch.bottomMiddle.v, remWidth.toDouble(), ninePatch.cornerHeight.toDouble(), ninePatch.texture.width, ninePatch.texture.height)
|
||
|
}
|
||
|
|
||
|
// Vertical
|
||
|
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(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
|
||
|
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(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() {
|
||
|
for (i in 0 until (center.height.toInt() / ninePatch.centerHeight)) {
|
||
|
drawCenterRow(center.top + i * ninePatch.centerHeight.toDouble(), ninePatch.centerHeight.toDouble())
|
||
|
}
|
||
|
val remHeight = center.height.toInt() % ninePatch.centerHeight
|
||
|
if (remHeight > 0) {
|
||
|
drawCenterRow(center.bottom - remHeight, remHeight.toDouble())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun drawCenterRow(y: Double, height: Double) {
|
||
|
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)
|
||
|
}
|
||
|
val remWidth = center.width.toInt() % ninePatch.centerWidth
|
||
|
if (remWidth > 0) {
|
||
|
RenderHelper.draw(center.right - remWidth, y, ninePatch.center.u, ninePatch.center.v, remWidth.toDouble(), height, ninePatch.texture.width, ninePatch.texture.height)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|