Add View intrinsic content size

This commit is contained in:
Shadowfacts 2019-06-22 10:21:29 -04:00
parent e28daf3b4a
commit 349c09f630
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 53 additions and 10 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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)
}
}

View File

@ -0,0 +1,3 @@
package net.shadowfacts.shadowui.geometry
data class Size(val width: Double, val height: Double)