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 }); }
protected override bool PostAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo info, List <Vector2> otherAnchors, JointHelpers.AnchorBias bias) { var wheelJoint2D = joint2D as WheelJoint2D; if (wheelJoint2D == null) { return(false); } var suspensionAngleControlID = info.GetControlID("suspensionAngle"); if (Event.current.type == EventType.repaint) { if (EditorHelpers.IsWarm(suspensionAngleControlID) && DragAndDrop.objectReferences.Length == 0) { var suspensionAngle = wheelJoint2D.suspension.angle; var labelContent = new GUIContent(String.Format("{0:0.00}", suspensionAngle)); Vector3 mainAnchorPosition = Helpers2D.GUIPointTo2DPosition(Event.current.mousePosition); var fontSize = HandleUtility.GetHandleSize(mainAnchorPosition) * (1f / 64f); var labelOffset = fontSize * EditorHelpers.FontWithBackgroundStyle.CalcSize(labelContent).y; EditorHelpers.OverlayLabel(mainAnchorPosition + (Camera.current.transform.up * labelOffset), labelContent, EditorHelpers.FontWithBackgroundStyle); } } else { if (EditorHelpers.IsWarm(suspensionAngleControlID) && DragAndDrop.objectReferences.Length == 0) { if (SceneView.lastActiveSceneView) { SceneView.lastActiveSceneView.Repaint(); } } } return(false); }
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); }