简体   繁体   English

TornadoFX覆盖布局区域上的子级

[英]TornadoFX overriding layoutChildren on Region

Im trying to translate this JavaFX class to TornadoFX. 我正在尝试将此 JavaFX类转换为TornadoFX。 Hover im not able to figure out how protected void layoutChildren() should be done with TornadoFX? 悬停即时通讯无法弄清楚应该如何使用TornadoFX完成protected void layoutChildren()

This is the code I have so far: 这是我到目前为止的代码:

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()
        }
    }
}

I'm not sure what you mean by translating to TornadoFX, but writing the layoutChildren in Kotlin would look something like this: 我不确定翻译成TornadoFX的意思,但是用Kotlin编写layoutChildren看起来像这样:

override fun layoutChildren() {
    layoutInArea(highlight, 0.0, 0.0, width, height, baselineOffset, HPos.CENTER, VPos.CENTER);
}

EDIT: You updated the code example to a View, so I think I understand what you want now :) 编辑:您将代码示例更新为视图,所以我想我现在明白您想要的是:)

First, make sure the View doesn't require parameters, because that would make it impossible to inject this view. 首先,请确保View不需要参数,因为这样将无法注入该视图。 Either pass parameters using by param() or better yet, inject a ViewModel in the scope of the View, and inject that ViewModel into your View. 使用by param()或更好by param()传递参数,在View范围内注入ViewModel,然后将该ViewModel注入您的View。

Maybe you can add x and y as properties to ReversiModel? 也许您可以将x和y作为属性添加到R​​eversiModel?

If you need to create a custom Region you can create what would be an anonymous inner class equivalent, in Java speak: 如果需要创建自定义区域,则可以创建等效于匿名内部类的语言,用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);
        }
    }
}

To open this View now, create a new scope and push the ReversiModel into it: 要立即打开此视图,请创建一个新的作用域并将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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM