JavaFX拖动未修饰的阶段“ Fallthrough”问题

时间:2019-01-30 20:33:26

标签: javafx kotlin

我有一个未经修饰的舞台,其中有一个自定义标题栏。该标题栏包含“ IconButton”,它们只是持有ImageViews的StackPanes。

我还使用此代码通过标题栏(科特琳代码)使舞台可拖动:

fun makeDraggable(stage: Stage, byNode: Node) {

    val dragDelta = Delta()

    byNode.setOnMousePressed { e ->
        if (stage.isFullScreen || stage.isMaximized) {
            return@setOnMousePressed
        }

        dragDelta.x = stage.x - e.screenX
        dragDelta.y = stage.y - e.screenY
    }

    byNode.setOnMouseDragged { e ->
        if (stage.isFullScreen || stage.isMaximized) {
            return@setOnMouseDragged
        }

        stage.x = e.screenX + dragDelta.x
        stage.y = e.screenY + dragDelta.y
    }
}

现在,问题在于,即使鼠标悬停在这些“ IconButton”之一上,也可以拖动舞台。对于普通的JavaFX Button,这似乎不是问题。我看不到为什么会这样,如何解决。

1 个答案:

答案 0 :(得分:0)

检查pickResult似乎可以正常工作(感谢fabian):

fun makeDraggable(stage: Stage, byNode: Node) {

    val dragDelta = Delta()

    var validTarget = true

    byNode.setOnMousePressed { e ->
        if (stage.isFullScreen || stage.isMaximized) {
            return@setOnMousePressed
        }

        val pickedNode = e.pickResult.intersectedNode

        if (pickedNode is IconButton) {
            validTarget = false
            return@setOnMousePressed
        }
        if (pickedNode.parent is IconButton) {
            validTarget = false
            return@setOnMousePressed
        }

        dragDelta.x = stage.x - e.screenX
        dragDelta.y = stage.y - e.screenY
    }

    byNode.setOnMouseReleased { e ->
        if (stage.isFullScreen || stage.isMaximized) {
            return@setOnMouseReleased
        }
        validTarget = true
    }

    byNode.setOnMouseDragged { e ->
        if (stage.isFullScreen || stage.isMaximized) {
            return@setOnMouseDragged
        }

        if (!validTarget) {
            return@setOnMouseDragged
        }

        stage.x = e.screenX + dragDelta.x
        stage.y = e.screenY + dragDelta.y
    }
}