示例#1
0
        public void DrawPreview(object fieldValue)
        {
            if (!showPreview)
            {
                return;
            }

            var area = ActionHelpers.GetControlPreviewRect(100f);

            var previewColor = Color.white;

            previewColor.a = 0.5f;
            Handles.color  = previewColor;

            start.x = area.x + area.width * 0.5f;
            start.y = area.y + area.height * 0.5f;

            var max = 40f;

            var angleStep = snapAngle.Value;

            if (angleStep < 1)
            {
                angleStep = 1;
            }

            for (float angle = 0; angle < 360; angle += angleStep)
            {
                var radians = Mathf.Deg2Rad * angle;
                end.x = start.x + Mathf.Cos(radians) * max;
                end.y = start.y + Mathf.Sin(radians) * max;

                Handles.DrawLine(start, end);
            }

            ActionHelpers.DrawAxisXY(area);

            Handles.color = Color.white;
        }
示例#2
0
        /* WIP edit time scrub bar and play button
         * Maybe move to own class so can re-use (e.g. ActionHelpers)
         * private static readonly GUIContent scrubLabel = new GUIContent("Preview", "Preview tween in the Scene View.");
         *
         *  private static Texture _playIcon;
         *
         *  private static Texture playIcon
         *  {
         *      get
         *      {
         *          if (_playIcon == null)
         *          {
         *              _playIcon = EditorGUIUtility.IconContent("IN foldout act").image;
         *          }
         *
         *          return _playIcon;
         *      }
         *  }
         *
         *  private static GUIStyle _buttonStyle;
         *
         *  private static GUIStyle buttonStyle
         *  {
         *      get
         *      {
         *          if (_buttonStyle == null)
         *          {
         *          _buttonStyle = new GUIStyle("Button");
         *          _buttonStyle.padding = new RectOffset(2,2,0,0);
         *          }
         *
         *          return _buttonStyle;
         *      }
         *  }
         */

        /// <summary>
        /// Draws scrubbing controls and a preview curve for EasingFunctions
        /// </summary>
        /// <param name="ease">The  Ease function to preview</param>
        /// <param name="playPreview">Did the user hit Play in the scrubbing slider</param>
        /// <param name="currentTime">Used to draw the current time on the preview</param>
        /// <param name="currentTimeColor">Color used to draw current time</param>
        public static void DrawPreviewCurve(EasingFunction.Ease ease, ref bool playPreview, ref float currentTime, Color currentTimeColor)
        {
            /* WIP edit time preview
             *  if (!EditorApplication.isPlaying)
             *  {
             *  GUILayout.BeginHorizontal();
             *
             *      EditorGUILayout.PrefixLabel(scrubLabel);
             *
             *      playPreview = GUILayout.Toggle(playPreview, playIcon, buttonStyle, GUILayout.Height(15f), GUILayout.Width(17f));
             *
             *  EditorGUI.BeginChangeCheck();
             *      currentTime = EditorGUILayout.Slider(currentTime, 0, 1);
             *      if (EditorGUI.EndChangeCheck())
             *          playPreview = false;
             *
             *  GUILayout.EndHorizontal();
             *  }*/

            if (ease == EasingFunction.Ease.CustomCurve)
            {
                return;
            }

            var func      = EasingFunction.GetEasingFunction(ease);
            var derivFunc = EasingFunction.GetEasingFunctionDerivative(ease);

            if (func == null || derivFunc == null)
            {
                return;
            }

            var area = ActionHelpers.GetControlPreviewRect(45f);

            // some ease functions overshoot 0 and 1
            // so leave some margins at top/bottom

            const float overflow = .3f;
            var         yRange   = area.height * (1 - 2 * overflow);

            // setup the range

            var origin = new Vector2(area.x, area.yMax - area.height * overflow);
            var yMax   = new Vector2(area.x, area.y + area.height * overflow);

            // draw axis (horizontal lines at 0 and 1)

            var axisColor = EditorStyles.label.normal.textColor;

            axisColor.a   = 0.2f;
            Handles.color = axisColor;

            Handles.DrawLine(origin, new Vector3(area.xMax, origin.y));
            Handles.DrawLine(yMax, new Vector3(area.xMax, yMax.y));

            // draw curve

            Handles.color = EditorStyles.label.normal.textColor;

            var numSampleSteps = (int)area.width / 2;

            // add start point: (0,0)

            pointBuffer[0] = new Vector3(origin.x, origin.y);

            // only add points to the curve when curve threshold is exceeded
            // e.g. so a linear ease will only have 2 points

            const float curveThreshold = 0.05f;
            var         prevSlope      = 0f;

            var pointIndex = 1;

            for (var i = 1; i < numSampleSteps && pointIndex < maxCurvePoints - 1; i++)
            {
                var step  = i / (float)numSampleSteps;
                var y     = func(0, 1, step);
                var slope = derivFunc(0, 1, step);

                if (Mathf.Abs(slope - prevSlope) > curveThreshold ||
                    Mathf.Sign(slope) != Mathf.Sign(prevSlope))
                {
                    prevSlope = slope;
                    pointBuffer[pointIndex++] = new Vector3(origin.x + step * area.width, origin.y - y * yRange);
                }
            }

            // add last point, to guarantee at least 2 points (e.g., linear)

            pointBuffer[pointIndex] = new Vector3(area.xMax, origin.y - func(0, 1, 1) * yRange);

            // draw the curve

            Handles.DrawAAPolyLine(2, pointIndex + 1, pointBuffer);

            // draw current time

            if (Application.isPlaying && currentTime > 0)
            {
                var x = area.x + area.width * currentTime;
                var y = origin.y - func(0, 1, currentTime) * yRange;

                var guiColor = GUI.color;
                GUI.color = currentTimeColor;
                GUI.DrawTexture(new Rect(x - 1, y - 1, 2, 2), EditorGUIUtility.whiteTexture);
                GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, 0.2f);
                GUI.DrawTexture(new Rect(x - 1, area.y, 2, area.height), EditorGUIUtility.whiteTexture);
                GUI.color = guiColor;

                //Handles.DrawLine(new Vector3(x, area.y), new Vector3(x, area.yMax));
            }

            // need to do this?
            Handles.color = Color.white;


#if DEBUG_EASE_EDITOR
// debug the number of points generated when sampling this curve

            if (debugged != ease && pointIndex > 1)
            {
                Debug.Log(pointIndex);
                debugged = ease;
            }
#endif
        }
示例#3
0
        public void DrawPreview(object fieldValue)
        {
            if (!showPreview)
            {
                return;
            }

            var area       = ActionHelpers.GetControlPreviewRect(100f);
            var axisCenter = ActionHelpers.DrawAxisXY(area);

            Handles.color = previewColor;

            // init min/max sizes for drawing

            var max = 40f;
            var min = 0f;

            if (maxLength.Value > 0)
            {
                min = max * (minLength.Value / maxLength.Value);
            }

            // setup x/y scales to fit in area

            var scaleX = 1f;
            var scaleY = yScale.Value;

            if (Mathf.Abs(scaleY) > 1f)
            {
                scaleX = 1f / scaleY;
                scaleY = 1f;
            }

            switch (shape)
            {
            case Option.Circle:

                ActionHelpers.DrawOval(axisCenter, max * scaleX, max * scaleY);
                ActionHelpers.DrawOval(axisCenter, min * scaleX, min * scaleY);

                break;

            case Option.Rectangle:

                ActionHelpers.DrawRect(axisCenter, max * scaleX, max * scaleY);
                ActionHelpers.DrawRect(axisCenter, min * scaleX, min * scaleY);

                break;

            case Option.InArc:

                var fromAngle = floatParam1.Value;
                var toAngle   = floatParam2.Value;

                ActionHelpers.DrawArc(axisCenter, fromAngle, toAngle, max * scaleX, max * scaleY);
                ActionHelpers.DrawArc(axisCenter, fromAngle, toAngle, min * scaleX, min * scaleY);

                ActionHelpers.DrawSpoke(axisCenter, fromAngle, min, max, scaleX, scaleY);
                ActionHelpers.DrawSpoke(axisCenter, toAngle, min, max, scaleX, scaleY);


                break;

            case Option.AtAngles:

                var angleStep = floatParam1.Value;
                if (angleStep < 1)
                {
                    angleStep = 1;
                }

                // make sure we draw at least a dot (should be a better way?!)
                if (max - min < 1.4f)
                {
                    min = max - 1.4f;
                }

                for (float angle = 0; angle < 360; angle += angleStep)
                {
                    ActionHelpers.DrawSpoke(axisCenter, angle, min, max, scaleX, scaleY);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Handles.color = Color.white;
        }