1cfd74d65d832137e20e193c960802afba73b5d38sm/* 23c1e67e433728684b5f228c5d4f3e5b1457bb271sm * Copyright (C) 2010 The Android Open Source Project 3cfd74d65d832137e20e193c960802afba73b5d38sm * 4cfd74d65d832137e20e193c960802afba73b5d38sm * Licensed under the Apache License, Version 2.0 (the "License"); 5cfd74d65d832137e20e193c960802afba73b5d38sm * you may not use this file except in compliance with the License. 6cfd74d65d832137e20e193c960802afba73b5d38sm * You may obtain a copy of the License at 7cfd74d65d832137e20e193c960802afba73b5d38sm * 8cfd74d65d832137e20e193c960802afba73b5d38sm * http://www.apache.org/licenses/LICENSE-2.0 9cfd74d65d832137e20e193c960802afba73b5d38sm * 10cfd74d65d832137e20e193c960802afba73b5d38sm * Unless required by applicable law or agreed to in writing, software 11cfd74d65d832137e20e193c960802afba73b5d38sm * distributed under the License is distributed on an "AS IS" BASIS, 12cfd74d65d832137e20e193c960802afba73b5d38sm * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cfd74d65d832137e20e193c960802afba73b5d38sm * See the License for the specific language governing permissions and 14cfd74d65d832137e20e193c960802afba73b5d38sm * limitations under the License. 15cfd74d65d832137e20e193c960802afba73b5d38sm */ 16cfd74d65d832137e20e193c960802afba73b5d38sm 17cfd74d65d832137e20e193c960802afba73b5d38smpackage com.replica.replicaisland; 18cfd74d65d832137e20e193c960802afba73b5d38sm 19cfd74d65d832137e20e193c960802afba73b5d38smimport java.util.Arrays; 20cfd74d65d832137e20e193c960802afba73b5d38sm 21cfd74d65d832137e20e193c960802afba73b5d38sm/** 22cfd74d65d832137e20e193c960802afba73b5d38sm * Describes a single animation for a sprite. 23cfd74d65d832137e20e193c960802afba73b5d38sm */ 24cfd74d65d832137e20e193c960802afba73b5d38smpublic class SpriteAnimation extends PhasedObject { 25cfd74d65d832137e20e193c960802afba73b5d38sm private final static int LINEAR_SEARCH_CUTOFF = 16; 26cfd74d65d832137e20e193c960802afba73b5d38sm 27cfd74d65d832137e20e193c960802afba73b5d38sm private FixedSizeArray<AnimationFrame> mFrames; 28cfd74d65d832137e20e193c960802afba73b5d38sm private float[] mFrameStartTimes; 29cfd74d65d832137e20e193c960802afba73b5d38sm private boolean mLoop; 30cfd74d65d832137e20e193c960802afba73b5d38sm private float mLength; 31cfd74d65d832137e20e193c960802afba73b5d38sm 32cfd74d65d832137e20e193c960802afba73b5d38sm public SpriteAnimation(int animationId, int frameCount) { 33cfd74d65d832137e20e193c960802afba73b5d38sm super(); 34cfd74d65d832137e20e193c960802afba73b5d38sm mFrames = new FixedSizeArray<AnimationFrame>(frameCount); 35cfd74d65d832137e20e193c960802afba73b5d38sm mFrameStartTimes = new float[frameCount]; 36cfd74d65d832137e20e193c960802afba73b5d38sm mLoop = false; 37cfd74d65d832137e20e193c960802afba73b5d38sm mLength = 0.0f; 38cfd74d65d832137e20e193c960802afba73b5d38sm setPhase(animationId); 39cfd74d65d832137e20e193c960802afba73b5d38sm } 40cfd74d65d832137e20e193c960802afba73b5d38sm 41cfd74d65d832137e20e193c960802afba73b5d38sm public AnimationFrame getFrame(float animationTime) { 42cfd74d65d832137e20e193c960802afba73b5d38sm AnimationFrame result = null; 43cfd74d65d832137e20e193c960802afba73b5d38sm final float length = mLength; 44cfd74d65d832137e20e193c960802afba73b5d38sm if (length > 0.0f) { 45cfd74d65d832137e20e193c960802afba73b5d38sm final FixedSizeArray<AnimationFrame> frames = mFrames; 46cfd74d65d832137e20e193c960802afba73b5d38sm assert frames.getCount() == frames.getCapacity(); 47cfd74d65d832137e20e193c960802afba73b5d38sm final int frameCount = frames.getCount(); 48cfd74d65d832137e20e193c960802afba73b5d38sm result = frames.get(frameCount - 1); 49cfd74d65d832137e20e193c960802afba73b5d38sm 50cfd74d65d832137e20e193c960802afba73b5d38sm if (frameCount > 1) { 51cfd74d65d832137e20e193c960802afba73b5d38sm float currentTime = 0.0f; 52cfd74d65d832137e20e193c960802afba73b5d38sm float cycleTime = animationTime; 53cfd74d65d832137e20e193c960802afba73b5d38sm if (mLoop) { 54cfd74d65d832137e20e193c960802afba73b5d38sm cycleTime = animationTime % length; 55cfd74d65d832137e20e193c960802afba73b5d38sm } 56cfd74d65d832137e20e193c960802afba73b5d38sm 57cfd74d65d832137e20e193c960802afba73b5d38sm if (cycleTime < length) { 58cfd74d65d832137e20e193c960802afba73b5d38sm // When there are very few frames it's actually slower to do a binary search 59cfd74d65d832137e20e193c960802afba73b5d38sm // of the frame list. So we'll use a linear search for small animations 60cfd74d65d832137e20e193c960802afba73b5d38sm // and only pull the binary search out when the frame count is large. 61cfd74d65d832137e20e193c960802afba73b5d38sm if (mFrameStartTimes.length > LINEAR_SEARCH_CUTOFF) { 62cfd74d65d832137e20e193c960802afba73b5d38sm int index = Arrays.binarySearch(mFrameStartTimes, cycleTime); 63cfd74d65d832137e20e193c960802afba73b5d38sm if (index < 0) { 64cfd74d65d832137e20e193c960802afba73b5d38sm index = -(index + 1) - 1; 65cfd74d65d832137e20e193c960802afba73b5d38sm } 66cfd74d65d832137e20e193c960802afba73b5d38sm result = frames.get(index); 67cfd74d65d832137e20e193c960802afba73b5d38sm } else { 68cfd74d65d832137e20e193c960802afba73b5d38sm for (int x = 0; x < frameCount; x++) { 69cfd74d65d832137e20e193c960802afba73b5d38sm AnimationFrame frame = frames.get(x); 70cfd74d65d832137e20e193c960802afba73b5d38sm currentTime += frame.holdTime; 71cfd74d65d832137e20e193c960802afba73b5d38sm if (currentTime > cycleTime) { 72cfd74d65d832137e20e193c960802afba73b5d38sm result = frame; 73cfd74d65d832137e20e193c960802afba73b5d38sm break; 74cfd74d65d832137e20e193c960802afba73b5d38sm } 75cfd74d65d832137e20e193c960802afba73b5d38sm } 76cfd74d65d832137e20e193c960802afba73b5d38sm } 77cfd74d65d832137e20e193c960802afba73b5d38sm } 78cfd74d65d832137e20e193c960802afba73b5d38sm } 79cfd74d65d832137e20e193c960802afba73b5d38sm } 80cfd74d65d832137e20e193c960802afba73b5d38sm return result; 81cfd74d65d832137e20e193c960802afba73b5d38sm } 82cfd74d65d832137e20e193c960802afba73b5d38sm 83cfd74d65d832137e20e193c960802afba73b5d38sm public void addFrame(AnimationFrame frame) { 84cfd74d65d832137e20e193c960802afba73b5d38sm mFrameStartTimes[mFrames.getCount()] = mLength; 85cfd74d65d832137e20e193c960802afba73b5d38sm mFrames.add(frame); 86cfd74d65d832137e20e193c960802afba73b5d38sm mLength += frame.holdTime; 87cfd74d65d832137e20e193c960802afba73b5d38sm } 88cfd74d65d832137e20e193c960802afba73b5d38sm 89cfd74d65d832137e20e193c960802afba73b5d38sm public float getLength() { 90cfd74d65d832137e20e193c960802afba73b5d38sm return mLength; 91cfd74d65d832137e20e193c960802afba73b5d38sm } 92cfd74d65d832137e20e193c960802afba73b5d38sm 93cfd74d65d832137e20e193c960802afba73b5d38sm public void setLoop(boolean loop) { 94cfd74d65d832137e20e193c960802afba73b5d38sm mLoop = loop; 95cfd74d65d832137e20e193c960802afba73b5d38sm } 96cfd74d65d832137e20e193c960802afba73b5d38sm 97cfd74d65d832137e20e193c960802afba73b5d38sm public boolean getLoop() { 98cfd74d65d832137e20e193c960802afba73b5d38sm return mLoop; 99cfd74d65d832137e20e193c960802afba73b5d38sm } 100cfd74d65d832137e20e193c960802afba73b5d38sm} 101