Implement sizeToContents() for GridPanel

This commit is contained in:
DBotThePony 2025-03-21 07:18:42 +07:00
parent 10f250b822
commit 9b384f2213
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -7,6 +7,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.core.collect.filter
import ru.dbotthepony.mc.otm.core.collect.maybe
import kotlin.math.max
open class GridPanel<out S : Screen>(
screen: S,
@ -36,6 +37,37 @@ open class GridPanel<out S : Screen>(
invalidateLayout()
}
override fun sizeToContents() {
if (visibleChildren.isEmpty()) {
// nothing to size against
return
}
visibleChildren.forEach { it.sizeToContents() }
var width = 0f
var height = 0f
val children = visibleChildren.iterator().filter { it.dock == Dock.NONE }
for (row in 0 until rows) {
var maxHeight = 0f
var rowWidth = 0f
for (column in 0 until columns) {
val nextChild = children.maybe() ?: break
rowWidth += nextChild.width + nextChild.dockMargin.horizontal
maxHeight = max(maxHeight, nextChild.height + nextChild.dockMargin.vertical)
}
height += maxHeight
width = max(width, rowWidth)
}
this.width = width
this.height = height
}
override fun performLayout() {
super.performLayout()
var children = visibleChildren.iterator().filter { it.dock == Dock.NONE }