Exemplo n.º 1
0
        /// <summary>
        /// Draws the curve using the provided color.
        /// </summary>
        /// <param name="curve">Curve to draw within the currently set range. </param>
        /// <param name="color">Color to draw the curve with.</param>
        private void DrawCurve(EdAnimationCurve curve, Color color)
        {
            float range = GetRange();
            float lengthPerPixel = range / drawableWidth;

            KeyFrame[] keyframes = curve.KeyFrames;
            if (keyframes.Length <= 0)
                return;

            // 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;

                Vector2I end = CurveToPixelSpace(new Vector2(curveStart, curveValue));

                canvas.DrawLine(start, end, COLOR_MID_GRAY);
            }

            List<Vector2I> linePoints = new List<Vector2I>();

            // Draw in between keyframes
            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);

                bool isStep = keyframes[i].outTangent == float.PositiveInfinity ||
                              keyframes[i + 1].inTangent == float.PositiveInfinity;

                // If step tangent, draw the required lines without sampling, as the sampling will miss the step
                if (isStep)
                {
                    float startValue = curve.Evaluate(start, false);
                    float endValue = curve.Evaluate(end, false);

                    linePoints.Add(CurveToPixelSpace(new Vector2(start, startValue)));
                    linePoints.Add(CurveToPixelSpace(new Vector2(end, startValue)));
                    linePoints.Add(CurveToPixelSpace(new Vector2(end, endValue)));
                }
                else // Draw normally
                {
                    float timeIncrement = LINE_SPLIT_WIDTH*lengthPerPixel;

                    int startPixel = (int)(start / lengthPerPixel);
                    int endPixel = (int)(end / lengthPerPixel);

                    int numSplits;
                    if (startPixel != endPixel)
                    {
                        float fNumSplits = (end - start) / timeIncrement;

                        numSplits = MathEx.CeilToInt(fNumSplits);
                        timeIncrement = (end - start)/numSplits;
                    }
                    else
                    {
                        numSplits = 1;
                        timeIncrement = 0.0f;
                    }

                    for (int j = 0; j < numSplits; j++)
                    {
                        float t = Math.Min(start + j * timeIncrement, end);
                        float value = curve.Evaluate(t, false);

                        linePoints.Add(CurveToPixelSpace(new Vector2(t, value)));
                    }
                }
            }

            canvas.DrawPolyLine(linePoints.ToArray(), color);

            // Draw end line
            {
                float curveEnd = MathEx.Clamp(keyframes[keyframes.Length - 1].time, 0.0f, range);
                float curveValue = curve.Evaluate(range, false);

                Vector2I start = CurveToPixelSpace(new Vector2(curveEnd, curveValue));
                Vector2I end = new Vector2I(width, start.y);

                canvas.DrawLine(start, end, COLOR_MID_GRAY);
            }
        }
Exemplo n.º 2
0
 public CurveDrawInfo(EdAnimationCurve curve, Color color)
 {
     this.curve = curve;
     this.color = color;
 }