PhysicalConnectivity/src/main/kotlin/net/shadowfacts/cacao/util/LayoutGuide.kt

34 lines
1.3 KiB
Kotlin

package net.shadowfacts.cacao.util
import net.shadowfacts.cacao.LayoutVariable
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.view.View
/**
* A layout guide is a non-view object that represents a rectangular area for the purposes of constraint-based layout.
* It allows you to define complex layouts without needing empty container views.
*
* A layout guide is always owned by a view. The owning view's dimensions are not necessarily tied to the layout guide's.
*
* To create a layout guide, call [View.addLayoutGuide] on the owning view.
*
* @author shadowfacts
*/
class LayoutGuide(
val owningView: View,
) {
val leftAnchor: LayoutVariable = LayoutVariable(this, "left")
val rightAnchor: LayoutVariable = LayoutVariable(this, "right")
val topAnchor: LayoutVariable = LayoutVariable(this, "top")
val bottomAnchor: LayoutVariable = LayoutVariable(this, "bottom")
val widthAnchor: LayoutVariable = LayoutVariable(this, "width")
val heightAnchor: LayoutVariable = LayoutVariable(this, "height")
val centerXAnchor: LayoutVariable = LayoutVariable(this, "centerX")
val centerYAnchor: LayoutVariable = LayoutVariable(this, "centerY")
val frame: Rect
get() = Rect(leftAnchor.value - owningView.leftAnchor.value, topAnchor.value - owningView.topAnchor.value, widthAnchor.value, heightAnchor.value)
}