001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/content/animation/src/main/jtemp/org/openimaj/content/animation/animator/Linear#TT#ValueAnimator.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} that linearly animates a Long value between two values.
038 * 
039 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
040 */
041public class LinearLongValueAnimator extends AbstractValueAnimator<Long> implements ReversableValueAnimator<Long> {
042        long start;
043        long finish;
044        long incr;
045        long current;
046        
047        /**
048         * Construct a {@link LinearLongValueAnimator} with the given
049         * start and finish values, and the given duration in ticks 
050         * (number of calls to {@link #nextValue()}. The animation
051         * starts immediately and completes once duration ticks have been 
052         * reached. 
053         * 
054         * @param start start value
055         * @param finish stop value
056         * @param duration
057         */
058        public LinearLongValueAnimator(long start, long finish, int duration) {
059                super(start, 0, 0);
060                
061                current = start;
062                this.start = start;
063                this.finish = finish;
064                this.incr = (long)((finish-start) / (long)duration);
065        }
066        
067        /**
068         * Construct a {@link LinearLongValueAnimator} with the given
069         * start and finish values, and the given duration in ticks 
070         * (number of calls to {@link #nextValue()}. The animation will
071         * start after startWait ticks, and finish stopWait ticks after
072         * startWait+duration.
073         * 
074         * @param startWait amount of time in ticks to wait before starting animation.
075         * @param stopWait amount of time in ticks to wait after finishing animation. 
076         * @param start start value
077         * @param finish stop value
078         * @param duration
079         */
080        public LinearLongValueAnimator(int startWait, int stopWait, long start, long finish, int duration) {
081                super(start, startWait, stopWait);
082                
083                current = start;
084                this.start = start;
085                this.finish = finish;
086                this.incr = (long)((finish-start) / (long)duration);
087        }
088        
089        @Override
090        public Long makeNextValue() {
091                current += incr;
092                
093                if (start<finish && current > finish) 
094                        current = finish;
095                else if (start>finish && current < finish) 
096                        current = finish;
097                
098                return current;
099        }
100
101        @Override
102        protected boolean complete() {
103                if (incr == 0) return true;
104                
105                long next = (long)(current+incr);
106                
107                if (start<finish && next-0.0000001 > finish) 
108                        return true;
109                else if (start>finish && next+0.0000001 < finish) 
110                        return true;
111                return false;
112        }
113
114        @Override
115        protected void resetToInitial() {
116                current = start;
117        }
118
119        @Override
120        public ReversableValueAnimator<Long> reverseAnimator() {
121                long curr = current;
122                long sta = start;
123
124                reset();
125
126                current = curr;
127                incr *= -1;
128                start=finish;
129                finish = sta;
130                
131                return this;
132        }
133}