net.peanuuutz.fork.ui.ui.node.LayoutInfo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fork-ui Show documentation
Show all versions of fork-ui Show documentation
Comprehensive API designed for Minecraft modders
The newest version!
/*
* Copyright 2020 The Android Open Source Project
* Modifications Copyright 2022 Peanuuutz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.peanuuutz.fork.ui.ui.node
import net.peanuuutz.fork.ui.ui.draw.shape.Rect
import net.peanuuutz.fork.ui.ui.draw.shape.toRect
import net.peanuuutz.fork.ui.ui.unit.FloatOffset
import net.peanuuutz.fork.ui.ui.unit.IntSize
import kotlin.math.max
import kotlin.math.min
sealed interface LayoutInfo : ParentDataContainer {
val isAttached: Boolean
val size: IntSize
val rootInfo: LayoutInfo
val parentInfo: LayoutInfo?
val childrenInfo: List
val outerInfo: LayoutInfo?
val innerInfo: LayoutInfo?
fun windowToLocal(relativeToWindow: FloatOffset): FloatOffset
fun localToWindow(relativeToLocal: FloatOffset): FloatOffset
fun localToRoot(relativeToLocal: FloatOffset): FloatOffset
fun localPositionOf(
other: LayoutInfo,
relativeToOther: FloatOffset = FloatOffset.Zero
): FloatOffset
fun localBoundsOf(
other: LayoutInfo,
clip: Boolean = true
): Rect
}
fun LayoutInfo.rootToLocal(relativeToRoot: FloatOffset): FloatOffset {
return localPositionOf(rootInfo, relativeToRoot)
}
fun LayoutInfo.positionInWindow(): FloatOffset {
return localToWindow(FloatOffset.Zero)
}
fun LayoutInfo.positionInRoot(): FloatOffset {
return localToRoot(FloatOffset.Zero)
}
fun LayoutInfo.positionInParent(): FloatOffset {
return parentInfo?.localPositionOf(this) ?: FloatOffset.Zero
}
fun LayoutInfo.boundsInWindow(clip: Boolean): Rect {
val rootInfo = rootInfo
val (leftX, topY, rightX, bottomY) = rootInfo.localBoundsOf(this, clip)
val topLeft = rootInfo.localToWindow(FloatOffset(leftX, topY))
val bottomLeft = rootInfo.localToWindow(FloatOffset(leftX, bottomY))
val bottomRight = rootInfo.localToWindow(FloatOffset(rightX, bottomY))
val topRight = rootInfo.localToWindow(FloatOffset(rightX, topY))
val windowLeftX = min(min(topLeft.x, bottomLeft.x), min(bottomRight.x, topRight.x))
val windowTopY = min(min(topLeft.y, bottomLeft.y), min(bottomRight.y, topRight.y))
val windowRightX = max(max(topLeft.x, bottomLeft.x), max(bottomRight.x, topRight.x))
val windowBottomY = max(max(topLeft.y, bottomLeft.y), max(bottomRight.y, topRight.y))
return Rect(windowLeftX, windowTopY, windowRightX, windowBottomY)
}
fun LayoutInfo.boundsInRoot(clip: Boolean): Rect {
return rootInfo.localBoundsOf(this, clip)
}
fun LayoutInfo.boundsInParent(clip: Boolean): Rect {
return parentInfo?.localBoundsOf(this, clip) ?: size.toRect()
}