ASMR/src/main/kotlin/net/shadowfacts/shadowui/View.kt

104 lines
2.9 KiB
Kotlin
Raw Normal View History

2019-06-21 20:22:23 +00:00
package net.shadowfacts.shadowui
import com.mojang.blaze3d.platform.GlStateManager
import net.shadowfacts.kiwidsl.dsl
import net.shadowfacts.shadowui.geometry.Rect
2019-06-22 14:21:29 +00:00
import net.shadowfacts.shadowui.geometry.Size
2019-06-21 20:22:23 +00:00
import net.shadowfacts.shadowui.util.Color
import net.shadowfacts.shadowui.util.RenderHelper
2019-06-22 14:21:29 +00:00
import no.birkett.kiwi.Constraint
2019-06-21 20:22:23 +00:00
import no.birkett.kiwi.Solver
class View {
2019-06-21 20:22:23 +00:00
lateinit var solver: Solver
// in the coordinate system of the screen
val leftAnchor = LayoutVariable(this, "left")
val rightAnchor = LayoutVariable(this, "right")
val topAnchor = LayoutVariable(this, "top")
val bottomAnchor = LayoutVariable(this, "bottom")
val widthAnchor = LayoutVariable(this, "width")
val heightAnchor = LayoutVariable(this, "height")
val centerXAnchor = LayoutVariable(this, "centerX")
val centerYAnchor = LayoutVariable(this, "centerY")
// The rectangle for this view in the coordinate system of the parent view.
lateinit var frame: Rect
2019-06-21 20:22:23 +00:00
// The rectangle for this view in its own coordinate system.
lateinit var bounds: Rect
2019-06-21 20:22:23 +00:00
2019-06-22 14:21:29 +00:00
var intrinsicContentSize: Size? = null
set(value) {
updateIntrinsicContentSizeConstraints(intrinsicContentSize, value)
field = value
}
private var intrinsicContentSizeWidthConstraint: Constraint? = null
private var intrinsicContentSizeHeightConstraint: Constraint? = null
2019-06-21 20:22:23 +00:00
var backgroundColor = Color(0xff0000)
var parent: View? = null
val subviews = mutableListOf<View>()
2019-06-21 20:22:23 +00:00
fun addSubview(view: View): View {
2019-06-21 20:22:23 +00:00
subviews.add(view)
view.parent = this
view.solver = solver
view.wasAdded()
return view
2019-06-21 20:22:23 +00:00
}
fun wasAdded() {
createInternalConstraints()
}
fun createInternalConstraints() {
solver.dsl {
rightAnchor equalTo (leftAnchor + widthAnchor)
bottomAnchor equalTo (topAnchor + heightAnchor)
centerXAnchor equalTo (leftAnchor + widthAnchor / 2)
centerYAnchor equalTo (topAnchor + heightAnchor / 2)
}
}
2019-06-22 14:21:29 +00:00
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))
}
}
}
2019-06-21 20:22:23 +00:00
fun didLayout() {
subviews.forEach(View::didLayout)
2019-06-21 20:22:23 +00:00
val parentLeft = parent?.leftAnchor?.value ?: 0.0
val parentTop = parent?.topAnchor?.value ?: 0.0
frame = Rect(leftAnchor.value - parentLeft, topAnchor.value - parentTop, widthAnchor.value, heightAnchor.value)
bounds = Rect(0.0, 0.0, widthAnchor.value, heightAnchor.value)
2019-06-21 20:22:23 +00:00
}
fun draw() {
GlStateManager.pushMatrix()
GlStateManager.translated(frame.left, frame.top, 0.0)
RenderHelper.fill(bounds, backgroundColor)
drawContent()
subviews.forEach(View::draw)
2019-06-21 20:22:23 +00:00
GlStateManager.popMatrix()
}
fun drawContent() {}
}