From beba8917dd08bc38b648e269814a7e7fc54b27f0 Mon Sep 17 00:00:00 2001 From: "marc." Date: Sat, 27 Apr 2024 00:00:43 +0200 Subject: [PATCH] row and column --- .../neptuneclient/voidui/widgets/Column.kt | 38 +++++++++++++++++++ .../com/neptuneclient/voidui/widgets/Row.kt | 38 +++++++++++++++++++ .../neptuneclient/voidui/tests/ScreenTest.kt | 3 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/neptuneclient/voidui/widgets/Column.kt create mode 100644 src/main/kotlin/com/neptuneclient/voidui/widgets/Row.kt diff --git a/src/main/kotlin/com/neptuneclient/voidui/widgets/Column.kt b/src/main/kotlin/com/neptuneclient/voidui/widgets/Column.kt new file mode 100644 index 0000000..857482b --- /dev/null +++ b/src/main/kotlin/com/neptuneclient/voidui/widgets/Column.kt @@ -0,0 +1,38 @@ +package com.neptuneclient.voidui.widgets + +import com.neptuneclient.voidui.framework.* +import kotlin.math.max + +/** + * A group widget which lets you place widgets in a cloumn. + * + * @param children The widgets in the row. + * @param gap The gap between the widgets. + */ +class Column(private val children: Array, private val gap: Float = 0f) : GroupWidget() { + + override fun layout(constraints: BoxConstraints) { + var w = 0f + var h = gap * (children.size - 1) + children.forEach { + it.layout(constraints) + w = max(h, it.size.width) + h += it.size.height + } + size = constraints.constrain(Size(w, h)) + } + + override fun postLayoutInit(parentOffset: Offset, parent: Widget) { + var y = parentOffset.y + children.forEach { + it.postLayoutInit(Offset(parentOffset.x, y), this) + y += it.size.height + gap + } + offset = parentOffset + } + + override fun buildGroup(): Array { + return children + } + +} diff --git a/src/main/kotlin/com/neptuneclient/voidui/widgets/Row.kt b/src/main/kotlin/com/neptuneclient/voidui/widgets/Row.kt new file mode 100644 index 0000000..774e509 --- /dev/null +++ b/src/main/kotlin/com/neptuneclient/voidui/widgets/Row.kt @@ -0,0 +1,38 @@ +package com.neptuneclient.voidui.widgets + +import com.neptuneclient.voidui.framework.* +import kotlin.math.max + +/** + * A group widget which lets you place widgets in a row. + * + * @param children The widgets in the row. + * @param gap The gap between the widgets. + */ +class Row(private val children: Array, private val gap: Float = 0f) : GroupWidget() { + + override fun layout(constraints: BoxConstraints) { + var w = gap * (children.size - 1) + var h = 0f + children.forEach { + it.layout(constraints) + w += it.size.width + h = max(h, it.size.height) + } + size = constraints.constrain(Size(w, h)) + } + + override fun postLayoutInit(parentOffset: Offset, parent: Widget) { + var x = parentOffset.x + children.forEach { + it.postLayoutInit(Offset(x, parentOffset.y), this) + x += it.size.width + gap + } + offset = parentOffset + } + + override fun buildGroup(): Array { + return children + } + +} \ No newline at end of file diff --git a/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt b/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt index 0addd04..4b3fcbb 100644 --- a/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt +++ b/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt @@ -24,7 +24,8 @@ class TestScreen(voidUI: VoidUI) : Screen(voidUI) { border = Border(2f, Color.RED), height = 600f, - child = Stack( + child = Column( + gap = 20f, children = arrayOf( Image( src = image("images/hampter.png"),