All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jfoenix.transitions.template.JFXAnimationTemplates Maven / Gradle / Ivy

There is a newer version: 9.0.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.jfoenix.transitions.template;

import com.jfoenix.transitions.JFXAnimationTimer;
import com.jfoenix.transitions.JFXKeyFrame;
import com.jfoenix.transitions.JFXKeyValue;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.value.WritableValue;
import javafx.event.ActionEvent;
import javafx.util.Duration;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Class which represents the specific animation implementations.
 *
 * @author Marcel Schlegel (schlegel11)
 * @version 1.0
 * @since 2018-09-22
 */
public class JFXAnimationTemplates {

  /**
   * The {@link Timeline} implementation which supports all {@link JFXAnimationTemplateAction} and
   * {@link JFXAnimationTemplateConfig} methods.
   */
  public static  Timeline buildTimeline(JFXAnimationTemplate creator) {

    Timeline timeline = new Timeline();
    JFXAnimationTemplateConfig creatorConfig = creator.buildAndGetTemplateConfig();

    creator
        .buildAndGetAnimationValues()
        .forEach(
            (percent, animationValues) -> {

              // calc the percentage duration of total duration.
              Duration percentageDuration = creatorConfig.getDuration().multiply((percent / 100));

              // Create the key values.
              KeyValue[] keyValues =
                  animationValues
                      .stream()
                      .flatMap(
                          animationValue ->
                              animationValue.mapTo(
                                  createKeyValue(creatorConfig.getInterpolator(), animationValue)))
                      .toArray(KeyValue[]::new);

              // Reduce the onFinish events to one consumer.
              Consumer onFinish =
                  animationValues
                      .stream()
                      .map(
                          animationValue ->
                              (Consumer)
                                  actionEvent -> {
                                    if (animationValue.isExecuted()) {
                                      animationValue.handleOnFinish(actionEvent);
                                      animationValue.addExecution(1);
                                    }
                                  })
                      .reduce(action -> {}, Consumer::andThen);

              KeyFrame keyFrame = new KeyFrame(percentageDuration, onFinish::accept, keyValues);
              timeline.getKeyFrames().add(keyFrame);
            });

    timeline.setAutoReverse(creatorConfig.isAutoReverse());
    timeline.setCycleCount(creatorConfig.getCycleCount());
    timeline.setDelay(creatorConfig.getDelay());
    timeline.setRate(creatorConfig.getRate());
    timeline.setOnFinished(creatorConfig::handleOnFinish);

    return timeline;
  }

  /**
   * The {@link JFXAnimationTimer} implementation which supports a subset of {@link
   * JFXAnimationTemplateAction} and {@link JFXAnimationTemplateAction} methods.
* Unsupported methods are:
* In {@link JFXAnimationTemplateAction}:
* - {@link JFXAnimationTemplateAction.Builder#executions(int)}
* - {@link JFXAnimationTemplateAction.Builder#onFinish(BiConsumer)}
* *

* *

In {@link JFXAnimationTemplateConfig}:
* - {@link JFXAnimationTemplateConfig.Builder#rate(double)}
* - {@link JFXAnimationTemplateConfig.Builder#delay(Duration)}
* - {@link JFXAnimationTemplateConfig.Builder#autoReverse(boolean)}
* - {@link JFXAnimationTemplateConfig.Builder#cycleCount(int)}
*/ public static JFXAnimationTimer buildAnimationTimer(JFXAnimationTemplate creator) { JFXAnimationTimer animationTimer = new JFXAnimationTimer(); JFXAnimationTemplateConfig creatorConfig = creator.buildAndGetTemplateConfig(); creator .buildAndGetAnimationValues() .forEach( (percent, animationValues) -> { // calc the percentage duration of total duration. Duration percentageDuration = creatorConfig.getDuration().multiply((percent / 100)); // Create the key values. JFXKeyValue[] keyValues = animationValues .stream() .flatMap( animationValue -> animationValue.mapTo( createJFXKeyValue( creatorConfig.getInterpolator(), animationValue))) .toArray(JFXKeyValue[]::new); JFXKeyFrame keyFrame = new JFXKeyFrame(percentageDuration, keyValues); try { animationTimer.addKeyFrame(keyFrame); } catch (Exception e) { // Nothing happens cause timer can't run at this point. } }); animationTimer.setOnFinished(() -> creatorConfig.handleOnFinish(new ActionEvent())); return animationTimer; } private static Function, KeyValue> createKeyValue( Interpolator globalInterpolator, JFXAnimationTemplateAction animationValue) { return (writableValue) -> { Interpolator interpolator = animationValue.getInterpolator().orElse(globalInterpolator); return new KeyValue( writableValue, animationValue.getEndValue(), new ConditionalInterpolator(interpolator, writableValue, animationValue::isExecuted)); }; } private static Function, JFXKeyValue> createJFXKeyValue( Interpolator globalInterpolator, JFXAnimationTemplateAction animationValue) { return (writableValue) -> { Interpolator interpolator = animationValue.getInterpolator().orElse(globalInterpolator); return JFXKeyValue.builder() .setTarget(writableValue) .setEndValue(animationValue.getEndValue()) .setInterpolator(interpolator) .setAnimateCondition(animationValue::isExecuteWhen) .build(); }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy