Fix grid panel layouts, introduce column major order

This commit is contained in:
DBotThePony 2025-03-21 18:17:50 +07:00
parent 7c028b1fa6
commit a2d9f43a2e
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -7,6 +7,8 @@ import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel 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.filter
import ru.dbotthepony.mc.otm.core.collect.maybe import ru.dbotthepony.mc.otm.core.collect.maybe
import java.util.stream.IntStream
import java.util.stream.Stream
import kotlin.math.max import kotlin.math.max
open class GridPanel<out S : Screen>( open class GridPanel<out S : Screen>(
@ -51,6 +53,14 @@ open class GridPanel<out S : Screen>(
} }
} }
var columnMajorOrder = false
set(value) {
if (field != value) {
field = value
invalidateLayout()
}
}
enum class Layout { enum class Layout {
TOP_LEFT { TOP_LEFT {
override fun get( override fun get(
@ -58,10 +68,15 @@ open class GridPanel<out S : Screen>(
row: Int, row: Int,
column: Int, column: Int,
rows: Int, rows: Int,
columns: Int columns: Int,
columnMajorOrder: Boolean
): EditablePanel<*>? { ): EditablePanel<*>? {
if (columnMajorOrder) {
return list.getOrNull(row + column * rows)
} else {
return list.getOrNull(column + row * columns) return list.getOrNull(column + row * columns)
} }
}
}, },
TOP_RIGHT { TOP_RIGHT {
@ -70,10 +85,15 @@ open class GridPanel<out S : Screen>(
row: Int, row: Int,
column: Int, column: Int,
rows: Int, rows: Int,
columns: Int columns: Int,
columnMajorOrder: Boolean
): EditablePanel<*>? { ): EditablePanel<*>? {
if (columnMajorOrder) {
return list.getOrNull(row + (columns - column - 1) * rows)
} else {
return list.getOrNull((columns - column - 1) + row * columns) return list.getOrNull((columns - column - 1) + row * columns)
} }
}
}, },
BOTTOM_LEFT { BOTTOM_LEFT {
@ -82,10 +102,15 @@ open class GridPanel<out S : Screen>(
row: Int, row: Int,
column: Int, column: Int,
rows: Int, rows: Int,
columns: Int columns: Int,
columnMajorOrder: Boolean
): EditablePanel<*>? { ): EditablePanel<*>? {
if (columnMajorOrder) {
return list.getOrNull((rows - row - 1) + column * rows)
} else {
return list.getOrNull(column + (rows - row - 1) * columns) return list.getOrNull(column + (rows - row - 1) * columns)
} }
}
}, },
BOTTOM_RIGHT { BOTTOM_RIGHT {
@ -94,13 +119,18 @@ open class GridPanel<out S : Screen>(
row: Int, row: Int,
column: Int, column: Int,
rows: Int, rows: Int,
columns: Int columns: Int,
columnMajorOrder: Boolean
): EditablePanel<*>? { ): EditablePanel<*>? {
if (columnMajorOrder) {
return list.getOrNull((rows - row - 1) + (columns - column - 1) * rows)
} else {
return list.getOrNull((columns - column - 1) + (rows - row - 1) * columns) return list.getOrNull((columns - column - 1) + (rows - row - 1) * columns)
} }
}
}; };
abstract fun get(list: List<EditablePanel<*>>, row: Int, column: Int, rows: Int, columns: Int): EditablePanel<*>? abstract fun get(list: List<EditablePanel<*>>, row: Int, column: Int, rows: Int, columns: Int, columnMajorOrder: Boolean): EditablePanel<*>?
} }
override fun sizeToContents() { override fun sizeToContents() {
@ -114,14 +144,14 @@ open class GridPanel<out S : Screen>(
var width = 0f var width = 0f
var height = 0f var height = 0f
val children = visibleChildren.iterator().filter { it.dock == Dock.NONE } val children = visibleChildren.filter { it.dock == Dock.NONE }
for (row in 0 until rows) { for (row in 0 until rows) {
var maxHeight = 0f var maxHeight = 0f
var rowWidth = 0f var rowWidth = 0f
for (column in 0 until columns) { for (column in 0 until columns) {
val nextChild = children.maybe() ?: break val nextChild = layout.get(children, row, column, rows, columns, columnMajorOrder) ?: continue
rowWidth += nextChild.width + nextChild.dockMargin.horizontal rowWidth += nextChild.width + nextChild.dockMargin.horizontal
maxHeight = max(maxHeight, nextChild.height + nextChild.dockMargin.vertical) maxHeight = max(maxHeight, nextChild.height + nextChild.dockMargin.vertical)
} }
@ -136,7 +166,7 @@ open class GridPanel<out S : Screen>(
override fun performLayout() { override fun performLayout() {
super.performLayout() super.performLayout()
val children = visibleChildren.stream().filter { it.dock == Dock.NONE }.toList() val children = visibleChildren.filter { it.dock == Dock.NONE }
var totalWidth = 0f var totalWidth = 0f
var totalHeight = 0f var totalHeight = 0f
@ -146,7 +176,7 @@ open class GridPanel<out S : Screen>(
var width = 0f var width = 0f
for (column in 0 until columns) { for (column in 0 until columns) {
val child = layout.get(children, row, column, rows, columns) ?: continue val child = layout.get(children, row, column, rows, columns, columnMajorOrder) ?: continue
width += child.dockedWidth width += child.dockedWidth
maxHeight = maxHeight.coerceAtLeast(child.dockedHeight) maxHeight = maxHeight.coerceAtLeast(child.dockedHeight)
} }
@ -166,7 +196,7 @@ open class GridPanel<out S : Screen>(
var width = 0f var width = 0f
for (column in 0 until columns) { for (column in 0 until columns) {
val child = layout.get(children, row, column, rows, columns) ?: continue val child = layout.get(children, row, column, rows, columns, columnMajorOrder) ?: continue
child.x = alignX + width child.x = alignX + width
child.y = alignY + totalHeight child.y = alignY + totalHeight
width += child.dockedWidth width += child.dockedWidth