/** * Initializes the button click handler. Every time the button is pressed, the react() method in * this class is called. */ private void initializeHandler() { theImage.setOnMouseClicked( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { reactClick(event); } }); theImage.setOnMousePressed( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { reactPressed(event); } }); theImage.setOnMouseReleased( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { reactReleased(event); } }); }
public void setFirstPersonNavigationEabled(boolean b) { if (b) { // Navigation setMouseTransparent(false); // First person shooter keyboard movement imageView.setOnKeyPressed( event -> { double change = 10.0; // Add shift modifier to simulate "Running Speed" if (event.isShiftDown()) { change = 50.0; } // What key did the user press? KeyCode keycode = event.getCode(); // Step 2c: Add Zoom controls if (keycode == KeyCode.W) { camera.setTranslateZ(camera.getTranslateZ() + change); } if (keycode == KeyCode.S) { camera.setTranslateZ(camera.getTranslateZ() - change); } // Step 2d: Add Strafe controls if (keycode == KeyCode.A) { camera.setTranslateX(camera.getTranslateX() - change); } if (keycode == KeyCode.D) { camera.setTranslateX(camera.getTranslateX() + change); } }); imageView.setOnScroll( (ScrollEvent event) -> { event.consume(); if (event.getDeltaY() == 0) { return; } double change = event.getDeltaY(); // Add shift modifier to simulate "Running Speed" if (event.isShiftDown()) { change *= 2; } camera.setTranslateZ(camera.getTranslateZ() + change); }); imageView.setOnMousePressed( (MouseEvent me) -> { mousePosX = me.getSceneX(); mousePosY = me.getSceneY(); mouseOldX = me.getSceneX(); mouseOldY = me.getSceneY(); }); imageView.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()) { camera.ry.setAngle( ((camera.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // + camera.rx.setAngle( ((camera.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // - } else if (me.isSecondaryButtonDown()) { double z = camera.getTranslateZ(); double newZ = z + mouseDeltaX * modifierFactor * modifier; camera.setTranslateZ(newZ); } else if (me.isMiddleButtonDown()) { camera.t.setX(camera.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3); // - camera.t.setY(camera.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3); // - } }); } else { imageView.setOnMouseDragged(null); imageView.setOnScroll(null); imageView.setOnMousePressed(null); imageView.setOnKeyPressed(null); imageView.setMouseTransparent(true); } }