From 52e973caf8fea7455e7e36673aa0068b5a2a26e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Wed, 12 Aug 2015 19:03:50 -0500 Subject: [PATCH] Added animation loop capabilities to MenuButton. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/ui/MenuButton.java | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/itdelatrisu/opsu/ui/MenuButton.java b/src/itdelatrisu/opsu/ui/MenuButton.java index 3491cdf3..e9fa1089 100644 --- a/src/itdelatrisu/opsu/ui/MenuButton.java +++ b/src/itdelatrisu/opsu/ui/MenuButton.java @@ -18,6 +18,7 @@ package itdelatrisu.opsu.ui; +import itdelatrisu.opsu.Utils; import itdelatrisu.opsu.ui.animations.AnimatedValue; import itdelatrisu.opsu.ui.animations.AnimationEquation; @@ -70,6 +71,9 @@ public class MenuButton { /** The hover animation equation. */ private AnimationEquation animationEqn = AnimationEquation.LINEAR; + /** Whether the animation is advancing forwards (if advancing automatically). */ + private boolean autoAnimationForward = true; + /** The scale of the button. */ private AnimatedValue scale; @@ -291,6 +295,7 @@ public class MenuButton { alpha.setTime(0); if ((hoverEffect & EFFECT_ROTATE) > 0) angle.setTime(0); + autoAnimationForward = true; } /** @@ -301,6 +306,7 @@ public class MenuButton { this.scale = null; this.alpha = null; this.angle = null; + autoAnimationForward = true; } /** @@ -438,6 +444,38 @@ public class MenuButton { angle.update(d); } + /** + * Automatically advances the hover animation in a loop. + * @param delta the delta interval + * @param reverseAtEnd whether to reverse or restart the animation upon reaching the end + */ + public void autoHoverUpdate(int delta, boolean reverseAtEnd) { + if (hoverEffect == 0) + return; + + int time = ((hoverEffect & EFFECT_EXPAND) > 0) ? scale.getTime() : + ((hoverEffect & EFFECT_FADE) > 0) ? alpha.getTime() : + ((hoverEffect & EFFECT_ROTATE) > 0) ? angle.getTime() : -1; + if (time == -1) + return; + + int d = delta * (autoAnimationForward ? 1 : -1); + if (Utils.getBoundedValue(time, d, 0, animationDuration) == time) { + if (reverseAtEnd) + autoAnimationForward = !autoAnimationForward; + else { + if ((hoverEffect & EFFECT_EXPAND) > 0) + scale.setTime(0); + if ((hoverEffect & EFFECT_FADE) > 0) + alpha.setTime(0); + if ((hoverEffect & EFFECT_ROTATE) > 0) + angle.setTime(0); + } + } + + hoverUpdate(delta, autoAnimationForward); + } + /** * Set x and y radius of the button based on current scale factor * and expansion direction.