EasingCurve.java

/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.statusshout.animations;

/**
 * An interface for easing curves.
 */
public abstract class EasingCurve {
    protected int[] _values = null;
    protected int _steps = 0;
    protected int _duration = 0;
    protected int _time = 0;
    protected int _currentStepIndex = 0;

    /**
     * Calculates the values for the animation.
     * @param first The "from" value.
     * @param last The "to" value.
     * @param duration The animation duration.
     * @return True if successfully calculated, false otherwise.
     */
    public abstract boolean calculate(int first, int last, int duration);

    /**
     * Returns the next animated value.
     * @param ticks The number of milliseconds since the last value request.
     * @return The next animated value.
     */
    public int getNextValue(int ticks) {
        _time += ticks;
        _currentStepIndex = getStep(_time);
        int value = 0;
        
        if (_values != null) {
            if (_currentStepIndex < _steps) {
                value = _values[_currentStepIndex];
            }
            else {
                value = _values[_steps - 1];
            }
        }
        
        return value;
    }

    /**
     * Checks if there are still values left for the animation.
     * @param ticks The number of milliseconds since the last value request.
     * @return True if one or more values are still available. False otherwise.
     */
    public boolean hasNext(int ticks) {
        //System.out.println("With time " + (_time + ticks) + " => " + getStep(_time + ticks));
        if (getStep(_time + ticks) < _steps) {
            return true;
        }
        
        return false;
    }

    /** 
     * @return The last, "to" value.
     */
    public int getLastValue() {
        if (_values != null) {
            return _values[_steps - 1];
        }
        
        return 0;
    }

    /**
     * Resolves the index of the next step based on the given time.
     * @param time The time elapsed since the beginning of the animation.
     * @return The index of the next step.
     */
    private final int getStep(final int time) {
        if (_duration > 0) {
            return (_steps - 1) * time / _duration;
        }
        
        return 0;
    }

}