From 9b384f22133c5d01e3e4a7ba78fa58fafe2bd09d Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 07:18:42 +0700 Subject: [PATCH 1/3] Implement sizeToContents() for GridPanel --- .../client/screen/panels/util/GridPanel.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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 } From a48aaf52ae4fcd3e398a2410770007ee67e23a95 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 07:19:03 +0700 Subject: [PATCH 2/3] Add DockProperty#all variant --- .../dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index bd1493163..eb2c0aa02 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -82,6 +82,10 @@ data class DockProperty(val left: Float = 0f, val top: Float = 0f, val right: Fl companion object { val EMPTY = DockProperty() + + fun all(value: Float): DockProperty { + return DockProperty(value, value, value, value) + } } } From e6c970865258d18fd575ebf6b38081bfc8e56113 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 08:07:43 +0700 Subject: [PATCH 3/3] Add layout modes to grid panel --- .../client/screen/panels/util/GridPanel.kt | 82 ++++++++++++++++--- 1 file changed, 70 insertions(+), 12 deletions(-) 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 9d17db40e..4e7dd5800 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 @@ -21,22 +21,80 @@ open class GridPanel( ) : EditablePanel(screen, parent, x, y, width, height) { var columns: Int = columns set(value) { - field = value - invalidateLayout() + if (field != value) { + field = value + invalidateLayout() + } } var rows: Int = rows set(value) { - field = value - invalidateLayout() + if (field != value) { + field = value + invalidateLayout() + } } var gravity: RenderGravity = RenderGravity.CENTER_CENTER set(value) { - field = value - invalidateLayout() + if (field != value) { + field = value + invalidateLayout() + } } + var layout: Layout = Layout.TOP_LEFT + set(value) { + if (field != value) { + field = value + invalidateLayout() + } + } + + enum class Layout { + TOP_LEFT { + override fun rows(last: Int): IntIterator { + return (0 until last).iterator() + } + + override fun columns(last: Int): IntIterator { + return (0 until last).iterator() + } + }, + + TOP_RIGHT { + override fun rows(last: Int): IntIterator { + return (0 until last).iterator() + } + + override fun columns(last: Int): IntIterator { + return (last - 1 downTo 1).iterator() + } + }, + BOTTOM_LEFT { + override fun rows(last: Int): IntIterator { + return (last - 1 downTo 1).iterator() + } + + override fun columns(last: Int): IntIterator { + return (0 until last).iterator() + } + }, + + BOTTOM_RIGHT { + override fun rows(last: Int): IntIterator { + return (last - 1 downTo 1).iterator() + } + + override fun columns(last: Int): IntIterator { + return (last - 1 downTo 1).iterator() + } + }; + + abstract fun rows(last: Int): IntIterator + abstract fun columns(last: Int): IntIterator + } + override fun sizeToContents() { if (visibleChildren.isEmpty()) { // nothing to size against @@ -50,11 +108,11 @@ open class GridPanel( val children = visibleChildren.iterator().filter { it.dock == Dock.NONE } - for (row in 0 until rows) { + for (row in layout.rows(rows)) { var maxHeight = 0f var rowWidth = 0f - for (column in 0 until columns) { + for (column in layout.columns(columns)) { val nextChild = children.maybe() ?: break rowWidth += nextChild.width + nextChild.dockMargin.horizontal maxHeight = max(maxHeight, nextChild.height + nextChild.dockMargin.vertical) @@ -75,11 +133,11 @@ open class GridPanel( var totalWidth = 0f var totalHeight = 0f - for (row in 0 until rows) { + for (row in layout.rows(rows)) { var maxHeight = 0f var width = 0f - for (column in 0 until columns) { + for (column in layout.columns(columns)) { val child = children.maybe() ?: break width += child.dockedWidth maxHeight = maxHeight.coerceAtLeast(child.dockedHeight) @@ -96,11 +154,11 @@ open class GridPanel( totalWidth = 0f totalHeight = 0f - for (row in 0 until rows) { + for (row in layout.rows(rows)) { var maxHeight = 0f var width = 0f - for (column in 0 until columns) { + for (column in layout.columns(columns)) { val child = children.maybe() ?: break child.x = alignX + width child.y = alignY + totalHeight