Skip to content

Commit

Permalink
(#485) make drawableName optional for scene2d image factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Klausner committed Jul 29, 2024
1 parent 6478c5f commit 6811c7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
14 changes: 9 additions & 5 deletions scene2d/src/main/kotlin/ktx/scene2d/factory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ inline fun <S> KWidget<S>.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.
Expand All @@ -219,12 +219,16 @@ inline fun <S> KWidget<S>.horizontalGroup(
@Scene2dDsl
@OptIn(ExperimentalContracts::class)
inline fun <S> KWidget<S>.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)
}
}

/**
Expand Down Expand Up @@ -279,7 +283,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 +292,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
27 changes: 22 additions & 5 deletions scene2d/src/test/kotlin/ktx/scene2d/factoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")) }

Expand All @@ -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)
},
)

Expand Down

0 comments on commit 6811c7c

Please sign in to comment.