Beispiel #1
0
  public Cutaway(SubScene scene, double width, double height) {
    // Make sure "world" is a group
    assert scene.getRoot().getClass().equals(Group.class);

    setFillWidth(true);
    setPrefSize(width, height + controlSize);
    setMaxSize(width, height + controlSize);
    this.setBorder(
        new Border(
            new BorderStroke(
                Color.DARKKHAKI,
                BorderStrokeStyle.SOLID,
                new CornerRadii(25),
                new BorderWidths(5))));

    Rectangle closeSquare = new Rectangle(controlSize, controlSize, Color.LIGHTCORAL);
    closeSquare.setOnMouseClicked(
        (MouseEvent me) -> {
          this.fireEvent(new CloseCutawayEvent(this));
        });
    Circle dragCircle = new Circle(controlSize / 2, Color.STEELBLUE);

    HBox top = new HBox(20, closeSquare, dragCircle);
    dragCircle.setVisible(false);
    StackPane.setAlignment(closeSquare, Pos.TOP_RIGHT);
    StackPane.setMargin(closeSquare, new Insets(50));

    top.setBackground(
        new Background(
            new BackgroundFill(Color.KHAKI, new CornerRadii(20, 20, 0, 0, false), Insets.EMPTY)));

    top.setAlignment(Pos.TOP_RIGHT);

    top.setOnMousePressed(
        (MouseEvent me) -> {
          cutawayPosX = me.getSceneX();
          cutawayPosY = me.getSceneY();
          cutawayOldX = me.getSceneX();
          cutawayOldY = me.getSceneY();
        });
    top.setOnMouseDragged(
        (MouseEvent me) -> {
          cutawayOldX = cutawayPosX;
          cutawayOldY = cutawayPosY;
          cutawayPosX = me.getSceneX();
          cutawayPosY = me.getSceneY();
          cutawayDeltaX = (cutawayPosX - cutawayOldX);
          cutawayDeltaY = (cutawayPosY - cutawayOldY);
          setTranslateX(getTranslateX() + cutawayDeltaX);
          setTranslateY(getTranslateY() + cutawayDeltaY);
        });

    getChildren().addAll(top, imageView);

    worldToView = (Group) scene.getRoot();

    camera.setNearClip(0.1);
    camera.setFarClip(15000.0);
    camera.setTranslateZ(-1500);
    params.setCamera(camera);

    params.setDepthBuffer(true);
    params.setFill(Color.rgb(0, 0, 0, 0.5));

    viewTimer =
        new AnimationTimer() {
          @Override
          public void handle(long now) {
            redraw();
          }
        };
    setOnMouseEntered(
        e -> {
          requestFocus();
        });
  }
Beispiel #2
0
  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);
    }
  }