Skip to content

Commit

Permalink
more work on component animator
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Nov 10, 2023
1 parent e44a561 commit e5587b5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 285 deletions.
257 changes: 0 additions & 257 deletions src/main/java/cyder/animation/AnimationUtil.java

This file was deleted.

49 changes: 39 additions & 10 deletions src/main/java/cyder/animation/ComponentAnimator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* A wrapper for a {@link Component} to perform cardinal direction animations on it.
* This component uses a slight modification on a builder pattern; accessor methods
* return {@code this} instance instead of {@link Void}.
*/
public final class ComponentAnimator {
/**
Expand Down Expand Up @@ -182,14 +184,17 @@ public boolean isAnimating() {
/**
* Animates this component.
*
* @return this animator
* @throws IllegalStateException if this component is already animating
*/
public synchronized void animate() {
@CanIgnoreReturnValue
public synchronized ComponentAnimator animate() {
Preconditions.checkArgument(!isAnimating.get());
Preconditions.checkArgument(!stoppingAnimation.get());

isAnimating.set(true);

String animationThreadName = getAnimationThreadName();
CyderThreadRunner.submit(() -> {
switch (animationDirection) {
case LEFT -> {
Expand Down Expand Up @@ -237,7 +242,9 @@ public synchronized void animate() {

isAnimating.set(false);
stoppingAnimation.set(false);
}, "todo custom thread name");
}, animationThreadName);

return this;
}

/**
Expand Down Expand Up @@ -297,13 +304,35 @@ public boolean equals(Object o) {
}

ComponentAnimator other = (ComponentAnimator) o;
return isAnimating.equals(other.isAnimating) &&
stoppingAnimation.equals(other.stoppingAnimation) &&
animationDirection.equals(other.animationDirection) &&
animationComponent.equals(other.animationComponent) &&
animationStart == other.animationStart &&
animationEnd == other.animationEnd &&
animationDelay.equals(other.animationDelay) &&
animationIncrement == other.animationIncrement;
return isAnimating.equals(other.isAnimating)
&& stoppingAnimation.equals(other.stoppingAnimation)
&& animationDirection.equals(other.animationDirection)
&& animationComponent.equals(other.animationComponent)
&& animationStart == other.animationStart
&& animationEnd == other.animationEnd
&& animationDelay.equals(other.animationDelay)
&& animationIncrement == other.animationIncrement;
}

/**
* Returns the name for the thread which animates this component.
*
* @return the name for the thread which animates this component
*/
private String getAnimationThreadName() {
int currentAnimationValue = switch (animationDirection) {
case LEFT, RIGHT -> animationComponent.getX();
case TOP, BOTTOM -> animationComponent.getY();
default -> throw new IllegalStateException("Invalid direction: " + animationDirection);
};

String currentAnimationValueRepresentation = isAnimating.get()
? "NOT ANIMATING" : String.valueOf(currentAnimationValue);

return "ComponentAnimator{direction=" + animationDirection
+ ", animationStart=" + animationStart
+ ", currentAnimationValue=" + currentAnimationValueRepresentation
+ ", animationEnd=" + animationEnd
+ "}";
}
}
Loading

0 comments on commit e5587b5

Please sign in to comment.