连接控制器与Scene Builider事件

时间:2016-05-16 17:41:01

标签: java javafx scenebuilder

如何使用Scene Builder连接键盘/鼠标等事件? 我的代码如下:

@FXML 
    public void onKeyPressed() {
          group.getScene().setOnKeyPressed(event -> {
              double change = 10.0;
              if(event.isShiftDown()) { change = 50.0; }
              KeyCode keycode = event.getCode();
              if(keycode == KeyCode.W) { perspectiveCamera.setTranslateZ(perspectiveCamera.getTranslateZ() + change); }
              if(keycode == KeyCode.S) { perspectiveCamera.setTranslateZ(perspectiveCamera.getTranslateZ() - change); }
              if(keycode == KeyCode.A) { perspectiveCamera.setTranslateX(perspectiveCamera.getTranslateX() - change); }
              if(keycode == KeyCode.D) { perspectiveCamera.setTranslateX(perspectiveCamera.getTranslateX() + change); }
          });    
    }
    @FXML
    public void onMousePressed() {
        group.getScene().setOnMousePressed((MouseEvent me) -> {
             mousePosX = me.getSceneX();
             mousePosY = me.getSceneY();
             mouseOldX = me.getSceneX();
             mouseOldY = me.getSceneY();
         });
    }
    @FXML
    public void onMouseDragged() {
        group.getScene().setOnMouseDragged((MouseEvent me) -> {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);

            double modifier = 10.0;
            double modifierFactor = 0.1;

            if (me.isControlDown()) {
                modifier = 0.1;
            }
            if (me.isShiftDown()) {
                modifier = 50.0;
            }
            if (me.isPrimaryButtonDown()) {
                cameraTransformer.ry.setAngle(((cameraTransformer.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);  // +
                cameraTransformer.rx.setAngle(((cameraTransformer.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);  // -
            } else if (me.isSecondaryButtonDown()) {
                double z = perspectiveCamera.getTranslateZ();
                double newZ = z + mouseDeltaX * modifierFactor * modifier;
                perspectiveCamera.setTranslateZ(newZ);
            } else if (me.isMiddleButtonDown()) {
                cameraTransformer.t.setX(cameraTransformer.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3);  // -
                cameraTransformer.t.setY(cameraTransformer.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3);  // -
            }
        });
    }

并且Scene Builder面板:

我通常以相同的方式做它并且它有效但现在使用这个面板却没有。我在哪里做错了?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

对于鼠标事件,请在anchorpane中设置它们:

<AnchorPane onMouseDragged="#onMouseDragged" onMousePressed="#onMousePressed" ...>
   <children>
      <Group fx:id="group" />
   </children>
</AnchorPane>

现在在您的控制器中直接传递事件:

@FXML
private void onMousePressed(MouseEvent me) {
    mousePosX = me.getSceneX();
    mousePosY = me.getSceneY();
    mouseOldX = me.getSceneX();
    mouseOldY = me.getSceneY();
}

@FXML
private void onMouseDragged(MouseEvent me) {
    mouseOldX = mousePosX;
    mouseOldY = mousePosY;
    mousePosX = me.getSceneX();
    mousePosY = me.getSceneY();
    ...
}

对于按下按键的事件,场景似乎接受此事件并消耗它,因此您需要将它从场景传播到控制器:

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Window3DSceneView.fxml"));
    Parent root = loader.load();
    Window3DController controller = loader.getController();

    Scene scene = new Scene(root, 1200, 800, true, SceneAntialiasing.BALANCED);
    scene.setOnKeyPressed(e -> controller.onKeyPressed(e));
    ...
}

现在在你的控制器中:

 public void onKeyPressed(KeyEvent event) {
      double change = 10.0;
      if(event.isShiftDown()) { change = 50.0; }
      KeyCode keycode = event.getCode();
      ...
 }