Skip to content

Commit

Permalink
Update UIContainerLayouts.kt so graphics{} work better (#2166)
Browse files Browse the repository at this point in the history
* Update UIContainerLayouts.kt so graphics{} work better

* fix for horizontal stack + tests
  • Loading branch information
rafi0 committed Mar 26, 2024
1 parent 9e95cf7 commit 387fa55
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions korge/src/korlibs/korge/ui/UIContainerLayouts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ open class UIVerticalStack(
forEachChild {
it.y = y
if (adjustSize && forcedWidth != null) it.scaledWidth = width
y += it.height + padding
val bounds = it.getLocalBounds()
y += bounds.bottom + padding
bb += it.getBounds(this@UIVerticalStack)
}
unscaledSize = Size(if (forcedWidth == null) bb.xmax else forcedWidth!!, y)
Expand Down Expand Up @@ -206,7 +207,8 @@ open class UIHorizontalStack(
forEachChild {
it.x = x
if (adjustSize && forcedHeight != null) it.scaledHeight = height
x += it.width + padding
val bounds = it.getLocalBounds()
x += bounds.right + padding
bb += it.getBounds(this@UIHorizontalStack)
}
//println("forcedHeight=$forcedHeight!")
Expand Down
21 changes: 21 additions & 0 deletions korge/test/korlibs/korge/ui/UIContainerLayoutsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package korlibs.korge.ui
import korlibs.image.color.*
import korlibs.korge.view.*
import korlibs.math.geom.*
import korlibs.number.*
import kotlin.test.*

class UIContainerLayoutsTest {
Expand Down Expand Up @@ -111,4 +112,24 @@ class UIContainerLayoutsTest {
).joinToString("\n")
)
}

@Test
fun testUIVerticalStack() {
val stack = UIVerticalStack()
val view1 = stack.solidRect(10, 10, Colors.RED)
val view2 = stack.graphics { fill(Colors.RED) { rect(10, 10, 20, 20) } }
val view3 = stack.solidRect(10, 10, Colors.RED)
stack.relayout()
assertEquals("0,10,40", "${view1.y.niceStr},${view2.y.niceStr},${view3.y.niceStr}")
}

@Test
fun testUIHorizontalStack() {
val stack = UIHorizontalStack()
val view1 = stack.solidRect(10, 10, Colors.RED)
val view2 = stack.graphics { fill(Colors.RED) { rect(10, 10, 20, 20) } }
val view3 = stack.solidRect(10, 10, Colors.RED)
stack.relayout()
assertEquals("0,10,40", "${view1.x.niceStr},${view2.x.niceStr},${view3.x.niceStr}")
}
}

0 comments on commit 387fa55

Please sign in to comment.