Browse Source

Added red frame markers to GUITimeline and GUICurveDrawing

BearishSun 9 years ago
parent
commit
2a4b411700

+ 63 - 19
Source/MBansheeEditor/Windows/Animation/GUICurveDrawing.cs

@@ -22,6 +22,8 @@ namespace BansheeEditor
         private int height;
         private float xRange = 60.0f;
         private float yRange = 20.0f;
+        private int fps = 1;
+        private int frameMarkerIdx = -1;
 
         private int drawableWidth;
         private GUICanvas canvas;
@@ -36,6 +38,49 @@ namespace BansheeEditor
             SetSize(width, height);
         }
 
+        public void SetCurves(EdAnimationCurve[] curves)
+        {
+            this.curves = curves;
+
+            Rebuild();
+        }
+
+        public void SetSize(int width, int height)
+        {
+            this.width = width;
+            this.height = height;
+
+            canvas.SetWidth(width);
+            canvas.SetHeight(height);
+
+            drawableWidth = Math.Max(0, width - PADDING * 2);
+
+            Rebuild();
+        }
+
+        public void SetRange(float xRange, float yRange)
+        {
+            this.xRange = xRange;
+            this.yRange = yRange;
+
+            Rebuild();
+        }
+
+        public void SetFPS(int fps)
+        {
+            this.fps = Math.Max(1, fps);
+
+            Rebuild();
+        }
+
+        // Set to -1 to clear it
+        public void SetFrameMarker(int frameIdx)
+        {
+            frameMarkerIdx = frameIdx;
+
+            Rebuild();
+        }
+
         public bool GetCurveCoordinates(Vector2I windowCoords, out Vector2 curveCoords)
         {
             Rect2I bounds = canvas.Bounds;
@@ -61,32 +106,22 @@ namespace BansheeEditor
             return true;
         }
 
-        public void SetCurves(EdAnimationCurve[] curves)
-        {
-            this.curves = curves;
-
-            Rebuild();
-        }
-
-        public void SetSize(int width, int height)
+        private void DrawFrameMarker(float t)
         {
-            this.width = width;
-            this.height = height;
-
-            canvas.SetWidth(width);
-            canvas.SetHeight(height);
+            int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
 
-            drawableWidth = Math.Max(0, width - PADDING * 2);
+            Vector2I start = new Vector2I(xPos, 0);
+            Vector2I end = new Vector2I(xPos, height);
 
-            Rebuild();
+            canvas.DrawLine(start, end, Color.Red);
         }
 
-        public void SetRange(float xRange, float yRange)
+        // Returns range rounded to the nearest multiple of FPS
+        private float GetRange()
         {
-            this.xRange = xRange;
-            this.yRange = yRange;
+            float spf = 1.0f / fps;
 
-            Rebuild();
+            return ((int)xRange / spf) * spf;
         }
 
         private void Rebuild()
@@ -104,6 +139,15 @@ namespace BansheeEditor
                 // TODO - Pick unique color for each curve
                 DrawCurve(curve, Color.Red);
             }
+
+            // Draw frame marker
+            float range = GetRange();
+
+            int numFrames = (int)range * fps;
+            float timePerFrame = range / numFrames;
+
+            if (frameMarkerIdx != -1)
+                DrawFrameMarker(frameMarkerIdx * timePerFrame);
         }
 
         private void DrawCurve(EdAnimationCurve curve, Color color)

+ 26 - 5
Source/MBansheeEditor/Windows/Animation/GUITimeline.cs

@@ -29,6 +29,7 @@ namespace BansheeEditor
         private int width;
         private int height;
         private int fps = 1;
+        private int frameMarkerIdx = -1;
 
         public int MinWidth
         {
@@ -46,23 +47,30 @@ namespace BansheeEditor
             SetSize(width, height);
         }
 
-        public bool GetTime(Vector2I windowCoords, out float time)
+        public int GetFrame(Vector2I windowCoords)
         {
             Rect2I bounds = canvas.Bounds;
 
             if (windowCoords.x < (bounds.x + PADDING) || windowCoords.x >= (bounds.x + bounds.width - PADDING) ||
                 windowCoords.y < bounds.y || windowCoords.y >= (bounds.y + bounds.height))
             {
-                time = 0.0f;
-                return false;
+                return -1;
             }
 
             Vector2I relativeCoords = windowCoords - new Vector2I(bounds.x + PADDING, bounds.y);
 
             float lengthPerPixel = rangeLength / drawableWidth;
-            time = relativeCoords.x * lengthPerPixel;
+            float time = relativeCoords.x * lengthPerPixel;
 
-            return true;
+            return (int)(time * fps);
+        }
+
+        // Set to -1 to clear it
+        public void SetFrameMarker(int frameIdx)
+        {
+            frameMarkerIdx = frameIdx;
+
+            Rebuild();
         }
 
         public void SetSize(int width, int height)
@@ -141,6 +149,16 @@ namespace BansheeEditor
             canvas.DrawLine(start, end, Color.LightGray);
         }
 
+        private void DrawFrameMarker(float t)
+        {
+            int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
+
+            Vector2I start = new Vector2I(xPos, 0);
+            Vector2I end = new Vector2I(xPos, height);
+
+            canvas.DrawLine(start, end, Color.Red);
+        }
+
         // Returns range rounded to the nearest multiple of FPS
         private float GetRange()
         {
@@ -193,6 +211,9 @@ namespace BansheeEditor
                 // Move to next tick
                 t += timePerFrame;
             }
+
+            if (frameMarkerIdx != -1)
+                DrawFrameMarker(frameMarkerIdx * timePerFrame);
         }
     }
 

+ 9 - 12
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -49,7 +49,11 @@ namespace BansheeEditor
                 timeline.SetRange(lengthField.Value);
                 curveDrawing.SetRange(lengthField.Value, yRangeField.Value);
             };
-            fpsField.OnChanged += x => timeline.SetFPS(x);
+            fpsField.OnChanged += x =>
+            {
+                timeline.SetFPS(x);
+                curveDrawing.SetFPS(x);
+            };
             yRangeField.OnChanged += x =>
             {
                 curveDrawing.SetRange(lengthField.Value, x);
@@ -96,7 +100,7 @@ namespace BansheeEditor
 
         private void OnEditorUpdate()
         {
-            if (Input.IsPointerButtonDown(PointerButton.Left))
+            if (Input.IsPointerButtonHeld(PointerButton.Left))
             {
                 Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
 
@@ -107,18 +111,11 @@ namespace BansheeEditor
                 }
                 else
                 {
-                    float time;
-                    if (timeline.GetTime(windowPos, out time))
-                    {
-                        Debug.Log("Click time: " + time);
-                    }
-                    else
-                        Debug.Log("Clicked outside!");
+                    int frameIdx = timeline.GetFrame(windowPos);
+                    timeline.SetFrameMarker(frameIdx);
+                    curveDrawing.SetFrameMarker(frameIdx);
                 }
             }
-
-            //int position = (int)(MathEx.Sin(Time.RealElapsed)*50.0f + 50.0f);
-            //canvas.SetPosition(position, 0);
         }
     }
 

+ 2 - 2
Source/MBansheeEngine/Input/Input.cs

@@ -220,7 +220,7 @@ namespace BansheeEngine
         }
 
         /// <summary>
-        /// Query if the provided button is currently being held (this frame or previous frames).
+        /// Query if the provided button is currently being held (true for every frame the button is held for).
         /// </summary>
         /// <param name="code">Code of the button to query.</param>
         /// <param name="deviceIdx">Device to query the button on (0 - primary).</param>
@@ -253,7 +253,7 @@ namespace BansheeEngine
         }
 
         /// <summary>
-        /// Query if the provided pointer button is currently being held (this frame or previous frames).
+        /// Query if the provided pointer button is currently being held (true for every frame the button is held for).
         /// </summary>
         /// <param name="code">Code of the button to query.</param>
         /// <returns>True if the button is being held.</returns>