コード例 #1
0
        // Finish drawing the graphic
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown  = false;
            startPoint   = Point.Empty;
            prevPoint    = Point.Empty;
            currentPoint = Point.Empty;

            if (mode == Mode.draw)
            {
                if (drawMode == DrawMode.polygon)
                {
                    // Check if a polygon is being drawn
                    // If it is, update the newest line
                    if (polygonInProgress)
                    {
                        try
                        {
                            graphic.Update(e.Location);
                            Polygon p = (Polygon)graphic;
                            polygonInProgress = p.AddLine(e.Location);
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show("An error has occured while attempting to draw a Polygon.\r\n" + exc.Message);
                        }
                    }
                }
            }

            else
            {
                if (isGraphicDragging)
                {
                    // Finish dragging the graphic
                    beginGraphicMove  = false;
                    isGraphicDragging = false;
                }
                else
                {
                    beginGraphicMove = false;

                    // If graphic is part of a group, get the group
                    List <GraphicObject> selectGroup = GetGroup(graphic);

                    if (graphic != null)
                    {
                        // Check if graphic is selected
                        if (graphic.IsGraphicSelected())
                        {
                            // Check if graphic is part of a group
                            // If part of group, deselect entire group

                            if (selectGroup.Count > 0)
                            {
                                foreach (GraphicObject g in selectGroup)
                                {
                                    // Deselect the graphic
                                    g.DeselectGraphic();

                                    // Remove from list of selected graphics
                                    selectedGraphics.Remove(g);
                                }
                            }
                            else
                            {
                                // Deselect the graphic
                                graphic.DeselectGraphic();

                                // Remove from list of selected graphics
                                selectedGraphics.Remove(graphic);
                            }

                            // Set the graphic reference to null (as nothing is selected)
                            graphic = null;
                        }
                        else
                        {
                            // Check if graphic is part of a group
                            // If part of group, select entire group

                            // Loop through group and select each graphic
                            if (selectGroup.Count > 0)
                            {
                                foreach (GraphicObject g in selectGroup)
                                {
                                    // Select the graphic and show this by changing the colour
                                    g.SelectGraphic(Color.Red);

                                    // Add the selected graphic to the list of selected graphics
                                    selectedGraphics.Add(g);
                                }
                            }
                            else
                            {
                                // Select the graphic and show this by changing the colour
                                graphic.SelectGraphic(Color.Red);

                                // Add the selected graphic to the list of selected graphics
                                selectedGraphics.Add(graphic);
                            }
                        }
                    }
                }
            }

            List <GraphicObject> copy = new List <GraphicObject>();
            GraphicObject        newG;

            // Loop through current graphics and create a copy
            foreach (GraphicObject g in graphicObjectList)
            {
                newG = g.Copy(id++);

                // Add the graphic to the list
                copy.Add(newG);
            }

            // Save the current state
            state.Push(currentState);
            currentState = copy;

            // Cause the picture box to re-paint (i.e. invoke Paint method)
            Redraw();
        }
コード例 #2
0
ファイル: Circle.cs プロジェクト: mmcken5/SimpleSketchPad
        public override bool Equals(object obj)
        {
            GraphicObject g = (GraphicObject)obj;

            return(id == g.GetId());
        }
コード例 #3
0
        // Begin drawing if in draw mode
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // Save the last point (assign it to the current mouse position)
            startPoint  = e.Location;
            isMouseDown = true;

            // Check to see if in draw mode
            if (mode == Mode.draw)
            {
                // Create a new GraphicsObject, depending on which draw mode is selected
                switch (drawMode)
                {
                case DrawMode.freehand:
                    graphic = new FreehandLine(colour, thickness, id++);
                    break;

                case DrawMode.line:
                    graphic = new Line(startPoint, colour, thickness, id++);
                    break;

                case DrawMode.rectangle:
                    graphic = new Rectangle(startPoint, colour, thickness, id++);
                    break;

                case DrawMode.ellipse:
                    graphic = new Ellipse(startPoint, colour, thickness, id++);
                    break;

                case DrawMode.square:
                    graphic = new Square(startPoint, colour, thickness, id++);
                    break;

                case DrawMode.circle:
                    graphic = new Circle(startPoint, colour, thickness, id++);
                    break;
                }

                if (drawMode == DrawMode.polygon)
                {
                    // Check if a polygon is being drawn
                    // If one is not, create one
                    if (!polygonInProgress)
                    {
                        polygonInProgress = true;

                        // Create a new polygon
                        graphic = new Polygon(e.Location, colour, thickness, id++);
                    }
                }

                // Add the graphic to the list of existing (already drawn) graphics
                graphicObjectList.Add(graphic);
            }

            // Must be in select mode
            else
            {
                // See if a graphic is under the mouse
                foreach (GraphicObject g in graphicObjectList)
                {
                    // Select the graphic (if there is more than one match only select the top layer graphic (i.e. the first match))
                    if (g.IsGraphicAtMousePoint(e.Location))
                    {
                        // Store a reference to the graphic
                        graphic = g;

                        // Set the point on the graphic where the mouse has clicked
                        g.SetMouseClickDragPoint(e.Location);

                        // If the graphic is part of a group, set the mouse point for all graphics in the group
                        List <GraphicObject> group = GetGroup(graphic);
                        if (group.Count > 0)
                        {
                            foreach (GraphicObject gO in group)
                            {
                                gO.SetMouseClickDragPoint(e.Location);
                            }
                        }

                        beginGraphicMove = true;

                        break;
                    }
                }
            }
            // Redraw
            Redraw();
        }