public void Delete()
        {
            if (!canDelete)
            {
                return;
            }

            var deleted = false;

            var deleteGroup = new HashSet <IGraphElement>(new IGraphElement[] { element });

            canvas.Widget(element).ExpandDeleteGroup(deleteGroup);

            foreach (var elementToDelete in deleteGroup)
            {
                if (canvas.Widget(elementToDelete).canDelete)
                {
                    UndoUtility.RecordEditedObject("Delete Graph Element");
                    element.graph.elements.Remove(elementToDelete);
                    selection.Remove(elementToDelete);
                    deleted = true;
                }
            }

            if (deleted)
            {
                GUI.changed = true;
                e.Use();
            }
        }
Exemple #2
0
        public static void PasteInside(IGraphElement element)
        {
            Ensure.That(nameof(element)).IsNotNull(element);

            if (!CanPasteInside(element))
            {
                throw new InvalidOperationException();
            }

            UndoUtility.RecordEditedObject("Paste Graph Element");

            throw new NotImplementedException();

            //GUI.changed = true;
            //e?.TryUse();
        }
Exemple #3
0
        public static void DuplicateSelection()
        {
            object singleClipboardRestore = null;
            object groupClipboardRestore  = null;

            if (singleClipboard.containsData)
            {
                singleClipboardRestore = singleClipboard.Paste();
            }

            if (groupClipboard.containsData)
            {
                groupClipboardRestore = groupClipboard.Paste();
            }

            UndoUtility.RecordEditedObject("Duplicate Graph Element Selection");
            CopySelection();
            PasteOutside(false);

            if (singleClipboardRestore != null)
            {
                singleClipboard.Copy(singleClipboardRestore);
            }
            else
            {
                singleClipboard.Clear();
            }

            if (groupClipboardRestore != null)
            {
                groupClipboard.Copy(groupClipboardRestore);
            }
            else
            {
                groupClipboard.Clear();
            }
        }
Exemple #4
0
 public void RecordUndo(string name)
 {
     UndoUtility.RecordEditedObject(name);
 }
Exemple #5
0
 public static void CutSelection()
 {
     UndoUtility.RecordEditedObject("Cut Graph Element Selection");
     CopySelection();
     canvas.DeleteSelection();
 }
Exemple #6
0
        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();
            }
        }