Jelajahi Sumber

Bugfix: Adding a new animation curve now adds an initial keyframe with the current value, to avoid resetting the animated value to 0

BearishSun 7 tahun lalu
induk
melakukan
b3c78b2fca
1 mengubah file dengan 111 tambahan dan 111 penghapusan
  1. 111 111
      Source/Scripting/MBansheeEditor/Windows/AnimationWindow.cs

+ 111 - 111
Source/Scripting/MBansheeEditor/Windows/AnimationWindow.cs

@@ -804,18 +804,7 @@ namespace BansheeEditor
         }
         }
 
 
         /// <summary>
         /// <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.
+        /// Stops animation preview on the selected object and resets it back to its non-animated state.
         /// </summary>
         /// </summary>
         private void ClearSO()
         private void ClearSO()
         {
         {
@@ -825,12 +814,6 @@ namespace BansheeEditor
                 if (animation != null)
                 if (animation != null)
                     animation.EditorStop();
                     animation.EditorStop();
             }
             }
-
-            if (soState != null)
-            {
-                soState.Restore();
-                soState = null;
-            }
         }
         }
 
 
         #endregion
         #endregion
@@ -968,7 +951,6 @@ namespace BansheeEditor
         }
         }
 
 
         private State state = State.Empty;
         private State state = State.Empty;
-        private SerializedSceneObject soState;
         private bool delayRecord = false;
         private bool delayRecord = false;
 
 
         /// <summary>
         /// <summary>
@@ -1038,15 +1020,12 @@ namespace BansheeEditor
                     switch (state)
                     switch (state)
                     {
                     {
                         case State.Normal:
                         case State.Normal:
-                            InitializeSO();
                             PreviewFrame(currentFrameIdx);
                             PreviewFrame(currentFrameIdx);
                             break;
                             break;
                         case State.Playback:
                         case State.Playback:
-                            InitializeSO();
                             StartPlayback();
                             StartPlayback();
                             break;
                             break;
                         case State.Recording:
                         case State.Recording:
-                            InitializeSO();
                             StartRecord();
                             StartRecord();
                             break;
                             break;
                     }
                     }
@@ -1133,6 +1112,25 @@ namespace BansheeEditor
         /// <param name="time">Time for which to record the state, in seconds.</param>
         /// <param name="time">Time for which to record the state, in seconds.</param>
         /// <returns>True if any changes were recorded, false otherwise.</returns>
         /// <returns>True if any changes were recorded, false otherwise.</returns>
         private bool RecordState(float time)
         private bool RecordState(float time)
+        {
+            bool changesMade = false;
+            foreach (var KVP in clipInfo.curves)
+            {
+                if (RecordState(KVP.Key, time))
+                    changesMade = true;
+            }
+
+            return changesMade;
+        }
+
+        /// <summary>
+        /// Records the state of the provided property and adds it as a keyframe to the related animation curve (or updates
+        /// an existing keyframe if the value is different).
+        /// </summary>
+        /// <param name="path">Path to the property whose state to record.</param>
+        /// <param name="time">Time for which to record the state, in seconds.</param>
+        /// <returns>True if any changes were recorded, false otherwise.</returns>
+        private bool RecordState(string path, float time)
         {
         {
             Action<EdAnimationCurve, float, float> addOrUpdateKeyframe = (curve, keyTime, keyValue) =>
             Action<EdAnimationCurve, float, float> addOrUpdateKeyframe = (curve, keyTime, keyValue) =>
             {
             {
@@ -1155,131 +1153,132 @@ namespace BansheeEditor
                 curve.Apply();
                 curve.Apply();
             };
             };
 
 
+            FieldAnimCurves curves;
+            if (!clipInfo.curves.TryGetValue(path, out curves))
+                return false;
+
+            string suffix;
+            SerializableProperty property = Animation.FindProperty(selectedSO, path, out suffix);
+
+            if (property == null)
+                return false;
+
             bool changesMade = false;
             bool changesMade = false;
-            foreach (var KVP in clipInfo.curves)
+            switch (curves.type)
             {
             {
-                string suffix;
-                SerializableProperty property = Animation.FindProperty(selectedSO, KVP.Key, out suffix);
-
-                if (property == null)
-                    continue;
+                case SerializableProperty.FieldType.Vector2:
+                    {
+                        Vector2 value = property.GetValue<Vector2>();
 
 
-                switch (KVP.Value.type)
-                {
-                    case SerializableProperty.FieldType.Vector2:
+                        for (int i = 0; i < 2; i++)
                         {
                         {
-                            Vector2 value = property.GetValue<Vector2>();
-
-                            for (int i = 0; i < 2; i++)
+                            float curveVal = curves.curveInfos[i].curve.Evaluate(time);
+                            if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
                             {
                             {
-                                float curveVal = KVP.Value.curveInfos[i].curve.Evaluate(time);
-                                if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
-                                {
-                                    addOrUpdateKeyframe(KVP.Value.curveInfos[i].curve, time, value[i]);
-                                    changesMade = true;
-                                }
+                                addOrUpdateKeyframe(curves.curveInfos[i].curve, time, value[i]);
+                                changesMade = true;
                             }
                             }
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Vector3:
-                        {
-                            Vector3 value = property.GetValue<Vector3>();
+                    }
+                    break;
+                case SerializableProperty.FieldType.Vector3:
+                    {
+                        Vector3 value = property.GetValue<Vector3>();
 
 
-                            for (int i = 0; i < 3; i++)
+                        for (int i = 0; i < 3; i++)
+                        {
+                            float curveVal = curves.curveInfos[i].curve.Evaluate(time);
+                            if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
                             {
                             {
-                                float curveVal = KVP.Value.curveInfos[i].curve.Evaluate(time);
-                                if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
-                                { 
-                                    addOrUpdateKeyframe(KVP.Value.curveInfos[i].curve, time, value[i]);
-                                    changesMade = true;
-                                }
+                                addOrUpdateKeyframe(curves.curveInfos[i].curve, time, value[i]);
+                                changesMade = true;
                             }
                             }
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Vector4:
+                    }
+                    break;
+                case SerializableProperty.FieldType.Vector4:
+                    {
+                        if (property.InternalType == typeof(Vector4))
                         {
                         {
-                            if (property.InternalType == typeof(Vector4))
-                            {
-                                Vector4 value = property.GetValue<Vector4>();
+                            Vector4 value = property.GetValue<Vector4>();
 
 
-                                for (int i = 0; i < 4; i++)
-                                {
-                                    float curveVal = KVP.Value.curveInfos[i].curve.Evaluate(time);
-                                    if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
-                                    { 
-                                        addOrUpdateKeyframe(KVP.Value.curveInfos[i].curve, time, value[i]);
-                                        changesMade = true;
-                                    }
-                                }
-                            }
-                            else if (property.InternalType == typeof(Quaternion))
+                            for (int i = 0; i < 4; i++)
                             {
                             {
-                                Quaternion value = property.GetValue<Quaternion>();
-
-                                for (int i = 0; i < 4; i++)
+                                float curveVal = curves.curveInfos[i].curve.Evaluate(time);
+                                if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
                                 {
                                 {
-                                    float curveVal = KVP.Value.curveInfos[i].curve.Evaluate(time);
-                                    if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
-                                    { 
-                                        addOrUpdateKeyframe(KVP.Value.curveInfos[i].curve, time, value[i]);
-                                        changesMade = true;
-                                    }
+                                    addOrUpdateKeyframe(curves.curveInfos[i].curve, time, value[i]);
+                                    changesMade = true;
                                 }
                                 }
                             }
                             }
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Color:
+                        else if (property.InternalType == typeof(Quaternion))
                         {
                         {
-                            Color value = property.GetValue<Color>();
+                            Quaternion value = property.GetValue<Quaternion>();
 
 
                             for (int i = 0; i < 4; i++)
                             for (int i = 0; i < 4; i++)
                             {
                             {
-                                float curveVal = KVP.Value.curveInfos[i].curve.Evaluate(time);
+                                float curveVal = curves.curveInfos[i].curve.Evaluate(time);
                                 if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
                                 if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
-                                { 
-                                    addOrUpdateKeyframe(KVP.Value.curveInfos[i].curve, time, value[i]);
+                                {
+                                    addOrUpdateKeyframe(curves.curveInfos[i].curve, time, value[i]);
                                     changesMade = true;
                                     changesMade = true;
                                 }
                                 }
                             }
                             }
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Bool:
-                        {
-                            bool value = property.GetValue<bool>();
+                    }
+                    break;
+                case SerializableProperty.FieldType.Color:
+                    {
+                        Color value = property.GetValue<Color>();
 
 
-                            bool curveVal = KVP.Value.curveInfos[0].curve.Evaluate(time) > 0.0f;
-                            if (value != curveVal)
-                            { 
-                                addOrUpdateKeyframe(KVP.Value.curveInfos[0].curve, time, value ? 1.0f : -1.0f);
+                        for (int i = 0; i < 4; i++)
+                        {
+                            float curveVal = curves.curveInfos[i].curve.Evaluate(time);
+                            if (!MathEx.ApproxEquals(value[i], curveVal, 0.001f))
+                            {
+                                addOrUpdateKeyframe(curves.curveInfos[i].curve, time, value[i]);
                                 changesMade = true;
                                 changesMade = true;
                             }
                             }
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Int:
-                        {
-                            int value = property.GetValue<int>();
+                    }
+                    break;
+                case SerializableProperty.FieldType.Bool:
+                    {
+                        bool value = property.GetValue<bool>();
 
 
-                            int curveVal = (int)KVP.Value.curveInfos[0].curve.Evaluate(time);
-                            if (value != curveVal)
-                            { 
-                                addOrUpdateKeyframe(KVP.Value.curveInfos[0].curve, time, value);
-                                changesMade = true;
-                            }
+                        bool curveVal = curves.curveInfos[0].curve.Evaluate(time) > 0.0f;
+                        if (value != curveVal)
+                        {
+                            addOrUpdateKeyframe(curves.curveInfos[0].curve, time, value ? 1.0f : -1.0f);
+                            changesMade = true;
                         }
                         }
-                        break;
-                    case SerializableProperty.FieldType.Float:
+                    }
+                    break;
+                case SerializableProperty.FieldType.Int:
+                    {
+                        int value = property.GetValue<int>();
+
+                        int curveVal = (int)curves.curveInfos[0].curve.Evaluate(time);
+                        if (value != curveVal)
                         {
                         {
-                            float value = property.GetValue<float>();
+                            addOrUpdateKeyframe(curves.curveInfos[0].curve, time, value);
+                            changesMade = true;
+                        }
+                    }
+                    break;
+                case SerializableProperty.FieldType.Float:
+                    {
+                        float value = property.GetValue<float>();
 
 
-                            float curveVal = KVP.Value.curveInfos[0].curve.Evaluate(time);
-                            if (!MathEx.ApproxEquals(value, curveVal, 0.001f))
-                            { 
-                                addOrUpdateKeyframe(KVP.Value.curveInfos[0].curve, time, value);
-                                changesMade = true;
-                            }
+                        float curveVal = curves.curveInfos[0].curve.Evaluate(time);
+                        if (!MathEx.ApproxEquals(value, curveVal, 0.001f))
+                        {
+                            addOrUpdateKeyframe(curves.curveInfos[0].curve, time, value);
+                            changesMade = true;
                         }
                         }
-                        break;
-                }
+                    }
+                    break;
             }
             }
 
 
             return changesMade;
             return changesMade;
@@ -1855,6 +1854,7 @@ namespace BansheeEditor
             pathNoRoot = pathNoRoot.Substring(separatorIdx + 1, pathNoRoot.Length - separatorIdx - 1);
             pathNoRoot = pathNoRoot.Substring(separatorIdx + 1, pathNoRoot.Length - separatorIdx - 1);
 
 
             AddNewField(pathNoRoot, type);
             AddNewField(pathNoRoot, type);
+            RecordState(pathNoRoot, 0.0f);
             ApplyClipChanges();
             ApplyClipChanges();
         }
         }