Przeglądaj źródła

implemented functionality, no tests yet

Vincenz Mechler 5 lat temu
rodzic
commit
b94b3e3cf4

+ 22 - 2
SketchAssistant/SketchAssistant/Form1.Designer.cs

@@ -37,6 +37,8 @@ namespace SketchAssistant
             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
             this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.examplePictureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.toolStrip1 = new System.Windows.Forms.ToolStrip();
             this.canvasButton = new System.Windows.Forms.ToolStripButton();
             this.drawButton = new System.Windows.Forms.ToolStripButton();
@@ -113,7 +115,8 @@ namespace SketchAssistant
             // fileToolStripMenuItem
             // 
             this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
-            this.loadToolStripMenuItem});
+            this.loadToolStripMenuItem,
+            this.importToolStripMenuItem});
             this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
             this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
             this.fileToolStripMenuItem.Text = "File";
@@ -121,10 +124,25 @@ namespace SketchAssistant
             // loadToolStripMenuItem
             // 
             this.loadToolStripMenuItem.Name = "loadToolStripMenuItem";
-            this.loadToolStripMenuItem.Size = new System.Drawing.Size(109, 22);
+            this.loadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
             this.loadToolStripMenuItem.Text = "Load...";
             this.loadToolStripMenuItem.Click += new System.EventHandler(this.loadToolStripMenuItem_Click);
             // 
+            // importToolStripMenuItem
+            // 
+            this.importToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
+            this.examplePictureToolStripMenuItem});
+            this.importToolStripMenuItem.Name = "importToolStripMenuItem";
+            this.importToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+            this.importToolStripMenuItem.Text = "Import...";
+            // 
+            // examplePictureToolStripMenuItem
+            // 
+            this.examplePictureToolStripMenuItem.Name = "examplePictureToolStripMenuItem";
+            this.examplePictureToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
+            this.examplePictureToolStripMenuItem.Text = "Example picture";
+            this.examplePictureToolStripMenuItem.Click += new System.EventHandler(this.examplePictureToolStripMenuItem_Click);
+            // 
             // toolStrip1
             // 
             this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
@@ -236,6 +254,8 @@ namespace SketchAssistant
         private System.Windows.Forms.ToolStripButton canvasButton;
         private System.Windows.Forms.ToolStripButton drawButton;
         private System.Windows.Forms.ToolStripButton deleteButton;
+        private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem;
+        private System.Windows.Forms.ToolStripMenuItem examplePictureToolStripMenuItem;
     }
 }
 

+ 168 - 1
SketchAssistant/SketchAssistant/Form1.cs

@@ -7,6 +7,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using System.Text.RegularExpressions;
 
 
 // This is the code for your desktop app.
@@ -24,7 +25,7 @@ namespace SketchAssistant
         /**********************************/
         /*** CLASS VARIABLES START HERE ***/
         /**********************************/
-
+        
         //Different Program States
         public enum ProgramState
         {
@@ -38,6 +39,10 @@ namespace SketchAssistant
         OpenFileDialog openFileDialogLeft = new OpenFileDialog();
         //Image loaded on the left
         Image leftImage = null;
+        /// <summary>
+        /// the graphic shown in the left window, represented as a list of polylines
+        /// </summary>
+        List<Line> templatePicture;
         //Image on the right
         Image rightImage = null;
         //Current Line being Drawn
@@ -90,6 +95,26 @@ namespace SketchAssistant
             }
         }
 
+        /// <summary>
+        /// Import button, will open an OpenFileDialog
+        /// </summary>
+        private void examplePictureToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+
+            
+            openFileDialogLeft.Filter = "Interactive Sketch-Assistant Drawing|*.isad";
+            if (openFileDialogLeft.ShowDialog() == DialogResult.OK)
+            {
+                toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
+
+                string[] allLines = System.IO.File.ReadAllLines(openFileDialogLeft.FileName);
+
+                ParseISADInput(allLines);
+
+                this.Refresh();
+            }
+        }
+
         //Changes the state of the program to drawing
         private void drawButton_Click(object sender, EventArgs e)
         {
@@ -233,6 +258,28 @@ namespace SketchAssistant
             pictureBoxRight.Refresh();
         }
 
+        /// <summary>
+        /// Creates an empty Canvas on the left
+        /// </summary>
+        /// <param name="width"> width of the new canvas in pixels </param>
+        /// <param name="height"> height of the new canvas in pixels </param>
+        private void DrawEmptyCanvasLeft(int width, int height)
+        {
+            if (width == 0)
+            {
+                leftImage = new Bitmap(pictureBoxLeft.Width, pictureBoxLeft.Height);
+            }
+            else
+            {
+                leftImage = new Bitmap(width, height);
+            }
+            Graphics.FromImage(leftImage).FillRectangle(Brushes.White, 0, 0, pictureBoxLeft.Width + 10, pictureBoxLeft.Height + 10);
+            pictureBoxLeft.Image = leftImage;
+            
+            this.Refresh();
+            pictureBoxLeft.Refresh();
+        }
+
         /// <summary>
         /// Redraws all lines in lineList, for which their associated boolean value equals true.
         /// </summary>
@@ -380,5 +427,125 @@ namespace SketchAssistant
             }
             return returnSet;
         }
+
+        /// <summary>
+        /// checks that all lines of the given picture are inside the constraints of the left canvas and binds it to templatePicture
+        /// </summary>
+        /// <param name="newTemplatePicture"> the new template picture, represented as a list of polylines </param>
+        /// <returns></returns>
+        private Boolean BindTemplatePicture(List<Line> newTemplatePicture)
+        {
+            templatePicture = newTemplatePicture;
+            foreach(Line l in templatePicture)
+            {
+                l.DrawLine(Graphics.FromImage(leftImage));
+            }
+            return true;
+        }
+
+        private void ParseISADInput(String[] allLines)
+        {
+
+
+            if (allLines.Length == 0)
+            {
+                MessageBox.Show("Could not import file:\n file is empty");
+                return;
+            }
+            if (!"drawing".Equals(allLines[0]))
+            {
+                MessageBox.Show("Could not import file:\n file is not an interactive sketch assistant drawing\n (Hint: .isad files ave to start with the 'drawing' token)");
+                return;
+            }
+            if (!"enddrawing".Equals(allLines[allLines.Length - 1]))
+            {
+                MessageBox.Show("Could not import file:\n unterminated drawing definition\n (Hint: .isad files ave to end with the 'enddrawing' token)");
+                return;
+            }
+
+            if (!parseISADHeader(allLines))
+            {
+                return;
+            }
+
+            if (!parseISADBody(allLines))
+            {
+                return;
+            }
+
+            
+            
+        }
+
+        private bool parseISADHeader(String[] allLines)
+        {
+
+            int width;
+            int height;
+
+            if (!(allLines.Length > 1) || !Regex.Match(allLines[1], @"(\d?\d*x?\d?\d*?)?", RegexOptions.None).Success)
+            {
+                MessageBox.Show("Could not import file:\n invalid or missing canvas size definition\n (Line: 2)");
+                return false;
+            }
+            String[] size = allLines[1].Split('x');
+            width = Convert.ToInt32(size[0]);
+            height = Convert.ToInt32(size[1]);
+
+            DrawEmptyCanvasLeft(width, height);
+
+            return true;
+        }
+
+        private bool parseISADBody(String[] allLines)
+        {
+
+            String lineStartString = "line";
+            String lineEndString = "endline";
+            
+            List<Line> drawing = new List<Line>();
+
+            int i = 2;
+            //parse 'line' token and complete line definition
+            while (lineStartString.Equals(allLines[i]))
+            {
+                i++;
+                List<Point> newLine = new List<Point>();
+                while (!lineEndString.Equals(allLines[i]))
+                {
+                    if (i == allLines.Length)
+                    {
+                        MessageBox.Show("Could not import file:\n unterminated line definition\n (Line: " + (i + 1) + ")");
+                        return false;
+                    }
+                    //parse single point definition
+                    if (!Regex.Match(allLines[i], @"(\d?\d*;?\d?\d*?)?", RegexOptions.None).Success)
+                    {
+                        MessageBox.Show("Could not import file:\n invalid Point definition: wrong format\n (Line: " + (i + 1) + ")");
+                        return false;
+                    }
+                    String[] coordinates = allLines[i].Split(';');
+                    //no errors possible, convertability to string already checked above
+                    int xCoordinate = Convert.ToInt32(coordinates[0]);
+                    int yCoordinate = Convert.ToInt32(coordinates[1]);
+                    if (xCoordinate < 0 || yCoordinate < 0 || xCoordinate > leftImage.Width - 1 || yCoordinate > leftImage.Height - 1)
+                    {
+                        MessageBox.Show("Could not import file:\n invalid Point definition: point out of bounds\n (Line: " + (i + 1) + ")");
+                        return false;
+                    }
+                    newLine.Add(new Point(xCoordinate, yCoordinate));
+                    i++;
+                }
+                //parse 'endline' token
+                i++;
+                //add line to drawing
+                drawing.Add(new Line(newLine));
+            }
+
+            //save parsed drawing to instance variable and draw it
+            BindTemplatePicture(drawing);
+
+            return true;
+        }
     }
 }