// When new data is loaded in, rebuild the frames and the text labels over the timeline
    // to appropriate size
    public void respawnWithFrameCount(int count)
    {
        UpdateKeyInfoList();

        if (count == 0)
        {
            count = 55;
        }

        if (hasBeenResized == false)
        {
            hasBeenResized = true;
        }

        //Updated by Chengde to reserve keyframe data when changing total number of frames
        ArrayList           oldKeys    = new ArrayList();
        List <KeyframeInfo> oldKeyInfo = new List <KeyframeInfo>();

        /////////////// 12.09.2013 //////////////////////

        if (keys != null)
        {
            //saveKeyframeData()

            //Updated by CHENGDE to reserve keyframe data when changing total number of frames
            for (int i = 0; i < keys.Count; i++)
            {
                oldKeys.Add(keys[i]);
            }
            /////////////// 12.09.2013 //////////////////////

            maxScrollLength = count;
            for (int i = 0; i < keys.Count; i++)
            {
                keys[i] = null;
            }
        }

        //Updated by CHENGDE to reserve keyframe data when changing total number of frames
        if (keyInfo.Count > 0)
        {
            for (int i = 0; i < keyInfo.Count; i++)
            {
                oldKeyInfo.Add(keyInfo[i]);
            }
        }
        /////////////// 12.09.2013 //////////////////////

        keyInfo = new List <KeyframeInfo>(count);

        keys = new ArrayList();
        for (int i = 0; i < count; i++)
        {
            KeyframeTexture keyTexture = new KeyframeTexture();
            keyTexture.x = i * 8;
            keyTexture.y = 20;
            if (i % 5 == 0)
            {
                keyTexture.isMultipleFive = true;
            }

            keys.Add(keyTexture);
            keyInfo.Add(new KeyframeInfo());
        }


        currentFrame = 1;
        labelBar     = new LabelBarTexture(count);

        keyInfo[0] = new KeyframeInfo();
        keyInfo[0].position(new Vector3(150f, 50f, 0f));
        keyInfo[0].rotation(new Quaternion(0.0f, 0.0f, 0.0f, 0.0f));
        keyInfo[0].isKeyed = true;
        keyInfo[count - 1] = new KeyframeInfo();
        keyInfo[count - 1].position(new Vector3(0f, 0f, 0f));
        keyInfo[count - 1].rotation(new Quaternion(0.0f, 0.0f, 0.0f, 0.0f));
        keyInfo[count - 1].isKeyed = true;

        //Updated by CHENGDE to reserve keyframe data when changing total number of frames
        if (keys != null)
        {
            int keyInfoCnt = (oldKeyInfo.Count < keyInfo.Count)?oldKeyInfo.Count:keyInfo.Count;
            for (int i = 0; i < keyInfoCnt; i++)
            {
                keyInfo[i] = oldKeyInfo[i];
            }

            int keyCnt = (oldKeys.Count < keys.Count)? oldKeys.Count: keys.Count;
            for (int i = 0; i < keyCnt; i++)
            {
                keys[i] = oldKeys[i];
            }
        }
        /////////////// 12.09.2013 //////////////////////


        gotoFrame(1);
        updateFrameDisplay();
        //Debug.Log("Rebuilding the Keyframebar took: " + (startTime - endTime) + " ms");
    }
    private void interpolateKeys(int i, out Quaternion rotation, out Vector3 position, out float dorsalAmount, out Vector2 lPelvic, out Vector2 rPelvic, out Vector2 lPectoral, out Vector2 rPectoral, out Vector2 anal)
    {
        KeyframeInfo lastKey = new KeyframeInfo();
        KeyframeInfo nextKey = new KeyframeInfo();
        int j = 0;
        int k = 0;
        // Check past keys
        for(j = i; j >= 0; j--)
        {
            if(keyInfo[j].isKeyed){
                lastKey = (KeyframeInfo)keyInfo[j];
                break;
            }
        }

        for(k = i; k < keyInfo.Capacity; k++)
        {
            if(keyInfo[k].isKeyed){
                nextKey = (KeyframeInfo)keyInfo[k];
                break;
            }
        }

        //Debug.Log("Last : " + j + " , next : " + k + ", and current: " + i + ", float: " + test);
        position = Vector3.Lerp(lastKey.getPosition(), nextKey.getPosition(), (float)(i - j)/(k - j));
        rotation = Quaternion.Slerp(lastKey.getRotation(), nextKey.getRotation(), (float)(i - j)/(k - j));
        dorsalAmount = Mathf.Lerp(lastKey.dorsalAngle, nextKey.dorsalAngle, (float)(i-j)/(k-j));

        lPelvic.x = Mathf.Lerp(lastKey.lpelvicAngles.x, nextKey.lpelvicAngles.x, (float)(i-j)/(k-j));
        lPelvic.y = Mathf.Lerp(lastKey.lpelvicAngles.y, nextKey.lpelvicAngles.y, (float)(i-j)/(k-j));

        rPelvic.x = Mathf.Lerp(lastKey.rpelvicAngles.x, nextKey.rpelvicAngles.x, (float)(i-j)/(k-j));
        rPelvic.y = Mathf.Lerp(lastKey.rpelvicAngles.y, nextKey.rpelvicAngles.y, (float)(i-j)/(k-j));

        lPectoral.x = Mathf.Lerp(lastKey.lpectAngles.x, nextKey.lpectAngles.x, (float)(i-j)/(k-j));
        lPectoral.y = Mathf.Lerp(lastKey.lpectAngles.y, nextKey.lpectAngles.y, (float)(i-j)/(k-j));

        rPectoral.x = Mathf.Lerp(lastKey.rpectAngles.x, nextKey.rpectAngles.x, (float)(i-j)/(k-j));
        rPectoral.y = Mathf.Lerp(lastKey.rpectAngles.y, nextKey.rpectAngles.y, (float)(i-j)/(k-j));

        anal.x = Mathf.Lerp(lastKey.analAngles.x, nextKey.analAngles.x, (float)(i-j)/(k-j));
        anal.y = Mathf.Lerp(lastKey.analAngles.y, nextKey.analAngles.y, (float)(i-j)/(k-j));
    }
 private void setKey()
 {
     Debug.Log("KeyframeBar::setKey ");
     keyInfo[currentFrame] = new KeyframeInfo();
     keyInfo[currentFrame].isKeyed = true;
     //KeyframeInfo test = (KeyframeInfo)keyInfo[currentFrame];
     keyInfo[currentFrame].position(keyedObject.transform.position);
     keyInfo[currentFrame].rotation(keyedObject.transform.rotation);
     Debug.Log(keyedObject.transform.position);
     //test.position(new Vector3(pos.x, pos.y, pos.z));
     if(AnimationEditorState.dorsalAmount != 0.0f)
     keyInfo[currentFrame].dorsalAngle = AnimationEditorState.dorsalAmount;
     keyInfo[currentFrame].lpelvicAngles = new Vector2(AnimationEditorState.lpelvicHAmount, AnimationEditorState.lpelvicVAmount);
     keyInfo[currentFrame].rpelvicAngles = new Vector2(AnimationEditorState.rpelvicHAmount, AnimationEditorState.rpelvicVAmount);
     keyInfo[currentFrame].lpectAngles = new Vector2(AnimationEditorState.lpectoralHAmount, AnimationEditorState.lpectoralVAmount);  // Mahmoud 02.17.14
     keyInfo[currentFrame].rpectAngles = new Vector2(AnimationEditorState.rpectoralHAmount, AnimationEditorState.rpectoralVAmount);  // Mahmoud 02.17.14
     keyInfo[currentFrame].analAngles = new Vector2(AnimationEditorState.analHAmount, AnimationEditorState.analVAmount);
     updateFrameDisplay();
 }
    // When new data is loaded in, rebuild the frames and the text labels over the timeline
    // to appropriate size
    public void respawnWithFrameCount(int count)
    {
        UpdateKeyInfoList();

        if(count == 0)
            count = 55;

        if(hasBeenResized == false)
        {
            hasBeenResized = true;
        }

        //Updated by Chengde to reserve keyframe data when changing total number of frames
        ArrayList oldKeys = new ArrayList();
        List<KeyframeInfo> oldKeyInfo = new List<KeyframeInfo>();
        /////////////// 12.09.2013 //////////////////////

        if(keys != null)
        {
            //saveKeyframeData()

            //Updated by CHENGDE to reserve keyframe data when changing total number of frames
            for(int i = 0; i < keys.Count; i++)
                oldKeys.Add(keys[i]);
            /////////////// 12.09.2013 //////////////////////

            maxScrollLength = count;
            for(int i = 0; i < keys.Count; i++)
            {
                keys[i] = null;
            }
        }

        //Updated by CHENGDE to reserve keyframe data when changing total number of frames
        if(keyInfo.Count > 0)
            for(int i=0; i<keyInfo.Count; i++)
                oldKeyInfo.Add (keyInfo[i]);
        /////////////// 12.09.2013 //////////////////////

        keyInfo = new List<KeyframeInfo>(count);

        keys = new ArrayList();
        for(int i = 0; i < count; i++)
        {
            KeyframeTexture keyTexture = new KeyframeTexture();
            keyTexture.x = i * 8;
            keyTexture.y = 20;
            if(i % 5 == 0)
                keyTexture.isMultipleFive = true;

            keys.Add(keyTexture);
            keyInfo.Add(new KeyframeInfo());
        }

        currentFrame = 1;
        labelBar = new LabelBarTexture(count);

        keyInfo[0] = new KeyframeInfo();
        keyInfo[0].position(new Vector3(150f, 50f, 0f));
        keyInfo[0].rotation(new Quaternion(0.0f, 0.0f, 0.0f, 0.0f));
        keyInfo[0].isKeyed = true;
        keyInfo[count-1 ] = new KeyframeInfo();
        keyInfo[count-1].position(new Vector3(0f, 0f, 0f));
        keyInfo[count-1].rotation(new Quaternion(0.0f, 0.0f, 0.0f, 0.0f));
        keyInfo[count-1].isKeyed = true;

        //Updated by CHENGDE to reserve keyframe data when changing total number of frames
        if(keys != null)
        {
            int keyInfoCnt = (oldKeyInfo.Count<keyInfo.Count)?oldKeyInfo.Count:keyInfo.Count;
            for(int i=0; i<keyInfoCnt; i++)
                keyInfo[i] = oldKeyInfo[i];

            int keyCnt = (oldKeys.Count<keys.Count)? oldKeys.Count: keys.Count;
            for(int i=0; i<keyCnt; i++)
                keys[i] = oldKeys[i];
        }
        /////////////// 12.09.2013 //////////////////////

        gotoFrame(1);
        updateFrameDisplay();
        //Debug.Log("Rebuilding the Keyframebar took: " + (startTime - endTime) + " ms");
    }