static bool HasCorner(GrabCorner c, GrabCorner f) { return (c & f) != GrabCorner.None; }
void DrawHandles(ref Rect uiRect, ref GrabCorner dragging) { var nr = Normalize(uiRect); foreach (var handle in grabHandles) { var r = handle.rect(nr); // AddCursorRect requires positive height and width (from 'Normalize') EditorGUIUtility.AddCursorRect(r, handle.cursor); if (handle.cursor != MouseCursor.MoveArrow) GUI.DrawTexture(r, handles); // Start drag if (Event.current.type == EventType.mouseDown && r.Contains(Event.current.mousePosition)) { var d = handle.corners; if (uiRect.width < 0f) // Invert X { if (HasCorner(d, GrabCorner.XMin) && !HasCorner(d, GrabCorner.XMax)) d = (d | GrabCorner.XMax) & ~GrabCorner.XMin; else if (HasCorner(d, GrabCorner.XMax) && !HasCorner(d, GrabCorner.XMin)) d = (d | GrabCorner.XMin) & ~GrabCorner.XMax; } if (uiRect.height < 0f) // Invert Y { if (HasCorner(d, GrabCorner.YMin) && !HasCorner(d, GrabCorner.YMax)) d = (d | GrabCorner.YMax) & ~GrabCorner.YMin; else if (HasCorner(d, GrabCorner.YMax) && !HasCorner(d, GrabCorner.YMin)) d = (d | GrabCorner.YMin) & ~GrabCorner.YMax; } dragging = d; // Using RecordObject doesn't work fully here, for some reason Undo.RegisterCompleteObjectUndo(this, "Drag font character"); } } if (dragging != GrabCorner.None) { // Move drag if (Event.current.type == EventType.mouseDrag) { var d = dragging; if (HasCorner(d, GrabCorner.XMin)) uiRect.xMin += Event.current.delta.x; if (HasCorner(d, GrabCorner.XMax)) uiRect.xMax += Event.current.delta.x; if (HasCorner(d, GrabCorner.YMin)) uiRect.yMin += Event.current.delta.y; if (HasCorner(d, GrabCorner.YMax)) uiRect.yMax += Event.current.delta.y; } else if (Event.current.type == EventType.mouseUp) { // Stop drag dragging = GrabCorner.None; } } }