Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UIContainerLayouts.kt so graphics{} work better #2166

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
}
}
Loading