private void RestoreControlsAndConnections() { Hashtable ht = new Hashtable(); // Add all objects and connections foreach (object o in this.alDeserializedObjects) { this.pneEditor.AddDeserializedControl((SelectableAndMovableControl)o); if (o is ConnectableControl) { ConnectableControl cc = (ConnectableControl)o; ht.Add(cc.GetType().FullName + cc.Index.ToString(), cc); } } foreach (object o in this.alDeserializedConnections) { Connection cn = (Connection)o; cn.RestoreLinks(ht); this.pneEditor.AddDeserializedConnection((Connection)o); } this.ssOwner.OnEditingFinished(); this.pneEditor_ContentsChanged(this, EventArgs.Empty); }
protected void EditorSurface_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; if (PetriNetDocument.AntiAlias == true) { g.SmoothingMode = SmoothingMode.AntiAlias; } #region if (bConnecting == true) // Draw currently dragged line if (bConnecting == true) { Pen pBlack = new Pen(Color.Black, this.fZoomLevel * 3); pBlack.CustomEndCap = new AdjustableArrowCap(3, 6, true); Pen pYellow = new Pen(Color.Yellow, this.fZoomLevel * 3); pYellow.CustomEndCap = new AdjustableArrowCap(3, 6, true); Pen pRed = new Pen(Color.Red, this.fZoomLevel * 3); pRed.CustomEndCap = new AdjustableArrowCap(3, 6, true); // Draw line as user moves the mouse pointer Control cBegin = this.GetChildAtPoint(this.ptDragBegin); Control cEnd = this.GetChildAtPoint(this.ptDragEnd); if (cBegin is ConnectableControl) { if (cEnd is ConnectableControl) { Point ptEndPoint = this.PointToScreen(this.ptDragEnd); ptEndPoint = cEnd.PointToClient(ptEndPoint); ConnectableControl ccBegin = (ConnectableControl)cBegin; ConnectableControl ccEnd = (ConnectableControl)cEnd; if (ccBegin.Childs.Contains(cEnd)) { g.DrawLine(pRed, this.ptDragBegin, this.ptDragEnd); } else { if (ccBegin.CanConnectTo(cEnd) && ccEnd.CanConnectToThisPoint(ptEndPoint)) { g.DrawLine(pYellow, this.ptDragBegin, this.ptDragEnd); } else { g.DrawLine(pBlack, this.ptDragBegin, this.ptDragEnd); } } } else { g.DrawLine(pBlack, this.ptDragBegin, this.ptDragEnd); } } } #endregion #region if (bSelecting == true) // Draw selecting rectangle if (bSelecting == true) { Brush b = new SolidBrush(Color.FromArgb(70, Color.FromArgb(49, 106, 197))); Pen p = new Pen(Color.FromArgb(120, SystemColors.HotTrack), 1f); // I quadrant if (this.meaLastMouseDown.X <= this.ptDragEnd.X && this.meaLastMouseDown.Y > this.ptDragEnd.Y) { g.FillRectangle(b, this.meaLastMouseDown.X, this.ptDragEnd.Y, this.ptDragEnd.X - this.meaLastMouseDown.X, this.meaLastMouseDown.Y - this.ptDragEnd.Y); g.DrawRectangle(p, this.meaLastMouseDown.X, this.ptDragEnd.Y, this.ptDragEnd.X - this.meaLastMouseDown.X, this.meaLastMouseDown.Y - this.ptDragEnd.Y); } // II quadrant else if (this.meaLastMouseDown.X > this.ptDragEnd.X && this.meaLastMouseDown.Y > this.ptDragEnd.Y) { g.FillRectangle(b, this.ptDragEnd.X, this.ptDragEnd.Y, this.meaLastMouseDown.X - this.ptDragEnd.X, this.meaLastMouseDown.Y - this.ptDragEnd.Y); g.DrawRectangle(p, this.ptDragEnd.X, this.ptDragEnd.Y, this.meaLastMouseDown.X - this.ptDragEnd.X, this.meaLastMouseDown.Y - this.ptDragEnd.Y); } // III quadrant else if (this.meaLastMouseDown.X > this.ptDragEnd.X && this.meaLastMouseDown.Y <= this.ptDragEnd.Y) { g.FillRectangle(b, this.ptDragEnd.X, this.meaLastMouseDown.Y, this.meaLastMouseDown.X - this.ptDragEnd.X, this.ptDragEnd.Y - this.meaLastMouseDown.Y); g.DrawRectangle(p, this.ptDragEnd.X, this.meaLastMouseDown.Y, this.meaLastMouseDown.X - this.ptDragEnd.X, this.ptDragEnd.Y - this.meaLastMouseDown.Y); } // IV quadrant else if (this.meaLastMouseDown.X <= this.ptDragEnd.X && this.meaLastMouseDown.Y <= this.ptDragEnd.Y) { g.FillRectangle(b, this.meaLastMouseDown.X, this.meaLastMouseDown.Y, this.ptDragEnd.X - this.meaLastMouseDown.X, this.ptDragEnd.Y - this.meaLastMouseDown.Y); g.DrawRectangle(p, this.meaLastMouseDown.X, this.meaLastMouseDown.Y, this.ptDragEnd.X - this.meaLastMouseDown.X, this.ptDragEnd.Y - this.meaLastMouseDown.Y); } } #endregion }
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()); } } } } } } }
public void OnEditingFinished() { this.OnResize(EventArgs.Empty); if (this.Parent is PetriNetEditor) { PetriNetEditor pne = (PetriNetEditor)this.Parent; //Check for valid connections for (int i = this.Parents.Count - 1; i >= 0; i--) { ConnectableControl cc = (ConnectableControl)this.Parents[i]; Connection cn = Connection.GetConnectionBetweenControls(cc, this); //Find input port with same number as connection output port if (!this.InputPortNumberExists(cn.ToPort)) { ConnectableControl ccFrom = cn.From; ConnectableControl ccTo = cn.To; // Dispose connection ccFrom.Childs.Remove(ccTo); ccTo.Parents.Remove(ccFrom); pne.Connections.Remove(cn); cn.Dispose(); } } for (int i = this.Childs.Count - 1; i >= 0; i--) { ConnectableControl cc = (ConnectableControl)this.Childs[i]; Connection cn = Connection.GetConnectionBetweenControls(this, cc); //Find input port with same number as connection output port if (!this.OutputPortNumberExists(cn.FromPort)) { ConnectableControl ccFrom = cn.From; ConnectableControl ccTo = cn.To; // Dispose connection ccFrom.Childs.Remove(ccTo); ccTo.Parents.Remove(ccFrom); pne.Connections.Remove(cn); cn.Dispose(); } } // Adjust Virtual connections weights foreach (Connection cn in pne.Connections) { if (cn.From == this) { cn.Weight = cn.GetOutputWeightsSum(this); } else if (cn.To == this) { cn.Weight = cn.GetInputWeightsSum(this); } } pne.OnContentsChanged(); } }
public object Clone(Subsystem s) { if (this.pnd != null) { SubsystemEditor se = new SubsystemEditor(this.pnd, s); Hashtable ht = new Hashtable(); for (int i = 0; i < this.pneEditor.Objects.Count; i++) { if (this.pneEditor.Objects[i] is ConnectableControl && this.pneEditor.Objects[i] is ICloneable) { ConnectableControl cc = (ConnectableControl)((ICloneable)this.pneEditor.Objects[i]).Clone(); ht.Add(this.pneEditor.Objects[i], cc); if (!(cc is Input) && !(cc is Output)) { cc.Index = this.pnd.InstanceCounter.GetAndIncreaseInstanceCount(cc); } else { cc.Index = ((ConnectableControl)this.pneEditor.Objects[i]).Index; } se.alDeserializedObjects.Add(cc); } else if (this.pneEditor.Objects[i] is DescriptionLabel && this.pneEditor.Objects[i] is ICloneable) { DescriptionLabel dl = (DescriptionLabel)((ICloneable)this.pneEditor.Objects[i]).Clone(); ht.Add(this.pneEditor.Objects[i], dl); dl.Index = ((DescriptionLabel)this.pneEditor.Objects[i]).Index; se.alDeserializedObjects.Add(dl); } } for (int i = 0; i < this.pneEditor.Connections.Count; i++) { Connection cn = (Connection)this.pneEditor.Connections[i]; cn = (Connection)cn.Clone((ConnectableControl)ht[cn.From], (ConnectableControl)ht[cn.To]); se.alDeserializedConnections.Add(cn); } foreach (object o in se.alDeserializedObjects) { if (o is Subsystem) { Subsystem ss = (Subsystem)o; ss.SuppresDeserializationCall(); } } se.RestoreControlsAndConnections(); foreach (object o in se.alDeserializedObjects) { if (o is Subsystem) { Subsystem ss = (Subsystem)o; ss.EnableDeserializationCall(); } } return(se); } return(null); }