public static void DrawThickLine(Vector3 p1, Vector3 p2, float thickness, bool alwaysVisible = false) { var current = Camera.current; if (!current || Event.current.type != EventType.Repaint) { return; } var handleColor = Handles.color; if (alwaysVisible) { Helpers.AlwaysVisibleVertexGUIMaterial.SetPass(0); } else { Helpers.VertexGUIMaterial.SetPass(0); } using (new GLMatrix()) { GL.MultMatrix(Handles.matrix); var screenPoint1 = current.WorldToScreenPoint(p1); var screenPoint2 = current.WorldToScreenPoint(p2); var dir = (screenPoint2 - screenPoint1).normalized; Vector3 perpendicular = Helpers2D.GetPerpendicularVector(dir).normalized *thickness *0.5f; dir *= (thickness * 0.5f); GL.Begin(GL.QUADS); GL.Color(new Color(handleColor.r, handleColor.g, handleColor.b, handleColor.a * 0.5f)); var extendedPerpendicular = (perpendicular + perpendicular.normalized * 1f); var extendedDir = (dir + dir.normalized * 1f); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - extendedDir - extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - extendedDir + extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + extendedDir + extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + extendedDir - extendedPerpendicular)); GL.End(); GL.Begin(GL.QUADS); GL.Color(new Color(handleColor.r, handleColor.g, handleColor.b, handleColor.a * 0.5f)); extendedPerpendicular = (perpendicular + perpendicular.normalized * .5f); extendedDir = (dir + dir.normalized * .5f); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - extendedDir - extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - extendedDir + extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + extendedDir + extendedPerpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + extendedDir - extendedPerpendicular)); GL.End(); GL.Begin(GL.QUADS); GL.Color(handleColor); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - dir - perpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint1 - dir + perpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + dir + perpendicular)); GL.Vertex(current.ScreenToWorldPoint(screenPoint2 + dir - perpendicular)); GL.End(); } }
public static float LineSlider(int controlID, Vector2 center, float distance, float angle, float handleScale = 1f, bool alwaysVisible = false, bool arrow = false) { var hoverState = StateObject.Get <HoverState>(controlID); var direction = Helpers2D.GetDirection(angle); var wantedPosition = center + direction * distance; var handleSize = HandleUtility.GetHandleSize(wantedPosition) * handleScale; var normal = Helpers2D.GetPerpendicularVector(direction) * handleSize; EditorGUI.BeginChangeCheck(); wantedPosition = Handles.Slider2D(controlID, wantedPosition, Vector3.forward, Vector3.up, Vector3.right, normal.magnitude * 2, (id, position, rotation, size) => { }, Vector2.zero); if (EditorGUI.EndChangeCheck()) { distance = Helpers2D.DistanceAlongLine(new Ray(center, direction), wantedPosition); } var current = Event.current; float drawScale = 1; switch (current.GetTypeForControl(controlID)) { case EventType.mouseMove: var hovering = HandleUtility.nearestControl == controlID; if (hoverState.hovering != hovering) { current.Use(); hoverState.hovering = hovering; } break; case EventType.repaint: var handleColor = Handles.color; if (GUIUtility.hotControl == controlID || hoverState.hovering) { if (GUIUtility.hotControl == controlID) { handleColor = Color.red; } else { handleColor = Color.yellow; } var cursor = RotatedResizeCursor(direction); SetEditorCursor(cursor, controlID); drawScale = 2; } else if (GUIUtility.hotControl != 0) { handleColor.a *= 0.5f; } var drawNormal = normal * drawScale; using (new HandleColor(handleColor)) { Vector3 a = wantedPosition - drawNormal; Vector3 b = wantedPosition + drawNormal; if (GUIUtility.hotControl == controlID) { var cameraVectorA = HandleToScreenPoint(a); var cameraVectorB = HandleToScreenPoint(b); cameraVectorA.z -= 0.01f; cameraVectorB.z -= 0.01f; a = ScreenToHandlePoint(cameraVectorA); b = ScreenToHandlePoint(cameraVectorB); } if (arrow) { Vector3 directionOffset = direction * handleSize * drawScale * 0.5f; var backgroundColor = Helpers.YIQ(Handles.color); backgroundColor.a = Handles.color.a; // using (new HandleColor(backgroundColor)) { DrawThickLine(a, b, 4, alwaysVisible); DrawThickLine(a - directionOffset, a, 4, alwaysVisible); DrawThickLine(b - directionOffset, b, 4, alwaysVisible); } DrawThickLine(a, b, 2, alwaysVisible); DrawThickLine(a - directionOffset, a, 2, alwaysVisible); DrawThickLine(b - directionOffset, b, 2, alwaysVisible); } else { DrawThickLineWithOutline(a, b, 2, 2, alwaysVisible); } } break; } return(distance); }