001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030/** 031 * 032 */ 033package org.openimaj.content.animation.animator; 034 035/** 036 * An animator that will animate a value over a given time period. The animator 037 * is constructed using a specific time period and on the first call of the 038 * makeNextValue() will start a timer. 039 * <p> 040 * Subclasses must implement a method that 041 * can return a value based on an absolute percentage value. This class will 042 * convert the time each nextValue() is called into a percentage value between 043 * the start and end times and use that to calculate the absolute value of 044 * the animator. 045 * 046 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 047 * @created 14 Aug 2012 048 * @version $Author$, $Revision$, $Date$ 049 * @param <T> The type of value produced 050 */ 051public abstract class TimeBasedValueAnimator<T> implements ValueAnimator<T> 052{ 053 /** The time the animation started */ 054 private long startTime = 0; 055 056 /** The length of the animation */ 057 private long animationLength = 0; 058 059 /** The start value of the animation */ 060 protected T startValue = null; 061 062 /** The end/target value of the animation */ 063 protected T endValue = null; 064 065 /** The current percentage */ 066 private double currentPC = 0; 067 068 /** 069 * @param initial The start value for the animator 070 * @param end The end value for the animator 071 * @param millis The length of time the animation should run 072 */ 073 public TimeBasedValueAnimator( final T initial, final T end, final long millis ) 074 { 075 this.startValue = initial; 076 this.endValue = end; 077 this.animationLength = millis; 078 } 079 080 /** 081 * Given a percentage value (0 < pc <=1), the method should return a value 082 * for the animation. 083 * 084 * @param pc the percentage value 085 * @return The animator value 086 */ 087 protected abstract T calculateValue( double pc ); 088 089 /** 090 * {@inheritDoc} 091 * @see org.openimaj.content.animation.animator.ValueAnimator#nextValue() 092 */ 093 @Override 094 public T nextValue() 095 { 096 final long currentTime = System.currentTimeMillis(); 097 if( this.startTime == 0 ) 098 this.startTime = currentTime; 099 100 this.currentPC = (currentTime - this.startTime) / (double)this.animationLength; 101 102 if( this.isComplete() ) return this.endValue; 103 104 return this.calculateValue( this.currentPC ); 105 } 106 107 /** 108 * {@inheritDoc} 109 * @see org.openimaj.content.animation.animator.ValueAnimator#reset() 110 */ 111 @Override 112 public void reset() 113 { 114 this.startTime = 0; 115 this.currentPC = 0; 116 } 117 118 /** 119 * Returns whether the animator has completed 120 * @return TRUE if the animator has completed. 121 */ 122 public boolean isComplete() 123 { 124 return this.currentPC >= 1.0; 125 } 126 127 /** 128 * A implementation sugar for {@link #isComplete()} 129 * 130 * {@inheritDoc} 131 * @see org.openimaj.content.animation.animator.ValueAnimator#hasFinished() 132 */ 133 @Override 134 public boolean hasFinished() 135 { 136 return this.isComplete(); 137 } 138}