private void DistanceContext(T jointWithDistance, int controlID)
    {
        var mousePosition = Event.current.mousePosition;

        EditorHelpers.ContextClick(controlID, () => {
            var menu = new GenericMenu();
            AddDistanceContextItem(jointWithDistance, menu, mousePosition);
            menu.ShowAsContext();
        });
    }
Beispiel #2
0
    private void LimitContext(HingeJoint2D hingeJoint2D, int controlID, Limit limit)
    {
        var mousePosition = Event.current.mousePosition;

        var limitName = (limit == Limit.Min ? "Lower" : "Upper") + " Angle Limit";

        EditorHelpers.ContextClick(controlID, () => {
            var menu = new GenericMenu();
            menu.AddItem(new GUIContent("Edit " + limitName), false, () =>
                         ShowUtility(
                             "Edit " + limitName,
                             new Rect(mousePosition.x - 250, mousePosition.y + 15, 500, EditorGUIUtility.singleLineHeight * 2),
                             delegate(Action close, bool focused) {
                EditorGUI.BeginChangeCheck();
                GUI.SetNextControlName(limitName);
                var newLimit = EditorGUILayout.FloatField(limitName,
                                                          limit == Limit.Min
                                ? hingeJoint2D.limits.min
                                : hingeJoint2D.limits.max);
                if (EditorGUI.EndChangeCheck())
                {
                    var limits = hingeJoint2D.limits;
                    if (limit == Limit.Min)
                    {
                        limits.min = newLimit;
                    }
                    else
                    {
                        limits.max = newLimit;
                    }
                    EditorHelpers.RecordUndo(limitName, hingeJoint2D);
                    hingeJoint2D.limits = limits;
                    EditorUtility.SetDirty(hingeJoint2D);
                }
                if (GUILayout.Button("Done") ||
                    (Event.current.isKey &&
                     (Event.current.keyCode == KeyCode.Escape) &&
                     focused))
                {
                    close();
                }
            }));
            menu.ShowAsContext();
        });
    }
Beispiel #3
0
    protected void DrawAngleWidget(TJointType joint2D, int controlID)
    {
        var joint2DSettings = GetSettings(joint2D);

        var worldAngle = joint2D.transform.eulerAngles.z + GetAngle(joint2D);

        HandleDragDrop(controlID, joint2D, joint2DSettings);

        EditorGUI.BeginChangeCheck();

        JointHelpers.AnchorBias bias;

        if (joint2DSettings.anchorPriority == JointSettingsWithBias.AnchorPriority.Main)
        {
            bias = JointHelpers.AnchorBias.Main;
        }
        else
        {
            bias = JointHelpers.AnchorBias.Connected;
        }

        var oppositeBias = JointHelpers.GetOppositeBias(bias);

        var angleWidgetPosition = JointHelpers.GetAnchorPosition(joint2D, bias);
        var otherAnchorPosition = JointHelpers.GetAnchorPosition(joint2D, oppositeBias);

        var offsetToOther = otherAnchorPosition - angleWidgetPosition;

        var newAngle = LineAngleHandle(controlID, worldAngle, angleWidgetPosition, 0.5f, 2);

        var mousePosition = Event.current.mousePosition;

        EditorHelpers.ContextClick(controlID, () => {
            var menu = new GenericMenu();
            AddEditAngleMenuItem(joint2D, menu, mousePosition);
            menu.ShowAsContext();
        });

        if (!EditorGUI.EndChangeCheck())
        {
            return;
        }
        var snapped = false;

        if (EditorGUI.actionKey)
        {
            var handleSize = HandleUtility.GetHandleSize(angleWidgetPosition);

            var mousePosition2D = Helpers2D.GUIPointTo2DPosition(Event.current.mousePosition);

            var currentAngleRay = new Ray(angleWidgetPosition, Helpers2D.GetDirection(newAngle));

            var mousePositionProjectedToAngle = Helpers2D.ClosestPointToRay(currentAngleRay, mousePosition2D);

            var directionsToSnapTo = new List <Vector2> {
                (GetTargetPosition(joint2D, bias) - angleWidgetPosition).normalized
            };

            if (!joint2DSettings.lockAnchors)
            {
                directionsToSnapTo.Insert(0, offsetToOther.normalized);
            }

            if (joint2D.connectedBody)
            {
                directionsToSnapTo.Add(
                    (GetTargetPosition(joint2D, oppositeBias) - angleWidgetPosition)
                    .normalized);
            }

            foreach (var direction in directionsToSnapTo)
            {
                var rayTowardsDirection = new Ray(angleWidgetPosition, direction);

                var closestPointTowardsDirection = Helpers2D.ClosestPointToRay(rayTowardsDirection,
                                                                               mousePositionProjectedToAngle);

                if (Vector2.Distance(closestPointTowardsDirection, mousePositionProjectedToAngle) <
                    handleSize * 0.125f)
                {
                    var currentDirection           = Helpers2D.GetDirection(newAngle);
                    var closestPositionToDirection =
                        Helpers2D.ClosestPointToRay(rayTowardsDirection,
                                                    angleWidgetPosition + currentDirection);

                    snapped  = true;
                    newAngle = Helpers2D.GetAngle(closestPositionToDirection - angleWidgetPosition);

                    break;
                }
            }
        }

        var wantedAngle = newAngle - joint2D.transform.eulerAngles.z;

        if (!snapped)
        {
            wantedAngle = Handles.SnapValue(wantedAngle, editorSettings.snapAngle);
        }

        EditorHelpers.RecordUndo("Alter Angle", joint2D);

        if (joint2DSettings.lockAnchors)
        {
            var angleDelta = Mathf.DeltaAngle(GetAngle(joint2D), wantedAngle);

            JointHelpers.SetWorldAnchorPosition(joint2D,
                                                angleWidgetPosition + (Vector2)(Helpers2D.Rotate(angleDelta) * offsetToOther), oppositeBias);
        }

        SetAngle(joint2D, wantedAngle);
    }