Browse Source

Several bug fixes, changes for clarity

Martin Edlund 5 years ago
parent
commit
0b217c4b6f

+ 30 - 43
SketchAssistant/SketchAssistant/Form1.Designer.cs

@@ -38,14 +38,12 @@ namespace SketchAssistant
             this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.toolStrip1 = new System.Windows.Forms.ToolStrip();
-            this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
+            this.canvasButton = new System.Windows.Forms.ToolStripButton();
+            this.drawButton = new System.Windows.Forms.ToolStripButton();
             this.statusStrip1 = new System.Windows.Forms.StatusStrip();
             this.toolStripLoadStatus = new System.Windows.Forms.ToolStripStatusLabel();
             this.backgroundWorker2 = new System.ComponentModel.BackgroundWorker();
-            this.timer1 = new System.Windows.Forms.Timer(this.components);
-            this.edittoolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
-            this.painttoolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
-            this.timer2 = new System.Windows.Forms.Timer(this.components);
+            this.drawTimer = new System.Windows.Forms.Timer(this.components);
             this.tableLayoutPanel1.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.pictureBoxRight)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.pictureBoxLeft)).BeginInit();
@@ -104,8 +102,7 @@ namespace SketchAssistant
             // menuStrip1
             // 
             this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
-            this.fileToolStripMenuItem,
-            this.edittoolStripMenuItem});
+            this.fileToolStripMenuItem});
             this.menuStrip1.Location = new System.Drawing.Point(0, 0);
             this.menuStrip1.MaximumSize = new System.Drawing.Size(1000, 0);
             this.menuStrip1.Name = "menuStrip1";
@@ -132,22 +129,33 @@ namespace SketchAssistant
             // toolStrip1
             // 
             this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
-            this.toolStripButton1});
+            this.canvasButton,
+            this.drawButton});
             this.toolStrip1.Location = new System.Drawing.Point(0, 24);
             this.toolStrip1.Name = "toolStrip1";
             this.toolStrip1.Size = new System.Drawing.Size(696, 25);
             this.toolStrip1.TabIndex = 3;
             this.toolStrip1.Text = "toolStrip1";
             // 
-            // toolStripButton1
+            // canvasButton
             // 
-            this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
-            this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
-            this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
-            this.toolStripButton1.Name = "toolStripButton1";
-            this.toolStripButton1.Size = new System.Drawing.Size(76, 22);
-            this.toolStripButton1.Text = "New Canvas";
-            this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
+            this.canvasButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
+            this.canvasButton.Image = ((System.Drawing.Image)(resources.GetObject("canvasButton.Image")));
+            this.canvasButton.ImageTransparentColor = System.Drawing.Color.Magenta;
+            this.canvasButton.Name = "canvasButton";
+            this.canvasButton.Size = new System.Drawing.Size(76, 22);
+            this.canvasButton.Text = "New Canvas";
+            this.canvasButton.Click += new System.EventHandler(this.canvasButton_Click);
+            // 
+            // drawButton
+            // 
+            this.drawButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
+            this.drawButton.Image = ((System.Drawing.Image)(resources.GetObject("drawButton.Image")));
+            this.drawButton.ImageTransparentColor = System.Drawing.Color.Magenta;
+            this.drawButton.Name = "drawButton";
+            this.drawButton.Size = new System.Drawing.Size(38, 22);
+            this.drawButton.Text = "Draw";
+            this.drawButton.Click += new System.EventHandler(this.drawButton_Click);
             // 
             // statusStrip1
             // 
@@ -167,29 +175,10 @@ namespace SketchAssistant
             this.toolStripLoadStatus.Text = "no file";
             this.toolStripLoadStatus.Click += new System.EventHandler(this.toolStripStatusLabel1_Click);
             // 
-            // timer1
-            // 
-            this.timer1.Interval = 10;
-            //this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
-            // 
-            // edittoolStripMenuItem
-            // 
-            this.edittoolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
-            this.painttoolStripMenuItem});
-            this.edittoolStripMenuItem.Name = "edittoolStripMenuItem";
-            this.edittoolStripMenuItem.Size = new System.Drawing.Size(39, 20);
-            this.edittoolStripMenuItem.Text = "Edit";
-            // 
-            // painttoolStripMenuItem
-            // 
-            this.painttoolStripMenuItem.Name = "painttoolStripMenuItem";
-            this.painttoolStripMenuItem.Size = new System.Drawing.Size(180, 22);
-            this.painttoolStripMenuItem.Text = "Paint";
-            this.painttoolStripMenuItem.Click += new System.EventHandler(this.painttoolStripMenuItem_Click);
-            // 
-            // timer2
+            // drawTimer
             // 
-            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
+            this.drawTimer.Interval = 2;
+            this.drawTimer.Tick += new System.EventHandler(this.drawTimer_Tick);
             // 
             // Form1
             // 
@@ -235,11 +224,9 @@ namespace SketchAssistant
         private System.ComponentModel.BackgroundWorker backgroundWorker2;
         private System.Windows.Forms.PictureBox pictureBoxRight;
         private System.Windows.Forms.PictureBox pictureBoxLeft;
-        private System.Windows.Forms.Timer timer1;
-        private System.Windows.Forms.ToolStripMenuItem edittoolStripMenuItem;
-        private System.Windows.Forms.ToolStripMenuItem painttoolStripMenuItem;
-        private System.Windows.Forms.Timer timer2;
-        private System.Windows.Forms.ToolStripButton toolStripButton1;
+        private System.Windows.Forms.Timer drawTimer;
+        private System.Windows.Forms.ToolStripButton canvasButton;
+        private System.Windows.Forms.ToolStripButton drawButton;
     }
 }
 

+ 101 - 36
SketchAssistant/SketchAssistant/Form1.cs

@@ -21,6 +21,14 @@ namespace SketchAssistant
             InitializeComponent();
         }
 
+        //Different Program States
+        public enum ProgramState
+        {
+            Idle,
+            Draw
+        }
+        //Current Program State
+        private ProgramState currentState;
         //Dialog to select a file.
         OpenFileDialog openFileDialogLeft = new OpenFileDialog();
         //Image loaded on the left
@@ -30,10 +38,11 @@ namespace SketchAssistant
 
         private void Form1_Load(object sender, EventArgs e)
         {
+            currentState = ProgramState.Idle;
             this.DoubleBuffered = true;
             //Connect the Paint event of the left picture box to the event handler method.
             pictureBoxLeft.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxLeft_Paint);
-            //pictureBoxRight.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxRight_Paint);
+            pictureBoxRight.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxRight_Paint);
         }
 
         //Resize Function connected to the form resize event, will refresh the form when it is resized
@@ -85,7 +94,7 @@ namespace SketchAssistant
                 pictureBoxLeft.Image = leftImage;
                 //Refresh the left image box when the content is changed
                 this.Refresh();
-                pictureBoxLeft.Refresh();
+                //pictureBoxLeft.Refresh();
             }
         }
 
@@ -108,13 +117,10 @@ namespace SketchAssistant
         //Beginn userstory4
         //Bitmap skizze = null;
         Graphics graph = null;
-        int x = 0;
-        int y = 0;
         PointF[] points = new PointF[10]; //array Mousepositons
         int i = 0;
         PointF first;
         PointF second;
-        bool clicked = false; //Button "Paint" is clicked or not
         PointF p;// = new PointF(x, y);
         bool mousedown = false;
         Pen pen = new Pen(Color.Black);
@@ -139,35 +145,23 @@ namespace SketchAssistant
 
         }
 
-        // creates an empty image and prepares rightPictureBox for drawing
-        private void painttoolStripMenuItem_Click(object sender, EventArgs e)
-        {
-            //rightImage = new Bitmap(500, 800);
-            //graph = Graphics.FromImage(rightImage);
-            //graph.FillRectangle(Brushes.White, 0, 0, 500, 800);
-            //pictureBoxRight.Image = rightImage;
-            timer2.Enabled = !clicked;
-            clicked = !clicked;
-        }
-
-        //add a Point on every tick to the Drawpath
-        private void timer2_Tick(object sender, EventArgs e)
+        //Changes The State of the Program to drawing
+        private void drawButton_Click(object sender, EventArgs e)
         {
-            timer2.Interval = 100;
-            if (clicked && mousedown)
+            if (currentState.Equals(ProgramState.Draw))
             {
-                addPath(p);
-                pictureBoxRight.Image = rightImage;
-                i++;
+                changeState(ProgramState.Idle);
+            }
+            else
+            {
+                changeState(ProgramState.Draw);
             }
         }
 
         //get current Mouse positon
         private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
         {
-            x = e.X;
-            y = e.Y;
-            p = new PointF(x, y);
+            p = convertCoordinates(new Point(e.X, e.Y));
         }
 
         //hold left mouse button to draw.
@@ -187,7 +181,7 @@ namespace SketchAssistant
         //Button to create a new Canvas. Will create an empty image 
         //which is the size of the left image, if there is one.
         //If there is no image loaded the canvas will be the size of the right picture box
-        private void toolStripButton1_Click(object sender, EventArgs e)
+        private void canvasButton_Click(object sender, EventArgs e)
         {
             if (leftImage == null)
             {
@@ -195,10 +189,6 @@ namespace SketchAssistant
                 graph = Graphics.FromImage(rightImage);
                 graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
                 pictureBoxRight.Image = rightImage;
-                /*using (Graphics grp = Graphics.FromImage(rightImage))
-                {
-                    grp.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
-                }*/
             }
             else
             {
@@ -206,15 +196,90 @@ namespace SketchAssistant
                 graph = Graphics.FromImage(rightImage);
                 graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
                 pictureBoxRight.Image = rightImage;
-                /*using (Graphics grp = Graphics.FromImage(rightImage))
-                {
-                    grp.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
-                }*/
             }
             this.Refresh();
             pictureBoxRight.Refresh();
         }
-        
 
+        //add a Point on every tick to the Drawpath
+        private void drawTimer_Tick(object sender, EventArgs e)
+        {
+            if (currentState.Equals(ProgramState.Draw) && mousedown)
+            {
+                addPath(p);
+                pictureBoxRight.Image = rightImage;
+                i++;
+            }
+        }
+
+        /// <summary>
+        /// A helper function which handles tasks associated witch changing states, 
+        /// such as checking and unchecking buttons and changing the state.
+        /// </summary>
+        /// <param name="newState">The new state of the program</param>
+        private void changeState(ProgramState newState)
+        {
+            switch (currentState)
+            {
+                case ProgramState.Draw:
+                    drawButton.CheckState = CheckState.Unchecked;
+                    drawTimer.Enabled = false;
+                    break;
+                default:
+                    break;
+            }
+            switch (newState)
+            {
+                case ProgramState.Draw:
+                    drawButton.CheckState = CheckState.Checked;
+                    drawTimer.Enabled = true;
+                    break;
+                default:
+                    break;
+            }
+            currentState = newState;
+        }
+
+        /// <summary>
+        /// A function that calculates the coordinates of a point on a zoomed in image.
+        /// </summary>
+        /// <param name="">The position of the mouse cursor</param>
+        /// <returns>The real coordinates of the mouse cursor on the image</returns>
+        private Point convertCoordinates(Point cursorPosition)
+        {
+            Point realCoordinates = new Point(5,3);
+            if(pictureBoxRight.Image == null)
+            {
+                return cursorPosition;
+            }
+
+            int widthImage = pictureBoxRight.Image.Width;
+            int heightImage = pictureBoxRight.Image.Height;
+            int widthBox = pictureBoxRight.Width;
+            int heightBox = pictureBoxRight.Height;
+
+            float imageRatio = (float)widthImage / (float)heightImage;
+            float containerRatio = (float)widthBox / (float)heightBox;
+
+            if (imageRatio >= containerRatio)
+            {
+                //Image is wider than it is high
+                float zoomFactor = (float)widthImage / (float)widthBox;
+                float scaledHeight = heightImage / zoomFactor;
+                float filler = (heightBox - scaledHeight) / 2;
+                realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
+                realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
+            }
+            else
+            {
+                //Image is higher than it is wide
+                float zoomFactor = (float)heightImage / (float)heightBox;
+                float scaledWidth = widthImage / zoomFactor;
+                float filler = (widthBox - scaledWidth) / 2;
+                realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
+                realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
+            }
+            return realCoordinates;
+        }
     }
 }

+ 17 - 5
SketchAssistant/SketchAssistant/Form1.resx

@@ -127,7 +127,22 @@
     <value>401, 17</value>
   </metadata>
   <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
-  <data name="toolStripButton1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+  <data name="canvasButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+    <value>
+        iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
+        YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIDSURBVDhPpZLrS5NhGMb3j4SWh0oRQVExD4gonkDpg4hG
+        YKxG6WBogkMZKgPNCEVJFBGdGETEvgwyO9DJE5syZw3PIlPEE9pgBCLZ5XvdMB8Ew8gXbl54nuf63dd9
+        0OGSnwCahxbPRNPAPMw9Xpg6ZmF46kZZ0xSKzJPIrhpDWsVnpBhGkKx3nAX8Pv7z1zg8OoY/cITdn4fw
+        bf/C0kYAN3Ma/w3gWfZL5kzTKBxjWyK2DftwI9tyMYCZKXbNHaD91bLYJrDXsYbrWfUKwJrPE9M2M1Oc
+        VzOOpHI7Jr376Hi9ogHqFIANO0/MmmmbmSmm9a8ze+I4MrNWAdjtoJgWcx+PSzg166yZZ8xM8XvXDix9
+        c4jIqFYAjoriBV9AhEPv1mH/sonogha0afbZMMZz+yreTGyhpusHwtNNCsA5U1zS4BLxzJIfg299qO32
+        Ir7UJtZfftyATqeT+8o2D8JSjQrAJblrncYL7ZJ2+bfaFnC/1S1NjL3diRat7qrO7wLRP3HjWsojBeCo
+        mDEo5mNjuweFGvjWg2EBhCbpkW78htSHHwRyNdmgAFzPEee2iFkzayy2OLXzT4gr6UdUnlXrullsxxQ+
+        kx0g8BTA3aZlButjSTyjODq/WcQcW/B/Je4OQhLvKQDnzN1mp0nnkvAhR8VuMzNrpm1mpjgkoVwB/v8D
+        TgDQASA1MVpwzwAAAABJRU5ErkJggg==
+</value>
+  </data>
+  <data name="drawButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
     <value>
         iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
         YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAIDSURBVDhPpZLrS5NhGMb3j4SWh0oRQVExD4gonkDpg4hG
@@ -148,12 +163,9 @@
   <metadata name="backgroundWorker2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>622, 17</value>
   </metadata>
-  <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+  <metadata name="drawTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>786, 17</value>
   </metadata>
-  <metadata name="timer2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
-    <value>873, 17</value>
-  </metadata>
   <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>47</value>
   </metadata>

+ 20 - 0
SketchAssistant/SketchAssistant/Line.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Drawing;
+
+namespace SketchAssistant
+{
+    class Line
+    {
+        private Point Start { get; set; }
+        private Point End { get; set; }
+
+        public Line(int[] points)
+        {
+
+        }
+    }
+}

+ 1 - 0
SketchAssistant/SketchAssistant/SketchAssistant.csproj

@@ -63,6 +63,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Line.cs" />
     <Compile Include="Form1.cs">
       <SubType>Form</SubType>
     </Compile>