Example #1
0
    protected void AddEditAngleMenuItem(TJointType joint2D, GenericMenu menu, Vector2 mousePosition)
    {
        var joint2DSettings    = GetSettings(joint2D);
        var mainAnchorPosition = JointHelpers.GetMainAnchorPosition(joint2D);

        var guiContent = GetAngleEditinGUIContent();

        menu.AddItem(new GUIContent("Edit " + guiContent.text), false,
                     () =>
                     ShowUtility(
                         "Edit " + guiContent.text,
                         new Rect(mousePosition.x - 250, mousePosition.y + 15, 500, EditorGUIUtility.singleLineHeight * 3),
                         delegate(Action close, bool focused) {
            EditorGUI.BeginChangeCheck();
            var newAngle =
                EditorGUILayout.FloatField(
                    guiContent,
                    GetAngle(joint2D));
            if (EditorGUI.EndChangeCheck())
            {
                using (new Modification(guiContent.text, joint2D)) {
                    if (joint2DSettings.lockAnchors)
                    {
                        var angleDelta = Mathf.DeltaAngle(GetAngle(joint2D), newAngle);

                        var connectedAnchorPosition =
                            JointHelpers.GetConnectedAnchorPosition(joint2D);
                        var connectedOffset = connectedAnchorPosition - mainAnchorPosition;

                        JointHelpers.SetWorldConnectedAnchorPosition(joint2D,
                                                                     mainAnchorPosition +
                                                                     (Vector2)(Helpers2D.Rotate(angleDelta) * connectedOffset));
                    }

                    SetAngle(joint2D, newAngle);
                }
            }
            if (GUILayout.Button("Done") ||
                (Event.current.isKey &&
                 (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.Escape) &&
                 focused))
            {
                close();
            }
        }));
    }