Search in sources :

Example 1 with PickResult

use of javafx.scene.input.PickResult in project Gargoyle by callakrsos.

the class Drag3DObject method loadControls.

private void loadControls(Scene scene) {
    //First person shooter keyboard movement  
    scene.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);
        }
    });
    scene.setOnMousePressed((MouseEvent me) -> {
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseOldX = me.getSceneX();
        mouseOldY = me.getSceneY();
        PickResult pr = me.getPickResult();
        if (pr != null && pr.getIntersectedNode() != null && pr.getIntersectedNode() instanceof Sphere) {
            distance = pr.getIntersectedDistance();
            s = (Sphere) pr.getIntersectedNode();
            isPicking = true;
            vecIni = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
        }
    });
    scene.setOnMouseDragged((MouseEvent me) -> {
        mouseOldX = mousePosX;
        mouseOldY = mousePosY;
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseDeltaX = (mousePosX - mouseOldX);
        mouseDeltaY = (mousePosY - mouseOldY);
        if (RUN_JASON) {
            //objPos += mouseMovementx*scale*RightAxis 
            if (isPicking) {
                if (mousePosX < mouseOldX) {
                    System.out.println("moving left");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() + (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                } else if (mousePosX > mouseOldX) {
                    System.err.println("moving right");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() - (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                }
                if (mousePosY < mouseOldY) {
                    System.out.println("moving up");
                } else if (mousePosY > mouseOldY) {
                    System.err.println("moving down");
                }
                return;
            }
        } else {
            if (isPicking) {
                double modifier = (me.isControlDown() ? 0.2 : me.isAltDown() ? 2.0 : 1) * (30d / camera.getFieldOfView());
                modifier *= (30d / camera.getFieldOfView());
                vecPos = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
                Point3D p = new Point3D(distance * (vecPos.x - vecIni.x), distance * (vecPos.y - vecIni.y), distance * (vecPos.z - vecIni.z));
                s.getTransforms().add(new Translate(modifier * p.getX(), 0, modifier * p.getZ()));
                vecIni = vecPos;
            } else {
                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);
                }
            }
        }
    });
    scene.setOnMouseReleased((MouseEvent me) -> {
        if (isPicking) {
            isPicking = false;
        }
    });
}
Also used : Sphere(javafx.scene.shape.Sphere) MouseEvent(javafx.scene.input.MouseEvent) Point3D(javafx.geometry.Point3D) PickResult(javafx.scene.input.PickResult) KeyCode(javafx.scene.input.KeyCode) Translate(javafx.scene.transform.Translate)

Example 2 with PickResult

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

the class Drag3DObject method loadControls.

private void loadControls(Scene scene) {
    //First person shooter keyboard movement 
    scene.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);
        }
    });
    scene.setOnMousePressed((MouseEvent me) -> {
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseOldX = me.getSceneX();
        mouseOldY = me.getSceneY();
        PickResult pr = me.getPickResult();
        if (pr != null && pr.getIntersectedNode() != null && pr.getIntersectedNode() instanceof Sphere) {
            distance = pr.getIntersectedDistance();
            s = (Sphere) pr.getIntersectedNode();
            isPicking = true;
            vecIni = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
        }
    });
    scene.setOnMouseDragged((MouseEvent me) -> {
        mouseOldX = mousePosX;
        mouseOldY = mousePosY;
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseDeltaX = (mousePosX - mouseOldX);
        mouseDeltaY = (mousePosY - mouseOldY);
        if (RUN_JASON) {
            //objPos += mouseMovementx*scale*RightAxis
            if (isPicking) {
                if (mousePosX < mouseOldX) {
                    System.out.println("moving left");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() + (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                } else if (mousePosX > mouseOldX) {
                    System.err.println("moving right");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() - (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                }
                if (mousePosY < mouseOldY) {
                    System.out.println("moving up");
                } else if (mousePosY > mouseOldY) {
                    System.err.println("moving down");
                }
                return;
            }
        } else {
            if (isPicking) {
                double modifier = (me.isControlDown() ? 0.2 : me.isAltDown() ? 2.0 : 1) * (30d / camera.getFieldOfView());
                modifier *= (30d / camera.getFieldOfView());
                vecPos = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
                Point3D p = new Point3D(distance * (vecPos.x - vecIni.x), distance * (vecPos.y - vecIni.y), distance * (vecPos.z - vecIni.z));
                s.getTransforms().add(new Translate(modifier * p.getX(), 0, modifier * p.getZ()));
                vecIni = vecPos;
            } else {
                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);
                }
            }
        }
    });
    scene.setOnMouseReleased((MouseEvent me) -> {
        if (isPicking) {
            isPicking = false;
        }
    });
}
Also used : Sphere(javafx.scene.shape.Sphere) MouseEvent(javafx.scene.input.MouseEvent) Point3D(javafx.geometry.Point3D) PickResult(javafx.scene.input.PickResult) KeyCode(javafx.scene.input.KeyCode) Translate(javafx.scene.transform.Translate)

Aggregations

Point3D (javafx.geometry.Point3D)2 KeyCode (javafx.scene.input.KeyCode)2 MouseEvent (javafx.scene.input.MouseEvent)2 PickResult (javafx.scene.input.PickResult)2 Sphere (javafx.scene.shape.Sphere)2 Translate (javafx.scene.transform.Translate)2