Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sneak.util;
- import com.jme3.animation.AnimChannel;
- import com.jme3.animation.AnimControl;
- import com.jme3.animation.LoopMode;
- import com.jme3.export.JmeExporter;
- import com.jme3.export.JmeImporter;
- import com.jme3.math.FastMath;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.Control;
- import java.io.IOException;
- /**
- * Add this control to a spatial with an AnimControl. allows setting an animation to play in reverse by using hackery.
- *
- * to play an animation in reverse use a negative speed
- *
- * 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.
- *
- * @author Daniel Strong
- * @see http://hub.jmonkeyengine.org/forum/topic/imported-blend-file-only-retains-1-animation/
- */
- public class ReversableAnimChannelControl implements Control {
- private final AnimChannel animChannel;
- private final String animName0;
- private LoopMode negativeLoopMode = null;
- private float timer = 0;
- public ReversableAnimChannelControl(AnimControl animControl) {
- this(animControl.createChannel());
- }
- public ReversableAnimChannelControl(AnimChannel animChannel) {
- this.animChannel = animChannel;
- animName0 = animChannel.getControl().getAnimationNames().iterator().next();
- }
- @Override
- public void update(float tpf) {
- if (!animChannel.getControl().isEnabled() || negativeLoopMode == null) {
- return;
- }
- timer -= tpf;
- if (timer < 0) {
- switch (negativeLoopMode) {
- case Loop:
- animChannel.setTime(animChannel.getAnimMaxTime());
- timer = animChannel.getAnimMaxTime();
- break;
- case DontLoop:
- animChannel.reset(true);
- negativeLoopMode = null;
- break;
- case Cycle:
- break;
- default:
- throw new AssertionError(negativeLoopMode.name());
- }
- }
- }
- public AnimControl getAnimControl() {
- return animChannel.getControl();
- }
- public AnimChannel getAnimChannel() {
- return animChannel;
- }
- public void setAnim() {
- setAnim(LoopMode.DontLoop, 1);
- }
- public void setAnim(LoopMode loopMode) {
- setAnim(loopMode, 1);
- }
- public void setAnim(float speed) {
- setAnim(LoopMode.DontLoop, speed);
- }
- public void setAnim(LoopMode loopMode, float speed) {
- setAnim(animName0, loopMode, speed);
- }
- public void setAnim(String animName, LoopMode loopMode, float speed) {
- animChannel.setAnim(animName);
- if (speed > 0) {
- negativeLoopMode = null;
- animChannel.setLoopMode(loopMode);
- animChannel.setSpeed(speed);
- } else {
- negativeLoopMode = loopMode;
- animChannel.setLoopMode(LoopMode.Cycle);
- animChannel.setSpeed(FastMath.abs(speed));
- animChannel.setTime(animChannel.getAnimMaxTime());
- timer = animChannel.getAnimMaxTime();
- }
- }
- @Override
- public Control cloneForSpatial(Spatial spatial) {
- return null;
- }
- @Override
- public void setSpatial(Spatial spatial) {
- }
- @Override
- public void render(RenderManager rm, ViewPort vp) {
- }
- @Override
- public void write(JmeExporter ex) throws IOException {
- }
- @Override
- public void read(JmeImporter im) throws IOException {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement