Skip to content

Commit 672c82e

Browse files
committed
stop scroll animation on edge
1 parent 76c8c63 commit 672c82e

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/main/java/com/cleanroommc/modularui/utils/Animator.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.function.DoubleConsumer;
10+
import java.util.function.DoublePredicate;
1011

1112
public class Animator {
1213

@@ -23,7 +24,7 @@ public static void advance() {
2324
private float min = 0, max = 1;
2425
private float value;
2526
private final IInterpolation interpolation;
26-
private DoubleConsumer callback;
27+
private DoublePredicate callback;
2728
private DoubleConsumer endCallback;
2829

2930
public Animator(int duration, IInterpolation interpolation) {
@@ -32,6 +33,14 @@ public Animator(int duration, IInterpolation interpolation) {
3233
}
3334

3435
public Animator setCallback(DoubleConsumer callback) {
36+
this.callback = val -> {
37+
callback.accept(val);
38+
return false;
39+
};
40+
return this;
41+
}
42+
43+
public Animator setCallback(DoublePredicate callback) {
3544
this.callback = callback;
3645
return this;
3746
}
@@ -100,7 +109,9 @@ public double getMax() {
100109
private boolean tick() {
101110
this.progress += this.dir;
102111
updateValue();
103-
if (this.callback != null) this.callback.accept(this.value);
112+
if (this.callback != null && this.callback.test(this.value)) {
113+
this.dir = 0; // stop animation
114+
}
104115
if (!isRunning()) {
105116
if (this.endCallback != null) this.endCallback.accept(this.value);
106117
return true;

src/main/java/com/cleanroommc/modularui/widget/scroll/ScrollData.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,31 +173,35 @@ public float getProgress(ScrollArea area, int mainAxisPos, int crossAxisPos) {
173173
/**
174174
* Clamp scroll to the bounds of the scroll size;
175175
*/
176-
public void clamp(ScrollArea area) {
176+
public boolean clamp(ScrollArea area) {
177177
int size = getVisibleSize(area);
178178

179+
int old = this.scroll;
179180
if (this.scrollSize <= size) {
180181
this.scroll = 0;
181182
} else {
182183
this.scroll = MathHelper.clamp(this.scroll, 0, this.scrollSize - size);
183184
}
185+
return old != this.scroll; // returns true if the area was clamped
184186
}
185187

186-
public void scrollBy(ScrollArea area, int x) {
188+
public boolean scrollBy(ScrollArea area, int x) {
187189
this.scroll += x;
188-
this.clamp(area);
190+
return clamp(area);
189191
}
190192

191193
/**
192194
* Scroll to the position in the scroll area
193195
*/
194-
public void scrollTo(ScrollArea area, int x) {
196+
public boolean scrollTo(ScrollArea area, int x) {
195197
this.scroll = x;
196-
this.clamp(area);
198+
return clamp(area);
197199
}
198200

199201
public void animateTo(ScrollArea area, int x) {
200-
this.scrollAnimator.setCallback(value -> scrollTo(area, (int) value));
202+
this.scrollAnimator.setCallback(value -> {
203+
return scrollTo(area, (int) value); // stop animation once an edge is hit
204+
});
201205
this.scrollAnimator.setValueBounds(this.scroll, x);
202206
this.scrollAnimator.forward();
203207
this.animatingTo = x;
@@ -235,6 +239,11 @@ public boolean isAnimating() {
235239
return this.scrollAnimator.isRunning();
236240
}
237241

242+
public int getAnimationDirection() {
243+
if (!isAnimating()) return 0;
244+
return this.scrollAnimator.getMax() >= this.scrollAnimator.getMin() ? 1 : -1;
245+
}
246+
238247
public int getAnimatingTo() {
239248
return this.animatingTo;
240249
}

0 commit comments

Comments
 (0)