diff --git a/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt b/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt index f234109..b658f66 100644 --- a/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt +++ b/src/main/kotlin/net/shadowfacts/asmr/TestScreen.kt @@ -4,6 +4,7 @@ import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.shadowui.Screen import net.shadowfacts.shadowui.View import net.shadowfacts.shadowui.Window +import net.shadowfacts.shadowui.geometry.Size import net.shadowfacts.shadowui.util.Color class TestScreen: Screen() { @@ -22,6 +23,7 @@ class TestScreen: Screen() { val purple = green.addSubview(View().apply { backgroundColor = Color(0x800080) }) + purple.intrinsicContentSize = Size(width = 150.0, height = 150.0) solver.dsl { red.leftAnchor equalTo 0 @@ -39,8 +41,8 @@ class TestScreen: Screen() { blue.topAnchor equalTo (green.topAnchor + green.heightAnchor) blue.heightAnchor equalTo 50 - purple.widthAnchor equalTo 100 - purple.heightAnchor equalTo 100 +// purple.widthAnchor equalTo 100 +// purple.heightAnchor equalTo 100 purple.centerXAnchor equalTo green.centerXAnchor purple.centerYAnchor equalTo green.centerYAnchor } diff --git a/src/main/kotlin/net/shadowfacts/shadowui/View.kt b/src/main/kotlin/net/shadowfacts/shadowui/View.kt index 944dfbb..a09ec40 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/View.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/View.kt @@ -3,8 +3,10 @@ package net.shadowfacts.shadowui import com.mojang.blaze3d.platform.GlStateManager import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.shadowui.geometry.Rect +import net.shadowfacts.shadowui.geometry.Size import net.shadowfacts.shadowui.util.Color import net.shadowfacts.shadowui.util.RenderHelper +import no.birkett.kiwi.Constraint import no.birkett.kiwi.Solver class View { @@ -26,6 +28,14 @@ class View { // The rectangle for this view in its own coordinate system. lateinit var bounds: Rect + var intrinsicContentSize: Size? = null + set(value) { + updateIntrinsicContentSizeConstraints(intrinsicContentSize, value) + field = value + } + private var intrinsicContentSizeWidthConstraint: Constraint? = null + private var intrinsicContentSizeHeightConstraint: Constraint? = null + var backgroundColor = Color(0xff0000) var parent: View? = null @@ -54,6 +64,19 @@ class View { } } + private fun updateIntrinsicContentSizeConstraints(old: Size?, new: Size?) { + if (old != null) { + solver.removeConstraint(intrinsicContentSizeWidthConstraint!!) + solver.removeConstraint(intrinsicContentSizeHeightConstraint!!) + } + if (new != null) { + solver.dsl { + this@View.intrinsicContentSizeWidthConstraint = (widthAnchor.equalTo(new.width, strength = WEAK)) + this@View.intrinsicContentSizeHeightConstraint = (heightAnchor.equalTo(new.height, strength = WEAK)) + } + } + } + fun didLayout() { subviews.forEach(View::didLayout) diff --git a/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt index 92c7e1e..3b37e14 100644 --- a/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt +++ b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Rect.kt @@ -2,14 +2,29 @@ package net.shadowfacts.shadowui.geometry data class Rect(val left: Double, val top: Double, val width: Double, val height: Double) { - val right: Double - get() = left + width - val bottom: Double - get() = top + height + val right: Double by lazy { + left + width + } + val bottom: Double by lazy { + top + height + } - val midX: Double - get() = left + width / 2 - val midY: Double - get() = top + height / 2 + val midX: Double by lazy { + left + width / 2 + } + val midY: Double by lazy { + top + height / 2 + } + + val origin: Point by lazy { + Point(left, top) + } + val center: Point by lazy { + Point(midX, midY) + } + + val size: Size by lazy { + Size(width, height) + } } \ No newline at end of file diff --git a/src/main/kotlin/net/shadowfacts/shadowui/geometry/Size.kt b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Size.kt new file mode 100644 index 0000000..b8aa8e9 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/shadowui/geometry/Size.kt @@ -0,0 +1,3 @@ +package net.shadowfacts.shadowui.geometry + +data class Size(val width: Double, val height: Double) \ No newline at end of file