Skip to content

Commit 9741c71

Browse files
committed
add AnimationMode
1 parent c09f3c6 commit 9741c71

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

animation/src/main/java/io/github/projectunified/craftux/animation/Animation.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
public class Animation<T> {
2121
private final List<T> frames;
2222
private final long periodMillis;
23+
private final AnimationMode mode;
2324
private final AtomicLong startMillis = new AtomicLong(-1);
2425

2526
/**
2627
* Creates a new Animation with the specified frames and period.
2728
*
2829
* @param frames the list of frames to cycle through
2930
* @param periodMillis the period in milliseconds between frame changes
31+
* @param mode the mode of the animation
3032
* @throws IllegalArgumentException if frames is empty or periodMillis is not positive
3133
*/
32-
public Animation(List<T> frames, long periodMillis) {
34+
public Animation(List<T> frames, long periodMillis, AnimationMode mode) {
3335
if (frames.isEmpty()) {
3436
throw new IllegalArgumentException("Frames cannot be empty");
3537
}
@@ -39,6 +41,18 @@ public Animation(List<T> frames, long periodMillis) {
3941

4042
this.frames = frames;
4143
this.periodMillis = periodMillis;
44+
this.mode = mode;
45+
}
46+
47+
/**
48+
* Creates a new Animation with the specified frames and period.
49+
*
50+
* @param frames the list of frames to cycle through
51+
* @param periodMillis the period in milliseconds between frame changes
52+
* @throws IllegalArgumentException if frames is empty or periodMillis is not positive
53+
*/
54+
public Animation(List<T> frames, long periodMillis) {
55+
this(frames, periodMillis, AnimationMode.REPEAT);
4256
}
4357

4458
/**
@@ -62,6 +76,15 @@ public T getCurrentFrame(long currentMillis) {
6276
startMillis = currentMillis;
6377
this.startMillis.set(startMillis);
6478
}
79+
80+
if (!isFirstRun(currentMillis)) {
81+
if (mode == AnimationMode.ONE_TIME) {
82+
return null;
83+
} else if (mode == AnimationMode.ONE_TIME_KEEP_LAST) {
84+
return frames.getLast();
85+
}
86+
}
87+
6588
long diff = currentMillis - startMillis;
6689
int index = (int) (diff / periodMillis) % frames.size();
6790
return frames.get(index);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.projectunified.craftux.animation;
2+
3+
/**
4+
* The mode of {@link Animation}
5+
*/
6+
public enum AnimationMode {
7+
/**
8+
* The frame will be repeated
9+
*/
10+
REPEAT,
11+
/**
12+
* The {@link Animation#getCurrentFrame(long)}} will return {@link null} if the animation is completed ({@link Animation#isFirstRun(long)} returns {@link false})
13+
*/
14+
ONE_TIME,
15+
/**
16+
* The {@link Animation#getCurrentFrame(long)}} will return the last element of the {@link Animation#getFrames()} if the animation is completed ({@link Animation#isFirstRun(long)} returns {@link false})
17+
*/
18+
ONE_TIME_KEEP_LAST
19+
}

button/src/main/java/io/github/projectunified/craftux/button/AnimatedButton.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.projectunified.craftux.button;
22

33
import io.github.projectunified.craftux.animation.Animation;
4+
import io.github.projectunified.craftux.animation.AnimationMode;
45
import io.github.projectunified.craftux.common.ActionItem;
56
import io.github.projectunified.craftux.common.Button;
67
import org.jetbrains.annotations.NotNull;
@@ -26,6 +27,7 @@
2627
public class AnimatedButton extends MultiButton {
2728
private final Map<UUID, Animation<Button>> animationMap = new ConcurrentHashMap<>();
2829
private long periodMillis = 50L;
30+
private AnimationMode mode = AnimationMode.REPEAT;
2931

3032
/**
3133
* Sets the period of the animation between frame changes.
@@ -40,8 +42,17 @@ public void setPeriodMillis(long periodMillis) {
4042
this.periodMillis = periodMillis;
4143
}
4244

45+
/**
46+
* Set the mode of the animation
47+
*
48+
* @param mode the mode of the animation
49+
*/
50+
public void setMode(AnimationMode mode) {
51+
this.mode = mode;
52+
}
53+
4354
private Animation<Button> getAnimation(UUID uuid) {
44-
return animationMap.computeIfAbsent(uuid, key -> new Animation<>(buttons, periodMillis));
55+
return animationMap.computeIfAbsent(uuid, key -> new Animation<>(buttons, periodMillis, mode));
4556
}
4657

4758
@Override

mask/src/main/java/io/github/projectunified/craftux/mask/AnimatedMask.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.projectunified.craftux.mask;
22

33
import io.github.projectunified.craftux.animation.Animation;
4+
import io.github.projectunified.craftux.animation.AnimationMode;
45
import io.github.projectunified.craftux.common.ActionItem;
56
import io.github.projectunified.craftux.common.Mask;
67
import io.github.projectunified.craftux.common.Position;
@@ -31,6 +32,7 @@
3132
public class AnimatedMask extends MultiMask<Mask> {
3233
private final Map<UUID, Animation<Mask>> animationMap = new ConcurrentHashMap<>();
3334
private long periodMillis = 50;
35+
private AnimationMode mode = AnimationMode.REPEAT;
3436

3537
/**
3638
* Set the period of the animation
@@ -44,8 +46,17 @@ public void setPeriodMillis(long periodMillis) {
4446
this.periodMillis = periodMillis;
4547
}
4648

49+
/**
50+
* Set the mode of the animation
51+
*
52+
* @param mode the mode of the animation
53+
*/
54+
public void setMode(AnimationMode mode) {
55+
this.mode = mode;
56+
}
57+
4758
private Animation<Mask> getAnimation(@NotNull UUID uuid) {
48-
return animationMap.computeIfAbsent(uuid, k -> new Animation<>(elements, periodMillis));
59+
return animationMap.computeIfAbsent(uuid, k -> new Animation<>(elements, periodMillis, mode));
4960
}
5061

5162
@Override

0 commit comments

Comments
 (0)