Skip to content

Commit

Permalink
A half-baked approach to repeating tweens. No way to cancel them curr…
Browse files Browse the repository at this point in the history
…ently.

More thinking needed.
  • Loading branch information
samskivert committed May 5, 2011
1 parent 6ea7f3a commit 3baae72
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
90 changes: 83 additions & 7 deletions core/src/main/java/com/threerings/jiggl/tween/Tween.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public One to (float value) {
return this;
}

@Override
protected void init (float time) {
super.init(time);
if (_from == Float.MIN_VALUE) _from = _value.value;
}

@Override
protected boolean apply (float time) {
float dt = time-_start;
if (dt < _duration) {
Expand Down Expand Up @@ -79,12 +81,14 @@ public Two to (float tox, float toy)
return this;
}

@Override
protected void init (float time) {
super.init(time);
if (_fromx == Float.MIN_VALUE) _fromx = _x.value;
if (_fromy == Float.MIN_VALUE) _fromy = _y.value;
}

@Override
protected boolean apply (float time) {
float dt = time-_start;
if (dt < _duration) {
Expand All @@ -110,10 +114,16 @@ public Delay (float duration) {
_duration = duration;
}

@Override
protected boolean apply (float time) {
return (time-_start >= _duration);
}

@Override
protected float getOverrun (float time) {
return (time - _start) - _duration;
}

protected final float _duration;
}

Expand All @@ -124,6 +134,7 @@ public Action (Runnable action) {
_action = action;
}

@Override
protected boolean apply (float time) {
_action.run();
return true;
Expand All @@ -132,15 +143,58 @@ protected boolean apply (float time) {
protected Runnable _action;
}

/** A tween that repeats its underlying tween over and over again (until removed). */
public static class Repeat extends Tween
{
/** Cancels this repeating tween. */
public void cancel () {
_cancelled = true;
}

@Override
protected void init (float time) {
// a normal tween will have _current initialized to itself; we want to skip ourselves
// and go right to our to-be-repeated tween, and initialize it immediately
_current = _next;
_current.init(time);
}

@Override
protected boolean apply (float time) {
return false; // not used
}

@Override
protected boolean apply (Tweener tweener, float time)
{
// if we're canceled, stop now
if (_cancelled) return true;

// if our current chain of tweens is still running, keep going
if (!super.apply(tweener, time)) return false;

// otherwise, reset to the head of the chain and keep going
float overrun = _current.getOverrun(time);
_current = _next;
_current.init(time-overrun);
return false;
}

protected boolean _cancelled = false;
}

/**
* Returns a tween factory for constructing a tween that will be queued up for execution when
* the current tween is completes.
*/
public Tweener then ()
{
if (_next != null) {
throw new IllegalStateException("This tween already has a 'then' tween.");
}
return new Tweener() {
protected <T extends Tween> T register (T tween) {
_then = tween;
_next = tween;
return tween;
}
};
Expand All @@ -157,15 +211,31 @@ protected void init (float time)

protected boolean apply (Tweener tweener, float time)
{
if (!apply(time)) return false;
if (_then != null) {
tweener.register(_then);
}
return true;
// if the current tween is still running, keep going
if (!_current.apply(time)) return false;

// if the current tween is finished and we have no next tween, then we're done
if (_current._next == null) return true;

// otherwise initialize our next tween (accounting for any overrun on our current tween)
// and keep going
float overrun = _current.getOverrun(time);
_current = _current._next;
_current.init(time-overrun);
return false;
}

protected abstract boolean apply (float time);

/**
* Returns the amount of time this tween has overrun its duration, given the supplied current
* timestamp. The result may be negative if the tween is not complete.
*/
protected float getOverrun (float time)
{
return 0f;
}

protected static abstract class Interped extends Tween
{
/**
Expand All @@ -180,10 +250,16 @@ protected Interped (Interpolator interp) {
_interp = interp;
}

@Override
protected float getOverrun (float time) {
return (time - _start) - _duration;
}

protected final Interpolator _interp;
protected float _duration = 1;
}

protected float _start;
protected Tween _then;
protected Tween _current = this;
protected Tween _next;
}
9 changes: 9 additions & 0 deletions core/src/main/java/com/threerings/jiggl/tween/Tweener.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public Tween.Delay delay (float seconds)
return register(new Tween.Delay(seconds));
}

/**
* Returns a tweener which can be used to construct a tween that will be repeated until
* canceled.
*/
public Tweener repeat ()
{
return register(new Tween.Repeat()).then();
}

/**
* Creates a tween that executes the supplied runnable and immediately completes.
*/
Expand Down
3 changes: 2 additions & 1 deletion webgl/src/test/java/com/threerings/jiggl/test/GeomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void run () {

quad3.x.value = 3;
quad3.y.value = 3;
ctx.tweener.linear(quad3.rotation).to((float)Math.PI).in(2);
ctx.tweener.repeat().linear(quad3.rotation).to(2*(float)Math.PI).in(2).
then().linear(quad3.rotation).to(0).in(0);

quad4.x.value = 3;
quad4.y.value = 3;
Expand Down

0 comments on commit 3baae72

Please sign in to comment.