Javafx TornadFX覆盖区域上的布局子项
我正在尝试将JavaFX类转换为TornadoFX。悬停我无法确定如何使用TornadFX执行受保护的void layoutChildren() 这是我目前掌握的代码:Javafx TornadFX覆盖区域上的布局子项,javafx,kotlin,tornadofx,Javafx,Kotlin,Tornadofx,我正在尝试将JavaFX类转换为TornadoFX。悬停我无法确定如何使用TornadFX执行受保护的void layoutChildren() 这是我目前掌握的代码: class ReversiSquare(x: Int, y: Int) : View() { var x by property(x) fun xProperty() = getProperty(ReversiSquare::y) var y by property(y) fun yPrope
class ReversiSquare(x: Int, y: Int) : View() {
var x by property(x)
fun xProperty() = getProperty(ReversiSquare::y)
var y by property(y)
fun yProperty() = getProperty(ReversiSquare::y)
var highlight: Region by singleAssign()
var highlightTransition: FadeTransition by singleAssign()
val model = ReversiModel
override val root = region {
region {
opacity = 0.0
style = "-fx-border-width: 3; -fx-border-color: dodgerblue"
highlight = this
}
// todo not sure this works with singleAssign
highlightTransition = FadeTransition(Duration.millis(200.0), highlight).apply {
fromValue = 0.0
toValue = 1.0
}
styleProperty().bind(Bindings.`when`(model.legalMove(x, y))
.then("-fx-background-color: derive(dodgerblue, -60%)")
.otherwise("-fx-background-color: burlywood"))
val light = Light.Distant().apply {
azimuth = -135.0
elevation = 30.0
}
effect = Lighting(light)
setPrefSize(200.0,200.0)
this += highlight
addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET) {
if(model.legalMove(x ,y).get()) {
with(highlightTransition) {
rate =1.0
play()
}
}
}
addEventHandler(MouseEvent.MOUSE_EXITED_TARGET) {
with(highlightTransition) {
rate = -1.0
play()
}
}
onDoubleClick {
model.play(x, y)
highlightTransition.rate = -1.0
highlightTransition.play()
}
}
}
我不知道你翻译成TornadoFX是什么意思,但是在Kotlin中编写
layoutChildren
看起来像这样:
override fun layoutChildren() {
layoutInArea(highlight, 0.0, 0.0, width, height, baselineOffset, HPos.CENTER, VPos.CENTER);
}
编辑:您将代码示例更新为一个视图,因此我想我现在明白了您的要求:)
首先,确保视图不需要参数,因为这样就不可能注入此视图。使用by param()
传递参数,或者在视图范围内插入ViewModel,然后将该ViewModel插入视图
也许可以将x和y作为属性添加到ReversiModel
如果需要创建自定义区域,您可以创建一个匿名内部类,用Java语言:
class ReversiSquare : View() {
val model: ReversiModel by inject()
override val root = object : Region() {
// ...
override fun layoutChildren() {
layoutInArea(highlight, 0.0, 0.0, width, height, baselineOffset, HPos.CENTER, VPos.CENTER);
}
}
}
要立即打开此视图,请创建一个新范围并将ReversiModel推入其中:
// Create the model, set x, y and other initial state in the model
val model = ReversiModel()
model.x = 42
// Create a new scope and push the ReversiModel into it
val scope = Scope(model)
// Find the ReversiSquare in the new scope
find<ReversiSquare>(scope) {
// Do something with the sequare, like openWindow() or similar
}
//创建模型,在模型中设置x、y和其他初始状态
val model=ReversiModel()
型号x=42
//创建一个新范围并将ReversiModel推入其中
val范围=范围(型号)
//在新范围中查找反向方形
查找(范围){
//使用Sequale执行一些操作,例如openWindow()或类似的操作
}
对不起,我的问题不好。我想知道如何使TornadFX视图的这一部分,我将发布到目前为止的代码,问题可能会更清楚。