Package de.lessvoid.nifty.tools.time

Source Code of de.lessvoid.nifty.tools.time.TimeInterpolator

package de.lessvoid.nifty.tools.time;

import java.util.Properties;
import java.util.logging.Logger;

import de.lessvoid.nifty.tools.TimeProvider;
import de.lessvoid.nifty.tools.time.interpolator.ExpTime;
import de.lessvoid.nifty.tools.time.interpolator.Interpolator;
import de.lessvoid.nifty.tools.time.interpolator.LinearTime;
import de.lessvoid.nifty.tools.time.interpolator.NullTime;
import de.lessvoid.nifty.tools.time.interpolator.OneTime;

/**
* TimeProvider class.
* @author void
*/
public class TimeInterpolator {

  /**
   * the logger.
   */
  private static Logger log = Logger.getLogger(TimeInterpolator.class.getName());

  /**
   * the time provider.
   */
  private TimeProvider timeProvider;

  /**
   * the InterpolatorProvider we use.
   */
  private Interpolator interpolatorProvider;

  /**
   * the current value [0,1].
   */
  private float value = 0.0f;

  /**
   * the start time in ms the effect starts.
   */
  private long startTime;

  /**
   * the maximum time in ms.
   */
  private long lengthParam;

  /**
   * start delay time in ms.
   */
  private long startDelayParam = 0;

  /**
   * initialize with the given parameters.
   *
   * @param parameter parameter props
   * @param newTimeProvider TimeProvider to use
   * @param infinite infinit effects never end
   */
  public TimeInterpolator(final Properties parameter, final TimeProvider newTimeProvider, final boolean infinite) {
    timeProvider = newTimeProvider;
    startDelayParam = Long.parseLong(parameter.getProperty("startDelay", "0"));

    String lengthDefault = null;
    if (infinite) {
      lengthDefault = "infinite";
    }

    interpolatorProvider = null;
    if ("infinite".equals(parameter.getProperty("length", lengthDefault))) {
      interpolatorProvider = new NullTime();
    } else {
      this.lengthParam = Long.parseLong(parameter.getProperty("length", "1000"));
      if (Boolean.parseBoolean(parameter.getProperty("oneShot"))) {
        interpolatorProvider = new OneTime();
      }
    }

    // check for the given timeType to create the appropriate interpolator
    if (interpolatorProvider == null) {
      String timeType = parameter.getProperty("timeType", "linear");
      if (timeType.equals("infinite")) {
        interpolatorProvider = new NullTime();
      } else if (timeType.equals("linear")) {
        interpolatorProvider = new LinearTime();
      } else if (timeType.equals("exp")) {
        interpolatorProvider = new ExpTime();
      } else {
        log.warning(timeType + " is not supported, using NullTime for fallback. probably not what you want...");
        interpolatorProvider = new NullTime();
      }
    }

    // initialize the provider
    interpolatorProvider.initialize(parameter);
  }

  /**
   * start the interpolation.
   */
  public void start() {
    interpolatorProvider.start();
    value = 0.0f;
    startTime = timeProvider.getMsTime() + startDelayParam;
  }

  /**
   * update the value.
   * @return true when still active and false when done
   */
  public final boolean update() {
    long now = timeProvider.getMsTime();
    long timePassed = now - startTime;

    if (timePassed < 0) {
      return true;
    }

    this.value = interpolatorProvider.getValue(lengthParam, timePassed);

    if (this.value > 1.0f) {
      this.value = 1.0f;
      return false;
    } else {
      return true;
    }
  }

  /**
   * get the current value [0.0, 1.0].
   * @return the current value
   */
  public final float getValue() {
    return value;
  }
}
TOP

Related Classes of de.lessvoid.nifty.tools.time.TimeInterpolator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.