예제 #1
0
        private void ReplaceUnit()
        {
            var oldUnit      = unit;
            var unitPosition = oldUnit.position;
            var preservation = UnitPreservation.Preserve(oldUnit);

            var options = new UnitOptionTree(new GUIContent("Node"));

            options.reference = reference;

            var activatorPosition = new Rect(e.mousePosition, new Vector2(200, 1));

            var context = this.context;

            LudiqGUI.FuzzyDropdown
            (
                activatorPosition,
                options,
                null,
                delegate(object _option)
            {
                var option = (IUnitOption)_option;

                context.BeginEdit();
                UndoUtility.RecordEditedObject("Replace Node");
                var graph = oldUnit.graph;
                oldUnit.graph.units.Remove(oldUnit);
                var newUnit      = option.InstantiateUnit();
                newUnit.guid     = Guid.NewGuid();
                newUnit.position = unitPosition;
                graph.units.Add(newUnit);
                preservation.RestoreTo(newUnit);
                option.PreconfigureUnit(newUnit);
                selection.Select(newUnit);
                GUI.changed = true;
                context.EndEdit();
            }
            );
        }
예제 #2
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();
            }
        }
예제 #3
0
 public void RecordUndo(string name)
 {
     UndoUtility.RecordEditedObject(name);
 }
        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();
            }
        }
예제 #5
0
        protected void ToggleStart()
        {
            UndoUtility.RecordEditedObject("Toggle State Start");

            state.isStart = !state.isStart;
        }
예제 #6
0
 public static void CutSelection()
 {
     UndoUtility.RecordEditedObject("Cut Graph Element Selection");
     CopySelection();
     canvas.DeleteSelection();
 }
예제 #7
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();
        }