Prechádzať zdrojové kódy

GUICurveDrawing - Basic drawing working

BearishSun 9 rokov pred
rodič
commit
ca6ca93db9

+ 2 - 2
Source/BansheeEngine/Include/BsGUIScrollBar.h

@@ -32,7 +32,7 @@ namespace BansheeEngine
 		UINT32 getScrollableSize() const;
 
 		/** @copydoc GUIElement::setTint */
-		virtual void setTint(const Color& color) override;
+		void setTint(const Color& color) override;
 
 		/**
 		 * Triggered whenever the scrollbar handle is moved. Value provided is the handle position in percent 
@@ -60,7 +60,7 @@ namespace BansheeEngine
 		void _setScrollPos(float pct);
 
 		/** @copydoc GUIElement::_getOptimalSize */
-		virtual Vector2I _getOptimalSize() const override;
+		Vector2I _getOptimalSize() const override;
 
 		/** @} */
 	protected:

+ 0 - 3
Source/BansheeEngine/Source/BsGUICanvas.cpp

@@ -50,10 +50,7 @@ namespace BansheeEngine
 	void GUICanvas::drawPolyLine(const Vector<Vector2I>& vertices, const Color& color)
 	{
 		if(vertices.size() < 2)
-		{
-			LOGWRN("Invalid number of vertices. Ignoring call.");
 			return;
-		}
 
 		mElements.push_back(CanvasElement());
 		CanvasElement& element = mElements.back();

+ 8 - 0
Source/MBansheeEditor/Utility/EdAnimationCurve.cs

@@ -34,6 +34,14 @@ namespace BansheeEditor
             get { return tangentModes; }
         }
 
+        internal EdAnimationCurve()
+        {
+            KeyFrame[] keyframes = new KeyFrame[0];
+            native = new AnimationCurve(keyframes);
+
+            tangentModes = new TangentMode[0];
+        }
+
         // Tangent modes should match number of curve keyframes
         internal EdAnimationCurve(AnimationCurve native, TangentMode[] tangentModes)
         {

+ 40 - 21
Source/MBansheeEditor/Windows/Animation/GUICurveDrawing.cs

@@ -14,7 +14,8 @@ namespace BansheeEditor
     internal class GUICurveDrawing
     {
         private const int PADDING = 30; // TODO - Shared with GUITimeline
-        private const int LINE_SPLIT_WIDTH = 5;
+        private const int LINE_SPLIT_WIDTH = 2;
+        private readonly static Color COLOR_MID_GRAY = new Color(90.0f / 255.0f, 90.0f / 255.0f, 90.0f / 255.0f, 1.0f);
 
         private EdAnimationCurve[] curves;
         private int width;
@@ -48,6 +49,9 @@ namespace BansheeEditor
             this.width = width;
             this.height = height;
 
+            canvas.SetWidth(width);
+            canvas.SetHeight(height);
+
             drawableWidth = Math.Max(0, width - PADDING * 2);
 
             Rebuild();
@@ -56,6 +60,14 @@ namespace BansheeEditor
         public void SetRange(float length, float minY, float maxY)
         {
             this.length = length;
+
+            if (minY > maxY)
+            {
+                float temp = maxY;
+                maxY = minY;
+                minY = temp;
+            }
+
             this.minY = minY;
             this.maxY = maxY;
 
@@ -82,7 +94,7 @@ namespace BansheeEditor
         private void DrawCurve(EdAnimationCurve curve, Color color)
         {
             float lengthPerPixel = length/drawableWidth;
-            float heightPerPixel = (maxY - minY)/height;
+            float pixelsPerHeight = height/(maxY - minY);
 
             int heightOffset = height/2; // So that y = 0 is at center of canvas
 
@@ -93,53 +105,60 @@ namespace BansheeEditor
             float start = MathEx.Clamp(keyframes[0].time, 0.0f, length);
             float end = MathEx.Clamp(keyframes[keyframes.Length - 1].time, 0.0f, length);
 
-            int startPixel = (int)(start * lengthPerPixel);
-            int endPixel = (int)(end * lengthPerPixel);
+            int startPixel = (int)(start / lengthPerPixel);
+            int endPixel = (int)(end / lengthPerPixel);
 
             // Draw start line
             {
-                int xPosStart = -PADDING;
-                int xPosEnd = startPixel;
+                int xPosStart = 0;
+                int xPosEnd = PADDING + startPixel;
 
-                int yPos = (int)(curve.Native.Evaluate(0.0f, false) * heightPerPixel);
+                int yPos = (int)(curve.Native.Evaluate(0.0f, false) * pixelsPerHeight);
                 yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
 
                 Vector2I a = new Vector2I(xPosStart, yPos);
                 Vector2I b = new Vector2I(xPosEnd, yPos);
 
-                canvas.DrawLine(a, b, Color.LightGray);
+                canvas.DrawLine(a, b, COLOR_MID_GRAY);
             }
 
             // Draw in between
-            List<Vector2I> linePoints = new List<Vector2I>();
+            float fNumSplits = (endPixel - startPixel)/(float) LINE_SPLIT_WIDTH;
 
-            int xPos = startPixel;
-            do
+            int numSplits = MathEx.FloorToInt(fNumSplits);
+            float remainder = fNumSplits - numSplits;
+
+            float lengthRounded = (end - start)*(numSplits / fNumSplits);
+            float timeIncrement = lengthRounded/numSplits;
+
+            numSplits += MathEx.CeilToInt(remainder) + 1;
+
+            Vector2I[] linePoints = new Vector2I[numSplits];
+            for (int i = 0; i < numSplits; i++)
             {
-                float t = xPos * lengthPerPixel;
+                int xPos = Math.Min(startPixel + i * LINE_SPLIT_WIDTH, endPixel);
+                float t = Math.Min(start + i * timeIncrement, end);
 
-                int yPos = (int)(curve.Native.Evaluate(t, false) * heightPerPixel);
+                int yPos = (int)(curve.Native.Evaluate(t, false) * pixelsPerHeight);
                 yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
 
-                linePoints.Add(new Vector2I(xPos, yPos));
-
-                xPos += LINE_SPLIT_WIDTH;
-            } while (xPos <= endPixel);
+                linePoints[i] = new Vector2I(PADDING + xPos, yPos);
+            }
 
-            canvas.DrawPolyLine(linePoints.ToArray(), color);
+            canvas.DrawPolyLine(linePoints, color);
 
             // Draw end line
             {
-                int xPosStart = endPixel;
+                int xPosStart = PADDING + endPixel;
                 int xPosEnd = width;
 
-                int yPos = (int)(curve.Native.Evaluate(length, false) * heightPerPixel);
+                int yPos = (int)(curve.Native.Evaluate(length, false) * pixelsPerHeight);
                 yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
 
                 Vector2I a = new Vector2I(xPosStart, yPos);
                 Vector2I b = new Vector2I(xPosEnd, yPos);
 
-                canvas.DrawLine(a, b, Color.LightGray);
+                canvas.DrawLine(a, b, COLOR_MID_GRAY);
             }
         }
     }

+ 26 - 16
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -17,6 +17,7 @@ namespace BansheeEditor
         private GUICurveDrawing curveDrawing;
         private GUIFloatField lengthField;
         private GUIIntField fpsField;
+        private GUIFloatField yRangeField;
 
         /// <summary>
         /// Opens the animation window.
@@ -37,21 +38,39 @@ namespace BansheeEditor
         {
             lengthField = new GUIFloatField(new LocEdString("Length"), 50);
             fpsField = new GUIIntField(new LocEdString("FPS"), 50);
+            yRangeField = new GUIFloatField(new LocEdString("Y range"), 50);
 
             lengthField.Value = 60.0f;
             fpsField.Value = 1;
+            yRangeField.Value = 20.0f;
 
-            lengthField.OnChanged += x => timeline.SetRange(lengthField.Value);
+            lengthField.OnChanged += x =>
+            {
+                timeline.SetRange(lengthField.Value);
+                curveDrawing.SetRange(lengthField.Value, yRangeField.Value * -0.5f, yRangeField.Value * 0.5f);
+            };
             fpsField.OnChanged += x => timeline.SetFPS(x);
+            yRangeField.OnChanged += x =>
+            {
+                curveDrawing.SetRange(lengthField.Value, x * -0.5f, x * 0.5f);
+            };
 
-            GUILayout buttonLayout = GUI.AddLayoutX();
+            GUILayout mainLayout = GUI.AddLayoutY();
+
+            GUILayout buttonLayout = mainLayout.AddLayoutX();
+            buttonLayout.AddSpace(5);
             buttonLayout.AddElement(lengthField);
+            buttonLayout.AddSpace(5);
+            buttonLayout.AddElement(yRangeField);
+            buttonLayout.AddSpace(5);
             buttonLayout.AddElement(fpsField);
+            buttonLayout.AddSpace(5);
 
-            timeline = new GUITimeline(GUI, Width, 20);
+            timeline = new GUITimeline(mainLayout, Width, 20);
 
             EdAnimationCurve[] curves = CreateDummyCurves();
-            curveDrawing = new GUICurveDrawing(GUI, Width, Height - 20, curves);
+            curveDrawing = new GUICurveDrawing(mainLayout, Width, Height - 20, curves);
+            curveDrawing.SetRange(60.0f, -10.0f, 10.0f);
 
             // TODO - Calculate min/max Y and range to set as default
             //  - Also recalculate whenever curves change and increase as needed
@@ -60,20 +79,11 @@ namespace BansheeEditor
         private EdAnimationCurve[] CreateDummyCurves()
         {
             EdAnimationCurve[] curves = new EdAnimationCurve[1];
+            curves[0] = new EdAnimationCurve();
 
             curves[0].AddKeyframe(0.0f, 1.0f);
-
-            KeyFrame[] keyFrames = new KeyFrame[3];
-            keyFrames[0].time = 0.0f;
-            keyFrames[0].value = 1.0f;
-
-            keyFrames[1].time = 10.0f;
-            keyFrames[1].value = 5.0f;
-
-            keyFrames[2].time = 15.0f;
-            keyFrames[2].value = -2.0f;
-
-            // TODO - Set up tangents
+            curves[0].AddKeyframe(10.0f, 5.0f);
+            curves[0].AddKeyframe(15.0f, -2.0f);
 
             return curves;
         }