-
Hi, I'm not pro in Kotlin yet and can't figure out how to add my custom functions to libktx DSL (in my project) to make code more readable and consistent. For instance, I have two similar builders for windows (for win and for defeat cases) and to avoid duplication I try to do this: fun endWindow(title: String, content: () -> Unit): VisWindow {
return window(title) {
centerWindow()
padTop(50f) // title height
defaults().top()
width = 320f
height = 500f
row()
content()
row()
label("end of endWindow")
}
}
fun winWindow(): VisWindow {
return endWindow("Victory!") {
label("You won!")
}
}
fun loseWindow(): VisWindow {
return endWindow("gameOver!") {
label("You lost!")
}
} But this code doesn't work and I feel like I'm using lambda as last parameter in And moreover it would be cool to use my custom functions like endWindow("Win!") {
table {
// stats and score here
}
} But as for now, I can do just old fashioned stuff like that: fun endWindow(title: String): VisWindow {
return window(title) {
table {
// stats and score here
}
}
}
// And add this it in init
stage.add(endWindow("Win!)) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You have to add the window to the lambda signature, so it becomes fun endWindow(title: String, content: KVisWindow.() -> Unit): VisWindow We're using a Also, you're not using an updated version of import com.kotcrab.vis.ui.widget.VisWindow
import ktx.scene2d.scene2d
import ktx.scene2d.vis.KVisWindow
import ktx.scene2d.vis.visLabel
import ktx.scene2d.vis.visWindow
fun endWindow(title: String, content: KVisWindow.() -> Unit): VisWindow {
return scene2d.visWindow(title) {
centerWindow()
padTop(50f) // title height
defaults().top()
width = 320f
height = 500f
row()
content()
row()
visLabel("end of endWindow")
}
}
fun winWindow(): VisWindow {
return endWindow("Victory!") {
visLabel("You won!")
}
}
fun loseWindow(): VisWindow {
return endWindow("gameOver!") {
visLabel("You lost!")
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you! Now my code should become more readable. I just updated to |
Beta Was this translation helpful? Give feedback.
-
Contracts API is already pretty stable, and we're only using the most common functionalities. I would consider waiting for Kotlin 1.4 if contacts were promoted to a stable feature in this release, but it seems that they won't just yet. Given their obvious benefits for DSLs, I think it's worth risking the future incompatibilities for today's convenience.
The You can omit stage.actors {
window("Title") {
label("This window will be added to the stage!")
}
} You can also use built-in Kotlin utilities like fun differentWaysToBuildALabel() {
val label1 = scene2d {
label("1")
}
val label2: Label
with(scene2d) {
label2 = label("2")
}
val label3: Label
scene2d.apply {
label3 = label("3")
}
} Check out the README files for more info. |
Beta Was this translation helpful? Give feedback.
You have to add the window to the lambda signature, so it becomes
this
, exposing all DSL methods:We're using a
KVisWindow
instead ofVisWindow
for the lambda, as it has access to the DSL.Also, you're not using an updated version of
ktx-vis
. There were some notable API changes and performance improvements, so I encourage you to try it out. Updated code that compiles: