Browse Source

Calculate visible range of curves whenever selected curves change in the animation editor

BearishSun 9 years ago
parent
commit
0bc1cca0f2

+ 24 - 3
Source/MBansheeEditor/Windows/Animation/GUICurveEditor.cs

@@ -52,6 +52,8 @@ namespace BansheeEditor
         private Vector2I contextClickPosition;
 
         private EdAnimationCurve[] curves = new EdAnimationCurve[0];
+        private float xRange;
+        private float yRange;
 
         private int markedFrameIdx;
         private List<SelectedKeyframes> selectedKeyframes = new List<SelectedKeyframes>();
@@ -64,6 +66,22 @@ namespace BansheeEditor
         private TangentRef draggedTangent;
         private Vector2I dragStart;
 
+        /// <summary>
+        /// Returns the displayed range of the curve on the x axis (time).
+        /// </summary>
+        public float XRange
+        {
+            get { return xRange; }
+        }
+
+        /// <summary>
+        /// Returns the displayed range of the curve on the y axis.
+        /// </summary>
+        public float YRange
+        {
+            get { return yRange; }
+        }
+
         public GUICurveEditor(EditorWindow window, GUILayout gui, int width, int height)
         {
             this.window = window;
@@ -376,12 +394,15 @@ namespace BansheeEditor
         /// </summary>
         /// <param name="xRange">Range of the horizontal area. Displayed area will range from [0, xRange].</param>
         /// <param name="yRange">Range of the vertical area. Displayed area will range from 
-        ///                      [-yRange * 0.5, yRange * 0.5]</param>
+        ///                      [-yRange, yRange]</param>
         public void SetRange(float xRange, float yRange)
         {
+            this.xRange = xRange;
+            this.yRange = yRange;
+
             guiTimeline.SetRange(xRange);
-            guiCurveDrawing.SetRange(xRange, yRange);
-            guiSidebar.SetRange(yRange * -0.5f, yRange * 0.5f);
+            guiCurveDrawing.SetRange(xRange, yRange * 2.0f);
+            guiSidebar.SetRange(yRange, yRange);
 
             Redraw();
         }

+ 37 - 0
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -246,9 +246,46 @@ namespace BansheeEditor
             }
 
             guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
+
+            float xRange;
+            float yRange;
+            CalculateRange(curvesToDisplay, out xRange, out yRange);
+
+            // Don't allow zero range
+            if (xRange == 0.0f)
+                xRange = 60.0f;
+
+            if (yRange == 0.0f)
+                yRange = 10.0f;
+
+            // Add padding to y range
+            yRange *= 1.05f;
+
+            // Don't reduce visible range
+            xRange = Math.Max(xRange, guiCurveEditor.XRange);
+            yRange = Math.Max(yRange, guiCurveEditor.YRange);
+
+            guiCurveEditor.SetRange(xRange, yRange);
             guiCurveEditor.Redraw();
         }
 
+        private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
+        {
+            xRange = 0.0f;
+            yRange = 0.0f;
+
+            foreach (var curve in curves)
+            {
+                KeyFrame[] keyframes = curve.KeyFrames;
+
+                foreach (var key in keyframes)
+                {
+                    xRange = Math.Max(xRange, key.time);
+                    yRange = Math.Max(yRange, Math.Abs(key.value));
+                }
+            }
+        }
+
         private void OnFieldAdded(string path, SerializableProperty.FieldType type)
         {
             guiFieldDisplay.AddField(path);