public static MouseCursor RotatedResizeCursor(Vector2 direction) { var cameraDirection = GetCameraDirection(direction); var angle = Helpers2D.GetAngle(cameraDirection); MouseCursor cursor; if (Mathf.Abs(Mathf.DeltaAngle(angle, 0)) <= 22.5f || Mathf.Abs(Mathf.DeltaAngle(angle, 180)) <= 22.5f) { cursor = MouseCursor.ResizeHorizontal; } else if (Mathf.Abs(Mathf.DeltaAngle(angle, 45)) <= 22.5f || Mathf.Abs(Mathf.DeltaAngle(angle, 225)) <= 22.5f) { cursor = MouseCursor.ResizeUpRight; } else if (Mathf.Abs(Mathf.DeltaAngle(angle, 135)) <= 22.5f || Mathf.Abs(Mathf.DeltaAngle(angle, 315)) <= 22.5f) { cursor = MouseCursor.ResizeUpLeft; } else { cursor = MouseCursor.ResizeVertical; } return(cursor); }
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); }
public static float AngleSlider(int controlID, HandleDrawerBase drawer, Vector2 center, float angle, float distanceFromCenter, float handleSize, float snap = 0) { var info = StateObject.Get <AngleSliderInfo>(controlID); var current = Event.current; if (GUIUtility.hotControl == controlID) { angle = info.angle; } var handlePosition = center + Helpers2D.GetDirection(angle) * distanceFromCenter; if (Event.current.type == EventType.layout) { var distanceFromDrawer = drawer.GetDistance(handlePosition, handleSize, angle); HandleUtility.AddControl(controlID, distanceFromDrawer); } var typeForControl = current.GetTypeForControl(controlID); switch (typeForControl) { case EventType.mouseMove: var hovering = HandleUtility.nearestControl == controlID && (GUIUtility.hotControl == 0 || GUIUtility.hotControl == controlID); if (info.hovering != hovering) { current.Use(); info.hovering = hovering; } break; } if (GUIUtility.hotControl == controlID) { //active! switch (typeForControl) { case EventType.mouseUp: if (current.button == info.button) { current.Use(); GUIUtility.hotControl = 0; } break; case EventType.mouseDrag: current.Use(); info.mousePosition += new Vector2(current.delta.x, current.delta.y); Vector2 worldMousePosition = HandlePointToWorld(info.mousePosition); var mouseAngle = Helpers2D.GetAngle(worldMousePosition - center); info.angle += Mathf.DeltaAngle(info.mouseAngle, mouseAngle); info.mouseAngle = mouseAngle; angle = Handles.SnapValue(info.angle, snap); GUI.changed = true; break; } } else { if (GUIUtility.hotControl == 0) { switch (typeForControl) { case EventType.mouseDown: if (HandleUtility.nearestControl == controlID && current.button == 0) { info.button = current.button; info.mousePosition = current.mousePosition; Vector2 worldMousePosition = HandlePointToWorld(info.mousePosition); var mouseAngle = Helpers2D.GetAngle(worldMousePosition - center); info.mouseAngle = mouseAngle; info.angle = angle; current.Use(); GUIUtility.hotControl = controlID; } break; } } } if (typeForControl == EventType.repaint) { if (GUIUtility.hotControl == controlID || (GUIUtility.hotControl == 0 && info.hovering)) { SetEditorCursor(MouseCursor.RotateArrow, controlID); } drawer.Draw(controlID, handlePosition, handleSize, angle, info.hovering); } return(angle); }