diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt index 55a550420..9d17db40e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt @@ -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( screen: S, @@ -36,6 +37,37 @@ open class GridPanel( 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 }