private void ConnectableControl_MouseMove(object sender, MouseEventArgs e) { if (this.Capture == true && e.Button == MouseButtons.Right) { PetriNetEditor pne = (PetriNetEditor)this.Parent; Panel pnl = (Panel)pne.Parent; Point ptBegin = this.PointToScreen(ptMouseDragOffset); ptBegin = pne.PointToClient(ptBegin); pne.ptDragBegin = ptBegin; Point ptEnd = this.PointToScreen(new Point(e.X, e.Y)); ptEnd = pne.PointToClient(ptEnd); pne.ptDragEnd = ptEnd; Control cBegin = this.GetChildAtPoint(ptBegin); Control cEnd = this.GetChildAtPoint(ptEnd); Point ptNewScrollPosition = new Point(0, 0); if (ptEnd.X < -pnl.AutoScrollPosition.X || ptEnd.Y < -pnl.AutoScrollPosition.Y) { ptNewScrollPosition = new Point(Math.Min(ptEnd.X, -pnl.AutoScrollPosition.X), Math.Min(ptEnd.Y, -pnl.AutoScrollPosition.Y)); pnl.AutoScrollPosition = ptNewScrollPosition; } else if (ptEnd.X > (pnl.Width - 25 - pnl.AutoScrollPosition.X) || ptEnd.Y > (pnl.Height + 15 - pnl.AutoScrollPosition.Y)) { ptNewScrollPosition = new Point(Math.Max(ptEnd.X - pnl.Width + 25, -pnl.AutoScrollPosition.X), Math.Min(ptEnd.Y - pnl.Height + 15, -pnl.AutoScrollPosition.Y)); pnl.AutoScrollPosition = ptNewScrollPosition; } else if (ptEnd.X > (pnl.Width - 25 - pnl.AutoScrollPosition.X) || ptEnd.Y > (pnl.Height - 15 - pnl.AutoScrollPosition.Y)) { ptNewScrollPosition = new Point(Math.Max(ptEnd.X - pnl.Width + 25, -pnl.AutoScrollPosition.X), Math.Max(ptEnd.Y - pnl.Height + 15, -pnl.AutoScrollPosition.Y)); pnl.AutoScrollPosition = ptNewScrollPosition; } pne.Refresh(); } }
private void ConnectableControl_MouseUp(object sender, MouseEventArgs e) { this.Cursor = Cursors.SizeAll; PetriNetEditor pne = (PetriNetEditor)this.Parent; pne.Invalidate(); if (pne.bConnecting == true) { pne.bConnecting = false; Point ptEnd = this.PointToScreen(new Point(e.X, e.Y)); ptEnd = pne.PointToClient(ptEnd); Control cEnd = pne.GetChildAtPoint(ptEnd); if (cEnd is ConnectableControl && cEnd != this) { ConnectableControl cc = (ConnectableControl)cEnd; if (this.CanConnectTo(cc)) { if (this.alChilds.Contains(cc) != true) { // Set ptEnd to client coordinates of cc ptEnd = pne.PointToScreen(ptEnd); ptEnd = cc.PointToClient(ptEnd); if (cc.CanConnectToThisPoint(ptEnd)) { if (this is IConnector && cc is IConnector) { this.alChilds.Add(cc); cc.alParents.Add(this); #region Determine Output port // Determine Output port Port pOutput = null; foreach (Port p in this.alOutputPorts) { GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(ptMouseDragOffset.X - 1, ptMouseDragOffset.Y - 1, 2, 2); Region r = new Region(gp); r.Intersect(p.PortRegion); RectangleF rf = r.GetBounds(this.CreateGraphics()); if (rf.Height != 0 || rf.Width != 0) { pOutput = p; break; } } #endregion #region Determine Input port // Determine Input port Port pInput = null; foreach (Port p in cc.alInputPorts) { GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(ptEnd.X - 1, ptEnd.Y - 1, 2, 2); Region r = new Region(gp); r.Intersect(p.PortRegion); RectangleF rf = r.GetBounds(this.CreateGraphics()); if (rf.Height != 0 || rf.Width != 0) { pInput = p; break; } } #endregion // Scale to Port Bounds RectangleF rfBegin = pOutput.PortRegion.GetBounds(this.CreateGraphics()); Point ptScaledBegin = new Point(ptMouseDragOffset.X - (int)rfBegin.X, ptMouseDragOffset.Y - (int)rfBegin.Y); RectangleF rfEnd = pInput.PortRegion.GetBounds(this.CreateGraphics()); Point ptScaledEnd = new Point(ptEnd.X - (int)rfEnd.X, ptEnd.Y - (int)rfEnd.Y); // Create new Connection Instance and add it Connection cn = pne.CreateNewConnection(this, cc, pOutput.PortNumber, pInput.PortNumber, ptScaledBegin, ptScaledEnd, 1); pne.Connections.Add(cn); // To notify parent PetriNetEditor that new connection has been created if (this.ConnectionCreated != null) { this.ConnectionCreated(cn, EventArgs.Empty); } // Raises PropertiesChanged event to notify Parent // that Childs property is changed if (this.PropertiesChanged != null) { this.PropertiesChanged(this, new EventArgs()); } } else { throw new InterfaceNotImplementedException("Not all conecting controls implement IConnector interface"); } } } else { if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete this connection?", "Petri .NET Simulator 1.0 - Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)) { // Remove this ConnectableControl from Parents collection of child cc int iParentIndex = cc.Parents.IndexOf(this); cc.Parents.Remove(this); // Remove cc from this places Childs collection this.alChilds.Remove(cc); // Remove connection between these two controls Connection cn = Connection.GetConnectionBetweenControls(this, cc); pne.Connections.Remove(cn); pne.Refresh(); if (this.PropertiesChanged != null) { this.PropertiesChanged(this, new EventArgs()); } } } } } } }