Browse Source

Drawing now works, and it's even better than before

- Sadly you cannot delete stuff yet :(
Martin Edlund 5 years ago
parent
commit
60122ede68

+ 11 - 0
SketchAssistant/SketchAssistantWPF/ActionHistory.cs

@@ -20,6 +20,17 @@ namespace SketchAssistantWPF
             AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1));
         }
 
+        /// <summary>
+        /// Resets the action history to its initial state.
+        /// </summary>
+        /// <returns>The new Last Action taken.</returns>
+        public String Reset()
+        {
+            actionHistory.Clear();
+            currentAction = new Tuple<int, SketchAction>(-1, null);
+            return AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1));
+        }
+
         /// <summary>
         /// Adds a new action to the action history.
         /// </summary>

+ 31 - 7
SketchAssistant/SketchAssistantWPF/MVP_Model.cs

@@ -93,8 +93,14 @@ namespace SketchAssistantWPF
         public ImageDimension leftImageSize { get; private set; }
 
         public ImageDimension rightImageSize { get; private set; }
-
+        /// <summary>
+        /// Indicates whether or not the canvas on the right side is active.
+        /// </summary>
         public bool canvasActive {get; set;}
+        /// <summary>
+        /// Indicates if there is a graphic loaded in the left canvas.
+        /// </summary>
+        public bool graphicLoaded { get; set; }
 
         //Images
         Image leftImage;
@@ -107,7 +113,7 @@ namespace SketchAssistantWPF
 
         List<Tuple<bool, InternalLine>> rightLineList;
 
-        List<Point> currentLine;
+        List<Point> currentLine = new List<Point>();
 
 
 
@@ -119,6 +125,9 @@ namespace SketchAssistantWPF
             //overlayItems = new List<Tuple<bool, HashSet<Point>>>();
             rightLineList = new List<Tuple<bool, InternalLine>>();
             canvasActive = false;
+            UpdateUI();
+            rightImageSize = new ImageDimension(0, 0);
+            leftImageSize = new ImageDimension(0, 0);
         }
 
         /**************************/
@@ -147,7 +156,7 @@ namespace SketchAssistantWPF
         /// </summary>
         private void RepopulateDeletionMatrixes()
         {
-            if (rightImageWithoutOverlay != null)
+            if (canvasActive)
             {
                 isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
                 linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
@@ -166,7 +175,7 @@ namespace SketchAssistantWPF
         /// </summary>
         private void UpdateUI()
         {
-            programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive);
+            programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded);
         }
 
         /// <summary>
@@ -202,7 +211,8 @@ namespace SketchAssistantWPF
         /// </summary>
         public void ResetRightImage()
         {
-            rightLineList = new List<Tuple<bool, InternalLine>>();
+            rightLineList.Clear();
+            programPresenter.PassLastActionTaken(historyOfActions.Reset());
             programPresenter.ClearRightLines();
         }
 
@@ -217,7 +227,9 @@ namespace SketchAssistantWPF
             leftImageSize = new ImageDimension(width, height);
             rightImageSize = new ImageDimension(width, height);
             leftLineList = listOfLines;
+            graphicLoaded = true;
             programPresenter.UpdateLeftLines(leftLineList);
+            CanvasActivated();
             /*
             var workingCanvas = GetEmptyCanvas(width, height);
             var workingGraph = Graphics.FromImage(workingCanvas);
@@ -237,6 +249,16 @@ namespace SketchAssistantWPF
             */
         }
 
+        /// <summary>
+        /// A function to tell the model a new canvas was activated.
+        /// </summary>
+        public void CanvasActivated()
+        {
+            canvasActive = true;
+            RepopulateDeletionMatrixes();
+            UpdateUI();
+        }
+
         /// <summary>
         /// Will undo the last action taken, if the action history allows it.
         /// </summary>
@@ -324,7 +346,7 @@ namespace SketchAssistantWPF
             mousePressed = true;
             if (inDrawingMode)
             {
-                currentLine = new List<Point>();
+                currentLine.Clear();
             }
         }
 
@@ -342,6 +364,8 @@ namespace SketchAssistantWPF
                 programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
                 //TODO: Add check if overlay needs to be added
                 programPresenter.UpdateRightLines(rightLineList);
+                currentLine.Clear();
+                programPresenter.UpdateCurrentLine(currentLine);
             }
             UpdateUI();
         }
@@ -421,7 +445,7 @@ namespace SketchAssistantWPF
         {
             return !historyOfActions.IsEmpty();
         }
-
+    
         /**************************/
         /*** INTERNAL FUNCTIONS ***/
         /**************************/

+ 15 - 13
SketchAssistant/SketchAssistantWPF/MVP_Presenter.cs

@@ -25,10 +25,6 @@ namespace SketchAssistantWPF
         /// A dictionary connecting the id of an InternalLine with the respective Polyline in the right canvas.
         /// </summary>
         Dictionary<int, Polyline> rightPolyLines;
-        /// <summary>
-        /// A dictionary connecting the id of an InternalLine with the respective Polyline in the left canvas.
-        /// </summary>
-        Dictionary<int, Polyline> leftPolyLines;
 
         ImageDimension CanvasSizeLeft = new ImageDimension(0,0);
 
@@ -109,6 +105,7 @@ namespace SketchAssistantWPF
                     Tuple<int, int, List<InternalLine>> values = fileImporter.ParseISADInputFile(fileNameTup.Item1);
                     programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
                     programModel.ChangeState(true);
+                    programModel.CanvasActivated();
                     programView.EnableTimer();
                 }
             }
@@ -159,9 +156,11 @@ namespace SketchAssistantWPF
             }
             if (okToContinue)
             {
-                programModel.canvasActive = true;
+                programModel.ResetRightImage();
+                programModel.CanvasActivated();
                 programModel.ChangeState(true);
                 programView.EnableTimer();
+                ClearRightLines();
             }
         }
 
@@ -244,7 +243,6 @@ namespace SketchAssistantWPF
                 if (!rightPolyLines.ContainsKey(line.GetID()))
                 {
                     Polyline newLine = new Polyline();
-                    newLine.Stroke = Brushes.Black;
                     newLine.Points = line.GetPointCollection();
                     rightPolyLines.Add(line.GetID(), newLine);
                     programView.AddNewLineRight(newLine);
@@ -259,15 +257,15 @@ namespace SketchAssistantWPF
         public void UpdateLeftLines(List<InternalLine> lines)
         {
             programView.RemoveAllLeftLines();
-            leftPolyLines = new Dictionary<int, Polyline>();
-            foreach(InternalLine line in lines)
+            foreach (InternalLine line in lines)
             {
                 Polyline newLine = new Polyline();
                 newLine.Stroke = Brushes.Black;
                 newLine.Points = line.GetPointCollection();
-                leftPolyLines.Add(line.GetID(), newLine);
                 programView.AddNewLineLeft(newLine);
             }
+            programView.SetCanvasState("LeftCanvas", true);
+            programView.SetCanvasState("RightCanvas", true);
         }
 
         /// <summary>
@@ -277,14 +275,15 @@ namespace SketchAssistantWPF
         /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
         /// <param name="canUndo">If actions in the model can be undone</param>
         /// <param name="canRedo">If actions in the model can be redone</param>
-        /// <param name="imageLoaded">If an image is loaded in the model</param>
-        public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
+        /// <param name="canvasActive">If the right canvas is active</param>
+        /// <param name="graphicLoaded">If an image is loaded in the model</param>
+        public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
         {
             Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
                 {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
                 {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton",  MainWindow.ButtonState.Disabled}};
 
-            if (imageLoaded)
+            if (canvasActive)
             {
                 if (inDrawingMode)
                 {
@@ -303,6 +302,8 @@ namespace SketchAssistantWPF
             {
                 programView.SetToolStripButtonStatus(entry.Key, entry.Value);
             }
+            programView.SetCanvasState("RightCanvas", canvasActive);
+            programView.SetCanvasState("LeftCanvas", graphicLoaded);
         }
 
 
@@ -354,7 +355,7 @@ namespace SketchAssistantWPF
         /// <param name="visible">Whether or not it should be visible.</param>
         private void SetVisibility(Polyline line, bool visible)
         {
-            if (visible)
+            if (!visible)
             {
                 line.Opacity = 0.00001;
             }
@@ -372,6 +373,7 @@ namespace SketchAssistantWPF
         private Point ConvertCoordinates(Point cursorPosition)
         {
             if (!programModel.canvasActive) { return cursorPosition; }
+            if (programModel.canvasActive && !programModel.graphicLoaded) { return cursorPosition; }
             ImageDimension rightImageDimensions = programModel.rightImageSize;
             Point realCoordinates = new Point(0, 0);
 

+ 6 - 0
SketchAssistant/SketchAssistantWPF/MVP_View.cs

@@ -10,6 +10,12 @@ namespace SketchAssistantWPF
 {
     public interface MVP_View
     {
+        /// <summary>
+        /// Updates the colour of a canvas.
+        /// </summary>
+        /// <param name="canvasName">The name of the canvas to be updated.</param>
+        /// <param name="active">Whether or not the canvas is active.</param>
+        void SetCanvasState(string canvasName, bool active);
 
         /// <summary>
         /// Remove the current line.

+ 13 - 12
SketchAssistant/SketchAssistantWPF/MainWindow.xaml

@@ -10,10 +10,11 @@
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="auto"/>
             <ColumnDefinition Width="auto"/>
-            <ColumnDefinition Width="450*"/>
+            <ColumnDefinition Width="*"/>
+            <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="5"/>
-            <ColumnDefinition Width="290*"/>
-            <ColumnDefinition Width="161*"/>
+            <ColumnDefinition Width="*"/>
+            <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="auto"/>
             <ColumnDefinition Width="auto"/>
         </Grid.ColumnDefinitions>
@@ -22,10 +23,10 @@
             <RowDefinition Height="*"/>
             <RowDefinition Height="auto"/>
         </Grid.RowDefinitions>
-        <ToolBar x:Name="MenuToolbar" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0">
-            <Menu>
+        <ToolBar x:Name="MenuToolbar" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="0" Background="LightGray">
+            <Menu Background="LightGray">
                 <MenuItem x:Name="LoadMenuButton" Header="Load">
-                    <MenuItem Header="Load SVG File"/>
+                    <MenuItem Header="Load SVG File" Click="MenuItem_Click"/>
                 </MenuItem>
                 <MenuItem x:Name="EditMenuButton" Header="Edit">
                     <MenuItem Header="New Canvas" Click="CanvasButton_Click"/>
@@ -35,7 +36,7 @@
             </Menu>
         </ToolBar>
         <!-- All Icons in the Toolbar taken from openclipart.org -->
-        <ToolBar x:Name="DrawingToolBar" Grid.Column="4" Grid.Row="0" Grid.ColumnSpan="2">
+        <ToolBar x:Name="DrawingToolBar" Grid.Column="5" Grid.Row="0" Grid.ColumnSpan="2" Background="LightGray">
             <Button x:Name="CanvasButton" ToolTip="Create a new Canvas" Click="CanvasButton_Click">
                 <Rectangle Width="30" Height="30">
                     <Rectangle.Fill>
@@ -169,12 +170,12 @@
                 </Rectangle>
             </Button>
         </ToolBar>
-        <Canvas Name="LeftCanvas" Background="SlateGray" Grid.Column="2" Grid.Row="1" Height="auto"/>
+        <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"/>
 
-        <Canvas Name="RightCanvas" Background="SlateGray" Grid.Column="4" Grid.Row="1" Height="auto"
-                    MouseDown="RightCanvas_MouseDown" MouseUp="RightCanvas_MouseUp" MouseMove="RightCanvas_MouseMove" Grid.ColumnSpan="2"/>
-
-        <DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="6">
+        <DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="7">
             <StatusBar DockPanel.Dock="Bottom" Name="StatusBar"  Background="LightGray">
                 <TextBox Name="LoadStatusBox" Text="nothing loaded" Background="LightGray"/>
                 <Separator/>

+ 80 - 29
SketchAssistant/SketchAssistantWPF/MainWindow.xaml.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Timers;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
@@ -28,9 +29,11 @@ namespace SketchAssistantWPF
             InitializeComponent();
             ProgramPresenter = new MVP_Presenter(this);
             //  DispatcherTimer setup
-            dispatcherTimer = new DispatcherTimer();
+            dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
             dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
-            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 33);
+            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
+            ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
+                new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
         }
 
         public enum ButtonState
@@ -149,40 +152,48 @@ namespace SketchAssistantWPF
             ProgramPresenter.Tick();
         }
 
+        /// <summary>
+        /// Import button, will open an OpenFileDialog
+        /// </summary>
+        private void MenuItem_Click(object sender, RoutedEventArgs e)
+        {
+            ProgramPresenter.ExamplePictureToolStripMenuItemClick();
+        }
+
         /*************************/
         /*** 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)
+
+        /*
+    /// <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)
         {
-            foreach (InternalLine line in lineList)
-            {
-                Polyline newPolyLine = new Polyline();
-                newPolyLine.Stroke = Brushes.Black;
-                newPolyLine.Points = line.GetPointCollection();
-                LeftCanvas.Children.Add(newPolyLine);
-            }
+            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)
+    /// <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)
         {
-            foreach (InternalLine line in lineList)
-            {
-                Polyline newPolyLine = new Polyline();
-                newPolyLine.Stroke = Brushes.Black;
-                newPolyLine.Points = line.GetPointCollection();
-                LeftCanvas.Children.Add(newPolyLine);
-            }
-        }*/
+            Polyline newPolyLine = new Polyline();
+            newPolyLine.Stroke = Brushes.Black;
+            newPolyLine.Points = line.GetPointCollection();
+            LeftCanvas.Children.Add(newPolyLine);
+        }
+    }*/
 
         /// <summary>
         /// Remove the current line.
@@ -228,6 +239,8 @@ namespace SketchAssistantWPF
         /// <param name="newLine">The new Polyline to be added displayed.</param>
         public void AddNewLineLeft(Polyline newLine)
         {
+            newLine.Stroke = Brushes.Black;
+            newLine.StrokeThickness = 2;
             LeftCanvas.Children.Add(newLine);
         }
 
@@ -237,6 +250,8 @@ namespace SketchAssistantWPF
         /// <param name="newLine">The new Polyline to be added displayed.</param>
         public void AddNewLineRight(Polyline newLine)
         {
+            newLine.Stroke = Brushes.Black;
+            newLine.StrokeThickness = 2;
             RightCanvas.Children.Add(newLine);
         }
 
@@ -315,14 +330,20 @@ namespace SketchAssistantWPF
                     case ButtonState.Active:
                         ((ToggleButton)buttonToChange).IsEnabled = true;
                         ((ToggleButton)buttonToChange).IsChecked = true;
+                        ((ToggleButton)buttonToChange).Opacity = 1;
+                        ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
                         break;
                     case ButtonState.Disabled:
                         ((ToggleButton)buttonToChange).IsEnabled = false;
                         ((ToggleButton)buttonToChange).IsChecked = false;
+                        ((ToggleButton)buttonToChange).Opacity = 0.5;
+                        ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
                         break;
                     case ButtonState.Enabled:
                         ((ToggleButton)buttonToChange).IsEnabled = true;
                         ((ToggleButton)buttonToChange).IsChecked = false;
+                        ((ToggleButton)buttonToChange).Opacity = 1;
+                        ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
                         break;
                 }
             }
@@ -332,9 +353,11 @@ namespace SketchAssistantWPF
                 {
                     case ButtonState.Disabled:
                         ((Button)buttonToChange).IsEnabled = false;
+                        ((Button)buttonToChange).Opacity = 0.5;
                         break;
                     default:
                         ((Button)buttonToChange).IsEnabled = true;
+                        ((Button)buttonToChange).Opacity = 1;
                         break;
                 }
             }
@@ -368,5 +391,33 @@ namespace SketchAssistantWPF
             MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
             return (result.Equals(MessageBoxResult.Yes));
         }
+
+        /// <summary>
+        /// Updates the colour of a canvas.
+        /// </summary>
+        /// <param name="canvasName">The name of the canvas to be updated.</param>
+        /// <param name="active">Whether or not the canvas is active.</param>
+        public void SetCanvasState(string canvasName, bool active)
+        {
+            Canvas canvas;
+            switch (canvasName){
+                case ("LeftCanvas"):
+                    canvas = LeftCanvas;
+                    break;
+                case ("RightCanvas"):
+                    canvas = RightCanvas;
+                    break;
+                default:
+                    throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
+            }
+            if (active)
+            {
+                canvas.Background = Brushes.White;
+            }
+            else
+            {
+                canvas.Background = Brushes.SlateGray;
+            }
+        }
     }
 }