Example #1
0
        private void drawingSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DrawingLayer.GetInstance.IsChanged = true;
            bool     isLin        = false; // for line
            Point    pointClicked = e.GetPosition(drawingSurface);
            OxFigure visual       = null;

            if (SelectionLayer.GetInstance.CurrentTool != Tools.Erase &&
                SelectionLayer.GetInstance.CurrentTool != Tools.Hand && SelectionLayer.GetInstance.CurrentTool != Tools.Fill)
            {
                #region point

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Point)
                {
                    visual = new OxPoint {
                        Name = OxFigure.Shape.Point
                    };
                }

                #endregion

                #region square

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Square) //
                {
                    visual = new OxRectangle {
                        Name = OxFigure.Shape.Square
                    };
                }

                #endregion

                #region ellipse

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Ellipse)
                {
                    visual = new OxCircle {
                        Name = OxFigure.Shape.Ellispe
                    };
                }

                #endregion

                #region line

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Line)
                {
                    visual = new OxLine {
                        Name = OxFigure.Shape.Line
                    };
                    if (DrawingLayer.GetInstance.IsPoint)
                    {
                        DrawingLayer.GetInstance.Point2 = pointClicked;
                        isLin = true;
                    }
                    else
                    {
                        DrawingLayer.GetInstance.Point1 = pointClicked;
                    }
                    DrawingLayer.GetInstance.IsPoint = !DrawingLayer.GetInstance.IsPoint;
                }

                #endregion

                #region rect

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Rectangle)
                {
                    visual = new OxRectangle {
                        Name = OxFigure.Shape.Rectangle
                    };
                }

                #endregion

                if (visual == null)
                {
                    return;
                }

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Line && isLin)
                {
                    DrawingLayer.GetInstance.DrawFigure(ref visual, pointClicked, false);
                    drawingSurface.AddVisual(visual);
                    UndoRedoLayer.GetInstance.Add(new AddElementCommand(visual, this.drawingSurface));
                }
                else if (SelectionLayer.GetInstance.CurrentTool != Tools.Line)
                {
                    DrawingLayer.GetInstance.DrawFigure(ref visual, pointClicked, false);
                    drawingSurface.AddVisual(visual);
                    //здесь добавление
                    UndoRedoLayer.GetInstance.Add(new AddElementCommand(visual, this.drawingSurface));
                }
            }
            else
            {
                #region erase

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Erase)
                {
                    visual = drawingSurface.GetVisual(pointClicked);
                    if (visual != null)
                    {
                        UndoRedoLayer.GetInstance.Add(new RemoveElementCommand(visual, this.drawingSurface));
                        drawingSurface.DeleteVisual(visual);
                    }
                }

                #endregion

                #region hand

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Hand)
                {
                    Point topLeftCorner = new Point();
                    visual = drawingSurface.GetVisual(pointClicked);

                    if (visual != null)
                    {
                        if (visual.Name == OxFigure.Shape.Line)
                        {
                            DrawingLayer.GetInstance.IsClicked = true;
                            DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, true);

                            DrawingLayer.GetInstance.ClickOffset = topLeftCorner - pointClicked;
                            DrawingLayer.GetInstance.IsDragging  = true;
                            if (DrawingLayer.GetInstance.SelectedVisual != null && DrawingLayer.GetInstance.SelectedVisual != visual)
                            {
                                DrawingLayer.GetInstance.ClearSelection();
                            }
                            DrawingLayer.GetInstance.SelectedVisual = visual;
                            DrawingLayer.GetInstance.LastPoint      = topLeftCorner;
                            DrawingLayer.GetInstance.IsSel          = true;
                        }
                        else
                        {
                            DrawingLayer.GetInstance.IsClicked = true;
                            topLeftCorner.X = visual.ContentBounds.TopLeft.X + DrawingLayer.GetInstance.DrawingPen.Thickness / 2;
                            topLeftCorner.Y = visual.ContentBounds.TopLeft.Y + DrawingLayer.GetInstance.DrawingPen.Thickness / 2;
                            DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, true);
                            DrawingLayer.GetInstance.ClickOffset = topLeftCorner - pointClicked;
                            DrawingLayer.GetInstance.IsDragging  = true;
                            if (DrawingLayer.GetInstance.SelectedVisual != null && DrawingLayer.GetInstance.SelectedVisual != visual)
                            {
                                DrawingLayer.GetInstance.ClearSelection();
                            }
                            DrawingLayer.GetInstance.SelectedVisual = visual;
                            DrawingLayer.GetInstance.IsSel          = true;
                            DrawingLayer.GetInstance.LastPoint      = topLeftCorner;
                        }
                        UndoRedoLayer.GetInstance.Add(new MoveCommand(DrawingLayer.GetInstance.SelectedVisual, this.drawingSurface, DrawingLayer.GetInstance.LastPoint, pointClicked));
                    }
                    else if (DrawingLayer.GetInstance.IsSel)
                    {
                        OxFigure selectedVisual = DrawingLayer.GetInstance.SelectedVisual;
                        DrawingLayer.GetInstance.DrawFigure(ref selectedVisual, DrawingLayer.GetInstance.LastPoint, false);
                        DrawingLayer.GetInstance.IsSel = false;
                        //UndoRedoLayer.GetInstance.Add(new MoveCommand(selectedVisual, this.drawingSurface, DrawingLayer.GetInstance.LastPoint, topLeftCorner));
                    }
                }

                #endregion

                #region fill

                if (SelectionLayer.GetInstance.CurrentTool == Tools.Fill)
                {
                    visual = drawingSurface.GetVisual(pointClicked);
                    if (visual != null)
                    {
                        DrawingLayer.GetInstance.IsFill = true;
                        Point topLeftCorner = new Point(visual.ContentBounds.TopLeft.X + DrawingLayer.GetInstance.DrawingPen.Thickness / 2,
                                                        visual.ContentBounds.TopLeft.Y + DrawingLayer.GetInstance.DrawingPen.Thickness / 2);
                        UndoRedoLayer.GetInstance.Add(new FillCommand(visual, topLeftCorner, SelectionLayer.GetInstance.CurrentColor, visual.Color));
                        visual.Color = SelectionLayer.GetInstance.CurrentColor;
                        DrawingLayer.GetInstance.IsClicked = true;
                        DrawingLayer.GetInstance.DrawFigure(ref visual, topLeftCorner, false);
                        DrawingLayer.GetInstance.IsClicked = false;
                    }
                    else
                    {
                        DrawingLayer.GetInstance.DrawingBrush =
                            (Brush)DrawingLayer.GetInstance.BrushConverter.ConvertFromString(SelectionLayer.GetInstance.CurrentColor.ToString());
                        drawingSurface.Background = DrawingLayer.GetInstance.DrawingBrush;
                    }
                }

                #endregion
            }
        }
Example #2
0
        public static void DeserializeFromXML(string filename, ref CanvasLayer canvas)
        {
            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(filename);
                var root = xmlDoc.FirstChild;

                if (xmlDoc.DocumentElement != null)
                {
                    //foreach (var node in xmlDoc.DocumentElement.ChildNodes)
                    foreach (XmlNode node in root.ChildNodes)
                    {
                        if (node.Attributes != null)
                        {
                            XmlAttributeCollection attributes;
                            switch (node.Name)
                            {
                            case "Point":
                            {
                                Debug.WriteLine("1");
                                attributes = node.Attributes;
                                OxFigure figure = new OxPoint();
                                figure.Name = OxFigure.Shape.Point;
                                var point = new Point(Int32.Parse(attributes["X"].Value), Int32.Parse(attributes["Y"].Value));
                                DrawingLayer.GetInstance.DrawFigure(ref figure, point, true);
                                canvas.AddVisual(figure);

                                break;
                            }

                            case "Square":
                            {
                                attributes = node.Attributes;
                                OxFigure figure = new OxRectangle();
                                figure.Name = OxFigure.Shape.Square;
                                var point1 = new Point(Int32.Parse(attributes["X"].Value), Int32.Parse(attributes["Y"].Value));
                                figure.size  = new System.Windows.Size(Double.Parse(attributes["SizeW"].Value), Double.Parse(attributes["SizeH"].Value));
                                figure.Color = Colors.Black;
                                DrawingLayer.GetInstance.DrawFigure(ref figure, point1, false);
                                canvas.AddVisual(figure);
                                break;
                            }

                            case "Circle":
                            {
                                attributes = node.Attributes;
                                OxFigure figure = new OxCircle();
                                figure.Name = OxFigure.Shape.Rectangle;
                                var point1 = new Point(Int32.Parse(attributes["X"].Value), Int32.Parse(attributes["Y"].Value));
                                figure.size  = new System.Windows.Size(Double.Parse(attributes["SizeW"].Value), Double.Parse(attributes["SizeH"].Value));
                                figure.Color = Colors.Black;
                                DrawingLayer.GetInstance.DrawFigure(ref figure, point1, false);
                                canvas.AddVisual(figure);
                                break;
                            }

                            case "Rectangle":
                            {
                                attributes = node.Attributes;
                                OxFigure figure = new OxRectangle();
                                figure.Name = OxFigure.Shape.Rectangle;
                                var point1 = new Point(Int32.Parse(attributes["X"].Value), Int32.Parse(attributes["Y"].Value));
                                figure.size  = new System.Windows.Size(Double.Parse(attributes["SizeW"].Value), Double.Parse(attributes["SizeH"].Value));
                                figure.Color = Colors.Black;
                                DrawingLayer.GetInstance.DrawFigure(ref figure, point1, false);
                                canvas.AddVisual(figure);
                                break;
                            }

                            case "Line":
                            {
                                attributes = node.Attributes;
                                OxFigure figure = new OxLine();
                                figure.Name = OxFigure.Shape.Line;
                                var point1 = new Point(Int32.Parse(attributes["X"].Value), Int32.Parse(attributes["Y"].Value));
                                var point2 = new Point(Int32.Parse(attributes["X2"].Value), Int32.Parse(attributes["Y2"].Value));
                                DrawingLayer.GetInstance.DrawLine(ref figure, point1, point2);
                                figure.Color = Colors.Black;
                                DrawingLayer.GetInstance.DrawFigure(ref figure, point1, false);
                                canvas.AddVisual(figure);
                                break;
                            }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }