예제 #1
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            UpdateOriginalLocation(e.Location);

            var element = FindElementAtOriginal(originalLocation);

            if ((e.Button & MouseButtons.Left) != 0)
            {
                leftMouseButton = false;

                if (_command == CommandMode.ScaleView)
                {
                    _command = CommandMode.Edit;
                    return;
                }

                if (_command == CommandMode.MarqueSelection)
                {
                    _command = CommandMode.Edit;
                    HandleSelection();
                    Refresh();
                    return;
                }

                if (_command == CommandMode.MoveSelection)
                {
                    _command = CommandMode.Edit;
                    return;
                }

                if (_command == CommandMode.Wiring && _tempWire != null)
                {
                    if (_tempWire.From != null && element is SocketIn @socketIn)
                    {
                        Connect(_tempWire.From, @socketIn);
                    }

                    if (_tempWire.To != null && element is SocketOut @socketOut)
                    {
                        Connect(@socketOut, _tempWire.To);
                    }

                    _tempWire = null;
                    _command  = CommandMode.Edit;
                    Refresh();
                    return;
                }
            }

            if ((e.Button & MouseButtons.Right) != 0)
            {
                rightMouseButton = false;
                if (_command == CommandMode.ScaleView)
                {
                    _command = CommandMode.Edit;
                    return;
                }
            }

            if ((e.Button & MouseButtons.Middle) != 0)
            {
                if (_command == CommandMode.TranslateView)
                {
                    _command = CommandMode.Edit;
                    return;
                }
            }

            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:
                    this.Invalidate();
                    return;

                case CommandMode.ScaleView:
                    return;

                case CommandMode.TranslateView:
                    return;

                default:
                case CommandMode.Edit:
                    break;
                }
            } finally {
                dragging = false;
                _command = CommandMode.Edit;

                base.OnMouseUp(e);
            }
        }
예제 #2
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Focus();

            UpdateOriginalLocation(e.Location);

            var element = FindElementAtOriginal(originalLocation);

            if ((e.Button & MouseButtons.Left) != 0)
            {
                leftMouseButton = true;

                if (_command == CommandMode.Edit)
                {
                    if (element == null && Control.ModifierKeys != Keys.Shift)
                    {
                        SetNodeSelected(false);
                        Refresh();
                    }

                    if (element is AbstractNode abstractNode)
                    {
                        if (Control.ModifierKeys != Keys.Shift && !abstractNode.Selected)
                        {
                            SetNodeSelected(false);
                        }

                        if (Control.ModifierKeys == Keys.Shift)
                        {
                            abstractNode.Selected = !abstractNode.Selected;
                        }
                        else
                        {
                            abstractNode.Selected = true;
                        }

                        HandleSelection();
                        Refresh();
                    }
                }

                if (leftMouseButton && rightMouseButton)
                {
                    _command = CommandMode.ScaleView;
                    return;
                }

                if (element == null)
                {
                    _command = CommandMode.MarqueSelection;
                    return;
                }

                if (element is Wire wire)
                {
                    _command = CommandMode.Wiring;
                    var from         = wire.From.Pivot;
                    var to           = wire.To.Pivot;
                    var fromDistance = Utils.Distance(from, originalLocation);
                    var toDistance   = Utils.Distance(to, originalLocation);
                    if (fromDistance < toDistance)
                    {
                        _tempWire = new Wire {
                            From = null, To = wire.To
                        };
                        Disconnect(wire);
                    }
                    else
                    {
                        _tempWire = new Wire {
                            From = wire.From, To = null
                        };
                        Disconnect(wire);
                    }
                }

                if (element is SocketIn socketIn)
                {
                    _command = CommandMode.Wiring;

                    if (socketIn.Hub || !socketIn.IsConnected())
                    {
                        _tempWire = new Wire {
                            From = null, To = socketIn
                        };
                    }
                    else
                    {
                        Wire connection = socketIn.GetAllConnections()[0];

                        _tempWire = new Wire {
                            From = connection.From, To = null
                        };
                        Disconnect(connection);
                    }

                    return;
                }

                if (element is SocketOut socketOut)
                {
                    _command  = CommandMode.Wiring;
                    _tempWire = new Wire {
                        From = socketOut
                    };
                    return;
                }

                if (element is AbstractNode node)
                {
                    if (node.GetBoundsArea(originalLocation) == NodeBoundsArea.HEADER)
                    {
                        _command = CommandMode.MoveSelection;
                        BringNodeToFront(node);
                    }
                    else
                    {
                        // rest of node
                    }
                }
            }

            if ((e.Button & MouseButtons.Right) != 0)
            {
                rightMouseButton = true;

                if (leftMouseButton && rightMouseButton)
                {
                    _command = CommandMode.ScaleView;
                    return;
                }

                if (_command == CommandMode.Edit && FindElementAtMousePoint(e.Location) == null)
                {
                    rightMouseButton = false;
                    OpenContextMenu(e.Location);
                    return;
                }
            }

            if (e.Button == MouseButtons.Middle)
            {
                _command = CommandMode.TranslateView;
            }

            var points = new[] { originalLocation };

            transformation.TransformPoints(points);
            originalMouseLocation = this.PointToScreen(new Point((int)points[0].X, (int)points[0].Y));
        }