Example #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;
                    }

                    // 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)
                                    {
                                        var connectionType = CompatibilityStrategy.CanConnect(connectorFrom, connectorTo);
                                        if (connectionType == HyperGraph.Compatibility.ConnectionType.Compatible)
                                        {
                                            SetFlag(connectorTo, RenderState.Compatible, true);
                                        }
                                        else if (connectionType == HyperGraph.Compatibility.ConnectionType.Conversion)
                                        {
                                            SetFlag(connectorTo, RenderState.Conversion, 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)
                                    {
                                        var connectionType = CompatibilityStrategy.CanConnect(connectorFrom, connectorTo);
                                        if (connectionType == HyperGraph.Compatibility.ConnectionType.Compatible)
                                        {
                                            SetFlag(connectorTo, RenderState.Compatible, true);
                                        }
                                        else if (connectionType == HyperGraph.Compatibility.ConnectionType.Conversion)
                                        {
                                            SetFlag(connectorTo, RenderState.Conversion, 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));
        }
Example #2
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 &&
                                (inputConnector.state & (RenderState.Compatible|RenderState.Conversion)) != 0)
                                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 &&
                                (outputConnector.state & (RenderState.Compatible | RenderState.Conversion)) != 0)
                                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 (HighlightCompatible)
                {
                    // Remove all highlight flags
                    foreach (Node graphNode in graphNodes)
                    {
                        foreach (NodeConnector inputConnector in graphNode.inputConnectors)
                            SetFlag(inputConnector, RenderState.Compatible | RenderState.Incompatible | RenderState.Conversion, false);

                        foreach (NodeConnector outputConnector in graphNode.outputConnectors)
                            SetFlag(outputConnector, RenderState.Compatible | RenderState.Incompatible | RenderState.Conversion, false);
                    }
                }

                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);
            }
        }