package org.gwtoolbox.commons.ui.client.animation;
import com.google.gwt.animation.client.Animation;
import com.google.gwt.core.client.Duration;
/**
* @author Uri Boness
*/
public class Animator {
private final int duration;
private final Transition transition;
public Animator(int duration) {
this(duration, Transitions.linear());
}
public Animator(int duration, Transition transition) {
this.duration = duration;
this.transition = transition;
}
public Cancellable start(Callback callback) {
return start(Duration.currentTimeMillis(), callback);
}
public Cancellable start(double startTime, final Callback callback) {
Animation animation = new Animation() {
@Override
protected void onUpdate(double progress) {
callback.update(transition.translate(progress));
if (progress == 1.0) {
callback.finished();
}
}
};
animation.run(duration, startTime);
return new AnimationCancellable(animation);
}
public interface Callback {
void update(double progress);
void finished();
}
//================================================= Inner Classes ==================================================
private final static class AnimationCancellable implements Cancellable {
private final Animation animation;
private AnimationCancellable(Animation animation) {
this.animation = animation;
}
public void cancel() {
animation.cancel();
}
}
}