Browse Source

All features now work as intended. Cleanup remains.

Martin Edlund 5 years ago
parent
commit
2db67bf66b

+ 17 - 228
SketchAssistant/SketchAssistantWPF/MVP_Model.cs

@@ -25,9 +25,6 @@ namespace SketchAssistantWPF
         /// </summary>
         //RedrawAssistant redrawAss;
 
-        /*******************/
-        /*** ENUMERATORS ***/
-        /*******************/
 
         /***********************/
         /*** CLASS VARIABLES ***/
@@ -102,15 +99,10 @@ namespace SketchAssistantWPF
         /// </summary>
         public bool graphicLoaded { get; set; }
 
-        //Images
-        Image leftImage;
-
-        List<InternalLine> leftLineList;
-
         Image rightImageWithoutOverlay;
 
-        Image rightImageWithOverlay;
-
+        List<InternalLine> leftLineList;
+        
         List<Tuple<bool, InternalLine>> rightLineList;
 
         List<Point> currentLine = new List<Point>();
@@ -131,7 +123,7 @@ namespace SketchAssistantWPF
         }
 
         /**************************/
-        /*** NEW INTERNAL FUNCTIONS ***/
+        /*** INTERNAL FUNCTIONS ***/
         /**************************/
 
 
@@ -203,9 +195,22 @@ namespace SketchAssistantWPF
         }
 
         /********************************************/
-        /*** NEW FUNCTIONS TO INTERACT WITH PRESENTER ***/
+        /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
         /********************************************/
 
+        /// <summary>
+        /// A function to update the dimensions of the left and right canvas when the window is resized.
+        /// </summary>
+        /// <param name="LeftCanvas">The size of the left canvas.</param>
+        /// <param name="RightCanvas">The size of the right canvas.</param>
+        public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
+        {
+            if(LeftCanvas.Height >= 0 && LeftCanvas.Width>= 0) { leftImageSize = LeftCanvas; }
+            if(RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
+          
+            RepopulateDeletionMatrixes();
+        }
+
         /// <summary>
         /// A function to reset the right image.
         /// </summary>
@@ -445,221 +450,5 @@ namespace SketchAssistantWPF
         {
             return !historyOfActions.IsEmpty();
         }
-    
-        /**************************/
-        /*** INTERNAL FUNCTIONS ***/
-        /**************************/
-
-        /*
-    /// <summary>
-    /// A function that returns a white canvas for a given width and height.
-    /// </summary>
-    /// <param name="width">The width of the canvas in pixels</param>
-    /// <param name="height">The height of the canvas in pixels</param>
-    /// <returns>The new canvas</returns>
-    private Image GetEmptyCanvas(int width, int height)
-    {
-        Image image;
-        WriteableBitmap newCanvas;
-        try
-        {
-            newCanvas = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
-            image = new Bitmap(width, height);
-        }
-        catch (ArgumentException e)
-        {
-            programPresenter.PassMessageToView("The requested canvas size caused an error: \n"
-                + e.ToString() + "\n The Canvas will be set to match your window.");
-            newCanvas = new WriteableBitmap(leftImageBoxWidth, leftImageBoxHeight, 96, 96, PixelFormats.Bgra32, null);
-        }
-        newCanvas.WritePixels()
-        Graphics graph = Graphics.FromImage(image);
-        graph.FillRectangle(Brushes.White, 0, 0, width + 10, height + 10);
-        return image;
-    }
-
-
-    /// <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 = GetEmptyCanvas(leftImageBoxWidth, leftImageBoxHeight);
-        }
-        else
-        {
-            leftImage = GetEmptyCanvas(width, height);
-        }
-        programPresenter.UpdateLeftImage(leftImage);
-    }
-
-    /// <summary>
-    /// Redraws all lines in rightLineList, for which their associated boolean value equals true and calls RedrawRightOverlay.
-    /// </summary>
-    private void RedrawRightImage()
-    {
-        var workingCanvas = GetEmptyCanvas(rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height);
-        var workingGraph = Graphics.FromImage(workingCanvas);
-        //Lines
-        foreach (Tuple<bool, InternalLine> lineBoolTuple in rightLineList)
-        {
-            if (lineBoolTuple.Item1)
-            {
-                lineBoolTuple.Item2.DrawLine(workingGraph);
-            }
-        }
-        //The Line being currently drawn
-        if (currentLine != null && currentLine.Count > 0 && inDrawingMode && mousePressed)
-        {
-            var currLine = new InternalLine(currentLine);
-            currLine.DrawLine(workingGraph);
-        }
-        rightImageWithoutOverlay = workingCanvas;
-        //Redraw the Overlay if needed
-        if (leftImage != null)
-        {
-            RedrawRightOverlay();
-        }
-        else
-        {
-            programPresenter.UpdateRightImage(rightImageWithoutOverlay);
-        }
-    }
-
-        /// <summary>
-        /// Redraws all elements in the overlay items for which the respective boolean value is true.
-        /// </summary>
-        private void RedrawRightOverlay()
-        {
-            var workingCanvas = rightImageWithoutOverlay;
-            var workingGraph = Graphics.FromImage(workingCanvas);
-            foreach (Tuple<bool, HashSet<Point>> tup in overlayItems)
-            {
-                if (tup.Item1)
-                {
-                    foreach (Point p in tup.Item2)
-                    {
-                        workingGraph.FillRectangle(Brushes.Green, p.X, p.Y, 1, 1);
-                    }
-                }
-            }
-            rightImageWithOverlay = workingCanvas;
-            programPresenter.UpdateRightImage(rightImageWithOverlay);
-        }
-    */
-
-
-
-
-        /*
-        /// <summary>
-        /// Will calculate the start and endpoints of the given line on the right canvas.
-        /// </summary>
-        /// <param name="line">The line.</param>
-        /// <param name="size">The size of the circle with which the endpoints of the line are marked.</param>
-        private Tuple<HashSet<Point>, HashSet<Point>> CalculateStartAndEnd(Line line, int size)
-        {
-            var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), size);
-            var circle1 = GeometryCalculator.FilledCircleAlgorithm(line.GetEndPoint(), size);
-            var currentLineEndings = new Tuple<HashSet<Point>, HashSet<Point>>(circle0, circle1);
-            return currentLineEndings;
-        }
-        */
-
-
-
-
-
-
-        /********************************************/
-        /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
-        /********************************************/
-        /*
-        /// <summary>
-        /// Creates an empty Canvas
-        /// </summary>
-        public void DrawEmptyCanvasRight()
-        {
-            if (leftImage == null)
-            {
-                rightImageWithoutOverlay = GetEmptyCanvas(leftImageBoxWidth, leftImageBoxHeight);
-            }
-            else
-            {
-                rightImageWithoutOverlay = GetEmptyCanvas(leftImage.Width, leftImage.Height);
-            }
-            RepopulateDeletionMatrixes();
-            rightImageWithOverlay = rightImageWithoutOverlay;
-            programPresenter.UpdateRightImage(rightImageWithOverlay);
-        }
-
-
-        /// <summary>
-        /// A method to get the dimensions of the right image.
-        /// </summary>
-        /// <returns>A tuple containing the width and height of the right image.</returns>
-        public Tuple<int, int> GetRightImageDimensions()
-        {
-            if (rightImageWithoutOverlay != null)
-            {
-                return new Tuple<int, int>(rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height);
-            }
-            else
-            {
-                return new Tuple<int, int>(0, 0);
-            }
-        }
-
-
-        /// <summary>
-        /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
-        /// </summary>
-        public void Tick()
-        {
-            if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
-            else { previousCursorPosition = currentCursorPosition; }
-            cursorPositions.Enqueue(currentCursorPosition);
-            //Drawing
-            if (inDrawingMode && mousePressed)
-            {
-                var rightGraph = Graphics.FromImage(rightImageWithoutOverlay);
-                currentLine.Add(currentCursorPosition);
-                InternalLine drawline = new InternalLine(currentLine);
-                drawline.DrawLine(rightGraph);
-                RedrawRightOverlay();
-            }
-            //Deleting
-            if (!inDrawingMode && mousePressed)
-            {
-                List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
-                foreach (Point currPoint in uncheckedPoints)
-                {
-                    HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
-                    if (linesToDelete.Count > 0)
-                    {
-                        programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
-                        foreach (int lineID in linesToDelete)
-                        {
-                            rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
-                        }
-                        RepopulateDeletionMatrixes();
-                        if (leftImage != null)
-                        {
-                            //Redraw overlay gets ticked
-                            //overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
-                        }
-                        RedrawRightImage();
-                    }
-                }
-            }
-        }
-
-    */
-
-
     }
 }

+ 7 - 27
SketchAssistant/SketchAssistantWPF/MVP_Presenter.cs

@@ -77,13 +77,8 @@ namespace SketchAssistantWPF
         {
             CanvasSizeLeft.ChangeDimension(leftPBS.Item1, leftPBS.Item2);
             CanvasSizeRight.ChangeDimension(rightPBS.Item1, rightPBS.Item2);
-            /*
-            programModel.leftImageBoxWidth = leftPBS.Item1;
-            programModel.leftImageBoxHeight = leftPBS.Item2;
-            programModel.rightImageBoxWidth = rightPBS.Item1;
-            programModel.rightImageBoxHeight = rightPBS.Item2;
-            */
             programModel.UpdateSizes(CanvasSizeRight);
+            programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
         }
 
         /// <summary>
@@ -104,9 +99,12 @@ namespace SketchAssistantWPF
                     programView.SetToolStripLoadStatus(fileNameTup.Item2);
                     Tuple<int, int, List<InternalLine>> values = fileImporter.ParseISADInputFile(fileNameTup.Item1);
                     programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
-                    programModel.ChangeState(true);
+
+                    programModel.ResetRightImage();
                     programModel.CanvasActivated();
+                    programModel.ChangeState(true);
                     programView.EnableTimer();
+                    ClearRightLines();
                 }
             }
         }
@@ -156,6 +154,7 @@ namespace SketchAssistantWPF
             }
             if (okToContinue)
             {
+                programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
                 programModel.ResetRightImage();
                 programModel.CanvasActivated();
                 programModel.ChangeState(true);
@@ -174,7 +173,7 @@ namespace SketchAssistantWPF
             switch (mouseAction)
             {
                 case MouseAction.Move:
-                    programModel.SetCurrentCursorPosition(ConvertCoordinates(position));
+                    programModel.SetCurrentCursorPosition(position);
                     break;
                 default:
                     break;
@@ -307,25 +306,6 @@ namespace SketchAssistantWPF
         }
 
 
-        /*
-        /// <summary>
-        /// Is called by the model when the left image is changed.
-        /// </summary>
-        /// <param name="lineList">The new image.</param>
-        public void UpdateLeftImage(List<InternalLine> lineList)
-        {
-            programView.DisplayInLeftPictureBox(lineList);
-        }
-
-        /// <summary>
-        /// Is called by the model when the right image is changed.
-        /// </summary>
-        /// <param name="lineList">The new image.</param>
-        public void UpdateRightImage(List<InternalLine> lineList)
-        {
-            programView.DisplayInRightPictureBox(lineList);
-        }*/
-
         /// <summary>
         /// Pass-trough function to display an info message in the view.
         /// </summary>

+ 1 - 1
SketchAssistant/SketchAssistantWPF/MainWindow.xaml

@@ -173,7 +173,7 @@
         <Canvas Name="LeftCanvas" Background="SlateGray" Grid.Column="2" Grid.Row="1" Height="auto" Grid.ColumnSpan="2"/>
         <Canvas Name="CanvasSeperator" Grid.Column="4" Grid.Row="1" Background="LightGray"/>
         <Canvas Name="RightCanvas" Background="SlateGray" Grid.Column="5" Grid.Row="1" Height="auto"
-            MouseDown="RightCanvas_MouseDown" MouseUp="RightCanvas_MouseUp" MouseMove="RightCanvas_MouseMove" Grid.ColumnSpan="2"/>
+            MouseDown="RightCanvas_MouseDown" MouseUp="RightCanvas_MouseUp" MouseMove="RightCanvas_MouseMove" Grid.ColumnSpan="2" MouseLeave="RightCanvas_MouseLeave"/>
 
         <DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="7">
             <StatusBar DockPanel.Dock="Bottom" Name="StatusBar"  Background="LightGray">

+ 11 - 34
SketchAssistant/SketchAssistantWPF/MainWindow.xaml.cs

@@ -31,7 +31,7 @@ namespace SketchAssistantWPF
             //  DispatcherTimer setup
             dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
             dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
-            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
+            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 5);
             ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
                 new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
         }
@@ -74,8 +74,8 @@ namespace SketchAssistantWPF
         /// </summary>
         private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
         {
-            ProgramPresenter.Resize(new Tuple<int, int>((int) LeftCanvas.Width, (int) LeftCanvas.Height),
-                new Tuple<int, int>((int) RightCanvas.Width, (int) RightCanvas.Height));
+            ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
+                new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
         }
 
         /// <summary>
@@ -126,6 +126,14 @@ namespace SketchAssistantWPF
             ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
         }
 
+        /// <summary>
+        /// If the mouse leaves the canvas, it is treated as if the mouse was released.
+        /// </summary>
+        private void RightCanvas_MouseLeave(object sender, MouseEventArgs e)
+        {
+            ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
+        }
+
         /// <summary>
         /// Get current Mouse positon within the right picture box.
         /// </summary>
@@ -164,37 +172,6 @@ namespace SketchAssistantWPF
         /*** PRESENTER -> VIEW ***/
         /*************************/
 
-        /*
-    /// <summary>
-    /// Displays a list of lines in the left Picture box.
-    /// </summary>
-    /// <param name="img">The new image.</param>
-    public void DisplayInLeftPictureBox(List<InternalLine> lineList)
-    {
-        foreach (InternalLine line in lineList)
-        {
-            Polyline newPolyLine = new Polyline();
-            newPolyLine.Stroke = Brushes.Black;
-            newPolyLine.Points = line.GetPointCollection();
-            LeftCanvas.Children.Add(newPolyLine);
-        }
-    }
-
-    /// <summary>
-    /// Displays a list of lines in the right Picture box.
-    /// </summary>
-    /// <param name="img">The new image.</param>
-    public void DisplayInRightPictureBox(List<InternalLine> lineList)
-    {
-        foreach (InternalLine line in lineList)
-        {
-            Polyline newPolyLine = new Polyline();
-            newPolyLine.Stroke = Brushes.Black;
-            newPolyLine.Points = line.GetPointCollection();
-            LeftCanvas.Children.Add(newPolyLine);
-        }
-    }*/
-
         /// <summary>
         /// Remove the current line.
         /// </summary>