Ejemplo n.º 1
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (currentButtons != MouseButtons.None)
                return;

            currentButtons |= e.Button;
            selectedNodes.Clear();
            unselectedNodes.Clear();
            dragging	= true;
            abortDrag	= false;
            mouseMoved	= false;
            snappedLocation = lastLocation = e.Location;

            var points = new PointF[] { e.Location };
            inverse_transformation.TransformPoints(points);
            var transformed_location = points[0];

            originalLocation = transformed_location;

            if (e.Button == MouseButtons.Left)
            {
                var element = FindElementAt(transformed_location);
                if (element != null)
                {
                    var selection = FocusElement as NodeSelection;
                    var element_node = element as Node;
                    if (element_node != null)
                    {
                        switch (ModifierKeys)
                        {
                            case Keys.None:
                            {
                                if (selection != null &&
                                    selection.Nodes.Contains(element_node))
                                {
                                    element = selection;
                                }
                                break;
                            }
                            case Keys.Shift:
                            {
                                if (selection != null)
                                {
                                    if (!selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Add(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                        element = new NodeSelection(new Node[] { focus_node, element_node });
                                }
                                break;
                            }
                            case Keys.Control:
                            {
                                if (selection != null)
                                {
                                    if (selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Remove(element_node);
                                        element = new NodeSelection(nodes);
                                    } else
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Add(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                    {
                                        if (focus_node == element_node)
                                            element = null;
                                        else
                                            element = new NodeSelection(new Node[] { focus_node, element_node });
                                    }
                                }
                                break;
                            }
                            case Keys.Alt:
                            {
                                if (selection != null)
                                {
                                    if (selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Remove(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                        element = null;
                                }
                                break;
                            }
                        }
                    }

                    var item = element as NodeItem;
                    if (item != null)
                    {
                        if (!item.OnStartDrag(transformed_location, out originalLocation))
                        {
                            element = item.Node;
                            originalLocation = transformed_location;
                        }
                    } else
                    {
                        var connection = element as NodeConnection;
                        if (connection != null)
                            originalLocation = connection.To.Center;
                    }
                    FocusElement =
                        DragElement = element;
                    BringElementToFront(element);
                    this.Refresh();
                    command = CommandMode.Edit;
                } else
                    command = CommandMode.MarqueSelection;
            } else
            {
                DragElement = null;
                command = CommandMode.TranslateView;
            }

            points = new PointF[] { originalLocation };
            transformation.TransformPoints(points);
            originalMouseLocation = this.PointToScreen(new Point((int)points[0].X, (int)points[0].Y));
        }
Ejemplo n.º 2
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (currentButtons != MouseButtons.None)
                return;

            currentButtons |= e.Button;
            selectedNodes.Clear();
            unselectedNodes.Clear();
            dragging	= true;
            abortDrag	= false;
            mouseMoved	= false;
            SnappedLocation = LastLocation = e.Location;

            var points = new PointF[] { e.Location };
            inverse_transformation.TransformPoints(points);
            var transformed_location = points[0];

            OriginalLocation = transformed_location;

            if (e.Button == MouseButtons.Left)
            {
                var element = FindElementAt(transformed_location);
                if (element != null)
                {
                    var selection = FocusElement as NodeSelection;
                    var element_node = element as Node;
                    if (element_node != null)
                    {
                        switch (ModifierKeys)
                        {
                            case Keys.None:
                            {
                                if (selection != null &&
                                    selection.Nodes.Contains(element_node))
                                {
                                    element = selection;
                                }
                                break;
                            }
                            case Keys.Shift:
                            {
                                if (selection != null)
                                {
                                    if (!selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Add(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                        element = new NodeSelection(new Node[] { focus_node, element_node });
                                }
                                break;
                            }
                            case Keys.Control:
                            {
                                if (selection != null)
                                {
                                    if (selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Remove(element_node);
                                        element = new NodeSelection(nodes);
                                    } else
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Add(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                    {
                                        if (focus_node == element_node)
                                            element = null;
                                        else
                                            element = new NodeSelection(new Node[] { focus_node, element_node });
                                    }
                                }
                                break;
                            }
                            case Keys.Alt:
                            {
                                if (selection != null)
                                {
                                    if (selection.Nodes.Contains(element_node))
                                    {
                                        var nodes = selection.Nodes.ToList();
                                        nodes.Remove(element_node);
                                        element = new NodeSelection(nodes);
                                    }
                                } else
                                {
                                    var focus_node = FocusElement as Node;
                                    if (focus_node != null)
                                        element = null;
                                }
                                break;
                            }
                        }
                    }

                    var item = element as NodeItem;
                    if (item != null)
                    {
                        if (!item.OnStartDrag(transformed_location, out OriginalLocation))
                        {
                            element = item.Node;
                            OriginalLocation = transformed_location;
                        }
                    } else
                    {
                        var connection = element as NodeConnection;
                        if (connection != null)
                            OriginalLocation = connection.To.Center;
                    }

                    // Should compatible connectors be highlighted?
                    if (HighlightCompatible && null != CompatibilityStrategy)
                    {
                        var connectorFrom = element as NodeConnector;
                        if (connectorFrom == null)
                        {
                            var connection = element as NodeConnection;
                            if (connection != null)
                                connectorFrom = connection.From;
                        }
                        if (connectorFrom != null)
                        {
                            if (element.ElementType == ElementType.InputConnector)
                            {
                                // Iterate over all nodes
                                foreach (Node graphNode in graphNodes)
                                {
                                    // Check compatibility of node connectors
                                    foreach (NodeConnector connectorTo in graphNode.outputConnectors)
                                    {
                                        if (CompatibilityStrategy.CanConnect(connectorFrom, connectorTo))
                                        {
                                            SetFlag(connectorTo, RenderState.Compatible, true);
                                        } else
                                            SetFlag(connectorTo, RenderState.Incompatible, true);
                                    }
                                }
                            } else
                            {
                                // Iterate over all nodes
                                foreach (Node graphNode in graphNodes)
                                {
                                    // Check compatibility of node connectors
                                    foreach (NodeConnector connectorTo in graphNode.inputConnectors)
                                    {
                                        if (CompatibilityStrategy.CanConnect(connectorFrom, connectorTo))
                                        {
                                            SetFlag(connectorTo, RenderState.Compatible, true);
                                        } else
                                            SetFlag(connectorTo, RenderState.Incompatible, true);
                                    }
                                }
                            }
                        }
                    }

                    FocusElement =
                        DragElement = element;
                    BringElementToFront(element);
                    this.Refresh();
                    command = CommandMode.Edit;
                } else
                    command = CommandMode.MarqueSelection;
            } else
            {
                DragElement = null;
                command = CommandMode.TranslateView;
            }

            points = new PointF[] { OriginalLocation };
            transformation.TransformPoints(points);
            OriginalMouseLocation = this.PointToScreen(new Point((int)points[0].X, (int)points[0].Y));
        }
Ejemplo n.º 3
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            currentButtons &= ~e.Button;

            bool needRedraw = false;
            if (!dragging)
                return;
            try
            {
                Point currentLocation;
                PointF transformed_location;
                if (abortDrag)
                {
                    transformed_location = originalLocation;

                    var points = new PointF[] { originalLocation };
                    transformation.TransformPoints(points);
                    currentLocation = new Point((int)points[0].X, (int)points[0].Y);
                } else
                {
                    currentLocation = e.Location;

                    var points = new PointF[] { currentLocation };
                    inverse_transformation.TransformPoints(points);
                    transformed_location = points[0];
                }

                switch (command)
                {
                    case CommandMode.MarqueSelection:
                        if (abortDrag)
                        {
                            foreach (var node in selectedNodes)
                                SetFlag(node, RenderState.Focus, false, false);

                            foreach (var node in unselectedNodes)
                                SetFlag(node, RenderState.Focus, true, false);

                        } else
                        {
                            NodeSelection selection = null;
                            if (graphNodes.Count > 0)
                            {
                                // select all focused nodes
                                var result = (from node in graphNodes
                                              where (node.state & RenderState.Focus) == RenderState.Focus
                                              select node).ToList();
                                if (result.Count > 0)
                                    selection = new NodeSelection(result);
                            }
                            FocusElement = selection;
                        }
                        this.Invalidate();
                        return;
                    case CommandMode.ScaleView:
                        return;
                    case CommandMode.TranslateView:
                        return;

                    default:
                    case CommandMode.Edit:
                        break;
                }
                if (DragElement != null)
                {
                    switch (DragElement.ElementType)
                    {
                        case ElementType.InputConnector:
                        {
                            var inputConnector	= (NodeConnector)DragElement;
                            var outputConnector = HoverElement as NodeOutputConnector;
                            if (outputConnector != null &&
                                outputConnector.Node != inputConnector.Node)
                                FocusElement = Connect(outputConnector, inputConnector);
                            needRedraw = true;
                            return;
                        }
                        case ElementType.OutputConnector:
                        {
                            var outputConnector = (NodeConnector)DragElement;
                            var inputConnector	= HoverElement as NodeInputConnector;
                            if (inputConnector != null &&
                                inputConnector.Node != outputConnector.Node)
                                FocusElement = Connect(outputConnector, inputConnector);
                            needRedraw = true;
                            return;
                        }
                        default:
                        case ElementType.NodeSelection:
                        case ElementType.Connection:
                        case ElementType.NodeItem:
                        case ElementType.Node:
                        {
                            needRedraw = true;
                            return;
                        }
                    }
                }
                if (DragElement != null ||
                    FocusElement != null)
                {
                    FocusElement = null;
                    needRedraw = true;
                }
            }
            finally
            {
                if (DragElement != null)
                {
                    var nodeItem = DragElement as NodeItem;
                    if (nodeItem != null)
                        nodeItem.OnEndDrag();
                    DragElement = null;
                    needRedraw = true;
                }

                dragging = false;
                command = CommandMode.Edit;
                selectedNodes.Clear();
                unselectedNodes.Clear();

                if (needRedraw)
                    this.Refresh();

                base.OnMouseUp(e);
            }
        }
Ejemplo n.º 4
0
        public void CopySelectedNodesToClipboard()
        {
            if (dockPanel.ActiveDocument is GraphLayoutForm)
            {
                GraphLayoutForm activeLayout = dockPanel.ActiveDocument as GraphLayoutForm;

                NodeSelection selection = null;

                if (activeLayout.Desktop.FocusElement is NodeSelection)
                {
                    selection = activeLayout.Desktop.FocusElement as NodeSelection;
                }
                else if (activeLayout.Desktop.FocusElement is Node)
                {
                    selection = new NodeSelection(new Node[] { activeLayout.Desktop.FocusElement as Node });
                }

                if (selection != null)
                {
                    HashSet<int> approvedNodes = new HashSet<int>();

                    var clipboardNetwork = Project.CreateNode<MyNetwork>();
                    clipboardNetwork.Name = "Clipboard";

                    foreach (MyNodeView nodeView in selection.Nodes)
                    {
                        MyNode selectedNode = nodeView.Node;

                        if (selectedNode is MyWorkingNode)
                        {
                            clipboardNetwork.Children.Add(nodeView.Node);
                            approvedNodes.Add(selectedNode.Id);
                        }

                        if (selectedNode is MyNodeGroup)
                        {
                            (selectedNode as MyNodeGroup).Iterate(true, true, node => approvedNodes.Add(node.Id));
                        }
                    }

                    if (approvedNodes.Count > 0)
                    {
                        clipboardNetwork.PrepareConnections();
                        clipboardNetwork.FilterPreparedCollection(approvedNodes);

                        YAXSerializer networkSerializer = new YAXSerializer(typeof(MyNetwork), YAXExceptionHandlingPolicies.ThrowErrorsOnly, YAXExceptionTypes.Warning, YAXSerializationOptions.DontSerializeNullObjects);
                        string xml = networkSerializer.Serialize(clipboardNetwork);

                        Clipboard.SetText(xml);
                    }
                    else
                    {
                        MyLog.WARNING.WriteLine("Copying is not allowed");
                    }
                }
                else
                {
                    MyLog.WARNING.WriteLine("Selection is empty");
                }
            }
        }