Skip to content

Commit

Permalink
stop scroll animation on edge
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Feb 10, 2025
1 parent 76c8c63 commit 672c82e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/cleanroommc/modularui/utils/Animator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleConsumer;
import java.util.function.DoublePredicate;

public class Animator {

Expand All @@ -23,7 +24,7 @@ public static void advance() {
private float min = 0, max = 1;
private float value;
private final IInterpolation interpolation;
private DoubleConsumer callback;
private DoublePredicate callback;
private DoubleConsumer endCallback;

public Animator(int duration, IInterpolation interpolation) {
Expand All @@ -32,6 +33,14 @@ public Animator(int duration, IInterpolation interpolation) {
}

public Animator setCallback(DoubleConsumer callback) {
this.callback = val -> {
callback.accept(val);
return false;
};
return this;
}

public Animator setCallback(DoublePredicate callback) {
this.callback = callback;
return this;
}
Expand Down Expand Up @@ -100,7 +109,9 @@ public double getMax() {
private boolean tick() {
this.progress += this.dir;
updateValue();
if (this.callback != null) this.callback.accept(this.value);
if (this.callback != null && this.callback.test(this.value)) {
this.dir = 0; // stop animation
}
if (!isRunning()) {
if (this.endCallback != null) this.endCallback.accept(this.value);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,35 @@ public float getProgress(ScrollArea area, int mainAxisPos, int crossAxisPos) {
/**
* Clamp scroll to the bounds of the scroll size;
*/
public void clamp(ScrollArea area) {
public boolean clamp(ScrollArea area) {
int size = getVisibleSize(area);

int old = this.scroll;
if (this.scrollSize <= size) {
this.scroll = 0;
} else {
this.scroll = MathHelper.clamp(this.scroll, 0, this.scrollSize - size);
}
return old != this.scroll; // returns true if the area was clamped
}

public void scrollBy(ScrollArea area, int x) {
public boolean scrollBy(ScrollArea area, int x) {
this.scroll += x;
this.clamp(area);
return clamp(area);
}

/**
* Scroll to the position in the scroll area
*/
public void scrollTo(ScrollArea area, int x) {
public boolean scrollTo(ScrollArea area, int x) {
this.scroll = x;
this.clamp(area);
return clamp(area);
}

public void animateTo(ScrollArea area, int x) {
this.scrollAnimator.setCallback(value -> scrollTo(area, (int) value));
this.scrollAnimator.setCallback(value -> {
return scrollTo(area, (int) value); // stop animation once an edge is hit
});
this.scrollAnimator.setValueBounds(this.scroll, x);
this.scrollAnimator.forward();
this.animatingTo = x;
Expand Down Expand Up @@ -235,6 +239,11 @@ public boolean isAnimating() {
return this.scrollAnimator.isRunning();
}

public int getAnimationDirection() {
if (!isAnimating()) return 0;
return this.scrollAnimator.getMax() >= this.scrollAnimator.getMin() ? 1 : -1;
}

public int getAnimatingTo() {
return this.animatingTo;
}
Expand Down

0 comments on commit 672c82e

Please sign in to comment.