Parcourir la source

Feature: AnimationWindow double-click to add keyframe

BearishSun il y a 7 ans
Parent
commit
093fc4020c

+ 36 - 1
Source/Scripting/MBansheeEditor/Windows/Animation/GUICurveDrawing.cs

@@ -126,6 +126,41 @@ namespace BansheeEditor
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Attempts to find a curve under the provided coordinates.
+        /// </summary>
+        /// <param name="pixelCoords">Coordinates relative to this GUI element in pixels.</param>
+        /// <returns>Index of the curve, or -1 if none found.</returns>
+        public int FindCurve(Vector2I pixelCoords)
+        {
+            PixelToCurveSpace(pixelCoords, out var curveCoords, true);
+
+            float time = curveCoords.x;
+
+            float nearestDistance = float.MaxValue;
+            int curveIdx = -1;
+            for (int i = 0; i < curveInfos.Length; i++)
+            {
+                EdAnimationCurve curve = curveInfos[i].curve;
+
+                float value = curve.Evaluate(time, false);
+                Vector2I curPixelPos = CurveToPixelSpace(new Vector2(time, value));
+
+                float distanceToKey = Vector2I.Distance(pixelCoords, curPixelPos);
+                if (distanceToKey < nearestDistance)
+                {
+                    nearestDistance = distanceToKey;
+                    curveIdx = i;
+                }
+            }
+
+            // We're not near any curve
+            if (nearestDistance > 5.0f)
+                return -1;
+
+            return curveIdx;
+        }
+
         /// <summary>
         /// <summary>
         /// Attempts to find a keyframe under the provided coordinates.
         /// Attempts to find a keyframe under the provided coordinates.
         /// </summary>
         /// </summary>
@@ -489,7 +524,7 @@ namespace BansheeEditor
             {
             {
                 EdAnimationCurve[] curves = {curveInfos[0].curve, curveInfos[1].curve};
                 EdAnimationCurve[] curves = {curveInfos[0].curve, curveInfos[1].curve};
 
 
-                DrawCurveRange(curves, new Color(1.0f, 0.0f, 0.0f, 0.7f));
+                DrawCurveRange(curves, new Color(1.0f, 0.0f, 0.0f, 0.3f));
                 curvesToDraw = 2;
                 curvesToDraw = 2;
             }
             }
 
 

+ 60 - 0
Source/Scripting/MBansheeEditor/Windows/Animation/GUICurveEditor.cs

@@ -506,6 +506,37 @@ namespace BansheeEditor
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Handles input. Should be called by the owning window whenever a pointer is double-clicked.
+        /// </summary>
+        /// <param name="ev">Object containing pointer press event information.</param>
+        internal void OnPointerDoubleClicked(PointerEvent ev)
+        {
+            if (ev.IsUsed)
+                return;
+
+            Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
+
+            Rect2I elementBounds = GUIUtility.CalculateBounds(gui, window.GUI);
+            Vector2I pointerPos = windowPos - new Vector2I(elementBounds.x, elementBounds.y);
+
+            bool isOverEditor = pointerPos.x >= 0 && pointerPos.x < width && pointerPos.y >= 0 && pointerPos.y < height;
+            if (!isOverEditor)
+                return;
+
+            Rect2I drawingBounds = drawingPanel.Bounds;
+            Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
+
+            if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out var curveCoord, true))
+            {
+                int curveIdx = guiCurveDrawing.FindCurve(drawingPos);
+                if (curveIdx == -1)
+                    return;
+
+                AddKeyframe(curveIdx, curveCoord.x);
+            }
+        }
+
         /// <summary>
         /// <summary>
         /// Handles input. Should be called by the owning window whenever a pointer is moved.
         /// Handles input. Should be called by the owning window whenever a pointer is moved.
         /// </summary>
         /// </summary>
@@ -926,6 +957,35 @@ namespace BansheeEditor
             guiCurveDrawing.Rebuild();
             guiCurveDrawing.Rebuild();
         }
         }
 
 
+        /// <summary>
+        /// Adds a new keyframe at the specified time on the provided curve.
+        /// </summary>
+        /// <param name="curveIdx">Index of the curve to add the keyframe to.</param>
+        /// <param name="time">Time at which to add the keyframe.</param>
+        private void AddKeyframe(int curveIdx, float time)
+        {
+            ClearSelection();
+
+            if (!disableCurveEdit)
+            {
+                if (curveIdx < curveInfos.Length)
+                {
+                    EdAnimationCurve curve = curveInfos[curveIdx].curve;
+
+                    float value = curve.Evaluate(time, false);
+
+                    curve.AddOrUpdateKeyframe(time, value);
+                    curve.Apply();
+                }
+            }
+            else
+                ShowReadOnlyMessage();
+
+            OnCurveModified?.Invoke();
+            guiCurveDrawing.Rebuild();
+            UpdateEventsGUI();
+        }
+
         /// <summary>
         /// <summary>
         /// Adds a new keyframe at the position the context menu was opened at.
         /// Adds a new keyframe at the position the context menu was opened at.
         /// </summary>
         /// </summary>

+ 12 - 0
Source/Scripting/MBansheeEditor/Windows/AnimationWindow.cs

@@ -95,6 +95,7 @@ namespace BansheeEditor
             if (selectedSO != null)
             if (selectedSO != null)
             {
             {
                 EditorInput.OnPointerPressed -= OnPointerPressed;
                 EditorInput.OnPointerPressed -= OnPointerPressed;
+                EditorInput.OnPointerDoubleClick -= OnPointerDoubleClicked;
                 EditorInput.OnPointerMoved -= OnPointerMoved;
                 EditorInput.OnPointerMoved -= OnPointerMoved;
                 EditorInput.OnPointerReleased -= OnPointerReleased;
                 EditorInput.OnPointerReleased -= OnPointerReleased;
                 EditorInput.OnButtonUp -= OnButtonUp;
                 EditorInput.OnButtonUp -= OnButtonUp;
@@ -756,6 +757,7 @@ namespace BansheeEditor
                 if (selectedSO != null && so == null)
                 if (selectedSO != null && so == null)
                 {
                 {
                     EditorInput.OnPointerPressed -= OnPointerPressed;
                     EditorInput.OnPointerPressed -= OnPointerPressed;
+                    EditorInput.OnPointerDoubleClick -= OnPointerDoubleClicked;
                     EditorInput.OnPointerMoved -= OnPointerMoved;
                     EditorInput.OnPointerMoved -= OnPointerMoved;
                     EditorInput.OnPointerReleased -= OnPointerReleased;
                     EditorInput.OnPointerReleased -= OnPointerReleased;
                     EditorInput.OnButtonUp -= OnButtonUp;
                     EditorInput.OnButtonUp -= OnButtonUp;
@@ -763,6 +765,7 @@ namespace BansheeEditor
                 else if (selectedSO == null && so != null)
                 else if (selectedSO == null && so != null)
                 {
                 {
                     EditorInput.OnPointerPressed += OnPointerPressed;
                     EditorInput.OnPointerPressed += OnPointerPressed;
+                    EditorInput.OnPointerDoubleClick += OnPointerDoubleClicked;
                     EditorInput.OnPointerMoved += OnPointerMoved;
                     EditorInput.OnPointerMoved += OnPointerMoved;
                     EditorInput.OnPointerReleased += OnPointerReleased;
                     EditorInput.OnPointerReleased += OnPointerReleased;
                     EditorInput.OnButtonUp += OnButtonUp;
                     EditorInput.OnButtonUp += OnButtonUp;
@@ -1927,6 +1930,15 @@ namespace BansheeEditor
             }
             }
         }
         }
 
 
+        /// <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)
+        {
+            guiCurveEditor.OnPointerDoubleClicked(ev);
+        }
+
         /// <summary>
         /// <summary>
         /// Triggered when the user moves the mouse.
         /// Triggered when the user moves the mouse.
         /// </summary>
         /// </summary>