Add offset properties to Simple and Random Animations Runners

This commit is contained in:
2021-03-22 12:35:42 +01:00
parent fd9eb07210
commit e68539575a
2 changed files with 33 additions and 7 deletions

View File

@@ -34,6 +34,9 @@ public class RandomAnimationsRunner implements AnimationRunner {
private float rotation = 0f;
private RealDistribution rotationDistribution;
private float offsetX = 0;
private float offsetY = 0;
public RandomAnimationsRunner(int count) {
this.count = max(count, 0);
}
@@ -119,15 +122,21 @@ public class RandomAnimationsRunner implements AnimationRunner {
return this;
}
public RandomAnimationsRunner offset(float x, float y) {
this.offsetX = x;
this.offsetY = y;
return this;
}
@Override
public void run(Context context, Layer layer, Vector2fc origin) {
for (int i = 0; i < count; ++i) {
var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size())));
if (rangeDistribution != null) {
animation.setPosition(origin.x() + (float) rangeDistribution.sample(), origin.y() + (float) rangeDistribution.sample());
animation.setPosition(origin.x() + (float) rangeDistribution.sample() + offsetX, origin.y() + (float) rangeDistribution.sample() + offsetY);
} else {
animation.setPosition(origin.x() + rangeX, origin.y() + rangeY);
animation.setPosition(origin.x() + rangeX + offsetX, origin.y() + rangeY + offsetY);
}
animation.setScale(scaleDistribution != null ? (float) scaleDistribution.sample() : scale);

View File

@@ -15,9 +15,11 @@ public class SimpleAnimationRunner implements AnimationRunner {
private float speed = 0.05f;
private float rotation = 0f;
private Path<Animation> path;
private Integer pathRepeat;
private Integer repeatPath;
private boolean finishOnPathEnd;
private boolean finishOnPathFail;
private float offsetX = 0;
private float offsetY = 0;
public SimpleAnimationRunner(String animationUid) {
this.animationUid = animationUid;
@@ -48,14 +50,29 @@ public class SimpleAnimationRunner implements AnimationRunner {
return this;
}
public SimpleAnimationRunner speed(float speed) {
this.speed = speed;
return this;
}
public SimpleAnimationRunner rotation(float rotation) {
this.rotation = rotation;
return this;
}
public SimpleAnimationRunner path(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
public SimpleAnimationRunner offset(float x, float y) {
this.offsetX = x;
this.offsetY = y;
return this;
}
public SimpleAnimationRunner repeatPath(int n) {
this.repeatPath = n;
return this;
}
public SimpleAnimationRunner path(Path<Animation> path, boolean finishOnEnd, boolean finishOnFail) {
this.path = path;
this.pathRepeat = repeat;
this.finishOnPathEnd = finishOnEnd;
this.finishOnPathFail = finishOnFail;
return this;
@@ -64,7 +81,7 @@ public class SimpleAnimationRunner implements AnimationRunner {
@Override
public void run(Context context, Layer layer, Vector2fc origin) {
var animation = new DelayedAnimation(context.createAnimation(animationUid), delay);
animation.setPosition(origin);
animation.setPosition(origin.x() + offsetX, origin.y() + offsetY);
animation.setScale(scale);
animation.setAnimationSpeed(animationSpeed);
animation.setSpeed(speed);
@@ -72,7 +89,7 @@ public class SimpleAnimationRunner implements AnimationRunner {
animation.setRepeat(repeat);
if (path != null) {
animation.followPath(path, pathRepeat, finishOnPathEnd, finishOnPathFail);
animation.followPath(path, repeatPath, finishOnPathEnd, finishOnPathFail);
}
layer.pushAnimation(animation);