package aurelienribon.tweenengine; /** * BaseTween is the base class of Tween and Timeline. It defines the * iteration engine used to play animations for any number of times, and in * any direction, at any speed. *

* * It is responsible for calling the different callbacks at the right moments, * and for making sure that every callbacks are triggered, even if the update * engine gets a big delta time at once. * * @see Tween * @see Timeline * @author Aurelien Ribon | http://www.aurelienribon.com/ */ public abstract class BaseTween { // General private int step; private int repeatCnt; private boolean isIterationStep; private boolean isYoyo; // Timings protected float delay; protected float duration; private float repeatDelay; private float currentTime; private float deltaTime; private boolean isStarted; // true when the object is started private boolean isInitialized; // true after the delay private boolean isFinished; // true when all repetitions are done private boolean isKilled; // true if kill() was called private boolean isPaused; // true if pause() was called // Misc private TweenCallback callback; private int callbackTriggers; private Object userData; // Package access boolean isAutoRemoveEnabled; boolean isAutoStartEnabled; // ------------------------------------------------------------------------- protected void reset() { step = -2; repeatCnt = 0; isIterationStep = isYoyo = false; delay = duration = repeatDelay = currentTime = deltaTime = 0; isStarted = isInitialized = isFinished = isKilled = isPaused = false; callback = null; callbackTriggers = TweenCallback.COMPLETE; userData = null; isAutoRemoveEnabled = isAutoStartEnabled = true; } // ------------------------------------------------------------------------- // Public API // ------------------------------------------------------------------------- /** * Builds and validates the object. Only needed if you want to finalize a * tween or timeline without starting it, since a call to ".start()" also * calls this method. * * @return The current object, for chaining instructions. */ public T build() { return (T) this; } /** * Starts or restarts the object unmanaged. You will need to take care of * its life-cycle. If you want the tween to be managed for you, use a * {@link TweenManager}. * * @return The current object, for chaining instructions. */ public T start() { build(); currentTime = 0; isStarted = true; return (T) this; } /** * Convenience method to add an object to a manager. Its life-cycle will be * handled for you. Relax and enjoy the animation. * * @return The current object, for chaining instructions. */ public T start(TweenManager manager) { manager.add(this); return (T) this; } /** * Adds a delay to the tween or timeline. * * @param delay A duration. * @return The current object, for chaining instructions. */ public T delay(float delay) { this.delay += delay; return (T) this; } /** * Kills the tween or timeline. If you are using a TweenManager, this object * will be removed automatically. */ public void kill() { isKilled = true; } /** * Stops and resets the tween or timeline, and sends it to its pool, for + * later reuse. Note that if you use a {@link TweenManager}, this method + * is automatically called once the animation is finished. */ public void free() { } /** * Pauses the tween or timeline. Further update calls won't have any effect. */ public void pause() { isPaused = true; } /** * Resumes the tween or timeline. Has no effect is it was no already paused. */ public void resume() { isPaused = false; } /** * Repeats the tween or timeline for a given number of times. * @param count The number of repetitions. For infinite repetition, * use Tween.INFINITY, or a negative number. * * @param delay A delay between each iteration. * @return The current tween or timeline, for chaining instructions. */ public T repeat(int count, float delay) { if (isStarted) throw new RuntimeException("You can't change the repetitions of a tween or timeline once it is started"); repeatCnt = count; repeatDelay = delay >= 0 ? delay : 0; isYoyo = false; return (T) this; } /** * Repeats the tween or timeline for a given number of times. * Every two iterations, it will be played backwards. * * @param count The number of repetitions. For infinite repetition, * use Tween.INFINITY, or '-1'. * @param delay A delay before each repetition. * @return The current tween or timeline, for chaining instructions. */ public T repeatYoyo(int count, float delay) { if (isStarted) throw new RuntimeException("You can't change the repetitions of a tween or timeline once it is started"); repeatCnt = count; repeatDelay = delay >= 0 ? delay : 0; isYoyo = true; return (T) this; } /** * Sets the callback. By default, it will be fired at the completion of the * tween or timeline (event COMPLETE). If you want to change this behavior * and add more triggers, use the {@link setCallbackTriggers()} method. * * @see TweenCallback */ public T setCallback(TweenCallback callback) { this.callback = callback; return (T) this; } /** * Changes the triggers of the callback. The available triggers, listed as * members of the {@link TweenCallback} interface, are: *

* * BEGIN: right after the delay (if any)
* START: at each iteration beginning
* END: at each iteration ending, before the repeat delay
* COMPLETE: at last END event
* BACK_BEGIN: at the beginning of the first backward iteration
* BACK_START: at each backward iteration beginning, after the repeat delay
* BACK_END: at each backward iteration ending
* BACK_COMPLETE: at last BACK_END event *

* *

 {@code
	 * forward :      BEGIN                                   COMPLETE
	 * forward :      START    END      START    END      START    END
	 * |--------------[XXXXXXXXXX]------[XXXXXXXXXX]------[XXXXXXXXXX]
	 * backward:      bEND  bSTART      bEND  bSTART      bEND  bSTART
	 * backward:      bCOMPLETE                                 bBEGIN
	 * }
* * @param flags one or more triggers, separated by the '|' operator. * @see TweenCallback */ public T setCallbackTriggers(int flags) { this.callbackTriggers = flags; return (T) this; } /** * Attaches an object to this tween or timeline. It can be useful in order * to retrieve some data from a TweenCallback. * * @param data Any kind of object. * @return The current tween or timeline, for chaining instructions. */ public T setUserData(Object data) { userData = data; return (T) this; } // ------------------------------------------------------------------------- // Getters // ------------------------------------------------------------------------- /** * Gets the delay of the tween or timeline. Nothing will happen before * this delay. */ public float getDelay() { return delay; } /** * Gets the duration of a single iteration. */ public float getDuration() { return duration; } /** * Gets the number of iterations that will be played. */ public int getRepeatCount() { return repeatCnt; } /** * Gets the delay occuring between two iterations. */ public float getRepeatDelay() { return repeatDelay; } /** * Returns the complete duration, including initial delay and repetitions. * The formula is as follows: *
	 * fullDuration = delay + duration + (repeatDelay + duration) * repeatCnt
	 * 
*/ public float getFullDuration() { if (repeatCnt < 0) return -1; return delay + duration + (repeatDelay + duration) * repeatCnt; } /** * Gets the attached data, or null if none. */ public Object getUserData() { return userData; } /** * Gets the id of the current step. Values are as follows:
*