private void MoleculeViewer_MouseDown(object sender, MouseEventArgs e) { if (Cursor == System.Windows.Forms.Cursors.Cross) { return; } System.Drawing.Point mousePT = gscTogoc(e.X, e.Y); Invalidate(); if (Cursor == System.Windows.Forms.Cursors.Arrow) { SelectedObject = m_DrawingObjects.FindObjectAtPoint(mousePT); if (SelectedObject != null) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { m_RotatingSelectedObject = true; startingRotation = AngleToPoint(SelectedObject.GetPosition(), mousePT); originalRotation = SelectedObject.Rotation; } else { m_DraggingSelectedObject = true; dragOffset.X = SelectedObject.X - mousePT.X; dragOffset.Y = SelectedObject.Y - mousePT.Y; } } else { if (e.Button == System.Windows.Forms.MouseButtons.Left) { m_SelectionDragging = true; m_SelectionRectangle.X = e.X; m_SelectionRectangle.Y = e.Y; m_SelectionRectangle.Height = 0; m_SelectionRectangle.Width = 0; } if (e.Button == System.Windows.Forms.MouseButtons.Right) { m_RotatingSurface = true; dragOffset.X = mousePT.X; dragOffset.Y = mousePT.Y; } } } else if (Cursor == System.Windows.Forms.Cursors.SizeNWSE || Cursor == System.Windows.Forms.Cursors.SizeNESW || Cursor == System.Windows.Forms.Cursors.SizeWE || Cursor == System.Windows.Forms.Cursors.SizeNS) { dragOffset.X = SelectedObject.X - mousePT.X; dragOffset.Y = SelectedObject.Y - mousePT.Y; resizing = true; } }
private void MoleculeViewer_MouseMove(object sender, MouseEventArgs e) { System.Drawing.Point dragPoint = gscTogoc(e.X, e.Y); System.Drawing.Point MousePoint = gscTogoc(e.X, e.Y); dragPoint.Offset(dragOffset.X, dragOffset.Y); System.Drawing.Size minSize = new System.Drawing.Size(16, 16); if (SelectedObject != null) { if (m_DraggingSelectedObject) { Rectangle rect = new System.Drawing.Rectangle(SelectedObject.GetPosition().X, SelectedObject.GetPosition().Y, SelectedObject.Width, SelectedObject.Height); SelectedObject.SetPosition(dragPoint); if (StatusUpdate != null) { StatusUpdate(this, new StatusUpdateEventArgs(StatusUpdateType.ObjectMoved, SelectedObject, String.Format("Object Moved to {0}, {1}", dragPoint.X, dragPoint.Y), dragPoint, 0)); } Invalidate(); } else if (m_RotatingSelectedObject) { float currentRotation; currentRotation = (float)AngleToPoint(SelectedObject.GetPosition(), dragPoint); currentRotation = (float)((int)(currentRotation - startingRotation + originalRotation) % 360); SelectedObject.Rotation = currentRotation; if (StatusUpdate != null) { StatusUpdate(this, new StatusUpdateEventArgs(StatusUpdateType.ObjectRotated, SelectedObject, String.Format("Object Rotated to {0} degrees", currentRotation), new System.Drawing.Point(0, 0), currentRotation)); } Invalidate(); } else if (resizing) { //combine code because you will need to move ports in both cases System.Drawing.Rectangle rect = new System.Drawing.Rectangle(SelectedObject.X, SelectedObject.Y, SelectedObject.Width, SelectedObject.Height); System.Drawing.Size sz = new System.Drawing.Size(0, 0); System.Drawing.Point fixedPT = new System.Drawing.Point(0, 0); System.Drawing.Point dragPT = new System.Drawing.Point(0, 0); SelectedObject.AutoSize = false; switch (sz_direct) { case SizeDirection.Northwest: //changing all { System.Drawing.Point ULHC = SelectedObject.GetPosition(); System.Drawing.Point LRHC = new System.Drawing.Point(0, 0); sz = SelectedObject.GetSize(); LRHC.X = ULHC.X + sz.Width; LRHC.Y = ULHC.Y + sz.Height; sz.Width = LRHC.X - MousePoint.X; sz.Height = LRHC.Y - MousePoint.Y; if (sz.Width < 16) { sz.Width = 16; } if (sz.Height < 16) { sz.Height = 16; } ULHC.X = LRHC.X - sz.Width; ULHC.Y = LRHC.Y - sz.Height; SelectedObject.SetPosition(ULHC); SelectedObject.SetSize(sz); break; } case SizeDirection.North: //changing top, and height { System.Drawing.Point ULHC = SelectedObject.GetPosition(); System.Drawing.Point LLHC = new System.Drawing.Point(0, 0); LLHC.X = ULHC.X; sz = SelectedObject.GetSize(); LLHC.Y = ULHC.Y + sz.Height; sz.Height = LLHC.Y - MousePoint.Y; if (sz.Height < 16) { sz.Height = 16; } ULHC.Y = LLHC.Y - sz.Height; SelectedObject.SetPosition(ULHC); SelectedObject.SetSize(sz); break; } case SizeDirection.Northeast: //changing top, width, and height { System.Drawing.Point ULHC = SelectedObject.GetPosition(); System.Drawing.Point LLHC = new System.Drawing.Point(0, 0); sz = SelectedObject.GetSize(); LLHC.X = ULHC.X; LLHC.Y = ULHC.Y + sz.Height; sz.Width = MousePoint.X - LLHC.X; sz.Height = LLHC.Y - MousePoint.Y; if (sz.Width < 16) { sz.Width = 16; } if (sz.Height < 16) { sz.Height = 16; } ULHC.X = LLHC.X; ULHC.Y = LLHC.Y - sz.Height; SelectedObject.SetPosition(ULHC); SelectedObject.SetSize(sz); break; } case SizeDirection.East: //changing width { System.Drawing.Point ULHC = SelectedObject.GetPosition(); sz = SelectedObject.GetSize(); sz.Width = MousePoint.X - ULHC.X; if (sz.Width < 16) { sz.Width = 16; } SelectedObject.SetSize(sz); break; } case SizeDirection.Southeast: //changing height, width { System.Drawing.Point ULHC = SelectedObject.GetPosition(); sz = SelectedObject.GetSize(); sz.Width = MousePoint.X - ULHC.X; sz.Height = MousePoint.Y - ULHC.Y; if (sz.Width < 16) { sz.Width = 16; } if (sz.Height < 16) { sz.Height = 16; } SelectedObject.SetSize(sz); break; } case SizeDirection.South: //changing height { System.Drawing.Point ULHC = SelectedObject.GetPosition(); sz = SelectedObject.GetSize(); sz.Height = MousePoint.Y - ULHC.Y; if (sz.Height < 16) { sz.Height = 16; } SelectedObject.SetSize(sz); break; } case SizeDirection.Southwest: //changing left,height, and width { System.Drawing.Point ULHC = SelectedObject.GetPosition(); System.Drawing.Point URHC = new System.Drawing.Point(0, 0); sz = SelectedObject.GetSize(); URHC.Y = ULHC.Y; URHC.X = ULHC.X + sz.Width; sz.Width = URHC.X - MousePoint.X; sz.Height = MousePoint.Y - URHC.Y; if (sz.Width < 16) { sz.Width = 16; } if (sz.Height < 16) { sz.Height = 16; } ULHC.X = URHC.X - sz.Width; SelectedObject.SetPosition(ULHC); SelectedObject.SetSize(sz); break; } case SizeDirection.West: //changing left and width { System.Drawing.Point ULHC = SelectedObject.GetPosition(); System.Drawing.Point URHC = new System.Drawing.Point(0, 0); sz = SelectedObject.GetSize(); URHC.Y = ULHC.Y; URHC.X = ULHC.X + sz.Width; sz.Width = URHC.X - MousePoint.X; if (sz.Width < 16) { sz.Width = 16; } ULHC.X = URHC.X - sz.Width; SelectedObject.SetPosition(ULHC); SelectedObject.SetSize(sz); break; } default: break; } } } //else if (this.GetDrawingLine()) //{ // //this.Cursor = Cursors.Cross; // System.Drawing.Point points[] = this.GetDrawingLinePoints(); // points[points.Length - 1] = gscTogoc(e.X, e.Y); // this.SetDrawingLinePoints(points); // this.Invalidate(); //} else if (m_SelectionDragging) { m_SelectionRectangle.Width = e.X - m_SelectionRectangle.X; m_SelectionRectangle.Height = e.Y - m_SelectionRectangle.Y; } else if (m_RotatingSurface) { surfaceRotation = surfaceRotation + (float)AngleToPoint(dragOffset, dragPoint) / 100; //currentRotation = ((int)(currentRotation - startingRotation + originalRotation) % 360); if (StatusUpdate != null) { StatusUpdate(this, new StatusUpdateEventArgs(StatusUpdateType.ObjectRotated, SelectedObject, String.Format("Surface Rotated to {0} degrees", surfaceRotation), new System.Drawing.Point(0, 0), surfaceRotation)); } Invalidate(); } Invalidate(); }