gpt4 book ai didi

kotlin - doubleBinding 没有效果

转载 作者:行者123 更新时间:2023-12-02 13:22:16 25 4
gpt4 key购买 nike

我正在使用 TornadoFX 进行第一次实验,但遇到了一个我不明白的问题。

我有一个对象 Wind :

enum class Direction(val displayName: String, val abbrevation: String, val deltaX: Int, val deltaY: Int) {

NORTH("Észak", "É", 0, -1),
NORTH_EAST("Északkelet", "ÉK", 1, -1),
EAST("Kelet", "K", 1, 0),
SOUTH_EAST("Délkelet", "DK", 1, 1),
SOUTH("Dél", "D", 0, 1),
SOUTH_WEST("Délnyugat", "DNy", -1, 1),
WEST("Nyugat", "Ny", -1, 0),
NORTH_WEST("Északnyugat", "ÉNy", -1, -1);

val diagonal: Boolean = deltaX != 0 && deltaY != 0
val degree: Double = ordinal * 45.0

fun turnClockwise(eighth: Int = 1) = values()[(ordinal + eighth) umod 8]
fun turnCounterClockwise(eighth: Int = 1) = values()[(ordinal - eighth) umod 8]
fun turn(eighth: Int = 1) = if (eighth < 0) turnCounterClockwise(eighth.absoluteValue) else turnClockwise(eighth)

infix operator fun plus(eighth: Int) = turn(eighth)
infix operator fun minus(eighth: Int) = turn(-eighth)

infix operator fun minus(other: Direction) = (ordinal - other.ordinal) umod 8
}

object Wind {

val directionProperty = SimpleObjectProperty<Direction>(Direction.NORTH)
var direction: Direction
get() = directionProperty.value
set(value) {
println("SET WIND: $value")
directionProperty.value = value
}
}

我想将旋转变换绑定(bind)到风向的设置。

当我使用旧的 JavaFX 样式时,它可以工作:
rot.angleProperty().bind(
createDoubleBinding(
Callable {
println("Direction: ${Wind.direction}");
Wind.directionProperty.value.degree * 45
},
Wind.directionProperty))

当我尝试使用更优雅的 Kotlin 风格版本时,它不会绑定(bind):
rot.angleProperty().doubleBinding(rot.angleProperty() ) {
println("Direction: ${Wind.direction}")
Wind.directionProperty.value.degree * 45
}

我错过了什么?

最佳答案

doubleBinding()函数创建一个 Binding ,但它不会将其绑定(bind)到任何东西。

事实上,我们有两种方法来创建这个绑定(bind):

  • doubleBinding(someProperty) { ... } .对属性 (this) 进行操作,并希望您返回 Double。它不能为空。
  • someProperty.doubleBinding() { ... }接收值作为参数并期望您返回一个 Double。该参数可以为空,因此您需要考虑

  • 这让您有两个选择:
    rot.angleProperty().bind(doubleBinding(Wind.directionProperty) {
    value.degree * 45
    })

    或者
    rot.angleProperty().bind(Wind.directionProperty.doubleBinding {
    it?.degree ?: 0.0 * 45
    })

    您选择哪一种主要取决于口味,尽管在某些情况下一种会比另一种更自然。

    关于kotlin - doubleBinding 没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53890890/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com