Ejemplo n.º 1
0
    protected override bool PostAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo info, List <Vector2> otherAnchors,
                                          JointHelpers.AnchorBias bias)
    {
        var hingeJoint2D = joint2D as HingeJoint2D;

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

        if (!hingeJoint2D.useLimits)
        {
            return(false);
        }

        var   showAngle    = false;
        float angle        = 0;
        float displayAngle = 0;

        float jointAngle;

        if (EditorApplication.isPlayingOrWillChangePlaymode || Application.isPlaying)
        {
            jointAngle = hingeJoint2D.jointAngle;
        }
        else
        {
            jointAngle = 0;
        }

        var startPosition = JointHelpers.GetAnchorPosition(hingeJoint2D, bias);

        var mainBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Main);

        var mainBodyAngle = JointHelpers.AngleFromAnchor(startPosition, mainBodyPosition,
                                                         JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Main));

        var connectedBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Connected);

        float connectedBodyAngle;

        if (hingeJoint2D.connectedBody)
        {
            connectedBodyAngle = JointHelpers.AngleFromAnchor(startPosition, connectedBodyPosition,
                                                              JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Connected));
        }
        else
        {
            connectedBodyAngle = 0;
        }

        var angleDiff = jointAngle - (connectedBodyAngle - mainBodyAngle);

        var liveMainAngle = connectedBodyAngle + angleDiff;

        var minMainAngle = liveMainAngle - hingeJoint2D.limits.min;
        var maxMainAngle = liveMainAngle - hingeJoint2D.limits.max;

        var labelText = "";

        float angleOffset = 0;

        var settings = SettingsHelper.GetOrCreate <HingeJoint2DSettings>(hingeJoint2D);

        if (EditorHelpers.IsWarm(info.GetControlID("lowerMainAngle")))
        {
            showAngle    = true;
            angle        = minMainAngle;
            displayAngle = hingeJoint2D.limits.min;
            labelText    = "Lower: ";
            angleOffset  = settings.mainAngleOffset;
        }
        else if (EditorHelpers.IsWarm(info.GetControlID("upperMainAngle")))
        {
            showAngle    = true;
            angle        = maxMainAngle;
            displayAngle = hingeJoint2D.limits.max;
            labelText    = "Upper: ";
            angleOffset  = settings.mainAngleOffset;
        }
        else if (EditorHelpers.IsWarm(info.GetControlID("lowerConnectedAngle")))
        {
            showAngle     = true;
            angle         = hingeJoint2D.limits.min;
            displayAngle  = angle;
            labelText     = "Lower: ";
            startPosition = JointHelpers.GetConnectedAnchorPosition(hingeJoint2D);
            angleOffset   = settings.connectedAngleOffset;
        }
        else if (EditorHelpers.IsWarm(info.GetControlID("upperConnectedAngle")))
        {
            showAngle     = true;
            angle         = hingeJoint2D.limits.max;
            labelText     = "Upper: ";
            displayAngle  = angle;
            startPosition = JointHelpers.GetConnectedAnchorPosition(hingeJoint2D);
            angleOffset   = settings.connectedAngleOffset;
        }

        LimitContext(hingeJoint2D, info.GetControlID("lowerMainAngle"), Limit.Min);
        LimitContext(hingeJoint2D, info.GetControlID("upperMainAngle"), Limit.Max);
        LimitContext(hingeJoint2D, info.GetControlID("lowerConnectedAngle"), Limit.Min);
        LimitContext(hingeJoint2D, info.GetControlID("upperConnectedAngle"), Limit.Max);

        if (showAngle)
        {
            var distanceFromCenter = GetAngleSliderRadius(startPosition);


            var anglePosition = startPosition + Helpers2D.GetDirection(angle + angleOffset) * distanceFromCenter;

            var labelContent = new GUIContent(labelText + "\n" + String.Format("{0:0.00}", displayAngle));

            var fontSize = HandleUtility.GetHandleSize(anglePosition) * (1f / 64f);

            var labelOffset = fontSize * EditorHelpers.FontWithBackgroundStyle.CalcSize(labelContent).y;

            EditorHelpers.OverlayLabel((Vector3)anglePosition + (Camera.current.transform.up * labelOffset),
                                       labelContent,
                                       EditorHelpers.FontWithBackgroundStyle);
        }
        return(false);
    }
Ejemplo n.º 2
0
    private bool DrawAngleLimits(HingeJoint2D hingeJoint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias)
    {
        var changed  = false;
        var settings = SettingsHelper.GetOrCreate <HingeJoint2DSettings>(hingeJoint2D);

        if (!settings.showAngleLimits)
        {
            return(false);
        }

        if (hingeJoint2D.useLimits)
        {
            var limits   = hingeJoint2D.limits;
            var minLimit = limits.min;
            var maxLimit = limits.max;


            var anchorPriority = settings.anchorPriority;

            var showMain = anchorPriority == HingeJoint2DSettings.AnchorPriority.Main ||
                           anchorPriority == HingeJoint2DSettings.AnchorPriority.Both;

            var showConnected = (anchorPriority == HingeJoint2DSettings.AnchorPriority.Connected ||
                                 anchorPriority == HingeJoint2DSettings.AnchorPriority.Both);

            var anchorPosition = JointHelpers.GetAnchorPosition(hingeJoint2D, bias);

            var distanceFromCenter = GetAngleSliderRadius(anchorPosition);
            var angleHandleSize    = editorSettings.angleHandleSize;

            float jointAngle;
            var   isPlaying = EditorApplication.isPlayingOrWillChangePlaymode || Application.isPlaying;
            if (isPlaying)
            {
                jointAngle = hingeJoint2D.jointAngle;
            }
            else
            {
                jointAngle = 0;
            }

            var mainBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Main);

            var mainBodyAngle = JointHelpers.AngleFromAnchor(anchorPosition, mainBodyPosition,
                                                             JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Main));

            var connectedBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Connected);

            float connectedBodyAngle;
            if (hingeJoint2D.connectedBody)
            {
                connectedBodyAngle = JointHelpers.AngleFromAnchor(anchorPosition, connectedBodyPosition,
                                                                  JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Connected));
            }
            else
            {
                connectedBodyAngle = 0;
            }

            var angleDiff = jointAngle - (connectedBodyAngle - mainBodyAngle);

            var liveMainAngle = connectedBodyAngle + angleDiff;

            var minMainAngle = liveMainAngle - minLimit;
            var maxMainAngle = liveMainAngle - maxLimit;

            var limitDifference = maxLimit - minLimit;

            Color limitColor, limitAreaColor;
            if (!isPlaying
                &&
                ((minLimit < jointAngle && maxLimit < jointAngle) || (minLimit > jointAngle && maxLimit > jointAngle)))
            {
                limitColor     = editorSettings.incorrectLimitsColor;
                limitAreaColor = editorSettings.incorrectLimitsArea;
            }
            else
            {
                limitColor     = editorSettings.correctLimitsColor;
                limitAreaColor = editorSettings.limitsAreaColor;
            }

            var angleWidgetColor = editorSettings.angleWidgetColor;
            var activeAngleColor = editorSettings.activeAngleColor;
            var hoverAngleColor  = editorSettings.hoverAngleColor;

            if (isCreatedByTarget)
            {
                angleWidgetColor.a *= editorSettings.connectedJointTransparency;
                activeAngleColor.a *= editorSettings.connectedJointTransparency;
                hoverAngleColor.a  *= editorSettings.connectedJointTransparency;

                limitColor.a     *= editorSettings.connectedJointTransparency;
                limitAreaColor.a *= editorSettings.connectedJointTransparency;
            }

            if (showMain && bias != JointHelpers.AnchorBias.Connected)   //main or 'both'
            {
                changed = HandleMainLimits(hingeJoint2D, anchorInfo, limitAreaColor, limitDifference, anchorPosition, distanceFromCenter, maxMainAngle, settings, limitColor, minMainAngle, angleWidgetColor, activeAngleColor, hoverAngleColor, angleHandleSize, limits, liveMainAngle);
            }
            if (showConnected && bias != JointHelpers.AnchorBias.Main)   //connected or both?
            {
                changed = HandleConnectedLimits(hingeJoint2D, anchorInfo, mainBodyAngle, angleDiff, minLimit, maxLimit, limitAreaColor, limitDifference, anchorPosition, distanceFromCenter, settings, limitColor, angleWidgetColor, activeAngleColor, hoverAngleColor, angleHandleSize, limits, changed);
            }
        }

        return(changed);
    }