先声明一个可以拖拽的scene
var scene = Label( "blablabla")
scene.id = "label"
定义两个记录point,分别记录translate和实放游标的点
class Delta {
var x = -1.0
var y = -1.0
}
var dragDelta = Delta()
var releasedDelta = Delta()
先加入mouseenter的手型游标时间
scene.setOnMouseEntered {
scene.cursor = Cursor.HAND
}
在mouserelease事件中记录坐标
scene.setOnMouseReleased {
releasedDelta.x = it.sceneX
releasedDelta.y = it.sceneY
scene.cursor = Cursor.HAND;
}
在mousepressed事件中,调整translate的计算方式:
scene.setOnMousePressed {
if (releasedDelta.x > 0 && releasedDelta.y > 0) {
val offsetX = it.sceneX - releasedDelta.x //already have translate point, recalculate the translate value
var offsetY = it.sceneY - releasedDelta.y
dragDelta.x = dragDelta.x + offsetX
dragDelta.y = dragDelta.y + offsetY
} else {
dragDelta.x = it.sceneX
dragDelta.y = it.sceneY
}
scene.cursor = Cursor.MOVE;
releasedDelta = Delta()
}
在mousedrag中设置scene的translate值
scene.setOnMouseDragged {
scene.translateX = it.sceneX - dragDelta.x;
scene.translateY = it.sceneY - dragDelta.y;
}