-
I have UI with several class MyTable: Table() {
init {
val subTable1 = scene2d.table { ... }
val subTable2 = scene2d.table { ... }
add(subTable1)
row()
add(subTable2)
}
} Can I use builders there somehow to get something like that? class MyTable: Table() {
init {
table { ... } // subTable1
row()
table { ... } // subTable2
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
Yes, you have to implement the import ktx.scene2d.KTable
import com.badlogic.gdx.scenes.scene2d.ui.Table
class MyTable: Table(), KTable {
init {
table { } // subTable1
row()
table { } // subTable2
}
} Since Similar interfaces exist for groups, trees, etc. |
Beta Was this translation helpful? Give feedback.
-
Awesome! I had gut feelling that it is possible) |
Beta Was this translation helpful? Give feedback.
-
I added a similar example to the README. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I just found out that in this case I can't use class fields, like: I faced this problem in #301, but in that case I just refused using |
Beta Was this translation helpful? Give feedback.
-
@Divelix This is because import com.badlogic.gdx.scenes.scene2d.Group
class Try: Group(), KGroup {
val str = "text"
init {
table {
label(this@Try.str)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Just wanted to say that this is an expected side effect of the Kotlin DSL builders, and it actually has some advantages. For example, you cannot immediately access properties of parent actors within the children blocks: table {
label("Child") {
// This will not compile, so you will not modify the table accidentally:
pad(2f)
// If you really need to access parent's properties, you can do so explicitly:
this@table.pad(2f)
}
// This compiles normally, as we're within the table block:
pad(2f)
} |
Beta Was this translation helpful? Give feedback.
Yes, you have to implement the
KTable
interface:Since
MyTable
already extendsTable
, there aren't any extra methods that you have to override - all you have to do is to add the interface.Similar interfaces exist for groups, trees, etc.