Exemplo n.º 1
0
    protected override bool SingleAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias)
    {
        var hingeJoint2D = joint2D as HingeJoint2D;

        if (hingeJoint2D == null)
        {
            return(false);
        }

        if (bias == JointHelpers.AnchorBias.Either)
        {
            DrawLinesAndDiscs(hingeJoint2D, anchorInfo, JointHelpers.AnchorBias.Main);
            DrawLinesAndDiscs(hingeJoint2D, anchorInfo, JointHelpers.AnchorBias.Connected);
        }
        else
        {
            DrawLinesAndDiscs(hingeJoint2D, anchorInfo, bias);
        }

        var mainAnchorPosition      = JointHelpers.GetMainAnchorPosition(hingeJoint2D);
        var connectedAnchorPosition = JointHelpers.GetConnectedAnchorPosition(hingeJoint2D);

        if (bias == JointHelpers.AnchorBias.Main &&
            Vector2.Distance(mainAnchorPosition, connectedAnchorPosition) > AnchorEpsilon)
        {
            using (new HandleColor(GetAdjustedColor(Color.green))) {
                Handles.DrawLine(mainAnchorPosition, connectedAnchorPosition);
            }
        }
        return(false);
    }
    protected override bool SingleAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias)
    {
        var jointWithDistance = joint2D as T;

        if (!jointWithDistance)
        {
            return(false);
        }

        var mainAnchorPosition      = JointHelpers.GetMainAnchorPosition(jointWithDistance);
        var connectedAnchorPosition = JointHelpers.GetConnectedAnchorPosition(jointWithDistance);

        if (Vector2.Distance(mainAnchorPosition, connectedAnchorPosition) > AnchorEpsilon)
        {
            using (new HandleColor(Color.green)) {
                Handles.DrawLine(mainAnchorPosition, connectedAnchorPosition);
            }
        }


        DrawDistance(jointWithDistance, anchorInfo, bias);

        Handles.DrawLine(mainAnchorPosition, connectedAnchorPosition);

        return(false);
    }
Exemplo n.º 3
0
    protected override void ReAlignAnchors(AnchoredJoint2D joint2D, JointHelpers.AnchorBias alignmentBias)
    {
        var sliderJoint2D = (SliderJoint2D)joint2D;

        //align the angle to the connected anchor
        var direction = JointHelpers.GetConnectedAnchorPosition(joint2D) -
                        JointHelpers.GetMainAnchorPosition(joint2D);

        if (direction.magnitude > AnchorEpsilon)
        {
            var wantedAngle = Helpers2D.GetAngle(direction);

            EditorHelpers.RecordUndo("Realign angle", sliderJoint2D);
            sliderJoint2D.angle = wantedAngle - sliderJoint2D.transform.eulerAngles.z;
        }
    }
Exemplo n.º 4
0
    protected override bool SingleAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias)
    {
        var sliderJoint2D = (SliderJoint2D)joint2D;

        var mainAnchorPosition      = JointHelpers.GetMainAnchorPosition(sliderJoint2D);
        var connectedAnchorPosition = JointHelpers.GetConnectedAnchorPosition(sliderJoint2D);

        var sliderAngleControlID = anchorInfo.GetControlID("sliderAngle");

        if (bias != JointHelpers.AnchorBias.Connected &&
            (
                GUIUtility.hotControl == sliderAngleControlID ||
                GUIUtility.hotControl == anchorInfo.GetControlID("minLimit") ||
                GUIUtility.hotControl == anchorInfo.GetControlID("maxLimit") ||
                !Event.current.shift))
        {
            DrawAngleWidget(sliderJoint2D, sliderAngleControlID);

            if (sliderJoint2D.useLimits)
            {
                HandleLimits(sliderJoint2D, anchorInfo);
            }
        }

        if (GUIUtility.hotControl == anchorInfo.GetControlID("slider"))
        {
            var snap = GetWantedAnchorPosition(sliderJoint2D, bias);
            using (
                new HandleColor(new Color(1, 1, 1,
                                          isCreatedByTarget ? .5f * editorSettings.connectedJointTransparency : .5f))) {
                Handles.DrawLine(connectedAnchorPosition, snap);
                Handles.DrawLine(mainAnchorPosition, snap);
            }
        }

        using (
            new HandleColor(new Color(1, 1, 1,
                                      0.125f * (isCreatedByTarget ? editorSettings.connectedJointTransparency : 1.0f)))) {
            Handles.DrawLine(mainAnchorPosition, connectedAnchorPosition);
            if (sliderJoint2D.connectedBody && GUIUtility.hotControl == sliderAngleControlID)
            {
                Handles.DrawLine(mainAnchorPosition, GetTargetPosition(sliderJoint2D, JointHelpers.AnchorBias.Connected));
            }
        }
        return(false);
    }
Exemplo n.º 5
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();
            }
        }));
    }
    protected override IEnumerable <Vector2> GetSnapPositions(AnchoredJoint2D joint2D, AnchorInfo anchorInfo,
                                                              JointHelpers.AnchorBias bias, Vector2 anchorPosition)
    {
        if (!EditorGUI.actionKey)
        {
            return(null);
        }

        var wheelJoint2D = (WheelJoint2D)joint2D;

        if (SettingsHelper.GetOrCreate(joint2D).lockAnchors ||
            Vector2.Distance(JointHelpers.GetMainAnchorPosition(joint2D),
                             JointHelpers.GetConnectedAnchorPosition(joint2D)) <= AnchorEpsilon)
        {
            return(null);
        }

        return(new[] { GetWantedAnchorPosition(wheelJoint2D, bias, anchorPosition) });
    }
    protected override bool SingleAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias)
    {
        var wheelJoint2D = (WheelJoint2D)joint2D;

        var mainAnchorPosition      = JointHelpers.GetMainAnchorPosition(wheelJoint2D);
        var connectedAnchorPosition = JointHelpers.GetConnectedAnchorPosition(wheelJoint2D);

        var suspensionAngleControlID = anchorInfo.GetControlID("suspensionAngle");


        if (bias != JointHelpers.AnchorBias.Connected &&
            (GUIUtility.hotControl == suspensionAngleControlID || !Event.current.shift))
        {
            DrawAngleWidget(wheelJoint2D, suspensionAngleControlID);
        }

        var snapLineColor = GetAdjustedColor(new Color(1, 1, 1, .5f));

        //if the joint anchors are being moved, then show snap lines
        var sliderControlID = anchorInfo.GetControlID("slider");

        if (GUIUtility.hotControl == sliderControlID)
        {
            var snap = GetWantedAnchorPosition(wheelJoint2D, bias);
            using (new HandleColor(snapLineColor)) {
                Handles.DrawLine(connectedAnchorPosition, snap);
                Handles.DrawLine(mainAnchorPosition, snap);
            }
        }

        using (new HandleColor(snapLineColor))
        {
            Handles.DrawLine(mainAnchorPosition, connectedAnchorPosition);
            if (wheelJoint2D.connectedBody && GUIUtility.hotControl == suspensionAngleControlID)
            {
                Handles.DrawLine(mainAnchorPosition, GetTargetPosition(wheelJoint2D, JointHelpers.AnchorBias.Connected));
            }
        }
        return(false);
    }
Exemplo n.º 8
0
    protected override IEnumerable <Vector2> GetSnapPositions(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias, Vector2 anchorPosition)
    {
        var sliderJoint2D = (SliderJoint2D)joint2D;

        var lockAnchors            = SettingsHelper.GetOrCreate(sliderJoint2D).lockAnchors;
        var oppositeBias           = JointHelpers.GetOppositeBias(bias);
        var oppositeAnchorPosition = JointHelpers.GetAnchorPosition(sliderJoint2D, oppositeBias);

        var snapPositions = new List <Vector2>();

        Vector2[] targetPositions;

        if (joint2D.connectedBody)
        {
            targetPositions = new[] {
                GetTargetPosition(joint2D, JointHelpers.AnchorBias.Main),
                GetTargetPosition(joint2D, JointHelpers.AnchorBias.Connected)
            };
        }
        else
        {
            targetPositions = new[] {
                GetTargetPosition(joint2D, JointHelpers.AnchorBias.Main)
            };
        }

        if (sliderJoint2D.useLimits)
        {
            Ray slideRay;

            var min = sliderJoint2D.limits.min;
            var max = sliderJoint2D.limits.max;


            if (lockAnchors)
            {
                slideRay = new Ray(oppositeAnchorPosition,
                                   (anchorPosition - oppositeAnchorPosition).normalized);

                foreach (var targetPosition in targetPositions)
                {
                    if (Vector2.Distance(oppositeAnchorPosition, targetPosition) <= AnchorEpsilon)
                    {
                        continue;
                    }

                    var fromConnectedToTarget = new Ray(oppositeAnchorPosition,
                                                        (targetPosition - oppositeAnchorPosition).normalized);

                    var closestPointToRay = Helpers2D.ClosestPointToRay(fromConnectedToTarget, anchorPosition);

                    var ray = new Ray(oppositeAnchorPosition, (closestPointToRay - oppositeAnchorPosition).normalized);

                    Vector2 wantedMinPosition = ray.GetPoint(min);
                    Vector2 wantedMaxPosition = ray.GetPoint(max);

                    snapPositions.Add(wantedMinPosition);
                    snapPositions.Add(wantedMaxPosition);
                }
            }
            else
            {
                var worldAngle = sliderJoint2D.transform.eulerAngles.z + sliderJoint2D.angle;

                if (bias == JointHelpers.AnchorBias.Main)
                {
                    worldAngle += 180;
                }

                slideRay = new Ray(oppositeAnchorPosition,
                                   Helpers2D.GetDirection(worldAngle));
            }


            Vector2 minPos = slideRay.GetPoint(min);

            snapPositions.Add(minPos);


            Vector2 maxPos = slideRay.GetPoint(max);

            snapPositions.Add(maxPos);
        }

        if (lockAnchors)
        {
            //align onto the rays from either target towards the opposite bias
            foreach (var targetPosition in targetPositions)
            {
                if (Vector2.Distance(targetPosition, oppositeAnchorPosition) <= AnchorEpsilon)
                {
                    continue;
                }
                var fromConnectedToTarget = new Ray(oppositeAnchorPosition,
                                                    (targetPosition - oppositeAnchorPosition).normalized);

//                if (Helpers2D.DistanceToLine(fromConnectedToTarget, anchorPosition) < snapDistance)
                {
                    var closestPointToRay = Helpers2D.ClosestPointToRay(fromConnectedToTarget, anchorPosition);
                    snapPositions.Add(closestPointToRay);
                }
            }
        }

        if (!lockAnchors &&
            !(Vector2.Distance(JointHelpers.GetMainAnchorPosition(joint2D),
                               JointHelpers.GetConnectedAnchorPosition(joint2D)) <= AnchorEpsilon))
        {
            var wantedAnchorPosition = GetWantedAnchorPosition(sliderJoint2D, bias, anchorPosition);

            snapPositions.Add(wantedAnchorPosition);
        }

        return(snapPositions);
    }