Просмотр исходного кода

Updated animation editor so it supports frame scrubbing

BearishSun 9 лет назад
Родитель
Сommit
85657ce1dc

+ 1 - 1
Source/MBansheeEditor/Windows/Animation/GUICurveEditor.cs

@@ -729,7 +729,7 @@ namespace BansheeEditor
         /// <returns>Time of the frame with the provided index. </returns>
         /// <returns>Time of the frame with the provided index. </returns>
         public float GetTimeForFrame(int frameIdx)
         public float GetTimeForFrame(int frameIdx)
         {
         {
-            return guiCurveDrawing.GetTimeForFrame(markedFrameIdx);
+            return guiCurveDrawing.GetTimeForFrame(frameIdx);
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 97 - 18
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -82,7 +82,7 @@ namespace BansheeEditor
 
 
         private void OnDestroy()
         private void OnDestroy()
         {
         {
-            SwitchState(State.Normal);
+            SwitchState(State.Empty);
 
 
             Selection.OnSelectionChanged -= OnSelectionChanged;
             Selection.OnSelectionChanged -= OnSelectionChanged;
 
 
@@ -208,20 +208,20 @@ namespace BansheeEditor
 
 
             prevFrameButton.OnClick += () =>
             prevFrameButton.OnClick += () =>
             {
             {
-                SwitchState(State.Normal);
                 SetCurrentFrame(currentFrameIdx - 1);
                 SetCurrentFrame(currentFrameIdx - 1);
+                SwitchState(State.Normal);
             };
             };
 
 
             frameInputField.OnChanged += x =>
             frameInputField.OnChanged += x =>
             {
             {
-                SwitchState(State.Normal);
                 SetCurrentFrame(x);
                 SetCurrentFrame(x);
+                SwitchState(State.Normal);
             };
             };
 
 
             nextFrameButton.OnClick += () =>
             nextFrameButton.OnClick += () =>
             {
             {
-                SwitchState(State.Normal);
                 SetCurrentFrame(currentFrameIdx + 1);
                 SetCurrentFrame(currentFrameIdx + 1);
+                SwitchState(State.Normal);
             };
             };
 
 
             addKeyframeButton.OnClick += () =>
             addKeyframeButton.OnClick += () =>
@@ -286,6 +286,8 @@ namespace BansheeEditor
                                 animation.DefaultClip = newClip;
                                 animation.DefaultClip = newClip;
                                 EditorApplication.SetSceneDirty();
                                 EditorApplication.SetSceneDirty();
 
 
+                                soState = new SerializedSceneObject(selectedSO, true);
+
                                 openPropertyWindow();
                                 openPropertyWindow();
                             }
                             }
                         }
                         }
@@ -698,10 +700,11 @@ namespace BansheeEditor
                     EditorInput.OnButtonUp += OnButtonUp;
                     EditorInput.OnButtonUp += OnButtonUp;
                 }
                 }
 
 
-                zoomAmount = 0.0f;
+                SwitchState(State.Empty);
+
                 selectedSO = so;
                 selectedSO = so;
+                zoomAmount = 0.0f;
                 selectedFields.Clear();
                 selectedFields.Clear();
-
                 clipInfo = null;
                 clipInfo = null;
 
 
                 RebuildGUI();
                 RebuildGUI();
@@ -721,11 +724,43 @@ namespace BansheeEditor
                 if(clipInfo == null)
                 if(clipInfo == null)
                     clipInfo = new EditorAnimClipInfo();
                     clipInfo = new EditorAnimClipInfo();
 
 
-                if(selectedSO != null)
+                SwitchState(State.Normal);
+
+                if (selectedSO != null)
                     UpdateDisplayedCurves(true);
                     UpdateDisplayedCurves(true);
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Initializes the currently selected scene object. This should be called whenever the selected scene object 
+        /// changes.
+        /// </summary>
+        private void InitializeSO()
+        {
+            if (selectedSO != null)
+                soState = new SerializedSceneObject(selectedSO, true);
+        }
+
+        /// <summary>
+        /// Clears a selected scene object. This must be called on every scene object <see cref="InitializeSO"/> has been called
+        /// on, after operations on it are done.
+        /// </summary>
+        private void ClearSO()
+        {
+            if (selectedSO != null)
+            {
+                Animation animation = selectedSO.GetComponent<Animation>();
+                if (animation != null)
+                    animation.EditorStop();
+            }
+
+            if (soState != null)
+            {
+                soState.Restore();
+                soState = null;
+            }
+        }
+
         #endregion
         #endregion
 
 
         #region Record/Playback
         #region Record/Playback
@@ -735,12 +770,13 @@ namespace BansheeEditor
         /// </summary>
         /// </summary>
         private enum State
         private enum State
         {
         {
+            Empty,
             Normal,
             Normal,
             Recording,
             Recording,
             Playback
             Playback
         }
         }
 
 
-        private State state = State.Normal;
+        private State state = State.Empty;
         private SerializedSceneObject soState;
         private SerializedSceneObject soState;
 
 
         /// <summary>
         /// <summary>
@@ -761,6 +797,9 @@ namespace BansheeEditor
                         case State.Recording:
                         case State.Recording:
                             StartRecord();
                             StartRecord();
                         break;
                         break;
+                        case State.Empty:
+                            ClearSO();
+                        break;
                     }
                     }
                 }
                 }
                     break;
                     break;
@@ -770,12 +809,17 @@ namespace BansheeEditor
                     {
                     {
                         case State.Normal:
                         case State.Normal:
                             EndPlayback();
                             EndPlayback();
+                            PreviewFrame(currentFrameIdx);
                         break;
                         break;
                         case State.Recording:
                         case State.Recording:
                             EndPlayback();
                             EndPlayback();
                             StartRecord();
                             StartRecord();
                         break;
                         break;
-                    }
+                        case State.Empty:
+                            EndPlayback();
+                            ClearSO();
+                        break;
+                        }
                 }
                 }
                     break;
                     break;
                 case State.Recording:
                 case State.Recording:
@@ -784,14 +828,38 @@ namespace BansheeEditor
                     {
                     {
                         case State.Normal:
                         case State.Normal:
                             EndRecord();
                             EndRecord();
+                            PreviewFrame(currentFrameIdx);
                         break;
                         break;
                         case State.Playback:
                         case State.Playback:
                             EndRecord();
                             EndRecord();
                             StartPlayback();
                             StartPlayback();
                         break;
                         break;
+                        case State.Empty:
+                            EndRecord();
+                            ClearSO();
+                        break;
                     }
                     }
                 }
                 }
                     break;
                     break;
+                case State.Empty:
+                {
+                    switch (state)
+                    {
+                        case State.Normal:
+                            InitializeSO();
+                            PreviewFrame(currentFrameIdx);
+                            break;
+                        case State.Playback:
+                            InitializeSO();
+                            StartPlayback();
+                            break;
+                        case State.Recording:
+                            InitializeSO();
+                            StartRecord();
+                            break;
+                    }
+                }
+                break;
             }
             }
 
 
             this.state = state;
             this.state = state;
@@ -805,10 +873,8 @@ namespace BansheeEditor
             EditorAnimClipTangents unused;
             EditorAnimClipTangents unused;
             clipInfo.Apply(out unused);
             clipInfo.Apply(out unused);
 
 
-            soState = new SerializedSceneObject(selectedSO, true);
-
             Animation animation = selectedSO.GetComponent<Animation>();
             Animation animation = selectedSO.GetComponent<Animation>();
-            if (animation != null)
+            if (animation != null && clipInfo.clip != null)
             {
             {
                 float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
                 float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
 
 
@@ -823,14 +889,26 @@ namespace BansheeEditor
         /// </summary>
         /// </summary>
         private void EndPlayback()
         private void EndPlayback()
         {
         {
-            Animation animation = selectedSO.GetComponent<Animation>();
-            if (animation != null)
-                animation.EditorStop();
+            PreviewFrame(currentFrameIdx);
+            playButton.Value = false;
+        }
 
 
-            if(soState != null)
-                soState.Restore();
+        /// <summary>
+        /// Updates the visible animation to display the provided frame.
+        /// </summary>
+        /// <param name="frameIdx">Index of the animation frame to display.</param>
+        private void PreviewFrame(int frameIdx)
+        {
+            if (selectedSO == null)
+                return;
 
 
-            playButton.Value = false;
+            Animation animation = selectedSO.GetComponent<Animation>();
+            if (animation != null && clipInfo.clip != null)
+            {
+                float time = guiCurveEditor.GetTimeForFrame(frameIdx);
+
+                animation.EditorPlay(clipInfo.clip, time, true);
+            }
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -1611,6 +1689,7 @@ namespace BansheeEditor
         private void OnFrameSelected(int frameIdx)
         private void OnFrameSelected(int frameIdx)
         {
         {
             SetCurrentFrame(frameIdx);
             SetCurrentFrame(frameIdx);
+            PreviewFrame(currentFrameIdx);
         }
         }
 
 
         /// <summary>
         /// <summary>