diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/ObservableLateInitProperty.kt b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitProperty.kt similarity index 85% rename from src/main/kotlin/net/shadowfacts/cacao/util/ObservableLateInitProperty.kt rename to src/main/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitProperty.kt index d9246c4..a967e04 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/util/ObservableLateInitProperty.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitProperty.kt @@ -9,6 +9,9 @@ class ObservableLateInitProperty(val observer: (T) -> Unit) { lateinit var storage: T + val isInitialized: Boolean + get() = this::storage.isInitialized + operator fun getValue(thisRef: Any, property: KProperty<*>): T { return storage } diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt new file mode 100644 index 0000000..d532d8d --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt @@ -0,0 +1,21 @@ +package net.shadowfacts.cacao.util + +import kotlin.reflect.KProperty + +/** + * @author shadowfacts + */ +class ResettableLazyProperty(val initializer: () -> Value) { + var value: Value? = null + + operator fun getValue(thisRef: Any, property: KProperty<*>): Value { + if (value == null) { + value = initializer() + } + return value!! + } + + fun reset() { + value = null + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/cacao/view/NinePatchView.kt b/src/main/kotlin/net/shadowfacts/cacao/view/NinePatchView.kt index 71f125b..3bb02b4 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/view/NinePatchView.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/view/NinePatchView.kt @@ -4,6 +4,7 @@ import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.util.NinePatchTexture import net.shadowfacts.cacao.util.RenderHelper +import net.shadowfacts.cacao.util.ResettableLazyProperty /** * A helper class for drawing a [NinePatchTexture] in a view. @@ -15,37 +16,62 @@ import net.shadowfacts.cacao.util.RenderHelper class NinePatchView(val ninePatch: NinePatchTexture): View() { // Corners - private val topLeft: Rect by lazy { + protected val `$topLeft` = ResettableLazyProperty { Rect(0.0, 0.0, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble()) } - private val topRight by lazy { + protected val topLeft by `$topLeft` + + protected val `$topRight` = ResettableLazyProperty { Rect(bounds.width - ninePatch.cornerWidth, 0.0, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble()) } - private val bottomLeft by lazy { + protected val topRight by `$topRight` + + protected val `$bottomLeft` = ResettableLazyProperty { Rect(0.0, bounds.height - ninePatch.cornerHeight, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble()) } - private val bottomRight by lazy { + protected val bottomLeft by `$bottomLeft` + + protected val `$bottomRight` = ResettableLazyProperty { Rect(bounds.width - ninePatch.cornerWidth, bounds.height - ninePatch.cornerHeight, ninePatch.cornerWidth.toDouble(), ninePatch.cornerHeight.toDouble()) } + protected val bottomRight by `$bottomRight` + // Edges - private val topMiddle by lazy { + protected val `$topMiddle` = ResettableLazyProperty { Rect(ninePatch.cornerWidth.toDouble(), topLeft.top, bounds.width - 2 * ninePatch.cornerWidth, ninePatch.cornerHeight.toDouble()) } - private val bottomMiddle by lazy { + protected val topMiddle by `$topMiddle` + + protected val `$bottomMiddle` = ResettableLazyProperty { Rect(topMiddle.left, bottomLeft.top, topMiddle.width, topMiddle.height) } - private val leftMiddle by lazy { + protected val bottomMiddle by `$bottomMiddle` + + protected val `$leftMiddle` = ResettableLazyProperty { Rect(topLeft.left, ninePatch.cornerHeight.toDouble(), ninePatch.cornerWidth.toDouble(), bounds.height - 2 * ninePatch.cornerHeight) } - private val rightMiddle by lazy { + protected val leftMiddle by `$leftMiddle` + + protected val `$rightMiddle` = ResettableLazyProperty { Rect(topRight.left, leftMiddle.top, leftMiddle.width, leftMiddle.height) } + protected val rightMiddle by `$rightMiddle` + // Center - private val center by lazy { + protected val `$center` = ResettableLazyProperty { Rect(topLeft.right, topLeft.bottom, topMiddle.width, leftMiddle.height) } + protected val center by `$center` + + protected val delegates = listOf(`$topLeft`, `$topRight`, `$bottomLeft`, `$bottomRight`, `$topMiddle`, `$bottomMiddle`, `$leftMiddle`, `$rightMiddle`, `$center`) + + override fun didLayout() { + super.didLayout() + + delegates.forEach(ResettableLazyProperty::reset) + } override fun drawContent(mouse: Point, delta: Float) { drawCorners()