Search in sources :

Example 16 with KeyValue

use of javafx.animation.KeyValue in project Media-Library by The-Rain-Goddess.

the class ApplicationWindow method updatePlayer.

private void updatePlayer() {
    timeSlider.valueProperty().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable ov) {
            if (timeSlider.isValueChanging()) {
                //Duration duration = player.getCurrentTime();
                Duration duration = player.getMedia().getDuration();
                if (duration != null) {
                    player.seek(duration.multiply(timeSlider.getValue() / 100.0));
                }
                updateValues();
            }
        }
    });
    player.setOnReady(() -> {
        duration = player.getMedia().getDuration();
        updateValues();
    });
    player.currentTimeProperty().addListener(new ChangeListener<Duration>() {

        @Override
        public void changed(ObservableValue<? extends Duration> arg0, Duration arg1, Duration arg2) {
            updateValues();
        }
    });
    player.volumeProperty().bindBidirectional(volumeSlider.valueProperty());
    Image PlayButtonImage = new Image("com/negativevr/media_library/res/play.png");
    Image PauseButtonImage = new Image("com/negativevr/media_library/res/pause.png");
    ImageView imageViewPlay = new ImageView(PlayButtonImage);
    ImageView imageViewPause = new ImageView(PauseButtonImage);
    play.setGraphic(imageViewPause);
    play.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {
            updateValues();
            Status status = player.getStatus();
            if (status == Status.PAUSED || status == Status.READY || status == Status.UNKNOWN || status == Status.STOPPED) {
                player.play();
                play.setGraphic(imageViewPause);
            } else {
                player.pause();
                play.setGraphic(imageViewPlay);
            }
        }
    });
    player.setOnEndOfMedia(() -> {
        play.setGraphic(imageViewPlay);
        if (status == MediaStatus.REPEAT_NONE) {
            player.seek(new Duration(0));
            player.pause();
            play.setGraphic(imageViewPlay);
        } else if (status == MediaStatus.REPEAT_SINGLE) {
            player.seek(new Duration(0));
            player.play();
            play.setGraphic(imageViewPause);
        }
    });
    reload.setOnAction((ActionEvent e) -> {
        player.seek(player.getStartTime());
    });
    skip.setOnAction((ActionEvent e) -> {
        player.seek(player.getStopTime());
    });
    //fade in time line
    final Timeline fadeInTimeline = new Timeline(new KeyFrame(FADE_DURATION, new KeyValue(player.volumeProperty(), 1.0)));
    //fade out timeline
    final Timeline fadeOutTimeline = new Timeline(new KeyFrame(FADE_DURATION, new KeyValue(player.volumeProperty(), 0.0)));
    //fade in button
    fadeIn = new Button("Fade In");
    fadeIn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            fadeInTimeline.play();
        }
    });
    fadeIn.setMaxWidth(Double.MAX_VALUE);
    //fade out button
    fadeOut = new Button("Fade Out");
    fadeOut.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            fadeOutTimeline.play();
        }
    });
    fadeOut.setMaxWidth(Double.MAX_VALUE);
}
Also used : Status(javafx.scene.media.MediaPlayer.Status) KeyValue(javafx.animation.KeyValue) ActionEvent(javafx.event.ActionEvent) Duration(javafx.util.Duration) Image(javafx.scene.image.Image) Observable(javafx.beans.Observable) Timeline(javafx.animation.Timeline) Button(javafx.scene.control.Button) MouseButton(javafx.scene.input.MouseButton) InvalidationListener(javafx.beans.InvalidationListener) KeyFrame(javafx.animation.KeyFrame) ImageView(javafx.scene.image.ImageView)

Example 17 with KeyValue

use of javafx.animation.KeyValue in project Media-Library by The-Rain-Goddess.

the class ApplicationWindow method setupMediaPlayer.

//private Media Player accessors / mutators
private HBox setupMediaPlayer() {
    HBox mediaSlot = new HBox();
    VBox timeControls = new VBox();
    VBox timeBox = new VBox();
    HBox mediaControlBox = new HBox();
    HBox searchBox = new HBox();
    HBox fadeBox = new HBox(5);
    VBox volumeControls = new VBox(10);
    if (Main.getMasterDataAsList().size() != 0) {
        Path path = Paths.get(Main.getMasterDataAsList().get(0).getLibraryFilePath());
        Media media = new Media(path.toFile().toURI().toString());
        player = new MediaPlayer(media);
    } else {
    //player = new MediaPlayer(new Media(Paths.get("src/com/negativevr/media_library/res/init.mp3").toFile().toURI().toString()));
    }
    //player.setAutoPlay(true);
    MediaView mediaView = new MediaView();
    mediaView.setMediaPlayer(player);
    Image PlayButtonImage = new Image("com/negativevr/media_library/res/play.png");
    Image PauseButtonImage = new Image("com/negativevr/media_library/res/pause.png");
    ImageView imageViewPlay = new ImageView(PlayButtonImage);
    ImageView imageViewPause = new ImageView(PauseButtonImage);
    play = new Button();
    play.setGraphic(imageViewPlay);
    play.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {
            updateValues();
            Status status = player.getStatus();
            if (status == Status.PAUSED || status == Status.READY || status == Status.UNKNOWN || status == Status.STOPPED) {
                player.play();
                play.setGraphic(imageViewPause);
            } else {
                player.pause();
                play.setGraphic(imageViewPlay);
            }
        }
    });
    reload = new Button();
    reload.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/reload.png")));
    reload.setOnAction((ActionEvent e) -> {
        player.seek(player.getStartTime());
    });
    skip = new Button();
    skip.setGraphic((new ImageView(new Image("com/negativevr/media_library/res/skip.png"))));
    skip.setOnAction((ActionEvent e) -> {
        player.seek(player.getStopTime());
    });
    previous = new Button();
    previous.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/previous.png")));
    next = new Button();
    next.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/next.png")));
    Button repeat = new Button();
    if (status == MediaStatus.REPEAT_NONE)
        repeat.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/repeat_none.png")));
    else if (status == MediaStatus.REPEAT_SINGLE)
        repeat.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/repeat_single.png")));
    repeat.setOnAction((ActionEvent e) -> {
        if (status == MediaStatus.REPEAT_SINGLE) {
            status = MediaStatus.REPEAT_NONE;
            repeat.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/repeat_none.png")));
        } else if (status == MediaStatus.REPEAT_NONE) {
            status = MediaStatus.REPEAT_SINGLE;
            repeat.setGraphic(new ImageView(new Image("com/negativevr/media_library/res/repeat_single.png")));
        }
    });
    timeSlider = new Slider();
    HBox.setHgrow(timeSlider, Priority.ALWAYS);
    timeSlider.setMinSize(100, 50);
    timeSlider.valueProperty().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable ov) {
            if (timeSlider.isValueChanging()) {
                Duration duration = player.getMedia().getDuration();
                if (duration != null) {
                    player.seek(duration.multiply(timeSlider.getValue() / 100.0));
                }
                updateValues();
            }
        }
    });
    List<MediaFile> data = Main.getMasterDataAsList();
    if (data.size() != 0) {
        artistLabel = new Label(data.get(0).getArtistName() + " - " + data.get(0).getAlbumName());
        songLabel = new Label(data.get(0).getSongName());
    } else {
        artistLabel = new Label();
        songLabel = new Label();
    }
    player.currentTimeProperty().addListener(new InvalidationListener() {

        @Override
        public void invalidated(Observable ov) {
            updateValues();
        }
    });
    player.currentTimeProperty().addListener(new ChangeListener<Duration>() {

        @Override
        public void changed(ObservableValue<? extends Duration> arg0, Duration arg1, Duration arg2) {
            updateValues();
        }
    });
    time = new Label();
    time.setTextFill(Color.BLACK);
    player.setOnReady(() -> {
        duration = player.getMedia().getDuration();
        updateValues();
    });
    //volume control slider
    volumeSlider = new Slider(0, 1, 0);
    player.volumeProperty().bindBidirectional(volumeSlider.valueProperty());
    player.setVolume(0.5);
    //fade in time line
    final Timeline fadeInTimeline = new Timeline(new KeyFrame(FADE_DURATION, new KeyValue(player.volumeProperty(), 1.0)));
    //fade out timeline
    final Timeline fadeOutTimeline = new Timeline(new KeyFrame(FADE_DURATION, new KeyValue(player.volumeProperty(), 0.0)));
    //fade in button
    fadeIn = new Button("Fade In");
    fadeIn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            fadeInTimeline.play();
        }
    });
    fadeIn.setMaxWidth(Double.MAX_VALUE);
    //fade out button
    fadeOut = new Button("Fade Out");
    fadeOut.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            fadeOutTimeline.play();
        }
    });
    fadeOut.setMaxWidth(Double.MAX_VALUE);
    player.setOnEndOfMedia(() -> {
        play.setGraphic(imageViewPlay);
        if (status == MediaStatus.REPEAT_NONE) {
            player.seek(new Duration(0));
            player.pause();
            play.setGraphic(imageViewPlay);
        } else if (status == MediaStatus.REPEAT_SINGLE) {
            player.seek(new Duration(0));
            player.play();
            play.setGraphic(imageViewPause);
        }
    });
    //volume cotrol box
    fadeBox.getChildren().addAll(fadeOut, fadeIn);
    fadeBox.setAlignment(Pos.CENTER);
    volumeControls.getChildren().setAll(new Label("Volume"), volumeSlider, fadeBox);
    volumeControls.setAlignment(Pos.CENTER);
    volumeControls.disableProperty().bind(Bindings.or(Bindings.equal(Timeline.Status.RUNNING, fadeInTimeline.statusProperty()), Bindings.equal(Timeline.Status.RUNNING, fadeOutTimeline.statusProperty())));
    timeControls.getChildren().addAll(songLabel, artistLabel, timeSlider);
    timeControls.setAlignment(Pos.CENTER);
    timeControls.setFillWidth(true);
    timeControls.setMinWidth(300);
    timeBox.getChildren().addAll(repeat, time);
    timeBox.setAlignment(Pos.CENTER);
    mediaControlBox.getChildren().addAll(previous, reload, play, skip, next);
    mediaControlBox.setAlignment(Pos.CENTER);
    searchBox.getChildren().addAll(new Label("Search"), search);
    searchBox.setAlignment(Pos.CENTER_RIGHT);
    mediaSlot.getChildren().addAll(mediaControlBox, timeBox, timeControls, volumeControls, mediaView, searchBox);
    mediaSlot.setSpacing(10);
    HBox.setHgrow(timeControls, Priority.ALWAYS);
    return mediaSlot;
}
Also used : HBox(javafx.scene.layout.HBox) KeyValue(javafx.animation.KeyValue) Slider(javafx.scene.control.Slider) ActionEvent(javafx.event.ActionEvent) Label(javafx.scene.control.Label) Image(javafx.scene.image.Image) MediaView(javafx.scene.media.MediaView) Button(javafx.scene.control.Button) MouseButton(javafx.scene.input.MouseButton) ImageView(javafx.scene.image.ImageView) Path(java.nio.file.Path) Status(javafx.scene.media.MediaPlayer.Status) MediaFile(com.negativevr.media_library.files.MediaFile) Media(javafx.scene.media.Media) Duration(javafx.util.Duration) Observable(javafx.beans.Observable) Timeline(javafx.animation.Timeline) InvalidationListener(javafx.beans.InvalidationListener) KeyFrame(javafx.animation.KeyFrame) VBox(javafx.scene.layout.VBox) MediaPlayer(javafx.scene.media.MediaPlayer)

Example 18 with KeyValue

use of javafx.animation.KeyValue in project FXyzLib by Birdasaur.

the class CameraViewTest method loadSubScene.

private void loadSubScene() throws NonInvertibleTransformException {
    camera = new PerspectiveCamera(true);
    cameraTransform.setTranslate(0, 0, -500);
    cameraTransform.getChildren().add(camera);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setTranslateZ(-1500);
    cameraTransform.ry.setAngle(-45.0);
    cameraTransform.rx.setAngle(-10.0);
    //add a Point Light for better viewing of the grid coordinate system
    PointLight light = new PointLight(Color.WHITE);
    cameraTransform.getChildren().add(light);
    light.setTranslateX(camera.getTranslateX());
    light.setTranslateY(camera.getTranslateY());
    light.setTranslateZ(camera.getTranslateZ());
    worldRoot.getChildren().add(cameraTransform);
    SubScene scene = new SubScene(worldRoot, 800, 600, true, SceneAntialiasing.BALANCED);
    scene.setFill(Color.DARKSLATEGRAY);
    scene.setCamera(camera);
    //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();
    });
    scene.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);
        }
    });
    root.getChildren().add(scene);
    scene.widthProperty().bind(root.widthProperty());
    scene.heightProperty().bind(root.heightProperty());
    cameraView = new CameraView(scene);
    cameraView.setFirstPersonNavigationEabled(true);
    cameraView.setFitWidth(350);
    cameraView.setFitHeight(225);
    cameraView.getRx().setAngle(-45);
    cameraView.getT().setZ(-1500);
    cameraView.getT().setY(-500);
    root.getChildren().add(cameraView);
    StackPane.setAlignment(cameraView, Pos.BOTTOM_RIGHT);
    StackPane.setMargin(cameraView, new Insets(5));
    //Add an aritrary object to scene
    int rDivs = 32, tDivs = 32;
    double rad = 600, trad = 400;
    TorusMesh torus = new TorusMesh(rDivs, tDivs, rad, trad);
    torus.setDrawMode(DrawMode.LINE);
    PhongMaterial mat = new PhongMaterial(Color.BLUEVIOLET);
    torus.setMaterial(mat);
    torus.setTranslateX(0);
    torus.setTranslateY(0);
    torus.setTranslateZ(0);
    worldRoot.getChildren().add(torus);
    final Timeline t = new Timeline();
    t.getKeyFrames().addAll(new KeyFrame[] { new KeyFrame(Duration.seconds(5), new KeyValue[] { // Frame End                
    new KeyValue(torus.tubeStartAngleOffsetProperty(), torus.getTubeStartAngleOffset() - 10, Interpolator.EASE_BOTH), new KeyValue(torus.xOffsetProperty(), torus.getxOffset() + 0.5, Interpolator.EASE_BOTH), new KeyValue(torus.yOffsetProperty(), torus.getyOffset() + 0.5, Interpolator.EASE_BOTH), new KeyValue(torus.zOffsetProperty(), torus.getzOffset() + 2, Interpolator.EASE_BOTH), new KeyValue(torus.tubeDivisionsProperty(), 120, Interpolator.EASE_BOTH), new KeyValue(torus.radiusDivisionsProperty(), 120, Interpolator.EASE_BOTH), new KeyValue(torus.tubeRadiusProperty(), 400, Interpolator.EASE_BOTH) }) });
    t.setCycleCount(Animation.INDEFINITE);
    t.setAutoReverse(true);
    t.playFromStart();
}
Also used : MouseEvent(javafx.scene.input.MouseEvent) Insets(javafx.geometry.Insets) KeyValue(javafx.animation.KeyValue) PerspectiveCamera(javafx.scene.PerspectiveCamera) CameraView(org.fxyz.tools.CameraView) Timeline(javafx.animation.Timeline) SubScene(javafx.scene.SubScene) KeyFrame(javafx.animation.KeyFrame) KeyCode(javafx.scene.input.KeyCode) PointLight(javafx.scene.PointLight) PhongMaterial(javafx.scene.paint.PhongMaterial) TorusMesh(org.fxyz.shapes.primitives.TorusMesh)

Example 19 with KeyValue

use of javafx.animation.KeyValue in project FXyzLib by Birdasaur.

the class IcosahedronTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    Group sceneRoot = new Group();
    Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
    //        scene.setFill(Color.BLACK);
    camera = new PerspectiveCamera(true);
    //setup camera transform for rotational support
    cameraTransform.setTranslate(0, 0, 0);
    cameraTransform.getChildren().add(camera);
    camera.setNearClip(0.001);
    camera.setFarClip(10000.0);
    camera.setTranslateZ(-10);
    cameraTransform.ry.setAngle(-45.0);
    cameraTransform.rx.setAngle(-10.0);
    //add a Point Light for better viewing of the grid coordinate system
    //        PointLight light = new PointLight(Color.WHITE);
    //        cameraTransform.getChildren().add(light);
    //        light.setTranslateX(camera.getTranslateX());
    //        light.setTranslateY(camera.getTranslateY());
    //        light.setTranslateZ(camera.getTranslateZ());        
    scene.setCamera(camera);
    rotateY = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
    Group group = new Group();
    group.getChildren().add(cameraTransform);
    ico = new IcosahedronMesh(5, 1f);
    //                ico.setDrawMode(DrawMode.LINE);
    ico.setCullFace(CullFace.NONE);
    // NONE
    //        ico.setTextureModeNone(Color.ROYALBLUE);
    // IMAGE
    //        ico.setTextureModeImage(getClass().getResource("res/0ZKMx.png").toExternalForm());
    // PATTERN
    //        ico.setTextureModePattern(2d);
    // DENSITY
    ico.setTextureModeVertices3D(1530, dens);
    // FACES
    //        ico.setTextureModeFaces(256);
    ico.getTransforms().addAll(new Rotate(30, Rotate.X_AXIS), rotateY);
    group.getChildren().add(ico);
    sceneRoot.getChildren().addAll(group);
    //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();
    });
    scene.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);
        }
    });
    lastEffect = System.nanoTime();
    AtomicInteger count = new AtomicInteger();
    AnimationTimer timerEffect = new AnimationTimer() {

        @Override
        public void handle(long now) {
            if (now > lastEffect + 100_000_000l) {
                double cont1 = 0.1 + (count.get() % 60) / 10d;
                double cont2 = 0.1 + (count.getAndIncrement() % 30) / 10d;
                //                    dens = p->(float)(3d*Math.pow(Math.sin(p.phi),2)*Math.pow(Math.abs(Math.cos(p.theta)),cont1)+
                //                            Math.pow(Math.cos(p.phi),2)*Math.pow(Math.abs(Math.sin(p.theta)),cont2));
                //dens = p->10*cont1*Math.pow(Math.abs(p.x),cont1)*Math.pow(Math.abs(p.y),cont2)*Math.pow(p.z,2);
                double t = count.getAndIncrement() % 10;
                dens = p -> (double) (p.x + t) * (p.y + t) * (p.z + t);
                ico.setDensity(dens);
                //                    ico.setColors((int)Math.pow(2,count.get()%16));
                //                    ico.setLevel(count.get()%8);
                //                    ico.setDiameter(0.5f+10*(float)cont1);
                lastEffect = now;
            }
        }
    };
    Timeline timeline = new Timeline();
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(50), new KeyValue(rotateY.angleProperty(), 360)));
    primaryStage.setTitle("F(X)yz - Moving Contour Plot Test");
    primaryStage.setScene(scene);
    primaryStage.show();
    timerEffect.start();
//        timeline.play();
}
Also used : Group(javafx.scene.Group) MouseEvent(javafx.scene.input.MouseEvent) KeyValue(javafx.animation.KeyValue) Rotate(javafx.scene.transform.Rotate) AnimationTimer(javafx.animation.AnimationTimer) PerspectiveCamera(javafx.scene.PerspectiveCamera) Scene(javafx.scene.Scene) Timeline(javafx.animation.Timeline) IcosahedronMesh(org.fxyz.shapes.primitives.IcosahedronMesh) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyFrame(javafx.animation.KeyFrame) KeyCode(javafx.scene.input.KeyCode)

Example 20 with KeyValue

use of javafx.animation.KeyValue in project FXyzLib by Birdasaur.

the class Text3DTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    Group sceneRoot = new Group();
    Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
    scene.setFill(Color.BLACK);
    camera = new PerspectiveCamera(true);
    //setup camera transform for rotational support
    cameraTransform.setTranslate(0, 0, 0);
    cameraTransform.getChildren().add(camera);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setTranslateX(800);
    camera.setTranslateZ(-3000);
    //        cameraTransform.ry.setAngle(-25.0);
    cameraTransform.rx.setAngle(10.0);
    //add a Point Light for better viewing of the grid coordinate system
    //        PointLight light1 = new PointLight(Color.WHITE);
    cameraTransform.getChildren().add(new AmbientLight());
    scene.setCamera(camera);
    Group group = new Group(cameraTransform);
    Text3DMesh letters = new Text3DMesh("3DMesh", "Gadugi Bold", 400, true, 120, 0, 1);
    //        letters.setDrawMode(DrawMode.LINE);
    // NONE
    //        letters.setTextureModeNone(Color.ROYALBLUE);
    // IMAGE
    //        letters.setTextureModeImage(getClass().getResource("res/steel-background1.jpg").toExternalForm());
    //        letters.setTextureModeImage(getClass().getResource("res/marvel1.jpg").toExternalForm());
    // DENSITY
    letters.setTextureModeVertices3D(1530, p -> p.magnitude());
    //        letters.setTextureModeVertices3D(1530,p->Math.sin(p.y/50)*Math.cos(p.x/40)*p.z);
    // FACES
    //        letters.setTextureModeFaces(Palette.ColorPalette.HSB,16);
    group.getChildren().add(letters);
    sceneRoot.getChildren().addAll(group);
    //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();
    });
    scene.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);
        }
    });
    primaryStage.setTitle("F(X)yz - Text3D");
    primaryStage.setScene(scene);
    primaryStage.show();
    // Letter transformations
    IntStream.range(0, letters.getChildren().size()).forEach(i -> {
        double y = (((double) i) / ((double) letters.getChildren().size()) * 3d * Math.PI);
        ((TexturedMesh) (letters.getChildren().get(i))).getTranslate().setY(100d * Math.sin(y));
    });
    // Letter animations
    final Timeline rotateEffect1 = new Timeline();
    rotateEffect1.setCycleCount(Timeline.INDEFINITE);
    TexturedMesh t0 = letters.getMeshFromLetter("M");
    final KeyValue kv1 = new KeyValue(t0.getRotateY().angleProperty(), 360);
    final KeyFrame kf1 = new KeyFrame(Duration.millis(3000), kv1);
    rotateEffect1.getKeyFrames().addAll(kf1);
    rotateEffect1.play();
    final Timeline rotateEffect2 = new Timeline();
    TexturedMesh t1 = letters.getMeshFromLetter("3");
    final KeyValue kv2 = new KeyValue(t1.getRotateX().angleProperty(), 360);
    final KeyFrame kf2 = new KeyFrame(Duration.millis(2000), kv2);
    rotateEffect2.getKeyFrames().addAll(kf2);
    rotateEffect2.play();
    //        
    final Timeline rotateEffect3 = new Timeline();
    rotateEffect3.setCycleCount(Timeline.INDEFINITE);
    TexturedMesh t5 = letters.getMeshFromLetter("h");
    final KeyValue kv1x = new KeyValue(t5.getScale().xProperty(), 1.2, Interpolator.EASE_BOTH);
    final KeyValue kv1y = new KeyValue(t5.getScale().yProperty(), 1.2, Interpolator.EASE_BOTH);
    final KeyValue kv1z = new KeyValue(t5.getScale().zProperty(), 1.2, Interpolator.EASE_BOTH);
    final KeyFrame kfs1 = new KeyFrame(Duration.millis(500), kv1x, kv1y, kv1z);
    final KeyValue kv2x = new KeyValue(t5.getScale().xProperty(), 0.3, Interpolator.EASE_BOTH);
    final KeyValue kv2y = new KeyValue(t5.getScale().yProperty(), 0.3, Interpolator.EASE_BOTH);
    final KeyValue kv2z = new KeyValue(t5.getScale().zProperty(), 0.3, Interpolator.EASE_BOTH);
    final KeyFrame kfs2 = new KeyFrame(Duration.millis(2000), kv2x, kv2y, kv2z);
    final KeyValue kv3x = new KeyValue(t5.getScale().xProperty(), 1, Interpolator.EASE_BOTH);
    final KeyValue kv3y = new KeyValue(t5.getScale().yProperty(), 1, Interpolator.EASE_BOTH);
    final KeyValue kv3z = new KeyValue(t5.getScale().zProperty(), 1, Interpolator.EASE_BOTH);
    final KeyFrame kfs3 = new KeyFrame(Duration.millis(3000), kv3x, kv3y, kv3z);
    rotateEffect3.getKeyFrames().addAll(kfs1, kfs2, kfs3);
    rotateEffect3.play();
    lastEffect = System.nanoTime();
    AtomicInteger count = new AtomicInteger(1);
    AnimationTimer timerEffect = new AnimationTimer() {

        @Override
        public void handle(long now) {
            if (now > lastEffect + 1_000_000_000l) {
                System.out.println("*** " + count.get());
                letters.setFont(Font.getFontNames().get(count.get() % Font.getFontNames().size()));
                if (count.get() % 10 < 2) {
                    letters.setTextureModeNone(Color.hsb(count.get() % 360, 1, 1));
                } else if (count.get() % 10 < 4) {
                    letters.setTextureModeImage(getClass().getResource("res/steel-background1.jpg").toExternalForm());
                } else if (count.get() % 10 < 8) {
                    letters.setTextureModeVertices3D(1530, p -> (double) (p.y / (20 + count.get() % 20)) * (p.x / (10 + count.get() % 10)));
                } else {
                    letters.setTextureModeFaces(1530);
                }
                if (count.get() % 20 > 15) {
                    letters.setDrawMode(DrawMode.LINE);
                } else {
                    letters.setDrawMode(DrawMode.FILL);
                }
                letters.getChildren().forEach(m -> ((TexturedMesh) m).setDensity(p -> p.magnitude() * Math.cos(p.y / 100d * (count.get() % 5))));
                count.getAndIncrement();
                lastEffect = now;
            }
        }
    };
    //        timerEffect.start();
    OBJWriter writer = new OBJWriter((TriangleMesh) ((TexturedMesh) (letters.getChildren().get(0))).getMesh(), "letter");
    writer.setTextureColors(6);
    //writer.setTexturePattern();
    //        writer.setTextureImage(getClass().getResource("res/LaminateSteel.jpg").toExternalForm());
    writer.exportMesh();
}
Also used : IntStream(java.util.stream.IntStream) Scene(javafx.scene.Scene) Arrays(java.util.Arrays) Palette(org.fxyz.utils.Palette) MouseEvent(javafx.scene.input.MouseEvent) PointLight(javafx.scene.PointLight) VBox(javafx.scene.layout.VBox) Function(java.util.function.Function) TriangleMesh(javafx.scene.shape.TriangleMesh) ArrayList(java.util.ArrayList) Application(javafx.application.Application) PerspectiveCamera(javafx.scene.PerspectiveCamera) CameraTransformer(org.fxyz.cameras.CameraTransformer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TexturedMesh(org.fxyz.shapes.primitives.TexturedMesh) ColorPalette(org.fxyz.utils.Palette.ColorPalette) Patterns(org.fxyz.utils.Patterns) KeyValue(javafx.animation.KeyValue) KeyCode(javafx.scene.input.KeyCode) Color(javafx.scene.paint.Color) KeyFrame(javafx.animation.KeyFrame) CuboidMesh(org.fxyz.shapes.primitives.CuboidMesh) Text3DMesh(org.fxyz.shapes.primitives.Text3DMesh) Font(javafx.scene.text.Font) Timeline(javafx.animation.Timeline) Group(javafx.scene.Group) DrawMode(javafx.scene.shape.DrawMode) AnimationTimer(javafx.animation.AnimationTimer) List(java.util.List) Duration(javafx.util.Duration) OBJWriter(org.fxyz.utils.OBJWriter) Interpolator(javafx.animation.Interpolator) Stage(javafx.stage.Stage) SceneAntialiasing(javafx.scene.SceneAntialiasing) TriangulatedMesh(org.fxyz.shapes.primitives.TriangulatedMesh) Point3D(org.fxyz.geometry.Point3D) Bounds(javafx.geometry.Bounds) AmbientLight(javafx.scene.AmbientLight) Group(javafx.scene.Group) MouseEvent(javafx.scene.input.MouseEvent) KeyValue(javafx.animation.KeyValue) AnimationTimer(javafx.animation.AnimationTimer) Text3DMesh(org.fxyz.shapes.primitives.Text3DMesh) PerspectiveCamera(javafx.scene.PerspectiveCamera) Scene(javafx.scene.Scene) OBJWriter(org.fxyz.utils.OBJWriter) Timeline(javafx.animation.Timeline) TexturedMesh(org.fxyz.shapes.primitives.TexturedMesh) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyFrame(javafx.animation.KeyFrame) KeyCode(javafx.scene.input.KeyCode) AmbientLight(javafx.scene.AmbientLight)

Aggregations

KeyValue (javafx.animation.KeyValue)85 KeyFrame (javafx.animation.KeyFrame)83 Timeline (javafx.animation.Timeline)82 Interpolator (javafx.animation.Interpolator)12 Rotate (javafx.scene.transform.Rotate)12 Duration (javafx.util.Duration)12 Insets (javafx.geometry.Insets)11 MouseEvent (javafx.scene.input.MouseEvent)10 PerspectiveCamera (javafx.scene.PerspectiveCamera)9 ArrayList (java.util.ArrayList)8 Scene (javafx.scene.Scene)7 Label (javafx.scene.control.Label)7 Rectangle (javafx.scene.shape.Rectangle)7 ActionEvent (javafx.event.ActionEvent)6 EventHandler (javafx.event.EventHandler)6 Group (javafx.scene.Group)6 Node (javafx.scene.Node)6 KeyCode (javafx.scene.input.KeyCode)6 VBox (javafx.scene.layout.VBox)6 JFXButton (com.jfoenix.controls.JFXButton)5