Bläddra i källkod

WIP: Curve editor window
- Initial version of the window that can be used for editing curves & curve ranges (similar to animation window, but more general)

BearishSun 7 år sedan
förälder
incheckning
be1e9d1577

+ 1 - 0
Source/Scripting/MBansheeEditor/MBansheeEditor.csproj

@@ -90,6 +90,7 @@
     <Compile Include="Inspectors\SliderJointInspector.cs" />
     <Compile Include="Inspectors\SphereColliderInspector.cs" />
     <Compile Include="Inspectors\SphericalJointInspector.cs" />
+    <Compile Include="Windows\CurveEditorWindow.cs" />
     <Compile Include="Windows\GradientPicker.cs" />
     <Compile Include="Windows\Inspector\InspectableRRef.cs" />
     <Compile Include="Windows\Inspector\Style\InspectableFieldRangeStyle.cs" />

+ 21 - 13
Source/Scripting/MBansheeEditor/Windows/Animation/GUICurveDrawing.cs

@@ -579,7 +579,7 @@ namespace BansheeEditor
         /// <param name="color">Color to draw the curve with.</param>
         private void DrawCurve(EdAnimationCurve curve, Color color)
         {
-            float range = GetRange();
+            float range = GetRange(true);
             float lengthPerPixel = range / drawableWidth;
 
             KeyFrame[] keyframes = curve.KeyFrames;
@@ -588,24 +588,28 @@ namespace BansheeEditor
 
             // Draw start line
             {
-                float curveStart = MathEx.Clamp(keyframes[0].time, 0.0f, range);
-                float curveValue = curve.Evaluate(0.0f, false);
-
-                Vector2I start = CurveToPixelSpace(new Vector2(0.0f, curveValue));
-                start.x -= GUIGraphTime.PADDING;
+                float curveStart = keyframes[0].time;
+                float curveValue = curve.Evaluate(curveStart, false);
 
                 Vector2I end = CurveToPixelSpace(new Vector2(curveStart, curveValue));
+                Vector2I start = new Vector2I(-GUIGraphTime.PADDING, end.y);
 
-                canvas.DrawLine(start, end, COLOR_MID_GRAY);
+                if(start.x < end.x)
+                    canvas.DrawLine(start, end, COLOR_MID_GRAY);
             }
 
             List<Vector2I> linePoints = new List<Vector2I>();
 
             // Draw in between keyframes
+            float startVisibleTime = rangeOffset;
+            float endVisibleTime = startVisibleTime + range;
             for (int i = 0; i < keyframes.Length - 1; i++)
             {
-                float start = MathEx.Clamp(keyframes[i].time, 0.0f, range);
-                float end = MathEx.Clamp(keyframes[i + 1].time, 0.0f, range);
+                float start = keyframes[i].time;
+                float end = keyframes[i + 1].time;
+
+                if (end < startVisibleTime || start > endVisibleTime)
+                    continue;
 
                 bool isStep = keyframes[i].outTangent == float.PositiveInfinity ||
                               keyframes[i + 1].inTangent == float.PositiveInfinity;
@@ -665,13 +669,14 @@ namespace BansheeEditor
 
             // Draw end line
             {
-                float curveEnd = MathEx.Clamp(keyframes[keyframes.Length - 1].time, 0.0f, range);
-                float curveValue = curve.Evaluate(range, false);
+                float curveEnd = keyframes[keyframes.Length - 1].time;
+                float curveValue = curve.Evaluate(curveEnd, false);
 
                 Vector2I start = CurveToPixelSpace(new Vector2(curveEnd, curveValue));
                 Vector2I end = new Vector2I(width, start.y);
 
-                canvas.DrawLine(start, end, COLOR_MID_GRAY);
+                if(start.x < end.x)
+                    canvas.DrawLine(start, end, COLOR_MID_GRAY);
             }
         }
 
@@ -682,7 +687,7 @@ namespace BansheeEditor
         /// <param name="color">Color to draw the area with.</param>
         private void DrawCurveRange(EdAnimationCurve[] curves, Color color)
         {
-            float range = GetRange();
+            float range = GetRange(true);
 
             if(curves.Length != 2 || curves[0] == null || curves[1] == null)
                 return;
@@ -695,6 +700,9 @@ namespace BansheeEditor
             float timePerSample = range / numSamples;
 
             float time = rangeOffset;
+            float lengthPerPixel = rangeLength / drawableWidth;
+            time -= lengthPerPixel * PADDING;
+
             int[] keyframeIndices = {0, 0};
 
             // Find first valid keyframe indices

+ 8 - 2
Source/Scripting/MBansheeEditor/Windows/Animation/GUICurveEditor.cs

@@ -879,7 +879,10 @@ namespace BansheeEditor
             position.x = guiEvents.GetOffset(animEvent.time);
             position.y = EVENTS_HEIGHT/2;
 
-            Rect2I eventBounds = GUIUtility.CalculateBounds(eventsPanel, window.GUI);
+            Rect2I eventBounds = new Rect2I();
+            if (showEvents)
+                eventBounds = GUIUtility.CalculateBounds(eventsPanel, window.GUI);
+
             Vector2I windowPos = position + new Vector2I(eventBounds.x, eventBounds.y);
 
             if (eventsSO == null)
@@ -940,7 +943,10 @@ namespace BansheeEditor
             Rect2I drawingBounds = drawingPanel.Bounds;
             Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
 
-            Rect2I eventBounds = eventsPanel.Bounds;
+            Rect2I eventBounds = new Rect2I();
+            if(showEvents)
+                eventBounds = eventsPanel.Bounds;
+
             Vector2I eventPos = pointerPos - new Vector2I(eventBounds.x, eventBounds.y);
 
             if (ev.Button == PointerButton.Left)

+ 150 - 0
Source/Scripting/MBansheeEditor/Windows/CurveEditorWindow.cs

@@ -0,0 +1,150 @@
+//********************************** Banshee Engine (www.banshee5d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Opens a window that allows the user to edit a single animation curve or two curves representing a range.
+    /// </summary>
+    [DefaultSize(600, 400)]
+    public class CurveEditorWindow : EditorWindow
+    {
+        private GUICurveEditor curveEditor;
+
+        #region Overrides
+
+        // DEBUG ONLY
+        [MenuItem("Windows/Dbg", 5000)]
+        public static void Open()
+        {
+            OpenWindow<CurveEditorWindow>();
+        }
+
+        /// <inheritdoc/>
+        protected override LocString GetDisplayName()
+        {
+            return new LocEdString("Curve editor");
+        }
+
+        private void OnInitialize()
+        {
+            // TODO - Add methods to allow the window to be open with user-defined curve(s)
+            // TODO - Add callbacks that trigger when user finishes editing
+            // TODO - Add OK/Cancel buttons? Make the window modal?
+            // TODO - Add a CurveField GUI element that can be used for curve preview, clicking on which opens this window
+
+            curveEditor = new GUICurveEditor(this, this.GUI, 600, 400, false);
+            curveEditor.Redraw();
+
+            EdAnimationCurve[] edAnimCurve =
+            {
+                new EdAnimationCurve(),
+                new EdAnimationCurve()
+            };
+
+            edAnimCurve[0].AddKeyframe(0.0f, 1.0f);
+            edAnimCurve[0].AddKeyframe(5.0f, 3.0f);
+            edAnimCurve[0].AddKeyframe(8.0f, -3.0f);
+            edAnimCurve[0].AddKeyframe(15.0f, 2.0f);
+            edAnimCurve[0].Apply();
+
+            edAnimCurve[1].AddKeyframe(0.0f, -3.0f);
+            edAnimCurve[1].AddKeyframe(3.0f, 0.0f);
+            edAnimCurve[1].AddKeyframe(10.0f, -1.0f);
+            edAnimCurve[1].AddKeyframe(13.0f, -5.0f);
+            edAnimCurve[1].Apply();
+
+            CurveDrawInfo[] drawinfo =
+            {
+                new CurveDrawInfo(edAnimCurve[0], Color.Green),
+                new CurveDrawInfo(edAnimCurve[1], Color.Red),
+            };
+
+            curveEditor.SetCurves(drawinfo);
+            curveEditor.CenterAndResize(true);
+            curveEditor.SetDrawRange(true);
+
+            EditorInput.OnPointerPressed += OnPointerPressed;
+            EditorInput.OnPointerDoubleClick += OnPointerDoubleClicked;
+            EditorInput.OnPointerMoved += OnPointerMoved;
+            EditorInput.OnPointerReleased += OnPointerReleased;
+            EditorInput.OnButtonUp += OnButtonUp;
+        }
+
+        private void OnEditorUpdate()
+        {
+            curveEditor.HandleDragAndZoomInput();
+        }
+
+        private void OnDestroy()
+        {
+            EditorInput.OnPointerPressed -= OnPointerPressed;
+            EditorInput.OnPointerDoubleClick -= OnPointerDoubleClicked;
+            EditorInput.OnPointerMoved -= OnPointerMoved;
+            EditorInput.OnPointerReleased -= OnPointerReleased;
+            EditorInput.OnButtonUp -= OnButtonUp;
+        }
+
+        /// <inheritdoc/>
+        protected override void WindowResized(int width, int height)
+        {
+            Vector2I curveEditorSize = new Vector2I(width, height);
+            curveEditor.SetSize(curveEditorSize.x, curveEditorSize.y);
+            curveEditor.Redraw();
+        }
+
+        #endregion
+
+        #region Input callbacks
+        /// <summary>
+        /// Triggered when the user presses a mouse button.
+        /// </summary>
+        /// <param name="ev">Information about the mouse press event.</param>
+        private void OnPointerPressed(PointerEvent ev)
+        {
+            curveEditor.OnPointerPressed(ev);
+
+        }
+
+        /// <summary>
+        /// Triggered when the user double clicks the left mouse button.
+        /// </summary>
+        /// <param name="ev">Information about the mouse event.</param>
+        private void OnPointerDoubleClicked(PointerEvent ev)
+        {
+            curveEditor.OnPointerDoubleClicked(ev);
+        }
+
+        /// <summary>
+        /// Triggered when the user moves the mouse.
+        /// </summary>
+        /// <param name="ev">Information about the mouse move event.</param>
+        private void OnPointerMoved(PointerEvent ev)
+        {
+            curveEditor.OnPointerMoved(ev);
+
+        }
+
+        /// <summary>
+        /// Triggered when the user releases a mouse button.
+        /// </summary>
+        /// <param name="ev">Information about the mouse release event.</param>
+        private void OnPointerReleased(PointerEvent ev)
+        {
+            curveEditor.OnPointerReleased(ev);
+        }
+
+        /// <summary>
+        /// Triggered when the user releases a keyboard button.
+        /// </summary>
+        /// <param name="ev">Information about the keyboard release event.</param>
+        private void OnButtonUp(ButtonEvent ev)
+        {
+            curveEditor.OnButtonUp(ev);
+        }
+        #endregion
+    }
+
+    /** @} */
+}