浏览代码

Fixed bugs with the redraw feature

Martin Edlund 6 年之前
父节点
当前提交
f612e79911
共有 2 个文件被更改,包括 53 次插入28 次删除
  1. 19 10
      SketchAssistant/SketchAssistant/Form1.cs
  2. 34 18
      SketchAssistant/SketchAssistant/RedrawAssistant.cs

+ 19 - 10
SketchAssistant/SketchAssistant/Form1.cs

@@ -182,7 +182,7 @@ namespace SketchAssistant
                         //Start the redraw mode
                         redrawAss = new RedrawAssistant(leftLineList);
                         UpdateSizes();
-                        overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                        overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
                         RedrawRightImage();
                         this.Refresh();
                     }
@@ -255,7 +255,7 @@ namespace SketchAssistant
                     default:
                         break;
                 }
-                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
                 RedrawRightImage();
             }
             historyOfActions.MoveAction(true);
@@ -285,7 +285,7 @@ namespace SketchAssistant
                     default:
                         break;
                 }
-                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
                 RedrawRightImage();
             }
             UpdateButtonStatus();
@@ -339,7 +339,7 @@ namespace SketchAssistant
                 newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
                 historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID()));
                 //Execute a RedrawAssistant tick with the currently finished Line
-                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, newLine.GetID());
+                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, newLine.GetID(), true);
                 RedrawRightImage();
             }
             UpdateButtonStatus();
@@ -365,7 +365,7 @@ namespace SketchAssistant
                 {
                     redrawAss = new RedrawAssistant(leftLineList);
                     UpdateSizes();
-                    overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                    overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
                     RedrawRightImage();
                 }
             }
@@ -390,8 +390,8 @@ namespace SketchAssistant
                 Line drawline = new Line(currentLine);
                 drawline.DrawLine(rightGraph);
                 pictureBoxRight.Image = rightImage;
-                //RedrawTick
-                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                //Redraw overlay gets ticked
+                overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, rightLineList.Count, false);
                 RedrawRightImage();
             }
             //Deleting
@@ -409,8 +409,8 @@ namespace SketchAssistant
                             rightLineList[lineID] = new Tuple<bool, Line>(false, rightLineList[lineID].Item2);
                         }
                         RepopulateDeletionMatrixes();
-                        //RedrawTick
-                        overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1);
+                        //Redraw overlay gets ticked
+                        overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
                         RedrawRightImage();
                     }
                 }
@@ -429,7 +429,16 @@ namespace SketchAssistant
         /// <returns>The new canvas</returns>
         private Image GetEmptyCanvas(int width, int height)
         {
-            Image image = new Bitmap(width, height);
+            Image image;
+            try
+            {
+                image = new Bitmap(width, height);
+            }
+            catch(ArgumentException e)
+            {
+                ShowInfoMessage("The requested canvas size caused an error: \n" + e.ToString() + "\n The Canvas will be set to match your window.");
+                image = new Bitmap(pictureBoxLeft.Width, pictureBoxLeft.Height);
+            }
             Graphics graph = Graphics.FromImage(image);
             graph.FillRectangle(Brushes.White, 0, 0, width + 10, height + 10);
             return image;

+ 34 - 18
SketchAssistant/SketchAssistant/RedrawAssistant.cs

@@ -29,6 +29,10 @@ namespace SketchAssistant
         /// </summary>
         int lineBeingRedrawn;
         /// <summary>
+        /// The id of the line being drawn on the right side. -1 if no line is being drawn.
+        /// </summary>
+        int currentLineID;
+        /// <summary>
         /// Whether or not the user is currently redrawing a line.
         /// </summary>
         bool currentlyRedrawing;
@@ -73,40 +77,30 @@ namespace SketchAssistant
         /// </summary>
         /// <param name="currentPoint">The current position of the cursor, as a point</param>
         /// <param name="rightLines">The lines on the right canvas</param>
-        /// <param name="currLineID">The id of the currently finished line, -1 if no line was finished.</param>
+        /// <param name="currLineID">The id of the line currently being drawn.</param>
+        /// <param name="lineFinished">A boolean to indicate that the line is finished</param>
         /// <returns>A List of HashSets of Points, which are markers for the user to redraw lines.</returns>
-        public List<HashSet<Point>> Tick(Point currentPoint, List<Tuple<bool, Line>> rightLines, int currLineID)
+        public List<HashSet<Point>> Tick(Point currentPoint, List<Tuple<bool, Line>> rightLines, int currLineID, bool lineFinished)
         {
             List<HashSet<Point>> returnList = new List<HashSet<Point>>();
             if (!isActive) { return returnList; }
             Tuple<Line, bool, int> newLineTuple = null;
             var returnAllStartPoints = true;
             CheckForUndrawnLines(rightLines);
-            // The current Endpoint has been intersected, and a line was finished here
-            if (currentlyRedrawing && startAndEndPoints[lineBeingRedrawn].Item2.Contains(currentPoint) && currLineID != -1)
-            {
-                newLineTuple = new Tuple<Line, bool, int>(linesToRedraw[lineBeingRedrawn].Item1, true, currLineID);
-                currentlyRedrawing = false;
-                lineBeingRedrawn = -1;
-            }
-            else if (currentlyRedrawing)
-            {
-                returnList.Add(startAndEndPoints[lineBeingRedrawn].Item1);
-                returnList.Add(startAndEndPoints[lineBeingRedrawn].Item2);
-                returnAllStartPoints = false;
-            }
-            else if (!currentlyRedrawing)
+
+            // Checking if a startpoint is intersected
+            if (!currentlyRedrawing)
             {
                 for (int i = 0; i < linesToRedraw.Count; i++)
                 {
                     Tuple<Line, bool, int> tup = linesToRedraw[i];
                     if (!tup.Item2)
                     {
-                        // A Starpoint has been intersected
-                        if (!currentlyRedrawing && startAndEndPoints[i].Item1.Contains(currentPoint))
+                        if (startAndEndPoints[i].Item1.Contains(currentPoint))
                         {
                             currentlyRedrawing = true;
                             lineBeingRedrawn = i;
+                            currentLineID = currLineID;
                             returnList.Add(startAndEndPoints[i].Item1);
                             returnList.Add(startAndEndPoints[i].Item2);
                             returnAllStartPoints = false;
@@ -114,6 +108,28 @@ namespace SketchAssistant
                     }
                 }
             }
+            //Currently redrawing a line, but a line hasn't been finished drawing.
+            else if (!lineFinished)
+            {
+                returnList.Add(startAndEndPoints[lineBeingRedrawn].Item1);
+                returnList.Add(startAndEndPoints[lineBeingRedrawn].Item2);
+                returnAllStartPoints = false;
+            }
+            //Line is finished, check if it is in the correct endpoint
+            else if (currLineID == currentLineID && startAndEndPoints[lineBeingRedrawn].Item2.Contains(currentPoint))
+            {
+                newLineTuple = new Tuple<Line, bool, int>(linesToRedraw[lineBeingRedrawn].Item1, true, currLineID);
+                currentlyRedrawing = false;
+                lineBeingRedrawn = -1;
+                currentLineID = -1;
+            }
+            //Line is finished, but not in the correct endpoint
+            else
+            {
+                currentlyRedrawing = false;
+                lineBeingRedrawn = -1;
+                currentLineID = -1;
+            }
 
             //Replace the changed line tuple in linesToRedraw
             if(newLineTuple != null)