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); }
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 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 }); }
public void SetOffset(JointHelpers.AnchorBias bias, Vector2 newOffset) { if (bias == JointHelpers.AnchorBias.Connected) { connectedBodyOffset = newOffset; return; } mainBodyOffset = newOffset; }
private static Vector2 GetWantedAnchorPosition(AnchoredJoint2D anchoredJoint2D, JointHelpers.AnchorBias bias, Vector2 position) { var wheelJoint2D = (WheelJoint2D)anchoredJoint2D; var otherBias = JointHelpers.GetOppositeBias(bias); var worldAngle = wheelJoint2D.transform.eulerAngles.z + wheelJoint2D.suspension.angle; var slideRay = new Ray(JointHelpers.GetAnchorPosition(wheelJoint2D, otherBias), Helpers2D.GetDirection(worldAngle)); var wantedAnchorPosition = Helpers2D.ClosestPointToRay(slideRay, position); return(wantedAnchorPosition); }
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; } }
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); }
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) }); }
public override Bounds OnGetFrameBounds() { var baseBounds = base.OnGetFrameBounds(); foreach (var joint2D in targets.Cast <T>()) { var mainAnchorPosition = JointHelpers.GetAnchorPosition(joint2D, JointHelpers.AnchorBias.Main); var connectedAnchorPosition = JointHelpers.GetAnchorPosition(joint2D, JointHelpers.AnchorBias.Connected); var diff = connectedAnchorPosition - mainAnchorPosition; if (diff.magnitude <= Mathf.Epsilon) { diff = -Vector2.up; } var normalizedDiff = diff.normalized; var wantedMainAnchorPosition = connectedAnchorPosition - normalizedDiff * GetDistance(joint2D); baseBounds.Encapsulate(wantedMainAnchorPosition); } return(baseBounds); }
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); }
protected virtual Vector2 GetTargetPosition(AnchoredJoint2D joint2D, JointHelpers.AnchorBias bias) { return JointHelpers.GetTargetPosition(joint2D, bias); }
protected Vector2 AnchorSlider(int controlID, float handleScale, IEnumerable<Vector2> snapPositions, JointHelpers.AnchorBias bias, AnchoredJoint2D joint, AnchorInfo anchorInfo) { var sliderState = StateObject.Get<AnchorSliderState>(controlID); var anchorPosition = JointHelpers.GetAnchorPosition(joint, bias); var handleSize = HandleUtility.GetHandleSize(anchorPosition) * handleScale; EditorGUI.BeginChangeCheck(); Vector2 targetPosition; var connectedBody = joint.connectedBody; if (bias == JointHelpers.AnchorBias.Connected) { if (connectedBody) { targetPosition = connectedBody.transform.position; } else { targetPosition = anchorPosition; } } else { targetPosition = joint.gameObject.transform.position; } var originalAngle = JointHelpers.AngleFromAnchor(anchorPosition, targetPosition, joint.gameObject.transform.rotation.eulerAngles.z); if (GUIUtility.hotControl == controlID) { using (var drawer = CreateHotDrawer(originalAngle)) { drawer.alwaysVisible = true; drawer.DrawSquare(anchorPosition, Quaternion.identity, handleSize); } } var hovering = HandleUtility.nearestControl == controlID; var hoveringOrHot = (hovering && GUIUtility.hotControl == 0) || controlID == GUIUtility.hotControl; if (hoveringOrHot && _hoverControlID != controlID) { _hoverControlID = controlID; HandleUtility.Repaint(); } else if (!hoveringOrHot && _hoverControlID == controlID) { _hoverControlID = 0; HandleUtility.Repaint(); } var joint2DSettings = SettingsHelper.GetOrCreate(joint); HandleAnchorContext(controlID, bias, joint, joint2DSettings, anchorPosition, connectedBody); HandleSliderEvents(controlID, joint, sliderState, anchorPosition, hoveringOrHot, handleSize); HandleDragDrop(controlID, joint, joint2DSettings); var result = HandleSliding(controlID, bias, originalAngle, anchorPosition, handleSize); result = HandleSnapping(controlID, snapPositions, bias, joint, anchorInfo, result, handleSize); return result; }
protected virtual IEnumerable<Vector2> GetSnapPositions(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias, Vector2 anchorPosition) { return null; }
private Vector2 HandleSnapping(int controlID, IEnumerable<Vector2> snapPositions, JointHelpers.AnchorBias bias, AnchoredJoint2D joint, AnchorInfo anchorInfo, Vector2 result, float handleSize) { Vector2[] snapPositionsArray; var customSnapPositions = GetSnapPositions(joint, anchorInfo, bias, result); if (customSnapPositions != null) { if (snapPositions == null) { snapPositions = customSnapPositions; } else { snapPositions = snapPositions.Concat(customSnapPositions); } } if (snapPositions != null) { snapPositionsArray = snapPositions as Vector2[] ?? snapPositions.ToArray(); } else { snapPositionsArray = null; } if (Event.current.type == EventType.repaint && (editorSettings.highlightSnapPositions && GUIUtility.hotControl == controlID && EditorGUI.actionKey && snapPositionsArray != null)) { var snapHighlightColor = GetAdjustedColor(editorSettings.snapHighlightColor); using (new HandleColor(snapHighlightColor)) { foreach (var snapPosition in snapPositionsArray) { Handles.CircleCap(0, snapPosition, Quaternion.identity, HandleUtility.GetHandleSize(snapPosition) * EditorHelpers.HandleSizeToPixels * editorSettings.snapDistance * 0.5f); } } } if (EditorGUI.EndChangeCheck() && EditorGUI.actionKey && snapPositionsArray != null) { foreach (var snapPosition in snapPositionsArray) { var distance = Vector2.Distance(result, snapPosition); if (distance < handleSize * EditorHelpers.HandleSizeToPixels * editorSettings.snapDistance) { result = snapPosition; break; } } } return result; }
protected bool AnchorDraggingWidgetGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, IEnumerable<Vector2> otherAnchors, JointHelpers.AnchorBias bias) { var lockID = anchorInfo.GetControlID("lock"); var changed = PreSliderGUI(joint2D, anchorInfo, bias); if (!changed && WantsLocking() && (Event.current.shift || GUIUtility.hotControl == lockID) && (GUIUtility.hotControl == lockID || !anchorInfo.IsActive())) { var farAway = Vector2.Distance( JointHelpers.GetMainAnchorPosition(joint2D), GetWantedAnchorPosition(joint2D, JointHelpers.AnchorBias.Main) ) > AnchorEpsilon || Vector2.Distance( JointHelpers.GetConnectedAnchorPosition(joint2D), GetWantedAnchorPosition(joint2D, JointHelpers.AnchorBias.Connected) ) > AnchorEpsilon; if (SettingsHelper.GetOrCreate(joint2D) .lockAnchors && (bias == JointHelpers.AnchorBias.Either || !farAway)) { //locked! show unlock if (ToggleUnlockButton(lockID, joint2D, bias)) { changed = true; } } else { if (ToggleLockButton(lockID, joint2D, bias)) { changed = true; } } } else if (SliderGUI(joint2D, anchorInfo, otherAnchors, bias)) { changed = true; } if (bias == JointHelpers.AnchorBias.Either) { DrawLineToBody(joint2D, JointHelpers.AnchorBias.Main); DrawLineToBody(joint2D, JointHelpers.AnchorBias.Connected); } else { DrawLineToBody(joint2D, bias); } changed = SingleAnchorGUI(joint2D, anchorInfo, bias) || changed; return changed; }
public Vector2 GetOffset(JointHelpers.AnchorBias bias) { return bias == JointHelpers.AnchorBias.Connected ? connectedBodyOffset : mainBodyOffset; }
private Vector2 HandleSliding(int controlID, JointHelpers.AnchorBias bias, float originalAngle, Vector2 anchorPosition, float handleSize) { Vector2 result; Texture2D sliderTexture; switch (bias) { case JointHelpers.AnchorBias.Main: sliderTexture = editorSettings.mainAnchorTexture; break; case JointHelpers.AnchorBias.Connected: sliderTexture = editorSettings.connectedAnchorTexture; break; case JointHelpers.AnchorBias.Either: sliderTexture = editorSettings.lockedAnchorTexture; break; default: throw new ArgumentOutOfRangeException("bias"); } using (var drawer = CreateTextureDrawer(sliderTexture, originalAngle)) { drawer.alwaysVisible = true; result = Handles.Slider2D(controlID, anchorPosition, Vector3.forward, Vector3.up, Vector3.right, handleSize, drawer.DrawSquare, Vector2.zero); } return result; }
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; }
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; }
protected bool ToggleLockButton(int controlID, AnchoredJoint2D joint2D, JointHelpers.AnchorBias bias) { Vector3 center = JointHelpers.GetAnchorPosition(joint2D, bias); var lockPressed = EditorHelpers.CustomHandleButton(controlID, center, HandleUtility.GetHandleSize(center) * editorSettings.lockButtonScale, editorSettings.unlockButtonTexture, editorSettings.lockButtonTexture, GetAdjustedColor(Color.white)); if (lockPressed) { var jointSettings = SettingsHelper.GetOrCreate(joint2D); EditorHelpers.RecordUndo("Lock Anchors", jointSettings, joint2D); jointSettings.lockAnchors = true; EditorUtility.SetDirty(jointSettings); ReAlignAnchors(joint2D, bias); } return lockPressed; }
private void HandleAnchorContext(int controlID, JointHelpers.AnchorBias bias, AnchoredJoint2D joint, Joint2DSettingsBase joint2DSettings, Vector2 anchorPosition, Rigidbody2D connectedBody) { EditorHelpers.ContextClick(controlID, () => { var menu = new GenericMenu(); menu.AddDisabledItem(new GUIContent(joint.GetType() .Name)); menu.AddSeparator(""); if (WantsLocking()) { menu.AddItem(new GUIContent("Lock Anchors", GetAnchorLockTooltip()), joint2DSettings.lockAnchors, () => { EditorHelpers.RecordUndo( joint2DSettings.lockAnchors ? "Unlock Anchors" : "Lock Anchors", joint2DSettings, joint); if (!joint2DSettings.lockAnchors) { ReAlignAnchors(joint, bias); } joint2DSettings.lockAnchors = !joint2DSettings.lockAnchors; EditorUtility.SetDirty(joint2DSettings); EditorUtility.SetDirty(joint); }); } { var otherBias = JointHelpers.GetOppositeBias(bias); var otherPosition = JointHelpers.GetAnchorPosition(joint, otherBias); if (Vector2.Distance(otherPosition, anchorPosition) <= AnchorEpsilon) { menu.AddDisabledItem(new GUIContent("Bring other anchor here")); } else { menu.AddItem(new GUIContent("Bring other anchor here"), false, () => { EditorHelpers.RecordUndo("Move Joint Anchor", joint); JointHelpers.SetWorldAnchorPosition(joint, anchorPosition, otherBias); EditorUtility.SetDirty(joint); }); } } menu.AddItem(new GUIContent("Enable Collision", "Should rigid bodies connected with this joint collide?"), joint.enableCollision, () => { EditorHelpers.RecordUndo("Move Joint Anchor", joint); joint.enableCollision = !joint.enableCollision; EditorUtility.SetDirty(joint); }); menu.AddSeparator(""); var itemCount = menu.GetItemCount(); ExtraMenuItems(menu, joint); if (itemCount != menu.GetItemCount()) { menu.AddSeparator(""); } if (connectedBody) { var connectedBodyName = connectedBody.name; var selectConnectedBodyContent = new GUIContent(string.Format("Select '{0}'", connectedBodyName)); if (isCreatedByTarget) { menu.AddDisabledItem(selectConnectedBodyContent); } else { menu.AddItem(selectConnectedBodyContent, false, () => { Selection.activeGameObject = connectedBody.gameObject; }); } menu.AddItem(new GUIContent(string.Format("Move ownership to '{0}'", connectedBodyName)), false, () => { var connectedObject = connectedBody.gameObject; var cloneJoint = Undo.AddComponent(connectedObject, joint.GetType()) as AnchoredJoint2D; if (!cloneJoint) { return; } EditorUtility.CopySerialized(joint, cloneJoint); cloneJoint.connectedBody = joint.GetComponent<Rigidbody2D>(); JointHelpers.SetWorldAnchorPosition(cloneJoint, JointHelpers.GetAnchorPosition(joint, JointHelpers.AnchorBias.Main), JointHelpers.AnchorBias.Connected); JointHelpers.SetWorldAnchorPosition(cloneJoint, JointHelpers.GetAnchorPosition(joint, JointHelpers.AnchorBias.Connected), JointHelpers.AnchorBias.Main); var jointSettings = SettingsHelper.GetOrCreate(joint); var cloneSettings = Undo.AddComponent(connectedObject, jointSettings.GetType()) as Joint2DSettingsBase; if (cloneSettings == null) { return; } cloneSettings.hideFlags = HideFlags.HideInInspector; EditorUtility.CopySerialized(jointSettings, cloneSettings); cloneSettings.Setup(cloneJoint); cloneSettings.SetOffset(JointHelpers.AnchorBias.Main, jointSettings.GetOffset(JointHelpers.AnchorBias.Connected)); cloneSettings.SetOffset(JointHelpers.AnchorBias.Connected, jointSettings.GetOffset(JointHelpers.AnchorBias.Main)); if (!Selection.Contains(connectedObject)) { var selectedObjects = new List<Object>(Selection.objects) {connectedObject}; if (selectedObjects.Contains(joint.gameObject)) { selectedObjects.Remove(joint.gameObject); } Selection.objects = selectedObjects.ToArray(); } Undo.DestroyObjectImmediate(joint); OwnershipMoved(cloneJoint); }); menu.AddItem(new GUIContent("Disconnect from '" + connectedBodyName + "'"), false, () => { var worldConnectedPosition = JointHelpers.GetConnectedAnchorPosition(joint); using (new Modification("Disconnect from connected body", joint)) { joint.connectedBody = null; JointHelpers.SetWorldConnectedAnchorPosition(joint, worldConnectedPosition); } }); } else { menu.AddDisabledItem(new GUIContent("Select connected body")); menu.AddDisabledItem(new GUIContent("Move ownership to connected body")); menu.AddDisabledItem(new GUIContent("Disconnect from connected body")); } menu.AddItem(new GUIContent("Delete " + joint.GetType() .Name), false, () => Undo.DestroyObjectImmediate(joint)); menu.ShowAsContext(); }); }
private void LimitWidget(SliderJoint2D sliderJoint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias, float worldAngle) { var anchorPosition = JointHelpers.GetAnchorPosition(sliderJoint2D, bias); var oppositeBias = JointHelpers.GetOppositeBias(bias); var oppositeAnchorPosition = JointHelpers.GetAnchorPosition(sliderJoint2D, oppositeBias); var direction = Helpers2D.GetDirection(worldAngle); if (bias == JointHelpers.AnchorBias.Connected) { direction *= -1f; } var delta = oppositeAnchorPosition - anchorPosition; var angleDiff = Mathf.DeltaAngle(Helpers2D.GetAngle(delta), worldAngle); Vector2 rotatedDelta = Helpers2D.Rotate(angleDiff) * delta; var wantedOppositeAnchorPosition = anchorPosition + rotatedDelta; var wantedOppositeAnchorPosition2 = anchorPosition - rotatedDelta; var minLimitControlID = anchorInfo.GetControlID("minLimit"); var maxLimitControlID = anchorInfo.GetControlID("maxLimit"); LimitContext(sliderJoint2D, minLimitControlID, Limit.Min); LimitContext(sliderJoint2D, maxLimitControlID, Limit.Max); var limitColor = sliderJoint2D.limits.min > sliderJoint2D.limits.max ? editorSettings.incorrectLimitsColor : editorSettings.correctLimitsColor; if (isCreatedByTarget) { limitColor.a *= editorSettings.connectedJointTransparency; } using (new HandleColor(limitColor)) { Handles.DrawLine(anchorPosition + direction * sliderJoint2D.limits.min, anchorPosition + direction * sliderJoint2D.limits.max); if (Event.current.type == EventType.repaint) { float fontSize; if (EditorHelpers.IsWarm(minLimitControlID) && DragAndDrop.objectReferences.Length == 0) { var labelContent = new GUIContent(string.Format("Min: {0:0.00}", sliderJoint2D.limits.min)); var sliderPosition = anchorPosition + (direction) * (sliderJoint2D.limits.min); fontSize = HandleUtility.GetHandleSize(sliderPosition) * ONE_OVER64; var labelOffset = fontSize * EditorHelpers.FontWithBackgroundStyle.CalcSize(labelContent).y + fontSize * 20 * Mathf.Abs(Mathf.Cos(Mathf.Deg2Rad * Helpers2D.GetAngle(direction))); EditorHelpers.OverlayLabel((Vector3)sliderPosition + (Camera.current.transform.up * labelOffset), labelContent, EditorHelpers.FontWithBackgroundStyle); } if (EditorHelpers.IsWarm(maxLimitControlID) && DragAndDrop.objectReferences.Length == 0) { var labelContent = new GUIContent(string.Format("Max: {0:0.00}", sliderJoint2D.limits.max)); var sliderPosition = anchorPosition + (direction) * (sliderJoint2D.limits.max); fontSize = HandleUtility.GetHandleSize(sliderPosition) * ONE_OVER64; var labelOffset = fontSize * EditorHelpers.FontWithBackgroundStyle.CalcSize(labelContent).y + fontSize * 20 * Mathf.Abs(Mathf.Cos(Mathf.Deg2Rad * Helpers2D.GetAngle(direction))); EditorHelpers.OverlayLabel((Vector3)sliderPosition + (Camera.current.transform.up * labelOffset), labelContent, EditorHelpers.FontWithBackgroundStyle); } } if (GUIUtility.hotControl == minLimitControlID || GUIUtility.hotControl == maxLimitControlID) { using ( new HandleColor(new Color(1, 1, 1, 0.25f * (isCreatedByTarget ? editorSettings.connectedJointTransparency : 1.0f)))) { var handleSize = HandleUtility.GetHandleSize(wantedOppositeAnchorPosition) * ONE_OVER16; Handles.DrawLine(wantedOppositeAnchorPosition - direction * handleSize, wantedOppositeAnchorPosition + direction * handleSize); handleSize = HandleUtility.GetHandleSize(wantedOppositeAnchorPosition2) * ONE_OVER16; Handles.DrawLine(wantedOppositeAnchorPosition2 - direction * handleSize, wantedOppositeAnchorPosition2 + direction * handleSize); Handles.DrawWireArc(anchorPosition, Vector3.forward, wantedOppositeAnchorPosition, 360, Vector2.Distance(wantedOppositeAnchorPosition, anchorPosition)); } } var actionKey = EditorGUI.actionKey; List <Vector2> snapList = null; if (actionKey) { snapList = new List <Vector2> { anchorPosition, wantedOppositeAnchorPosition, wantedOppositeAnchorPosition2 }; } var minLimitColor = editorSettings.minLimitColor; var maxLimitColor = editorSettings.maxLimitColor; if (isCreatedByTarget) { minLimitColor.a *= editorSettings.connectedJointTransparency; maxLimitColor.a *= editorSettings.connectedJointTransparency; } using (new HandleColor(minLimitColor)) { DrawLimitSlider(sliderJoint2D, minLimitControlID, anchorPosition, direction, snapList, Limit.Min); } using (new HandleColor(maxLimitColor)) { DrawLimitSlider(sliderJoint2D, maxLimitControlID, anchorPosition, direction, snapList, Limit.Max); } } }
protected virtual void ReAlignAnchors(AnchoredJoint2D joint2D, JointHelpers.AnchorBias alignmentBias) { var oppositeBias = JointHelpers.GetOppositeBias(alignmentBias); JointHelpers.SetWorldAnchorPosition(joint2D, GetWantedAnchorPosition(joint2D, oppositeBias), oppositeBias); }
protected override Vector2 GetWantedAnchorPosition(AnchoredJoint2D anchoredJoint2D, JointHelpers.AnchorBias bias) { return(GetWantedAnchorPosition(anchoredJoint2D, bias, JointHelpers.GetAnchorPosition(anchoredJoint2D, bias))); }
protected virtual Vector2 GetWantedAnchorPosition(AnchoredJoint2D anchoredJoint2D, JointHelpers.AnchorBias bias) { var oppositeBias = JointHelpers.GetOppositeBias(bias); return JointHelpers.GetAnchorPosition(anchoredJoint2D, oppositeBias); }
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); }
protected virtual bool PostAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo info, List<Vector2> otherAnchors, JointHelpers.AnchorBias bias) { return false; }
private void DrawDistance(T jointWithDistance, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias) { if (jointWithDistance == null) { return; } var otherBias = bias == JointHelpers.AnchorBias.Main ? JointHelpers.AnchorBias.Connected : JointHelpers.AnchorBias.Main; var anchorPosition = JointHelpers.GetAnchorPosition(jointWithDistance, bias); var otherAnchorPosition = JointHelpers.GetAnchorPosition(jointWithDistance, otherBias); var diff = anchorPosition - otherAnchorPosition; if (diff.magnitude <= Mathf.Epsilon) { diff = Vector2.up * (bias == JointHelpers.AnchorBias.Connected ? 1 : -1); } var normalizedDiff = diff.normalized; JointHelpers.AnchorBias wantedBias; switch (GetSettings(jointWithDistance).anchorPriority) { case JointSettingsWithBias.AnchorPriority.Main: wantedBias = JointHelpers.AnchorBias.Main; break; case JointSettingsWithBias.AnchorPriority.Connected: wantedBias = JointHelpers.AnchorBias.Connected; break; default: throw new ArgumentOutOfRangeException(); } if (bias == wantedBias && EditorGUI.actionKey && GUIUtility.hotControl == anchorInfo.GetControlID("slider")) { Handles.DrawWireDisc(otherAnchorPosition, Vector3.forward, GetDistance(jointWithDistance)); } if (bias != wantedBias) { var distanceControlID = anchorInfo.GetControlID("distance"); EditorGUI.BeginChangeCheck(); float newDistance; using ( new HandleColor(isCreatedByTarget ? new Color(1, 1, 1, editorSettings.connectedJointTransparency) : Color.white)) { newDistance = EditorHelpers.LineSlider(distanceControlID, otherAnchorPosition, GetDistance(jointWithDistance), Helpers2D.GetAngle(normalizedDiff), 0.125f, true); EditorHelpers.DrawThickLine(anchorPosition, otherAnchorPosition + normalizedDiff * newDistance, Vector2.Distance(anchorPosition, otherAnchorPosition) > newDistance ? 2 : 1, true); } if (Event.current.type == EventType.repaint) { if (EditorHelpers.IsWarm(distanceControlID) && DragAndDrop.objectReferences.Length == 0) { var labelContent = new GUIContent(string.Format("Distance: {0:0.00}", GetDistance(jointWithDistance))); var sliderPosition = otherAnchorPosition + normalizedDiff * GetDistance(jointWithDistance); var fontSize = HandleUtility.GetHandleSize(sliderPosition) * (1f / 64f); var labelOffset = fontSize * EditorHelpers.FontWithBackgroundStyle.CalcSize(labelContent).y + fontSize * 20 * Mathf.Abs(Mathf.Cos(Mathf.Deg2Rad * Helpers2D.GetAngle(normalizedDiff))); EditorHelpers.OverlayLabel((Vector3)sliderPosition + (Camera.current.transform.up * labelOffset), labelContent, EditorHelpers.FontWithBackgroundStyle); } } if (EditorGUI.EndChangeCheck()) { using (new Modification("Change Distance", jointWithDistance)) { if (newDistance < 0) { SetDistance(jointWithDistance, 0f); } else { var distanceBetweenAnchors = Vector2.Distance(otherAnchorPosition, anchorPosition); SetDistance(jointWithDistance, EditorGUI.actionKey && Mathf.Abs(newDistance - distanceBetweenAnchors) < HandleUtility.GetHandleSize(anchorPosition) * 0.125f ? distanceBetweenAnchors : newDistance); } } } DistanceContext(jointWithDistance, distanceControlID); } }
protected virtual bool SingleAnchorGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias) { return false; }
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; }
protected virtual void ToggleIndividualAnchorLock(bool wantsLock, AnchoredJoint2D joint2D, JointHelpers.AnchorBias alignmentBias) { var jointSettings = SettingsHelper.GetOrCreate(joint2D); var action = wantsLock ? "Lock Anchors" : "Unlock Anchors"; EditorHelpers.RecordUndo(action, jointSettings); jointSettings.lockAnchors = wantsLock; EditorUtility.SetDirty(jointSettings); if (wantsLock) { EditorHelpers.RecordUndo(action, joint2D); ReAlignAnchors(joint2D, alignmentBias); EditorUtility.SetDirty(joint2D); } }
protected override bool PreSliderGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias) { var hingeJoint2D = joint2D as HingeJoint2D; if (hingeJoint2D == null) { return false; } return DrawAngleLimits(hingeJoint2D, anchorInfo, bias); }
private void DrawLineToBody(AnchoredJoint2D joint2D, JointHelpers.AnchorBias bias) { if (bias == JointHelpers.AnchorBias.Connected && !joint2D.connectedBody) { return; } if (editorSettings.drawLinesToBodies || isCreatedByTarget) { Color lineColor; if (bias == JointHelpers.AnchorBias.Main) { lineColor = editorSettings.anchorsToMainBodyColor; } else { lineColor = editorSettings.anchorsToConnectedBodyColor; } using (new HandleColor(GetAdjustedColor(lineColor))) { Vector3 bodyPosition = JointHelpers.GetTargetPosition(joint2D, bias); var anchorPosition = JointHelpers.GetAnchorPosition(joint2D, bias); if (Vector2.Distance(bodyPosition, anchorPosition) > AnchorEpsilon) { Handles.DrawLine(bodyPosition, anchorPosition); } } } }
private void DrawLinesAndDiscs(HingeJoint2D hingeJoint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias) { var center = JointHelpers.GetAnchorPosition(hingeJoint2D, bias); var scale = editorSettings.anchorScale; var handleSize = HandleUtility.GetHandleSize(center) * scale; var mainBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Main); var connectedBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Connected); var settings = SettingsHelper.GetOrCreate<HingeJoint2DSettings>(hingeJoint2D); if (bias == JointHelpers.AnchorBias.Main) { float angleToMain; if (Vector2.Distance(mainBodyPosition, center) > AnchorEpsilon) { angleToMain = Helpers2D.GetAngle(mainBodyPosition - center); } else { angleToMain = JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Main); } using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToMainBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(angleToMain + settings.mainAngleOffset) * handleSize); } } else if (bias == JointHelpers.AnchorBias.Connected) { if (hingeJoint2D.connectedBody) { float angleToConnected; if (Vector2.Distance(connectedBodyPosition, center) > AnchorEpsilon) { angleToConnected = Helpers2D.GetAngle(connectedBodyPosition - center); } else { angleToConnected = JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Connected); } using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToConnectedBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(angleToConnected + settings.connectedAngleOffset) * handleSize); } } else { using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToConnectedBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(settings.connectedAngleOffset) * handleSize); } } } if (settings.showDiscs) { var sliderControlID = anchorInfo.GetControlID("slider"); if (editorSettings.ringDisplayMode == JointEditorSettings.RingDisplayMode.Always || (editorSettings.ringDisplayMode == JointEditorSettings.RingDisplayMode.Hover && //if nothing else is hot and we are being hovered, or the anchor's widgets are hot ((GUIUtility.hotControl == 0 && HandleUtility.nearestControl == sliderControlID) || GUIUtility.hotControl == sliderControlID)) ) { using (new HandleColor(GetAdjustedColor(editorSettings.mainRingColor))) { Handles.DrawWireDisc(center, Vector3.forward, Vector2.Distance(center, mainBodyPosition)); } if (hingeJoint2D.connectedBody) { using (new HandleColor((editorSettings.connectedRingColor))) { Handles.DrawWireDisc(center, Vector3.forward, Vector2.Distance(center, connectedBodyPosition)); } } } } HandleUtility.Repaint(); }
private void DrawLinesAndDiscs(HingeJoint2D hingeJoint2D, AnchorInfo anchorInfo, JointHelpers.AnchorBias bias) { var center = JointHelpers.GetAnchorPosition(hingeJoint2D, bias); var scale = editorSettings.anchorScale; var handleSize = HandleUtility.GetHandleSize(center) * scale; var mainBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Main); var connectedBodyPosition = GetTargetPosition(hingeJoint2D, JointHelpers.AnchorBias.Connected); var settings = SettingsHelper.GetOrCreate <HingeJoint2DSettings>(hingeJoint2D); if (bias == JointHelpers.AnchorBias.Main) { float angleToMain; if (Vector2.Distance(mainBodyPosition, center) > AnchorEpsilon) { angleToMain = Helpers2D.GetAngle(mainBodyPosition - center); } else { angleToMain = JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Main); } using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToMainBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(angleToMain + settings.mainAngleOffset) * handleSize); } } else if (bias == JointHelpers.AnchorBias.Connected) { if (hingeJoint2D.connectedBody) { float angleToConnected; if (Vector2.Distance(connectedBodyPosition, center) > AnchorEpsilon) { angleToConnected = Helpers2D.GetAngle(connectedBodyPosition - center); } else { angleToConnected = JointHelpers.GetTargetRotation(hingeJoint2D, JointHelpers.AnchorBias.Connected); } using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToConnectedBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(angleToConnected + settings.connectedAngleOffset) * handleSize); } } else { using (new HandleColor(GetAdjustedColor(editorSettings.anchorsToConnectedBodyColor))) { Handles.DrawLine(center, center + Helpers2D.GetDirection(settings.connectedAngleOffset) * handleSize); } } } if (settings.showDiscs) { var sliderControlID = anchorInfo.GetControlID("slider"); if (editorSettings.ringDisplayMode == JointEditorSettings.RingDisplayMode.Always || (editorSettings.ringDisplayMode == JointEditorSettings.RingDisplayMode.Hover && //if nothing else is hot and we are being hovered, or the anchor's widgets are hot ((GUIUtility.hotControl == 0 && HandleUtility.nearestControl == sliderControlID) || GUIUtility.hotControl == sliderControlID)) ) { using (new HandleColor(GetAdjustedColor(editorSettings.mainRingColor))) { Handles.DrawWireDisc(center, Vector3.forward, Vector2.Distance(center, mainBodyPosition)); } if (hingeJoint2D.connectedBody) { using (new HandleColor((editorSettings.connectedRingColor))) { Handles.DrawWireDisc(center, Vector3.forward, Vector2.Distance(center, connectedBodyPosition)); } } } } HandleUtility.Repaint(); }
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); }
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); }
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); }
protected bool SliderGUI(AnchoredJoint2D joint2D, AnchorInfo anchorInfo, IEnumerable<Vector2> otherAnchors, JointHelpers.AnchorBias bias) { var sliderID = anchorInfo.GetControlID("slider"); List<Vector2> snapPositions = null; if (EditorGUI.actionKey) { snapPositions = new List<Vector2> { GetTargetPosition(joint2D, JointHelpers.AnchorBias.Main), JointHelpers.GetTargetTransform(joint2D, JointHelpers.AnchorBias.Main) .position }; if (joint2D.connectedBody) { snapPositions.Add(GetTargetPosition(joint2D, JointHelpers.AnchorBias.Connected)); snapPositions.Add(JointHelpers.GetTargetTransform(joint2D, JointHelpers.AnchorBias.Connected) .position); } switch (bias) { case JointHelpers.AnchorBias.Main: snapPositions.Add(JointHelpers.GetAnchorPosition(joint2D, JointHelpers.AnchorBias.Connected)); break; case JointHelpers.AnchorBias.Connected: snapPositions.Add(JointHelpers.GetAnchorPosition(joint2D, JointHelpers.AnchorBias.Main)); break; } snapPositions.AddRange(otherAnchors); } EditorGUI.BeginChangeCheck(); var position = AnchorSlider(sliderID, editorSettings.anchorScale, snapPositions, bias, joint2D, anchorInfo); var changed = false; if (EditorGUI.EndChangeCheck()) { EditorHelpers.RecordUndo("Anchor Move", joint2D); changed = true; JointHelpers.SetWorldAnchorPosition(joint2D, position, bias); } return changed; }