protected override IEnumerable <Vector2> GetSnapPositions(AnchoredJoint2D joint2D, AnchorInfo anchorInfo,
                                                              JointHelpers.AnchorBias bias, Vector2 anchorPosition)
    {
        if (!EditorGUI.actionKey)
        {
            return(null);
        }

        var otherBias = bias == JointHelpers.AnchorBias.Main
            ? JointHelpers.AnchorBias.Connected
            : JointHelpers.AnchorBias.Main;

        var jointWithDistance = (T)joint2D;

        var anchorSliderState     = StateObject.Get <AnchorSliderState>(anchorInfo.GetControlID("slider"));
        var currentMousePosition  = Helpers2D.GUIPointTo2DPosition(Event.current.mousePosition);
        var currentAnchorPosition = currentMousePosition - anchorSliderState.mouseOffset;

        var otherAnchorPosition = JointHelpers.GetAnchorPosition(jointWithDistance, otherBias);
        var diff = otherAnchorPosition - currentAnchorPosition;

        if (diff.magnitude <= Mathf.Epsilon)
        {
            diff = -Vector2.up;
        }

        var normalizedDiff = diff.normalized;

        var wantedAnchorPosition = otherAnchorPosition - normalizedDiff * GetDistance(jointWithDistance);

        return(new[] { wantedAnchorPosition });
    }
Exemple #2
0
        public Pullout(Rect screenRect, float minWidth, bool scrollable = false, Color?backgroundColor = null)
        {
            var maxWidth = screenRect.width;

            minWidth = Mathf.Max(minWidth, 0);
            minWidth = Mathf.Min(maxWidth, minWidth);

            var controlID = GUIUtility.GetControlID(FocusType.Keyboard, screenRect);

            state = StateObject.Get <PulloutState>(controlID);
            state.Tick();

            if (backgroundColor != null)
            {
                state.Setup(backgroundColor.Value);
            }

            var mousePosition = Event.current.mousePosition;

            if (screenRect.Contains(mousePosition) && mousePosition.x - screenRect.x < state.edgePosition)
            {
                if (!state.mouseIn && (GUIUtility.hotControl == 0 || GUIUtility.hotControl == controlID))
                {
                    state.mouseIn = true;
                    wantsRepaint  = true;
                }

                if (state.mouseIn)
                {
                    state.wantedEdgePosition = maxWidth;
                }

                if (GUIUtility.keyboardControl == controlID)
                {
                    GUIUtility.hotControl = controlID;
                }

                HandleUtility.AddDefaultControl(controlID);
            }
            else
            {
                if (state.mouseIn)
                {
                    state.mouseIn = false;
                    wantsRepaint  = true;
                }
                state.wantedEdgePosition = minWidth;
            }

            disposables.Push(new HandleGUI());
            disposables.Push(new GUILayoutArea(screenRect));

            //groupArea = new GroupArea(new Rect(screenRect.x, screenRect.y, screenRect.width, screenRect.height));


            var offset = maxWidth - state.edgePosition;

            disposables.Push(new GUICustomViewport(new Rect(0, 0, screenRect.width, screenRect.height),
                                                   new Rect(offset, 0, screenRect.width - offset, screenRect.height)));

            disposables.Push(new GUIMatrix(Matrix4x4.TRS(new Vector3(-offset, 0, 0), Quaternion.identity,
                                                         new Vector3(1, 1, 1))));

            using (new HandleColor(Color.red)) {
                Handles.DrawLine(new Vector3(maxWidth + 1, 0, 0),
                                 new Vector3(maxWidth + 1, screenRect.height, 0));
                Handles.DrawLine(new Vector3(0, 0, 0),
                                 new Vector3(maxWidth + 1, 0, 0));
                Handles.DrawLine(new Vector3(0, screenRect.height, 0),
                                 new Vector3(maxWidth + 1, screenRect.height, 0));
            }

            GUI.DrawTexture(new Rect(0, 0, maxWidth, screenRect.height),
                            state.background, ScaleMode.ScaleAndCrop);

            using (new HandleColor(Color.green)) {
                Handles.DrawLine(new Vector3(0, 0, 0),
                                 new Vector3(0, screenRect.height, 0));
            }

            if (scrollable)
            {
                disposables.Push(new ScrollView());
            }

            oldGUIEnabled = GUI.enabled;
            GUI.enabled   = state.mouseIn && state.Opened(maxWidth);

            if (Math.Abs(state.edgePosition - maxWidth) > 0.25f)
            {
                wantsRepaint = true;
            }
        }