From a9d8e10ff5ba82c02cbf3ad826c11c31d3fd653f Mon Sep 17 00:00:00 2001 From: yugecin Date: Sat, 7 Jul 2018 01:42:37 +0200 Subject: [PATCH] allow changing AnimatedValue properties without losing current progress This also means changing easing function will not modify the progress --- .../opsu/ui/animations/AnimatedValue.java | 15 +++++++++++++++ .../opsu/ui/animations/AnimationEquation.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/itdelatrisu/opsu/ui/animations/AnimatedValue.java b/src/itdelatrisu/opsu/ui/animations/AnimatedValue.java index 0074ae20..907e8c93 100644 --- a/src/itdelatrisu/opsu/ui/animations/AnimatedValue.java +++ b/src/itdelatrisu/opsu/ui/animations/AnimatedValue.java @@ -58,6 +58,21 @@ public class AnimatedValue { this.eqn = eqn; } + public void change(int duration, float min, float max, AnimationEquation eqn) { + float progress = (float) this.time / this.duration; + if (this.eqn != eqn) { + if (this.time != 0 && this.time != this.duration) { + progress = this.eqn.uncalc(progress); + } + this.eqn = eqn; + } + this.duration = duration; + this.time = (int) (this.duration * progress); + this.base = min; + this.diff = max - min; + this.updateValue(); + } + /** * Change the from and to values * @param min start value diff --git a/src/itdelatrisu/opsu/ui/animations/AnimationEquation.java b/src/itdelatrisu/opsu/ui/animations/AnimationEquation.java index 146be941..db78fd1e 100644 --- a/src/itdelatrisu/opsu/ui/animations/AnimationEquation.java +++ b/src/itdelatrisu/opsu/ui/animations/AnimationEquation.java @@ -312,4 +312,19 @@ public enum AnimationEquation { * @return the new {@code t} value [0,1] */ public abstract float calc(float t); + + public float uncalc(float x) { + float min = 0f; + float max = 1f; + // 7 iterations is already decent, do we need 8? + for (int i = 0; i < 8; i++) { + float pos = (min + max) / 2f; + if (this.calc(pos) > x) { + max = pos; + } else { + min = pos; + } + } + return (min + max) / 2f; + } }