From 6811c7c645571eb7d680c6ccc39c0bcc36bc94f0 Mon Sep 17 00:00:00 2001 From: Simon Klausner Date: Mon, 29 Jul 2024 10:34:24 +0200 Subject: [PATCH] (#485) make drawableName optional for scene2d image factory --- .../src/main/kotlin/ktx/scene2d/factory.kt | 14 ++++++---- .../test/kotlin/ktx/scene2d/factoryTest.kt | 27 +++++++++++++++---- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/scene2d/src/main/kotlin/ktx/scene2d/factory.kt b/scene2d/src/main/kotlin/ktx/scene2d/factory.kt index 9c361392..3340ebbe 100644 --- a/scene2d/src/main/kotlin/ktx/scene2d/factory.kt +++ b/scene2d/src/main/kotlin/ktx/scene2d/factory.kt @@ -209,7 +209,7 @@ inline fun KWidget.horizontalGroup( } /** - * @param drawableName name of a drawable stored in the chosen skin. + * @param drawableName optional name of a drawable stored in the chosen skin. * @param skin [Skin] instance that contains the widget style. Defaults to [Scene2DSkin.defaultSkin]. * @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. @@ -219,12 +219,16 @@ inline fun KWidget.horizontalGroup( @Scene2dDsl @OptIn(ExperimentalContracts::class) inline fun KWidget.image( - drawableName: String, + drawableName: String? = null, skin: Skin = Scene2DSkin.defaultSkin, init: (@Scene2dDsl Image).(S) -> Unit = {}, ): Image { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } - return actor(Image(skin.getDrawable(drawableName)), init) + return if (drawableName == null) { + actor(Image(), init) + } else { + actor(Image(skin.getDrawable(drawableName)), init) + } } /** @@ -279,7 +283,7 @@ inline fun KWidget.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. @@ -288,7 +292,7 @@ inline fun KWidget.image( @Scene2dDsl @OptIn(ExperimentalContracts::class) inline fun KWidget.image( - drawable: Drawable, + drawable: Drawable? = null, init: (@Scene2dDsl Image).(S) -> Unit = {}, ): Image { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } diff --git a/scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt b/scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt index afd74a92..7b4bb79d 100644 --- a/scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt +++ b/scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt @@ -6,10 +6,7 @@ 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.assertTrue +import org.junit.Assert.* import org.junit.Test import org.mockito.kotlin.mock import com.badlogic.gdx.utils.Array as GdxArray @@ -129,6 +126,18 @@ class NoInitBlockActorFactoriesTest : ApplicationTest() { }, ) + @Test + fun `should create Image with null drawable name`() = test( + widget = { + image(drawableName = null) { + color = Color.BLUE + } + }, + validate = { + assertNull(it.drawable) + }, + ) + @Test fun `should create Image with nine patch`() = test { image(VisUI.getSkin().getPatch("button")) } @@ -142,7 +151,15 @@ 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`() = test( + widget = { image(drawable = null) }, + validate = { + assertNull(it.drawable) }, )