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 bool CustomHandleButton(int controlID, Vector3 buttonPosition, float buttonSize, Texture2D texture, Texture2D hotTexture, Color color) { var distance = HandleUtility.DistanceToRectangle(buttonPosition, Quaternion.identity, buttonSize * 0.5f); var buttonState = StateObject.Get <CustomButtonState>(controlID); switch (Event.current.type) { case EventType.layout: HandleUtility.AddControl(controlID, distance); break; case EventType.repaint: using (var drawer = new GUITextureDrawer(texture, hotTexture)) { drawer.alwaysVisible = true; using ( new MaterialProperty(drawer.Material, "_Hot", GUIUtility.hotControl == controlID && distance <= 0 ? 1f : 0f) ) { using (new MaterialColor(drawer.Material, color)) { drawer.DrawSquare(buttonPosition, Quaternion.identity, buttonSize); } } HandleUtility.Repaint(); if (buttonState.hovering) { SetEditorCursor(MouseCursor.Link, controlID); } } break; case EventType.mouseMove: { if (HandleUtility.nearestControl == controlID) { buttonState.hovering = true; } else { buttonState.hovering = false; } break; } } switch (Event.current.GetTypeForControl(controlID)) { case EventType.mouseDown: if (HandleUtility.nearestControl == controlID && Event.current.button == 0) { GUIUtility.hotControl = controlID; Event.current.Use(); HandleUtility.Repaint(); } break; case EventType.mouseUp: if (GUIUtility.hotControl == controlID) { GUIUtility.hotControl = 0; if (distance <= 0) { GUI.changed = true; Event.current.Use(); HandleUtility.Repaint(); return(true); } } break; } return(false); //throw new NotImplementedException(); }
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); }