/*private void PreviewAnimationEvent( FAnimationEventEditor animEvtEditor, int frame )
         * {
         *      FPlayAnimationEvent animEvt = (FPlayAnimationEvent)animEvtEditor._evt;
         *
         *      if( animEvt._animationClip == null )
         *              return;
         *
         *      bool isEditable = Flux.FUtility.IsAnimationEditable(animEvt._animationClip);
         *
         *      // render path
         *      if( isEditable )
         *      {
         *              TransformCurves transformCurves = new TransformCurves(animEvt.Owner, animEvt._animationClip);
         *
         *              RenderTransformPath( transformCurves, animEvt.LengthTime, 1f/animEvt.Sequence.FrameRate );
         *
         *              float t = (float)(frame + animEvt._startOffset - animEvt.Start) / animEvt.Sequence.FrameRate;
         *
         *              if( animEvt.FrameRange.Contains( frame ) )
         *              {
         * //					float t = (float)(frame + animEvt._startOffset - animEvt.Start) / animEvt.Sequence.FrameRate;
         *                      RenderTransformAnimation( transformCurves, t );
         *              }
         *
         * //				AnimationClipCurveData[] allCurves = AnimationUtility.GetAllCurves( animEvt._animationClip, true );
         * //				foreach( AnimationClipCurveData curve in allCurves )
         * //				{
         * //
         * //				}
         *      }
         *      else if( animEvt.FrameRange.Contains( frame ) )
         *      {
         *              float t = (float)(frame + animEvt._startOffset - animEvt.Start) / animEvt.Sequence.FrameRate;
         *
         *              bool wasInAnimationMode = AnimationMode.InAnimationMode();
         *
         *              if( !AnimationMode.InAnimationMode() )
         *              {
         *                      AnimationMode.StartAnimationMode();
         *              }
         *              AnimationMode.BeginSampling();
         *              AnimationMode.SampleAnimationClip( animEvt.Owner.gameObject, animEvt._animationClip, t );
         *              AnimationMode.EndSampling();
         *
         *              if( !wasInAnimationMode )
         *                      AnimationMode.StopAnimationMode();
         *      }
         * }*/


        private void RenderTransformPath(TransformCurves transformCurves, float length, float samplingDelta)
        {
            float t = 0;

            int numberSamples = Mathf.RoundToInt(length / samplingDelta) + 1;

            float delta = length / numberSamples;

            Vector3[] pts = new Vector3[numberSamples];

            int index = 0;

            while (index < numberSamples)
            {
                pts[index++] = transformCurves.GetPosition(t);
                t           += delta;
            }

            if (index != pts.Length)
            {
                Debug.LogError("Number of samples doesn't match: " + (index + 1) + " instead of " + pts.Length);
            }

            Handles.DrawPolyLine(pts);
        }
        private void RenderTransformAnimation(TransformCurves transformCurves, float time)
        {
            Vector3    pos   = transformCurves.GetPosition(time);//new Vector3( xPos.Evaluate(t), yPos.Evaluate(t), zPos.Evaluate(t) );
            Quaternion rot   = transformCurves.GetRotation(time);
            Vector3    scale = transformCurves.GetScale(time);

            transformCurves.bone.localScale    = scale;
            transformCurves.bone.localRotation = rot;
            transformCurves.bone.localPosition = pos;

            Handles.RectangleCap(0, pos, rot, 0.1f);
            Handles.RectangleCap(0, pos + rot * Vector3.forward, rot, 0.4f);
        }
        public void RenderTransformCurves(int samplesPerSecond)
        {
            if (_transformCurves == null || _transformCurves.clip == null)
            {
                CreateTransformCurves();
            }
            else
            {
                _transformCurves.RefreshCurves();
            }

            float totalTime     = AnimEvt.LengthTime;
            float timePerSample = totalTime / samplesPerSecond;

            int numSamples = Mathf.RoundToInt(totalTime / timePerSample) + 1;

            Vector3[] pts = new Vector3[numSamples];
            float     t   = 0;

            for (int i = 0; i < numSamples; ++i)
            {
                pts[i] = _transformCurves.GetWorldPosition(t);
                t     += timePerSample;
            }

            Handles.DrawPolyLine(pts);

            FAnimationTrackEditor animTrackEditor = (FAnimationTrackEditor)TrackEditor;

//			if( _transformCurves.clip.name.Contains("1") )
//				Debug.Log( _transformCurves.xRot.keys[0].tangentMode + " " + _transformCurves.yRot[0].tangentMode + " " + _transformCurves.zRot[0].tangentMode + " " + _transformCurves.wRot[0].tangentMode );

            if (animTrackEditor.ShowKeyframes || animTrackEditor.ShowKeyframeTimes)
            {
                int animFramerate = Mathf.RoundToInt(_transformCurves.clip.frameRate);

                Keyframe[] keyframes = _transformCurves.GetPositionKeyframes();
                for (int i = 0; i != keyframes.Length; ++i)
                {
                    Keyframe keyframe = keyframes[i];

                    Vector3    pos = _transformCurves.GetPosition(keyframe.time);
                    Quaternion rot = _transformCurves.GetRotation(keyframe.time);

                    Quaternion toolRot          = rot;
                    bool       isGlobalRotation = Tools.pivotRotation == PivotRotation.Global;
                    if (isGlobalRotation)
                    {
                        toolRot = _globalRotation;                        //Quaternion.identity;
                    }
//					Handles.color = Color.gray;
                    //				Vector3 newPos = Handles.FreeMoveHandle(pos, rot, 1, Vector3.one*0.2f, Handles.SphereCap);//Handles.PositionHandle( pos, rot );
                    if (animTrackEditor.ShowKeyframes)
                    {
                        if (Tools.current == Tool.Move)
                        {
                            Vector3 newPos = Handles.DoPositionHandle(pos, toolRot);                             //Handles.PositionHandle( pos, rot );
                            if (newPos != pos)
                            {
                                Undo.RecordObject(_transformCurves.clip, "Change Keyframe");
                                _transformCurves.SetPosition(newPos, keyframe.time);
                            }
                        }
                        else if (Tools.current == Tool.Rotate)
                        {
                            Quaternion newRot = Handles.DoRotationHandle(toolRot, pos);
                            if (newRot != toolRot)
                            {
                                Undo.RecordObject(_transformCurves.clip, "Change Keyframe");
                                _transformCurves.SetRotation(isGlobalRotation ? (newRot * Quaternion.Inverse(toolRot)) * rot : newRot, keyframe.time);
                                if (isGlobalRotation)
                                {
                                    _globalRotation = newRot;
                                }
                            }
                        }
                    }

                    if (Event.current.type == EventType.MouseUp || Event.current.type == EventType.Ignore)
                    {
                        _globalRotation = Quaternion.identity;
                    }

                    if (animTrackEditor.ShowKeyframeTimes)
                    {
                        int frame = Mathf.RoundToInt(keyframe.time * animFramerate);
                        Handles.Label(pos + new Vector3(0, .25f, 0), FUtility.GetTime(AnimEvt.Start + frame, animFramerate), EditorStyles.toolbarButton);
                    }

                    //				Handles.BeginGUI();
                    //				Vector3 screenPos = SceneView.currentDrawingSceneView.camera.WorldToScreenPoint( pos );
                    //				GUI.Label( new Rect( screenPos.x-10, SceneView.currentDrawingSceneView.position.height-screenPos.y-50, 20, 20 ), keyframe.time.ToString() );
                    //				Handles.EndGUI();
                }
            }
        }