001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jon/Work/openimaj/tags/openimaj-1.3.1/content/animation/src/main/jtemp/org/openimaj/content/animation/animator/#TT#ArrayValueAnimator.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.content.animation.animator;
035
036/**
037 * A {@link ValueAnimator} capable of producing double arrays from
038 * a set of potentially independent underlying {@link ValueAnimator}s. 
039 * 
040 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
041 */
042public class DoubleArrayValueAnimator implements ValueAnimator<double[]> {
043        ValueAnimator<Double> [] animators;
044        
045        /**
046         * Construct from an array of {@link ValueAnimator}s
047         * @param animators the animators
048         */
049        public DoubleArrayValueAnimator(ValueAnimator<Double>... animators) {
050                this.animators = animators;
051        }
052        
053        /**
054         * Construct a new DoubleArrayValueAnimator from an array of 
055         * values using {@link LinearDoubleValueAnimator}s with the 
056         * range -max to +max and the given duration.
057         * 
058         * @param duration the duration of the underlying animators 
059         * @param maxs max distance from 0 (+/-)
060         * @return new {@link DoubleArrayValueAnimator}
061         */
062        @SuppressWarnings("unchecked")
063        public static DoubleArrayValueAnimator makeLinear(int duration, double... maxs) {
064                ValueAnimator<Double> [] animators = new ValueAnimator[maxs.length];
065                
066                for (int i=0; i<maxs.length; i++)
067                        animators[i] = new LinearDoubleValueAnimator((double)(-maxs[i]), maxs[i], duration);
068                
069                return new DoubleArrayValueAnimator(animators);
070        }
071        
072        /**
073         * Construct a new DoubleArrayValueAnimator from count 
074         * {@link LinearDoubleValueAnimator}s with the given start 
075         * and finish values, and the given duration.
076         * 
077         * @param duration the duration
078         * @param count the number of animators
079         * @param start the starting value
080         * @param finish the finishing value
081         * @return new {@link DoubleArrayValueAnimator}
082         */
083        @SuppressWarnings("unchecked")
084        public static DoubleArrayValueAnimator makeLinear(int duration, int count, double start, double finish) {
085                ValueAnimator<Double> [] animators = new ValueAnimator[count];
086                
087                for (int i=0; i<count; i++)
088                        animators[i] = new LinearDoubleValueAnimator(start, finish, duration);
089                
090                return new DoubleArrayValueAnimator(animators);
091        }
092        
093        /**
094         * Construct a new DoubleArrayValueAnimator from an array of 
095         * values using {@link RandomLinearDoubleValueAnimator}s with the 
096         * range -max to +max and the given duration.
097         * 
098         * @param duration the duration of the underlying animators 
099         * @param maxs max distance from 0 (+/-)
100         * @return new {@link DoubleArrayValueAnimator}
101         */
102        @SuppressWarnings("unchecked")
103        public static DoubleArrayValueAnimator makeRandomLinear(int duration, double... maxs) {
104                ValueAnimator<Double> [] animators = new ValueAnimator[maxs.length];
105                
106                for (int i=0; i<maxs.length; i++)
107                        animators[i] = new RandomLinearDoubleValueAnimator((double)(-maxs[i]), maxs[i], duration);
108                
109                return new DoubleArrayValueAnimator(animators);
110        }
111        
112        /**
113         * Construct a new DoubleArrayValueAnimator from count 
114         * {@link RandomLinearDoubleValueAnimator}s with the given start 
115         * and finish values, and the given duration.
116         * 
117         * @param duration the duration
118         * @param count the number of animators
119         * @param start the starting value
120         * @param finish the finishing value
121         * @return new {@link DoubleArrayValueAnimator}
122         */
123        @SuppressWarnings("unchecked")
124        public static DoubleArrayValueAnimator makeRandomLinear(int duration, int count, double start, double finish) {
125                ValueAnimator<Double> [] animators = new ValueAnimator[count];
126                
127                for (int i=0; i<count; i++)
128                        animators[i] = new RandomLinearDoubleValueAnimator(start, finish, duration);
129                
130                return new DoubleArrayValueAnimator(animators);
131        }
132        
133        @Override
134        public double[] nextValue() {
135                double[] value = new double[animators.length];
136                
137                for (int i=0; i<animators.length; i++)
138                        value[i] = animators[i].nextValue();
139                
140                return value;
141        }
142
143        @Override
144        public boolean hasFinished() {
145                for (ValueAnimator<Double> a : animators) {
146                        if (!a.hasFinished())
147                                return false;
148                }
149                return true;
150        }
151
152        @Override
153        public void reset() {
154                for (ValueAnimator<Double> animator : animators)
155                        animator.reset();
156        }
157}