Search in sources :

Example 1 with ScrollEvent

use of javafx.scene.input.ScrollEvent in project FXyzLib by Birdasaur.

the class Cutaway method setFirstPersonNavigationEabled.

public void setFirstPersonNavigationEabled(boolean b) {
    if (b) {
        // Navigation
        setMouseTransparent(false);
        //First person shooter keyboard movement
        imageView.setOnKeyPressed(event -> {
            double change = 10.0;
            if (event.isShiftDown()) {
                change = 50.0;
            }
            KeyCode keycode = event.getCode();
            if (keycode == KeyCode.W) {
                camera.setTranslateZ(camera.getTranslateZ() + change);
            }
            if (keycode == KeyCode.S) {
                camera.setTranslateZ(camera.getTranslateZ() - change);
            }
            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()) {
                // +
                cameraTransform.ry.setAngle(((cameraTransform.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
                // -                
                cameraTransform.rx.setAngle(((cameraTransform.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()) {
                // -
                cameraTransform.t.setX(cameraTransform.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3);
                // -
                cameraTransform.t.setY(cameraTransform.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3);
            }
        });
    } else {
        imageView.setOnMouseDragged(null);
        imageView.setOnScroll(null);
        imageView.setOnMousePressed(null);
        imageView.setOnKeyPressed(null);
        imageView.setMouseTransparent(true);
    }
}
Also used : MouseEvent(javafx.scene.input.MouseEvent) ScrollEvent(javafx.scene.input.ScrollEvent) KeyCode(javafx.scene.input.KeyCode)

Aggregations

KeyCode (javafx.scene.input.KeyCode)1 MouseEvent (javafx.scene.input.MouseEvent)1 ScrollEvent (javafx.scene.input.ScrollEvent)1