Advertisement
Guest User

ReversableAnimChannelControl

a guest
Mar 14th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. package sneak.util;
  2.  
  3. import com.jme3.animation.AnimChannel;
  4. import com.jme3.animation.AnimControl;
  5. import com.jme3.animation.LoopMode;
  6. import com.jme3.export.JmeExporter;
  7. import com.jme3.export.JmeImporter;
  8. import com.jme3.math.FastMath;
  9. import com.jme3.renderer.RenderManager;
  10. import com.jme3.renderer.ViewPort;
  11. import com.jme3.scene.Spatial;
  12. import com.jme3.scene.control.Control;
  13. import java.io.IOException;
  14.  
  15. /**
  16.  * Add this control to a spatial with an AnimControl. allows setting an animation to play in reverse by using hackery.
  17.  *
  18.  * to play an animation in reverse use a negative speed
  19.  *
  20.  * note: will not notify onAnimDone() to animaitonal listeners. though this could be added very easily. just choose your hack. reflection, or changing the package name.
  21.  *
  22.  * @author Daniel Strong
  23.  * @see http://hub.jmonkeyengine.org/forum/topic/imported-blend-file-only-retains-1-animation/
  24.  */
  25. public class ReversableAnimChannelControl implements Control {
  26.  
  27.         private final AnimChannel animChannel;
  28.         private final String animName0;
  29.         private LoopMode negativeLoopMode = null;
  30.         private float timer = 0;
  31.  
  32.         public ReversableAnimChannelControl(AnimControl animControl) {
  33.                 this(animControl.createChannel());
  34.         }
  35.  
  36.         public ReversableAnimChannelControl(AnimChannel animChannel) {
  37.                 this.animChannel = animChannel;
  38.                 animName0 = animChannel.getControl().getAnimationNames().iterator().next();
  39.         }
  40.  
  41.         @Override
  42.         public void update(float tpf) {
  43.                 if (!animChannel.getControl().isEnabled() || negativeLoopMode == null) {
  44.                         return;
  45.                 }
  46.  
  47.                 timer -= tpf;
  48.                 if (timer < 0) {
  49.                         switch (negativeLoopMode) {
  50.                                 case Loop:
  51.                                         animChannel.setTime(animChannel.getAnimMaxTime());
  52.                                         timer = animChannel.getAnimMaxTime();
  53.                                         break;
  54.                                 case DontLoop:
  55.                                         animChannel.reset(true);
  56.                                         negativeLoopMode = null;
  57.                                         break;
  58.                                 case Cycle:
  59.                                         break;
  60.                                 default:
  61.                                         throw new AssertionError(negativeLoopMode.name());
  62.  
  63.                         }
  64.                 }
  65.         }
  66.  
  67.         public AnimControl getAnimControl() {
  68.                 return animChannel.getControl();
  69.         }
  70.  
  71.         public AnimChannel getAnimChannel() {
  72.                 return animChannel;
  73.         }
  74.  
  75.         public void setAnim() {
  76.                 setAnim(LoopMode.DontLoop, 1);
  77.         }
  78.  
  79.         public void setAnim(LoopMode loopMode) {
  80.                 setAnim(loopMode, 1);
  81.         }
  82.  
  83.         public void setAnim(float speed) {
  84.                 setAnim(LoopMode.DontLoop, speed);
  85.         }
  86.  
  87.         public void setAnim(LoopMode loopMode, float speed) {
  88.                 setAnim(animName0, loopMode, speed);
  89.         }
  90.  
  91.         public void setAnim(String animName, LoopMode loopMode, float speed) {
  92.                 animChannel.setAnim(animName);
  93.                 if (speed > 0) {
  94.                         negativeLoopMode = null;
  95.                         animChannel.setLoopMode(loopMode);
  96.                         animChannel.setSpeed(speed);
  97.                 } else {
  98.                         negativeLoopMode = loopMode;
  99.                         animChannel.setLoopMode(LoopMode.Cycle);
  100.                         animChannel.setSpeed(FastMath.abs(speed));
  101.                         animChannel.setTime(animChannel.getAnimMaxTime());
  102.                         timer = animChannel.getAnimMaxTime();
  103.                 }
  104.         }
  105.  
  106.         @Override
  107.         public Control cloneForSpatial(Spatial spatial) {
  108.                 return null;
  109.         }
  110.  
  111.         @Override
  112.         public void setSpatial(Spatial spatial) {
  113.         }
  114.  
  115.         @Override
  116.         public void render(RenderManager rm, ViewPort vp) {
  117.         }
  118.  
  119.         @Override
  120.         public void write(JmeExporter ex) throws IOException {
  121.         }
  122.  
  123.         @Override
  124.         public void read(JmeImporter im) throws IOException {
  125.         }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement