package de.lessvoid.nifty.effects.impl;


import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.effects.EffectImpl;
import de.lessvoid.nifty.effects.EffectProperties;
import de.lessvoid.nifty.effects.Falloff;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.render.NiftyRenderEngine;
import de.lessvoid.nifty.spi.sound.SoundHandle;
import de.lessvoid.nifty.tools.SizeValue;

/**
 * Play a sound.
 * @author void
 */
public class PlaySound implements EffectImpl {
  private boolean done;
  private boolean repeat;
  private SizeValue volume;
  private SoundHandle soundHandle;

  public void activate(final Nifty nifty, final Element element, final EffectProperties parameter) {
    soundHandle = nifty.getSoundSystem().getSound(parameter.getProperty("sound"));
    repeat = Boolean.valueOf(parameter.getProperty("repeat", "false"));
    volume = new SizeValue(parameter.getProperty("volume", "100%"));
    done = false;
  }

  public void execute(
      final Element element,
      final float normalizedTime,
      final Falloff falloff,
      final NiftyRenderEngine r) {
    if (normalizedTime > 0.0f) {
      if (soundHandle != null) {
        if (!done) {
          playSound();
          done = true;
        } else {
          // in repeat mode, when the sound is not playing anymore we'll simply start it again
          if (repeat) {
            if (!soundHandle.isPlaying()) {
              playSound();
            }
          }
        }
      }
    }
  }

  private void playSound() {
    soundHandle.play();
    soundHandle.setVolume(volume.getValue(1.0f));
  }

  public void deactivate() {
  }
}

@@ -36,18 +36,16 @@
 mpl;%0D%0A%0D%0A
-%0D%0A
 import d
@@ -358,51 +358,8 @@
 le;%0D
-%0Aimport de.lessvoid.nifty.tools.SizeValue;%0D
 %0A%0D%0A/
@@ -506,37 +506,8 @@
 t;%0D%0A
-  private SizeValue volume;%0D%0A
   pr
@@ -537,16 +537,40 @@
 Handle;%0D
+%0A  private Nifty nifty;%0D
 %0A%0D%0A  pub
@@ -662,24 +662,49 @@
 rameter) %7B%0D%0A
+    this.nifty = nifty;%0D%0A
     soundHan
@@ -852,78 +852,8 @@
 );%0D%0A
-    volume = new SizeValue(parameter.getProperty(%22volume%22, %22100%25%22));%0D%0A
     
@@ -1526,28 +1526,46 @@
 ume(
-volume.getVa
+nifty.getSoundSystem().getMusicVo
 lu
+m
 e(
-1.0f
 ));%0D

work in progress changes to the PlaySound effect - not yet happy with it tho