libktx / ktx

Kotlin extensions for the libGDX game framework
https://libktx.github.io/
Creative Commons Zero v1.0 Universal
1.33k stars 71 forks source link

scene2d image without drawable #485

Open Quillraven opened 1 week ago

Quillraven commented 1 week ago

Hi,

right now the image scene2d extension requires a non-null Drawable:

@Scene2dDsl
@OptIn(ExperimentalContracts::class)
inline fun <S> KWidget<S>.image(
  drawable: Drawable,
  init: (@Scene2dDsl Image).(S) -> Unit = {},
): Image {
  contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
  return actor(Image(drawable), init)
}

However, it is valid to pass "null" as a drawable and this can be useful if the real drawable is set later on but you still want to add the image to the table to have the correct layout.

Would it be okay to make it Drawable? instead of Drawable? This might be also true for other extensions with drawables but I did not check them all.

Or is there a different solution to it?

czyzby commented 1 week ago

If an image without a drawable can exist and might make sense for some use cases, it should be possible to create with the DSL. This might be an oversight, but it can be easily amended without breaking the API. Drawable? with a null default could be a way to solve this, I just have to check first if an image without a drawable isn't otherwise broken (e.g., thowing NPE on draw). Thanks for bringing this up.

Quillraven commented 1 week ago

Imo this should be just fine and at least in my case it works. Here is also the official documentation of setDrawable which mentions it may be null:

image