Exemple #1
0
    private static void DrawLimitSlider(SliderJoint2D sliderJoint2D, int limitControlID, Vector2 anchorPosition,
                                        Vector2 direction, IEnumerable <Vector2> snapList, Limit limit)
    {
        EditorGUI.BeginChangeCheck();

        float val;
        var   limits2D = sliderJoint2D.limits;

        switch (limit)
        {
        case Limit.Min:
            val = limits2D.min;
            break;

        case Limit.Max:
            val = limits2D.max;
            break;

        default:
            throw new ArgumentOutOfRangeException("limit");
        }

        var newLimit = EditorHelpers.LineSlider(limitControlID, anchorPosition,
                                                val,
                                                Helpers2D.GetAngle(direction), 0.125f, false, limit == Limit.Min);

        if (!EditorGUI.EndChangeCheck())
        {
            return;
        }

        if (snapList != null)
        {
            var limitSnapList = new List <Vector2>(snapList);
            switch (limit)
            {
            case Limit.Min:
                limitSnapList.Add(anchorPosition + direction * limits2D.max);
                break;

            case Limit.Max:
                limitSnapList.Add(anchorPosition + direction * limits2D.min);
                break;

            default:
                throw new ArgumentOutOfRangeException("limit");
            }

            var limitGUIPosition =
                HandleUtility.WorldToGUIPoint(anchorPosition + direction * newLimit);

            foreach (
                var snapPosition in
                from snapPosition in limitSnapList
                let snapGUIPosition = HandleUtility.WorldToGUIPoint(snapPosition)
                                      where Vector2.Distance(limitGUIPosition, snapGUIPosition) < 10
                                      select snapPosition)
            {
                newLimit = Helpers2D.DistanceAlongLine(new Ray(anchorPosition, direction),
                                                       snapPosition);
            }
        }


        EditorHelpers.RecordUndo("Change slider limit", sliderJoint2D);
        switch (limit)
        {
        case Limit.Min:
            limits2D.min = newLimit;
            break;

        case Limit.Max:
            limits2D.max = newLimit;
            break;

        default:
            throw new ArgumentOutOfRangeException("limit");
        }
        sliderJoint2D.limits = limits2D;
    }