Skip to content

Commit

Permalink
Optional drawable in image actor. #485
Browse files Browse the repository at this point in the history
* (#485) make drawableName optional for scene2d image factory

* (#485) make drawable optional for scene2d image factory

- revert drawableName change
- fix asterisk import

---------

Co-authored-by: Simon Klausner <[email protected]>
  • Loading branch information
Quillraven and Simon Klausner authored Jul 30, 2024
1 parent 6478c5f commit ac8e6e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions scene2d/src/main/kotlin/ktx/scene2d/factory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ inline fun <S> KWidget<S>.image(
}

/**
* @param drawable will be drawn by the [Image].
* @param drawable will be drawn by the [Image]. Per default it is null.
* @param init will be invoked with the widget as "this". Consumes actor container (usually a [Cell] or [Node]) that
* contains the widget. Might consume the actor itself if this group does not keep actors in dedicated containers.
* Inlined.
Expand All @@ -288,7 +288,7 @@ inline fun <S> KWidget<S>.image(
@Scene2dDsl
@OptIn(ExperimentalContracts::class)
inline fun <S> KWidget<S>.image(
drawable: Drawable,
drawable: Drawable? = null,
init: (@Scene2dDsl Image).(S) -> Unit = {},
): Image {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
Expand Down
21 changes: 19 additions & 2 deletions scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import com.badlogic.gdx.scenes.scene2d.ui.Cell
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Tree.Node
import com.kotcrab.vis.ui.VisUI
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.kotlin.mock
Expand Down Expand Up @@ -142,7 +143,23 @@ class NoInitBlockActorFactoriesTest : ApplicationTest() {
fun `should create Image with drawable`() = test(
widget = { image(VisUI.getSkin().getDrawable("button")) },
validate = {
Assert.assertSame(VisUI.getSkin().getDrawable("button"), it.drawable)
assertSame(VisUI.getSkin().getDrawable("button"), it.drawable)
},
)

@Test
fun `should create Image with null drawable passing null argument`() = test(
widget = { image(drawable = null) },
validate = {
assertNull(it.drawable)
},
)

@Test
fun `should create Image with null drawable passing no arguments`() = test(
widget = { image() },
validate = {
assertNull(it.drawable)
},
)

Expand Down

0 comments on commit ac8e6e9

Please sign in to comment.