Skip to content

Commit

Permalink
row and column
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Apr 26, 2024
1 parent 9b86cb0 commit beba891
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Column.kt
Original file line number Diff line number Diff line change
@@ -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<Widget>, 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<Widget> {
return children
}

}
38 changes: 38 additions & 0 deletions src/main/kotlin/com/neptuneclient/voidui/widgets/Row.kt
Original file line number Diff line number Diff line change
@@ -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<Widget>, 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<Widget> {
return children
}

}
3 changes: 2 additions & 1 deletion src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit beba891

Please sign in to comment.