public Editor2D(PictureBox editorWindow) { EditorWindow = editorWindow; StatusBar = new StatusBar(); GeometryEngine = new GeometryEngine(); //default Zoom vales ZoomRatio = .1f; Zoom = 1; InitialWindowWidth = EditorWindow.Width; initialWindowHeight = EditorWindow.Height; //grid Grid = new Grid(editorWindow.Width, editorWindow.Height); Grid.Generate(); // wiring the mouse events EditorWindow.MouseDown += EditorWindow_MouseDown; EditorWindow.MouseMove += EditorWindow_MouseMove; EditorWindow.MouseUp += EditorWindow_MouseUp; EditorWindow.Paint += EditorWindow_Paint; EditorWindow.KeyPress += EditorWindow_KeyPress; EditorWindow.MouseDoubleClick += EditorWindow_MouseDoubleClick; EditorWindow.MouseWheel += EditorWindow_MouseWheel; EditorWindow.SizeChanged += EditorWindow_SizeChanged; }
private void EditorWindow_MouseMove(object sender, MouseEventArgs e) { StatusBar.Message($"X: {e.X} , Y: {e.Y}"); if (ActiveCommand != DrawCommands.Noun && clickCount >= 1) { //delta_Y = p2.Sub(e.Location); var offsetPoint = new PointF(offsetX, offsetY).Scale(Zoom); p2 = (new PointF(e.X, e.Y).Sub(offsetPoint)).Scale(1 / Zoom); switch (ActiveCommand) { case DrawCommands.Line: #region Line tempShape = new GLine(p1, p2); #endregion break; case DrawCommands.Circle: #region Circle tempShape = new GCircle(p1, p1.Distance(p2)); Debug.WriteLine($"x:{p1.X},Y:{p1.Y},R:{p1.Distance(p2)}"); Debug.WriteLine($"offsetX:{offsetX},OffsetY:{ offsetY}"); #endregion break; case DrawCommands.Rectangle: #region Rectangle tempShape = new GRectangle(p1, p2); #endregion break; case DrawCommands.PolyLine: #region PolyLine tempShape = tempPolyLine; if (clickCount == 1) { if (tempPolyLine == null) { tempPolyLine = new GPolyLine(); tempPolyLine.Lines.Add(new GLine(p1, p2)); } else { tempPolyLine.Lines.LastOrDefault().EndPoint = p2; } } else if (clickCount == 2) { //check end line position to exit or continue drawing. var prevLine = tempPolyLine.Lines.LastOrDefault(); tempPolyLine.Lines.Add(new GLine(prevLine.EndPoint, p2)); if (p2.Distance(tempPolyLine.Lines.FirstOrDefault().StartPoint) < Setup.Snap) { GeometryEngine.AddShape(tempShape); clickCount = 0; tempShape = null; tempPolyLine = null; // ActiveCommand = DrawCommands.Noun; } else { clickCount--; } } #endregion break; case DrawCommands.Curve: #region Curve if (clickCount == 1) { tempShape = new GLine(p1, p2); } else if (clickCount == 2) { if (tempShape is GCurve) { ((GCurve)tempShape).Center = p2; } else { var l = (GLine)tempShape; tempShape = new GCurve(l.StartPoint, p2, l.EndPoint); } } else if (clickCount == 3) { ((GCurve)tempShape).Center = p2; GeometryEngine.AddShape(tempShape); clickCount = 0; } #endregion break; case DrawCommands.Parabola: #region Parabola if (clickCount == 1) { tempShape = new GLine(p1, p2); } else if (clickCount == 2) { if (tempShape is GParabola) { ((GParabola)tempShape).Points[2] = p2; } else { var l = (GLine)tempShape; tempShape = new GParabola(l.StartPoint, l.EndPoint, p2); } } else if (clickCount == 3) { ((GParabola)tempShape).Points[2] = p2; GeometryEngine.AddShape(tempShape); clickCount = 0; } #endregion break; case DrawCommands.Clear: break; case DrawCommands.Noun: break; default: break; } EditorWindow.Invalidate(); } else if (clickCount == 0) { MouseEventArgs mouse = e as MouseEventArgs; if (mouse.Button == MouseButtons.Left) { Point mousePosNow = mouse.Location; int deltaX = mousePosNow.X - mouseDown.X; // the distance the mouse has been moved since mouse was pressed int deltaY = mousePosNow.Y - mouseDown.Y; offsetX = (int)(startx + (deltaX / Zoom)); // calculate new offset of image based on the current zoom factor offsetY = (int)(starty + (deltaY / Zoom)); EditorWindow.Invalidate(); } } }