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 */ 030package org.openimaj.content.animation.animator; 031 032/** 033 * Base class for objects capable of "animating" a value; 034 * that is providing a new value everytime {@link #nextValue()} is 035 * called, subject to some constraints. 036 * 037 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 038 * 039 * @param <T> Type of value produced 040 */ 041public abstract class AbstractValueAnimator<T> implements ValueAnimator<T> { 042 private T currentValue; 043 044 /** 045 * Number of times {@link #nextValue()} has been called since construction or last reset 046 */ 047 private int currentCount = 0; 048 049 private int startWait = 0; 050 private int stopWait = 0; 051 private int completedAt = -1; 052 053 /** 054 * Construct with initial value 055 * @param initial initial value 056 * @param startWait amount of time in ticks to wait before starting animation. 057 * @param stopWait amount of time in ticks to wait after finishing animation. 058 */ 059 public AbstractValueAnimator(T initial, int startWait, int stopWait) { 060 currentValue = initial; 061 this.startWait = startWait; 062 this.stopWait = stopWait; 063 } 064 065 /** 066 * Get the next value. If the animator has finished, 067 * the continuation is checked to see if there are 068 * more animators to run. If not, then the last value 069 * is returned. 070 * 071 * @return the next value. 072 */ 073 @Override 074 public T nextValue() { 075 if (!(currentCount < startWait || hasFinished() || completedAt > 0)) { 076 currentValue = makeNextValue(); 077 } 078 079 currentCount++; 080 081 return currentValue; 082 } 083 084 protected abstract T makeNextValue(); 085 086 protected abstract void resetToInitial(); 087 088 @Override 089 public final void reset() { 090 resetToInitial(); 091 currentCount = 0; 092 completedAt = -1; 093 } 094 095 protected abstract boolean complete(); 096 097 @Override 098 public final boolean hasFinished() { 099 boolean comp = complete(); 100 101 if (!comp) 102 return false; 103 104 if (completedAt < 0) 105 completedAt = currentCount; 106 107 if (currentCount - completedAt < stopWait) 108 return false; 109 110 return true; 111 } 112}