diff --git a/src/main/java/schule/ngb/zm/anim/Animations.java b/src/main/java/schule/ngb/zm/anim/Animations.java index c80c008..682fb98 100644 --- a/src/main/java/schule/ngb/zm/anim/Animations.java +++ b/src/main/java/schule/ngb/zm/anim/Animations.java @@ -11,6 +11,7 @@ import schule.ngb.zm.util.Validator; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.function.*; @@ -145,20 +146,6 @@ public class Animations { } public static final Future animate( T target, int runtime, DoubleUnaryOperator easing, DoubleConsumer stepper ) { - /*final long starttime = System.currentTimeMillis(); - return TaskRunner.run(() -> { - double t = 0.0; - do { - // One animation step for t in [0,1] - stepper.accept(easing.applyAsDouble(t)); - try { - Thread.sleep(1000 / Constants.framesPerSecond); - } catch( InterruptedException ex ) { - } - t = (double) (System.currentTimeMillis() - starttime) / (double) runtime; - } while( t < 1.0 ); - stepper.accept(easing.applyAsDouble(1.0)); - }, target);*/ return TaskRunner.run(new FramerateLimitedTask() { double t = 0.0; final long starttime = System.currentTimeMillis(); @@ -177,6 +164,21 @@ public class Animations { }, target); } + public static final T animateAndWait( T target, int runtime, DoubleUnaryOperator easing, DoubleConsumer stepper ) { + Future future = animate(target, runtime, easing, stepper); + while( !future.isDone() ) { + try { + return future.get(); + } catch( InterruptedException iex ) { + // Keep waiting + } catch( ExecutionException eex ) { + LOG.error(eex.getCause(), "Animation task terminated with exception"); + return target; + } + } + return target; + } + public static final Future animate( T target, int runtime, Animator animator ) { return animate( target, runtime, @@ -186,7 +188,7 @@ public class Animations { ); } - public static Future animate( Animation animation ) { + public static Future> animate( Animation animation ) { return TaskRunner.run(new FramerateLimitedTask() { @Override protected void initialize() { @@ -201,6 +203,12 @@ public class Animations { }, animation); } + public static Animation animateAndWait( Animation animation ) { + Future> future = animate(animation); + animation.await(); + return animation; + } + public static Future> animate( Animation animation, DoubleUnaryOperator easing ) { final AnimationFacade facade = new AnimationFacade<>(animation, animation.getRuntime(), easing); return TaskRunner.run(new FramerateLimitedTask() {