public void Drag(Vector2 delta, Vector2 constraint) { dragPosition.position += delta; var lockedPosition = dragPosition; if (constraint.x == 0) { lockedPosition.x = dragLockOrigin.x; } if (constraint.y == 0) { lockedPosition.y = dragLockOrigin.y; } if (snapToGrid) { position = GraphGUI.SnapToGrid(lockedPosition, canResizeHorizontal && canResizeVertical); } else { position = lockedPosition.PixelPerfect(); } Reposition(); }
public override void DrawForeground() { base.DrawForeground(); if (e.IsRepaint) { GraphGUI.Node(edgePosition.PixelPerfect(), shape, color, isSelected); } }
protected Rect InnerToEdgePosition(Rect position) { return(GraphGUI.GetNodeInnerToEdgePosition(position, shape)); }
protected Rect EdgeToInnerPosition(Rect position) { return(GraphGUI.GetNodeEdgeToInnerPosition(position, shape)); }
public static void PasteOutside(bool reposition, Vector2?position = null) { if (!canPasteOutside) { throw new InvalidOperationException(); } UndoUtility.RecordEditedObject("Paste Graph Elements"); var pastedElements = GetPasteGroup(); // Assign new GUIDs foreach (var pastedElement in pastedElements) { pastedElement.guid = Guid.NewGuid(); } // Add elements to graph and selection selection.Clear(); foreach (var pastedElement in pastedElements) { if (!pastedElement.HandleDependencies()) { continue; } graph.elements.Add(pastedElement); selection.Add(pastedElement); } canvas.Cache(); foreach (var pastedElement in pastedElements) { var pastedWidget = canvas.Widget(pastedElement); pastedWidget.BringToFront(); } var pastedWidgets = pastedElements.Select(e => canvas.Widget(e)).ToList(); // Recenter elements in graph view if (reposition) { var area = GraphGUI.CalculateArea(pastedWidgets.Where(widget => widget.canDrag)); Vector2 delta; if (position.HasValue) { delta = position.Value - area.position; } else { delta = graph.pan - area.center; } foreach (var pastedWidget in pastedWidgets) { if (pastedWidget.canDrag) { pastedWidget.position = new Rect(pastedWidget.position.position + delta, pastedWidget.position.size).PixelPerfect(); pastedWidget.Reposition(); pastedWidget.CachePositionFirstPass(); pastedWidget.CachePosition(); } } } // Space out overlapping elements foreach (var pastedWidget in pastedWidgets) { if (pastedWidget.canDrag) { var distanciation = 20; var timeout = 100; var timeoutIndex = 0; while (GraphGUI.PositionOverlaps(canvas, pastedWidget, 5)) { // Space the widget out pastedWidget.position = new Rect(pastedWidget.position.position + new Vector2(distanciation, distanciation), pastedWidget.position.size).PixelPerfect(); // Calculate the resulting position immediately pastedWidget.CachePositionFirstPass(); pastedWidget.CachePosition(); // Mark it as invalid still so dependencies like ports will be recached pastedWidget.Reposition(); // Failsafe to keep the editor from freezing if (++timeoutIndex > timeout) { Debug.LogWarning($"Failed to space out pasted element: {pastedWidget.element}"); break; } } } } canvas.Cache(); GUI.changed = true; e?.TryUse(); }
private void HandleResizing() { if (e.IsMouseDrag(MouseButton.Left) && !isResizing && canStartResize) { if (resizeLeftArea.Contains(mousePosition)) { isResizingXMin = true; xMinResizeOffset = mousePosition.x - position.xMin; } if (resizeRightArea.Contains(mousePosition)) { isResizingXMax = true; xMaxResizeOffset = mousePosition.x - position.xMax; } if (resizeTopArea.Contains(mousePosition)) { isResizingYMin = true; yMinResizeOffset = mousePosition.y - position.yMin; } if (resizeBottomArea.Contains(mousePosition)) { isResizingYMax = true; yMaxResizeOffset = mousePosition.y - position.yMax; } if (resizeTopLeftArea.Contains(mousePosition)) { isResizingXMin = true; isResizingYMin = true; xMinResizeOffset = mousePosition.x - position.xMin; yMinResizeOffset = mousePosition.y - position.yMin; } if (resizeTopRightArea.Contains(mousePosition)) { isResizingXMax = true; isResizingYMin = true; xMaxResizeOffset = mousePosition.x - position.xMax; yMinResizeOffset = mousePosition.y - position.yMin; } if (resizeBottomLeftArea.Contains(mousePosition)) { isResizingXMin = true; isResizingYMax = true; xMinResizeOffset = mousePosition.x - position.xMin; yMaxResizeOffset = mousePosition.y - position.yMax; } if (resizeBottomRightArea.Contains(mousePosition)) { isResizingXMax = true; isResizingYMax = true; xMaxResizeOffset = mousePosition.x - position.xMax; yMaxResizeOffset = mousePosition.y - position.yMax; } e.Use(); } else if (e.IsMouseDrag(MouseButton.Left) && isResizing) { var resizedPosition = position; if (isResizingXMin) { resizedPosition.xMin = Mathf.Min(position.xMax - minResizeSize.x, mousePosition.x - xMinResizeOffset); if (snapToGrid) { resizedPosition.xMin = GraphGUI.SnapToGrid(resizedPosition.xMin); } } if (isResizingXMax) { resizedPosition.xMax = Mathf.Max(position.xMin + minResizeSize.x, mousePosition.x - xMaxResizeOffset); if (snapToGrid) { resizedPosition.xMax = GraphGUI.SnapToGrid(resizedPosition.xMax); } } if (isResizingYMin) { resizedPosition.yMin = Mathf.Min(position.yMax - minResizeSize.y, mousePosition.y - yMinResizeOffset); if (snapToGrid) { resizedPosition.yMin = GraphGUI.SnapToGrid(resizedPosition.yMin); } } if (isResizingYMax) { resizedPosition.yMax = Mathf.Max(position.yMin + minResizeSize.y, mousePosition.y - yMaxResizeOffset); if (snapToGrid) { resizedPosition.yMax = GraphGUI.SnapToGrid(resizedPosition.yMax); } } resizedPosition.width = Mathf.Clamp(resizedPosition.width, minResizeSize.x, maxResizeSize.x); resizedPosition.height = Mathf.Clamp(resizedPosition.height, minResizeSize.y, maxResizeSize.y); UndoUtility.RecordEditedObject("Resize Graph Element"); position = resizedPosition; Reposition(); e.Use(); } else if (e.IsMouseUp(MouseButton.Left) && isResizing) { isResizingXMin = false; isResizingXMax = false; isResizingYMin = false; isResizingYMax = false; e.Use(); } }