package de.lessvoid.nifty.tools.pulsate.provider;

import de.lessvoid.nifty.tools.pulsate.PulsatorProvider;

import javax.annotation.Nonnull;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * SinusPulsator.
 *
 * @author void
 */
public class SinusPulsator implements PulsatorProvider {
  /**
   * the logger.
   */
  @Nonnull
  private static final Logger log = Logger.getLogger(SinusPulsator.class.getName());

  /**
   * constant to half things.
   */
  private static final float HALF = 0.5f;

  /**
   * period for one full cycle in ms.
   */
  private float period;

  /**
   * start time.
   */
  private long startTime;

  /**
   * cycle (true) or one shot (false).
   */
  private boolean cycle = true;

  /**
   * Initialize the Pulsator.
   *
   * @param parameter the parameters
   */
  @Override
  public void initialize(@Nonnull final Properties parameter) {
    try {
      period = Float.parseFloat(parameter.getProperty("period", "1000"));
    } catch (NumberFormatException e) {
      log.log(Level.SEVERE, "Failed to parse \"period\" value.", e);
      period = 1000.f;
    }
    this.cycle = Boolean.parseBoolean(parameter.getProperty("cycle", Boolean.toString(true)));
  }

  /**
   * Get current value for the given time.
   *
   * @param msTime current time
   * @return the pulsate value in [0,1] interval
   */
  @Override
  public float getValue(final long msTime) {
    long t = msTime - startTime;
    if (cycle) {
      return getSinusValue(t);
    } else {
      if (t > period * HALF) {
        return 1.0f;
      } else {
        return getSinusValue(t);
      }
    }
  }

  /**
   * get scaled sinus.
   *
   * @param x input
   * @return sinus
   */
  private float getSinusValue(final long x) {
    return (float) Math.abs(Math.sin((Math.PI * x / period)));
  }

  /**
   * Reset.
   *
   * @param msTime current time
   */
  @Override
  public void reset(final long msTime) {
    this.startTime = msTime;
  }
}

@@ -795,24 +795,127 @@
  = true;%0D%0A%0D%0A
+  /**%0D%0A   * start the pulsator at 1 (true) or 0 (false).%0D%0A   */%0D%0A  private boolean reverse = false;%0D%0A%0D%0A
   /**%0D%0A   * 
@@ -936,24 +936,24 @@
  Pulsator.%0D%0A
-
    *%0D%0A   * @
@@ -1389,24 +1389,125 @@
 g(true)));%0D%0A
+    this.reverse = Boolean.parseBoolean(parameter.getProperty(%22reverse%22, Boolean.toString(false)));%0D%0A
   %7D%0D%0A%0D%0A  /**
@@ -1753,74 +1753,18 @@
 if (
+!
 cycle
-) %7B%0D%0A      return getSinusValue(t);%0D%0A    %7D else %7B%0D%0A      if (
+ && 
 t %3E 
@@ -1777,18 +1777,16 @@
  * HALF)
- %7B
 %0D%0A      
@@ -1797,36 +1797,44 @@
 turn
+ (reverse? 0.0f:
  1.0f
+)
 ;%0D%0A    
-  %7D else %7B%0D%0A    
+else%0D%0A%09
     
@@ -1859,26 +1859,49 @@
 ue(t
-);%0D%0A      %7D%0D%0A    %7D
+ + (reverse? (long)(period * HALF): 0l));
 %0D%0A  

Update SinusPulsator to accept "reverse" parameter