使ScalaFX场景可拖动

时间:2014-11-01 18:24:44

标签: scalafx

我实际上在没有窗口装饰的ScalaFX应用程序上工作。 我想要做的就是让它可以拖动,这样用户无论如何都可以移动它。

我通过简单地将拖动鼠标的坐标写入舞台X / Y坐标来管理移动舞台。然而,这导致了一个滞后和闪烁的窗口。

如何在ScalaFX中顺利实现拖动舞台?

2 个答案:

答案 0 :(得分:2)

这是一个适合我的例子。它改编自“JavaFX 8 Introduction by Example”一书中的一个例子。它与你的尝试相比如何?

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Point2D
import scalafx.scene.Scene
import scalafx.scene.input.MouseEvent
import scalafx.scene.paint.Color
import scalafx.stage.{WindowEvent, StageStyle}

object DraggableApp extends JFXApp {

  private var anchorPt: Point2D = null
  private var previousLocation: Point2D = null

  stage = new PrimaryStage {
    initStyle(StageStyle.TRANSPARENT)
    scene = new Scene {
      fill = Color.rgb(0, 0, 0, 1.0)
    }
  }

  // Initialize stage to be movable via mouse
  initMovablePlayer()

  /**
   * Initialize the stage to allow the mouse cursor to move the application
   * using dragging.
   */
  private def initMovablePlayer(): Unit = {
    val scene = stage.getScene

    scene.onMousePressed = (event: MouseEvent) => anchorPt = new Point2D(event.screenX, event.screenY)

    scene.onMouseDragged = (event: MouseEvent) =>
      if (anchorPt != null && previousLocation != null) {
        stage.x = previousLocation.x + event.screenX - anchorPt.x
        stage.y = previousLocation.y + event.screenY - anchorPt.y
      }

    scene.onMouseReleased = (event: MouseEvent) => previousLocation = new Point2D(stage.getX, stage.getY)

    stage.onShown = (event: WindowEvent) => previousLocation = new Point2D(stage.getX, stage.getY)
  }
}

编辑:关于舞台调整大小的问题,我尝试了以下变体,右键单击并拖动时调整舞台大小;我没有看到任何闪烁(在OS X上)。

import javafx.scene.{input => jfxsi}
// (...)

object DraggableApp extends JFXApp {

  private var previousHeight = 0.0
  private var previousWidth = 0.0
  // (...)

  private def initMovablePlayer(): Unit = {
    val scene = stage.getScene

    scene.onMousePressed = (event: MouseEvent) => anchorPt = new Point2D(event.screenX, event.screenY)

    scene.onMouseDragged = (event: MouseEvent) =>
      if (anchorPt != null && previousLocation != null) {
        if (event.getButton == jfxsi.MouseButton.PRIMARY) {
          stage.x = previousLocation.x + event.screenX - anchorPt.x
          stage.y = previousLocation.y + event.screenY - anchorPt.y
        } else if (event.getButton == jfxsi.MouseButton.SECONDARY) {
          stage.width = previousWidth + event.screenX - anchorPt.x
          stage.height = previousHeight + event.screenY - anchorPt.y
        }
      }

    scene.onMouseReleased = (_: MouseEvent) => reset()

    stage.onShown = (_: WindowEvent) => reset()

    def reset (): Unit = {
      previousLocation = new Point2D(stage.getX, stage.getY)
      previousHeight = stage.getHeight
      previousWidth = stage.getWidth
    }
  }
}

答案 1 :(得分:0)

我知道这已经得到了解答,但为了完整起见,我想包括我过去做过的方式。

现金:
可拖动窗口:Pro JavaFX 8示例
可调整大小的窗口:小孩 - Allow user to resize an undecorated Stage
恢复上/下:Undecorator - UndecoratorController.java

为(杂乱的)代码的墙壁做好准备

class UndecoratedWindowHelper(val target: Stage) {

   var anchorPt: Point2D = null
   var prevLoc: Point2D = null
   private var savedBounds: BoundingBox = _
   private val _maximized: ReadOnlyBooleanWrapper = new ReadOnlyBooleanWrapper {value = false}

   // begin init
   val resizeListener = new ResizeListener(target, this)
   target.scene.get.addEventHandler(jfxme.MOUSE_MOVED, resizeListener)
   target.scene.get.addEventHandler(jfxme.MOUSE_PRESSED, resizeListener)
   target.scene.get.addEventHandler(jfxme.MOUSE_DRAGGED, resizeListener)
   // end init

   def addWindowDragPoint(dragNode: Node): Unit = {
      dragNode.onMousePressed = (event: MouseEvent) => {
         anchorPt = new Point2D(event.screenX, event.screenY)
         prevLoc = new Point2D(target.getX, target.getY)
      }

      dragNode.onMouseClicked = (event: MouseEvent) =>
         if (event.clickCount == 2)
            maximizeOrRestore()

      dragNode.onMouseDragged = (event: MouseEvent) =>
         if (resizeListener.cursorEvent == Cursor.DEFAULT && !maximized) {
            target.x = prevLoc.x + event.screenX - anchorPt.x
            target.y = prevLoc.y + event.screenY - anchorPt.y
         }

      target.onShown = (event: WindowEvent) => prevLoc = new Point2D(target.getX, target.getY)
   }


   def maximizeOrRestore() {
      if (maximized) {
         restoreSavedBounds()
         savedBounds = null
         _maximized set false
      } else {
         val screensForRectangle = Screen.screensForRectangle(target.getX, target.getY, target.getWidth, target.getHeight)
         val screen = screensForRectangle.get(0)
         val visualBounds = screen.visualBounds
         savedBounds = new BoundingBox(target.getX, target.getY, target.getWidth, target.getHeight)

         target.setX(visualBounds.getMinX)
         target.setY(visualBounds.getMinY)
         target.setWidth(visualBounds.getWidth)
         target.setHeight(visualBounds.getHeight)
         _maximized set true
      }
   }
   def saveBounds() {
      savedBounds = new BoundingBox(target.getX, target.getY, target.getWidth, target.getHeight)
   }

   def restoreSavedBounds() {
      target.setX(savedBounds.getMinX)
      target.setY(savedBounds.getMinY)
      target.setWidth(savedBounds.getWidth)
      target.setHeight(savedBounds.getHeight)
      savedBounds = null
   }

   def maximized = _maximized.getValue
   def maximizedProp = _maximized.getReadOnlyProperty
}

//formatter:off
protected class ResizeListener(
                                  val stage: Stage, owner: UndecoratedWindowHelper) extends EventHandler[jfxme] {

   var cursorEvent = Cursor.DEFAULT
   val border = 4
   var startX = 0d
   var startY = 0d
   //formatter:on

   def handle(mouseEvent: jfxme) {
      val mouseEventType = mouseEvent.getEventType
      val scene = stage.getScene

      val mouseEventX = mouseEvent.getSceneX
      val mouseEventY = mouseEvent.getSceneY
      val sceneWidth = scene.getWidth
      val sceneHeight = scene.getHeight

      if (jfxme.MOUSE_MOVED == mouseEventType) {

         //@formatter:off
         cursorEvent =
               if (mouseEventX < border && mouseEventY < border) Cursor.NW_RESIZE
               else if (mouseEventX < border && mouseEventY > sceneHeight - border) Cursor.SW_RESIZE
               else if (mouseEventX > sceneWidth - border && mouseEventY < border) Cursor.NE_RESIZE
               else if (mouseEventX > sceneWidth - border && mouseEventY > sceneHeight - border) Cursor.SE_RESIZE
               else if (mouseEventX < border) Cursor.W_RESIZE
               else if (mouseEventX > sceneWidth - border) Cursor.E_RESIZE
               else if (mouseEventY < border) Cursor.N_RESIZE
               else if (mouseEventY > sceneHeight - border) Cursor.S_RESIZE
               else Cursor.DEFAULT
         //@formatter:on
         if (owner.maximized)
            cursorEvent = Cursor.DEFAULT


         scene.setCursor(cursorEvent)
      } else if (mouseEventType == jfxme.MOUSE_PRESSED) {

         startX = stage.getWidth - mouseEventX
         startY = stage.getHeight - mouseEventY

      } else if (mouseEventType == jfxme.MOUSE_DRAGGED) {
         if (Cursor.DEFAULT != cursorEvent) {


            if (Cursor.W_RESIZE != cursorEvent && Cursor.E_RESIZE != cursorEvent) {// not west or east

               val minHeight = Math.max(stage.getMinHeight, border * 2)


               if (Cursor.NW_RESIZE == cursorEvent || Cursor.N_RESIZE == cursorEvent || Cursor.NE_RESIZE == cursorEvent) {// NW or N or NE

                  val attemptedSize = stage.getY - mouseEvent.getScreenY + stage.getHeight
                  val actSize = Math.max(minHeight, attemptedSize)
                  val diff = actSize - attemptedSize

                  stage.setHeight(actSize)
                  stage.setY(mouseEvent.getScreenY - diff)

               } else {// SW or S or SE

                  val attemptedSize = mouseEventY + startY
                  val actSize = Math.max(minHeight, attemptedSize)

                  stage.setHeight(actSize)
               }
            }

            if (Cursor.N_RESIZE != cursorEvent && Cursor.S_RESIZE != cursorEvent) {

               val minWidth = Math.max(stage.getMinWidth, border * 2)

               if (Cursor.NW_RESIZE == cursorEvent || Cursor.W_RESIZE == cursorEvent || Cursor.SW_RESIZE == cursorEvent) {
                  val attemptedSize = stage.getX - mouseEvent.getScreenX + stage.getWidth
                  val actSize = Math.max(minWidth, attemptedSize)
                  val diff = actSize - attemptedSize

                  stage.setWidth(actSize)
                  stage.setX(mouseEvent.getScreenX - diff)

               } else {
                  val attemptedSize = mouseEventX + startX
                  val actSize = Math.max(minWidth, attemptedSize)

                  stage.setWidth(actSize)

               }
            }
            owner.saveBounds()
         }

      }
   }
}