001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/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 short 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 ShortArrayValueAnimator implements ValueAnimator<short[]> {
043        ValueAnimator<Short> [] animators;
044        
045        /**
046         * Construct from an array of {@link ValueAnimator}s
047         * @param animators the animators
048         */
049        @SafeVarargs
050        public ShortArrayValueAnimator(ValueAnimator<Short>... animators) {
051                this.animators = animators;
052        }
053        
054        /**
055         * Construct a new ShortArrayValueAnimator from an array of 
056         * values using {@link LinearShortValueAnimator}s with the 
057         * range -max to +max and the given duration.
058         * 
059         * @param duration the duration of the underlying animators 
060         * @param maxs max distance from 0 (+/-)
061         * @return new {@link ShortArrayValueAnimator}
062         */
063        @SuppressWarnings("unchecked")
064        public static ShortArrayValueAnimator makeLinear(int duration, short... maxs) {
065                ValueAnimator<Short> [] animators = new ValueAnimator[maxs.length];
066                
067                for (int i=0; i<maxs.length; i++)
068                        animators[i] = new LinearShortValueAnimator((short)(-maxs[i]), maxs[i], duration);
069                
070                return new ShortArrayValueAnimator(animators);
071        }
072        
073        /**
074         * Construct a new ShortArrayValueAnimator from count 
075         * {@link LinearShortValueAnimator}s with the given start 
076         * and finish values, and the given duration.
077         * 
078         * @param duration the duration
079         * @param count the number of animators
080         * @param start the starting value
081         * @param finish the finishing value
082         * @return new {@link ShortArrayValueAnimator}
083         */
084        @SuppressWarnings("unchecked")
085        public static ShortArrayValueAnimator makeLinear(int duration, int count, short start, short finish) {
086                ValueAnimator<Short> [] animators = new ValueAnimator[count];
087                
088                for (int i=0; i<count; i++)
089                        animators[i] = new LinearShortValueAnimator(start, finish, duration);
090                
091                return new ShortArrayValueAnimator(animators);
092        }
093        
094        /**
095         * Construct a new ShortArrayValueAnimator from an array of 
096         * values using {@link RandomLinearShortValueAnimator}s with the 
097         * range -max to +max and the given duration.
098         * 
099         * @param duration the duration of the underlying animators 
100         * @param maxs max distance from 0 (+/-)
101         * @return new {@link ShortArrayValueAnimator}
102         */
103        @SuppressWarnings("unchecked")
104        public static ShortArrayValueAnimator makeRandomLinear(int duration, short... maxs) {
105                ValueAnimator<Short> [] animators = new ValueAnimator[maxs.length];
106                
107                for (int i=0; i<maxs.length; i++)
108                        animators[i] = new RandomLinearShortValueAnimator((short)(-maxs[i]), maxs[i], duration);
109                
110                return new ShortArrayValueAnimator(animators);
111        }
112        
113        /**
114         * Construct a new ShortArrayValueAnimator from count 
115         * {@link RandomLinearShortValueAnimator}s with the given start 
116         * and finish values, and the given duration.
117         * 
118         * @param duration the duration
119         * @param count the number of animators
120         * @param start the starting value
121         * @param finish the finishing value
122         * @return new {@link ShortArrayValueAnimator}
123         */
124        @SuppressWarnings("unchecked")
125        public static ShortArrayValueAnimator makeRandomLinear(int duration, int count, short start, short finish) {
126                ValueAnimator<Short> [] animators = new ValueAnimator[count];
127                
128                for (int i=0; i<count; i++)
129                        animators[i] = new RandomLinearShortValueAnimator(start, finish, duration);
130                
131                return new ShortArrayValueAnimator(animators);
132        }
133        
134        @Override
135        public short[] nextValue() {
136                short[] value = new short[animators.length];
137                
138                for (int i=0; i<animators.length; i++)
139                        value[i] = animators[i].nextValue();
140                
141                return value;
142        }
143
144        @Override
145        public boolean hasFinished() {
146                for (ValueAnimator<Short> a : animators) {
147                        if (!a.hasFinished())
148                                return false;
149                }
150                return true;
151        }
152
153        @Override
154        public void reset() {
155                for (ValueAnimator<Short> animator : animators)
156                        animator.reset();
157        }
158}