diff --git a/src/main/java/org/saturnclient/ui2/Element.java b/src/main/java/org/saturnclient/ui2/Element.java index 1d520bd..cf3627f 100644 --- a/src/main/java/org/saturnclient/ui2/Element.java +++ b/src/main/java/org/saturnclient/ui2/Element.java @@ -1,14 +1,19 @@ package org.saturnclient.ui2; +import java.util.function.Function; + import org.saturnclient.ui2.anim.Animation; +import org.saturnclient.ui2.anim.Curve; public class Element { public int x; public int y; public int width; public int height; + public float opacity = 1.0f; public boolean focused; public Animation animation; + public Function curve = Curve::easeInOutCubic; public void render(RenderScope renderScope, RenderContext ctx) { } @@ -46,7 +51,12 @@ public class Element { public final Element animation(Animation animation) { this.animation = animation; + return this; + } + public final Element animation(Animation animation, Function curve) { + this.animation = animation; + this.curve = curve; return this; } } diff --git a/src/main/java/org/saturnclient/ui2/RenderScope.java b/src/main/java/org/saturnclient/ui2/RenderScope.java index f42e4df..c0a239d 100644 --- a/src/main/java/org/saturnclient/ui2/RenderScope.java +++ b/src/main/java/org/saturnclient/ui2/RenderScope.java @@ -246,6 +246,7 @@ public class RenderScope { x2 *= 4; y1 *= 4; y2 *= 4; + color = getColor(color); matrices.push(); matrices.scale(0.25f, 0.25f, 1.0f); diff --git a/src/main/java/org/saturnclient/ui2/SaturnScreen.java b/src/main/java/org/saturnclient/ui2/SaturnScreen.java index c00c221..874292e 100644 --- a/src/main/java/org/saturnclient/ui2/SaturnScreen.java +++ b/src/main/java/org/saturnclient/ui2/SaturnScreen.java @@ -27,53 +27,46 @@ public class SaturnScreen extends Screen { if (element.animation != null) { element.animation.init(element); + + try { + Thread.sleep(25); + } catch (Exception e) { + e.printStackTrace(); + } + new Thread(() -> { - Function curveFunction = Curve::easeInOutCubicReverse; - int delay = (element.animation.duration / 20) - 15; long startTime = System.currentTimeMillis(); - float lastProgress = 0.0f; - while (true) { - int elapsed = (int) (System.currentTimeMillis() - startTime); + Function curveFunction = Curve::easeInOutCubic; + float progress = 0.0f; + double incremental = (1000 - element.animation.duration) / 10000.0; - float progress = Math.round(curveFunction.apply((double) elapsed / element.animation.duration) * 1000.0f) / 1000.0f; + System.out.println("Starting animation..."); + // System.out.printf("Increment per step: %.6f%n", incremental); - float p = lastProgress; + for (int i = 0; i <= 100; i++) { + float newProgress = (float) (double) curveFunction.apply(i / 100.0); - while (progress != p) { - if (p < progress) { - p += 0.01f; - if (p > progress) p = progress; - } else if (p > progress) { - p -= 0.01f; - if (p < progress) p = progress; + while (Math.abs(newProgress - progress) > 1e-6) { + if (progress < newProgress) { + progress += incremental; + if (progress > newProgress) progress = newProgress; + } else { + progress -= incremental; + if (progress < newProgress) progress = newProgress; } - element.animation.tick(p, element); + element.animation.tick(progress, element); try { - Thread.sleep(4); + Thread.sleep(element.animation.duration / 100); } catch (Exception e) { e.printStackTrace(); - } - } - - if (elapsed > (element.animation.duration + 30)) { - break; - } - - if (progress != lastProgress) { - lastProgress = p; - } - - if (delay > 0) { - try { - Thread.sleep(delay); - } catch (Exception e) { - e.printStackTrace(); - } + } } } + + System.out.println("Animation complete. " + (System.currentTimeMillis() - startTime) + "MS"); }).start(); } } @@ -96,6 +89,7 @@ public class SaturnScreen extends Screen { renderScope.matrices.push(); for (Element element : elements) { + renderScope.setOpacity(element.opacity); renderScope.matrices.push(); renderScope.matrices.translate(element.x, element.y, 0); element.render(renderScope, new RenderContext(mouseX, mouseY, element)); diff --git a/src/main/java/org/saturnclient/ui2/anim/Curve.java b/src/main/java/org/saturnclient/ui2/anim/Curve.java index 82db6fb..c7f0a1c 100644 --- a/src/main/java/org/saturnclient/ui2/anim/Curve.java +++ b/src/main/java/org/saturnclient/ui2/anim/Curve.java @@ -1,6 +1,10 @@ package org.saturnclient.ui2.anim; public class Curve { + public static double easeOutCubic(double x) { + return 1 - Math.pow(1 - x, 3); + } + public static double easeInOutCubic(double x) { return (x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2); } @@ -8,4 +12,15 @@ public class Curve { public static double easeInOutCubicReverse(double x) { return -(x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2); } + + public static double easeInOutBack(double x) { + double c1 = 1.70158; + double c2 = c1 * 1.525; + + if (x < 0.5) { + return (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2; + } else { + return (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (2 * x - 2) + c2) + 2) / 2; + } + } } diff --git a/src/main/java/org/saturnclient/ui2/anim/Fade.java b/src/main/java/org/saturnclient/ui2/anim/Fade.java new file mode 100644 index 0000000..38e870a --- /dev/null +++ b/src/main/java/org/saturnclient/ui2/anim/Fade.java @@ -0,0 +1,20 @@ +package org.saturnclient.ui2.anim; + +import org.saturnclient.ui2.Element; + +public class Fade extends Animation { + public Fade(int duration) { + super(duration); + } + + @Override + public void tick(float progress, Element element) { + element.opacity = progress; + // element.opacity += 0.1f; + } + + @Override + public void init(Element element) { + element.opacity = 0; + } +} diff --git a/src/main/java/org/saturnclient/ui2/anim/SlideY.java b/src/main/java/org/saturnclient/ui2/anim/SlideY.java index f2c49ed..48ca61b 100644 --- a/src/main/java/org/saturnclient/ui2/anim/SlideY.java +++ b/src/main/java/org/saturnclient/ui2/anim/SlideY.java @@ -27,16 +27,12 @@ public class SlideY extends Animation { @Override public void tick(float progress, Element element) { - if (progress > 0) { - element.y = elementY + Math.min(maxOffset, (int) (offset * progress)); - } else { - element.y = elementY + Math.max(-offset, (int) (offset * progress)); - } + element.y = elementY + (int) (offset * progress); } @Override public void init(Element element) { - element.y += offset; + element.y -= offset; elementY = element.y; } } diff --git a/src/main/java/org/saturnclient/ui2/screens/ShiftMenu.java b/src/main/java/org/saturnclient/ui2/screens/ShiftMenu.java index f69620a..d6806c9 100644 --- a/src/main/java/org/saturnclient/ui2/screens/ShiftMenu.java +++ b/src/main/java/org/saturnclient/ui2/screens/ShiftMenu.java @@ -10,6 +10,7 @@ import org.saturnclient.ui2.SaturnScreen; import org.saturnclient.ui2.elements.Button; import org.saturnclient.ui2.elements.ImageTexture; import org.saturnclient.ui2.elements.TextureButton; +import org.saturnclient.ui2.anim.Fade; import org.saturnclient.ui2.anim.SlideY; public class ShiftMenu extends SaturnScreen { @@ -19,8 +20,8 @@ public class ShiftMenu extends SaturnScreen { @Override public void ui() { - draw(new ImageTexture(Textures.LOGO).dimensions(49, 49).centerOffset(width, height, 0, -45).animation(new SlideY(300))); - draw(new ImageTexture(Textures.LOGO_TEXT).dimensions(49, 8).centerOffset(width, height, 0, -23)); + draw(new ImageTexture(Textures.LOGO).dimensions(49, 49).centerOffset(width, height, 0, -40).animation(new SlideY(700, -20))); + draw(new ImageTexture(Textures.LOGO_TEXT).dimensions(49, 8).centerOffset(width, height, 0, -18).animation(new Fade(700))); Element button = new Button("Settings", () -> { SaturnClient.client.setScreen(new ModMenu());