Beispiel #1
0
        static void Main()
        {
            // this will creates the originator canvas and the other lists neccessary to run the application
            Originator canvas = new Originator();
            // This will add shapes to the array list
            ArrayList      mem    = new ArrayList();
            Memento        m      = new Memento(mem);
            List <Memento> holder = new List <Memento>();

            //this random number generator
            Random rnd = new Random();

            //This will prompt for user to use application adn help the user
            Console.WriteLine("Welcome to the canvas!");
            Console.WriteLine("Please enter a command: (H for Help)" + Environment.NewLine);

            while (true)
            {
                //gets the size of the current canvas for referencing
                int    x  = canvas.Count();
                string sc = Console.ReadLine();
                //closing sequence for the application if the user chooses too.
                if (sc.Equals("q") || sc.Equals("Q"))
                {
                    Console.WriteLine("Closing application....");
                    System.Threading.Thread.Sleep(2000);
                    Console.WriteLine("Succusfully closed application");
                    break;
                }
                else
                {
                    switch (sc)
                    {
                    //If user chooses this command it will show this drop down menu
                    case "H":
                        Console.WriteLine("Commands: ");
                        Console.WriteLine("  A <shape>   Add <shape> to canvas");
                        Console.WriteLine("  U           Undo last operation");
                        Console.WriteLine("  R           Redo last operation");
                        Console.WriteLine("  D           Display Canvas");
                        Console.WriteLine("  S           Save canvas to SVG file");
                        Console.WriteLine("  C           Clear canvas");
                        Console.WriteLine("  Q           Close application");
                        break;

                    //all these cases will add whatever shape the user chooses onto the canvas
                    case "A Rectangle":
                        Rectangle rect = new Rectangle(rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500));
                        string    Rect = rect.ToString();
                        canvas.Add(Rect);
                        m = canvas.SaveMemento();
                        break;

                    case "A Circle":
                        Circle circle = new Circle();
                        string Circle = circle.ToString();
                        canvas.Add(Circle);
                        m = canvas.SaveMemento();
                        break;

                    case "A Ellipse":
                        Ellipse ellipse = new Ellipse(rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500));
                        string  Ellipse = ellipse.ToString();
                        canvas.Add(Ellipse);
                        m = canvas.SaveMemento();
                        break;

                    case "A Line":
                        Line   line = new Line(rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500), rnd.Next(1, 500));
                        string Line = line.ToString();
                        canvas.Add(Line);
                        m = canvas.SaveMemento();
                        break;

                    case "A Polyline":
                        Polyline polyline = new Polyline();
                        string   Polyline = polyline.ToString();
                        canvas.Add(Polyline);
                        m = canvas.SaveMemento();
                        break;

                    case "A Polygon":
                        Polygon polygon = new Polygon();
                        string  Polygon = polygon.ToString();
                        canvas.Add(Polygon);
                        m = canvas.SaveMemento();
                        break;

                    case "A Path":
                        Path   path = new Path();
                        string Path = path.ToString();
                        canvas.Add(Path);
                        m = canvas.SaveMemento();
                        break;

                    case "U":
                        // if statement to check if the canvas is not empty if it is not then it will do the undo operation
                        if (x == 0)
                        {
                            Console.WriteLine("Canvas is empty! No undo is possible.");
                        }
                        else if (x > 0)
                        {
                            m = canvas.SaveMemento();
                            canvas.RemoveAt();
                        }
                        break;

                    case "R":
                        //this will restore the canvas if an undo is completed
                        canvas.SaveMemento();
                        canvas.RestoreMemento(m);
                        break;

                    case "C":
                        //if statment to check if the canvas is empty. If it is it will not perform the clear operation as there is nothing to clear.
                        if (x == 0)
                        {
                            Console.WriteLine("Canvas is empty! Nothing to clear.");
                        }
                        //if it isnt empty then obviously clear the canvas
                        else if (x > 0)
                        {
                            canvas = new Originator();
                            Console.WriteLine("Successfully cleared canvas.");
                        }
                        break;

                    //This will also check if the canvas is empty and if it is it will inform the user there is nothing to display
                    case "D":
                        if (x == 0)
                        {
                            Console.WriteLine("Canvas is empty! Nothing to display.");
                        }
                        else if (x > 0)
                        {
                            canvas.getShapes();
                        }
                        break;

                    case "S":
                        String PathToSVG = @"./Shapes.svg";

                        List <string> Shapes = canvas.ListShapes();

                        //https://docs.microsoft.com/en-us/dotnet/api/system.io.file.create?view=net-5.0
                        using (FileStream fs = File.Create(PathToSVG))
                        {
                            // insert open svg tag into first line
                            byte[] info = new UTF8Encoding(true).GetBytes(@"<svg height=""2000"" width=""2000"" xmlns=""http://www.w3.org/2000/svg"">" + Environment.NewLine);
                            // Add some information to the file.
                            fs.Write(info, 0, info.Length);
                        }
                        //add close tag to the end
                        using (StreamWriter sw = File.AppendText(PathToSVG))
                        {
                            foreach (string item in Shapes)
                            {
                                sw.WriteLine(item);
                            }
                            sw.WriteLine("</svg>");
                        }
                        Console.WriteLine("Successfully saved your canvas!");
                        break;

                    default:
                        //informs the user his/her input is invalid
                        Console.WriteLine("Invalid Input");
                        break;
                    }
                }
            }
        }