MVP_Presenter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Ink;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Shapes;
  12. namespace SketchAssistantWPF
  13. {
  14. public class MVP_Presenter
  15. {
  16. /// <summary>
  17. /// The View of the MVP-Model, in this case Form1.
  18. /// </summary>
  19. MVP_View programView;
  20. /// <summary>
  21. /// The Model of the MVP-Model.
  22. /// </summary>
  23. MVP_Model programModel;
  24. /// <summary>
  25. /// A dictionary connecting the id of an InternalLine with the respective Polyline in the right canvas.
  26. /// </summary>
  27. Dictionary<int, Shape> rightPolyLines;
  28. ImageDimension CanvasSizeLeft = new ImageDimension(0,0);
  29. ImageDimension CanvasSizeRight = new ImageDimension(0, 0);
  30. ImageDimension ImageSizeLeft = new ImageDimension(0, 0);
  31. ImageDimension ImageSizeRight = new ImageDimension(0, 0);
  32. List<double> ImageSimilarity = new List<double>();
  33. List<InternalLine> LeftLines = new List<InternalLine>();
  34. /*******************/
  35. /*** ENUMERATORS ***/
  36. /*******************/
  37. public enum MouseAction
  38. {
  39. Down,
  40. Up,
  41. Up_Invalid,
  42. Move
  43. }
  44. /***********************/
  45. /*** CLASS VARIABLES ***/
  46. /***********************/
  47. /// <summary>
  48. /// Instance of FileImporter to handle drawing imports.
  49. /// </summary>
  50. private FileImporter fileImporter;
  51. public MVP_Presenter(MVP_View form)
  52. {
  53. programView = form;
  54. programModel = new MVP_Model(this);
  55. //Initialize Class Variables
  56. fileImporter = new FileImporter();
  57. }
  58. /***********************************/
  59. /*** FUNCTIONS VIEW -> PRESENTER ***/
  60. /***********************************/
  61. /// <summary>
  62. /// Pass-trough function to update the appropriate information of the model, when the window is resized.
  63. /// </summary>
  64. /// <param name="leftPBS">The new size of the left picture box.</param>
  65. /// <param name="rightPBS">The new size of the left picture box.</param>
  66. public void Resize(Tuple<int, int> leftPBS, Tuple<int, int> rightPBS)
  67. {
  68. CanvasSizeLeft.ChangeDimension(leftPBS.Item1, leftPBS.Item2);
  69. CanvasSizeRight.ChangeDimension(rightPBS.Item1, rightPBS.Item2);
  70. //programModel.UpdateSizes(CanvasSizeRight);
  71. programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
  72. }
  73. /// <summary>
  74. /// Display a new FileDialog to a svg drawing.
  75. /// </summary>
  76. /// <returns>True if loading was a success</returns>
  77. public bool SVGToolStripMenuItemClick()
  78. {
  79. var okToContinue = true; bool returnval = false;
  80. if (programModel.HasUnsavedProgress())
  81. {
  82. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  83. }
  84. if (okToContinue)
  85. {
  86. var fileNameTup = programView.openNewDialog("Scalable Vector Graphics|*.svg");
  87. if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  88. {
  89. programView.SetToolStripLoadStatus(fileNameTup.Item2);
  90. try
  91. {
  92. Tuple<int, int, List<InternalLine>> values = fileImporter.ParseSVGInputFile(fileNameTup.Item1, programModel.leftImageBoxWidth, programModel.leftImageBoxHeight);
  93. values.Item3.ForEach(line => line.MakePermanent(0)); //Make all lines permanent
  94. programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
  95. programModel.ResetRightImage();
  96. programModel.CanvasActivated();
  97. programModel.ChangeState(true);
  98. programView.EnableTimer();
  99. ClearRightLines();
  100. returnval = true;
  101. }
  102. catch (FileImporterException ex)
  103. {
  104. programView.ShowInfoMessage(ex.ToString());
  105. }
  106. catch (Exception ex)
  107. {
  108. programView.ShowInfoMessage("exception occured while trying to parse svg file:\n\n" + ex.ToString() + "\n\n" + ex.StackTrace);
  109. }
  110. }
  111. }
  112. return returnval;
  113. }
  114. /// <summary>
  115. /// Pass-trough function to change the drawing state of the model.
  116. /// </summary>
  117. /// <param name="NowDrawing">Indicates if the program is in drawing (true) or deletion (false) mode.</param>
  118. public void ChangeState(bool NowDrawing)
  119. {
  120. programModel.ChangeState(NowDrawing);
  121. }
  122. /// <summary>
  123. /// Pass-trough function to undo an action.
  124. /// </summary>
  125. public void Undo()
  126. {
  127. programModel.Undo();
  128. }
  129. /// <summary>
  130. /// Pass-trough function to redo an action.
  131. /// </summary>
  132. public void Redo()
  133. {
  134. programModel.Redo();
  135. }
  136. /// <summary>
  137. /// Pass-trough function for ticking the model.
  138. /// </summary>
  139. public void Tick()
  140. {
  141. programModel.Tick();
  142. }
  143. /// <summary>
  144. /// Checks if there is unsaved progress, and promts the model to generate a new canvas if not.
  145. /// </summary>
  146. public void NewCanvas()
  147. {
  148. var okToContinue = true;
  149. if (programModel.HasUnsavedProgress())
  150. {
  151. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  152. }
  153. if (okToContinue)
  154. {
  155. programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
  156. programModel.ResetRightImage();
  157. programModel.CanvasActivated();
  158. programModel.ChangeState(true);
  159. programView.EnableTimer();
  160. ClearRightLines();
  161. }
  162. }
  163. /// <summary>
  164. /// Pass-trough when the mouse is moved.
  165. /// </summary>
  166. /// <param name="mouseAction">The action which is sent by the View.</param>
  167. /// <param name="e">The Mouse event arguments.</param>
  168. public void MouseEvent(MouseAction mouseAction, Point position)
  169. {
  170. switch (mouseAction)
  171. {
  172. case MouseAction.Move:
  173. programModel.SetCurrentCursorPosition(position);
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. /// <summary>
  180. /// Pass-trough function that calls the correct Mouse event of the model, when the mouse is clicked.
  181. /// </summary>
  182. /// <param name="mouseAction">The action which is sent by the View.</param>
  183. /// <param name="strokes">The Strokes.</param>
  184. public void MouseEvent(MouseAction mouseAction, StrokeCollection strokes)
  185. {
  186. switch (mouseAction)
  187. {
  188. case MouseAction.Down:
  189. programModel.MouseDown();
  190. break;
  191. case MouseAction.Up:
  192. if(strokes.Count > 0)
  193. {
  194. StylusPointCollection sPoints = strokes.First().StylusPoints;
  195. List<Point> points = new List<Point>();
  196. foreach(StylusPoint p in sPoints)
  197. points.Add(new Point(p.X, p.Y));
  198. programModel.MouseUp(points);
  199. }
  200. else
  201. {
  202. programModel.MouseUp(true);
  203. }
  204. break;
  205. case MouseAction.Up_Invalid:
  206. programModel.MouseUp(false);
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. /************************************/
  213. /*** FUNCTIONS MODEL -> PRESENTER ***/
  214. /************************************/
  215. /// <summary>
  216. /// Return the position of the cursor
  217. /// </summary>
  218. /// <returns>The position of the cursor</returns>
  219. public Point GetCursorPosition()
  220. {
  221. return programView.GetCursorPosition();
  222. }
  223. /// <summary>
  224. /// Clears all Lines in the right canvas.
  225. /// </summary>
  226. public void ClearRightLines()
  227. {
  228. programView.RemoveAllRightLines();
  229. rightPolyLines = new Dictionary<int, Shape>();
  230. //Reset the similarity display
  231. UpdateSimilarityScore(Double.NaN);
  232. }
  233. /// <summary>
  234. /// A function to update the displayed lines in the right canvas.
  235. /// </summary>
  236. public void UpdateRightLines(List<Tuple<bool, InternalLine>> lines)
  237. {
  238. foreach(Tuple<bool, InternalLine> tup in lines)
  239. {
  240. var status = tup.Item1;
  241. var line = tup.Item2;
  242. if (!rightPolyLines.ContainsKey(line.GetID()))
  243. {
  244. if (!line.isPoint)
  245. {
  246. Polyline newLine = new Polyline();
  247. newLine.Points = line.GetPointCollection();
  248. rightPolyLines.Add(line.GetID(), newLine);
  249. programView.AddNewLineRight(newLine);
  250. }
  251. else
  252. {
  253. Ellipse newPoint = new Ellipse();
  254. rightPolyLines.Add(line.GetID(), newPoint);
  255. programView.AddNewPointRight(newPoint, line);
  256. }
  257. }
  258. SetVisibility(rightPolyLines[line.GetID()], status);
  259. }
  260. //Calculate similarity scores
  261. UpdateSimilarityScore(Double.NaN); var templist = lines.Where(tup => tup.Item1).ToList();
  262. if(LeftLines.Count > 0)
  263. {
  264. for(int i = 0; i < LeftLines.Count; i++)
  265. {
  266. if (templist.Count == i) break;
  267. UpdateSimilarityScore(GeometryCalculator.CalculateSimilarity(templist[i].Item2, LeftLines[i]));
  268. }
  269. }
  270. else if(templist.Count > 1)
  271. {
  272. UpdateSimilarityScore(GeometryCalculator.CalculateSimilarity(templist[templist.Count-2].Item2, templist[templist.Count-1].Item2));
  273. }
  274. }
  275. /*
  276. /// <summary>
  277. /// Updates the currentline
  278. /// </summary>
  279. /// <param name="linepoints">The points of the current line.</param>
  280. public void UpdateCurrentLine(List<Point> linepoints)
  281. {
  282. Polyline currentLine = new Polyline();
  283. currentLine.Stroke = Brushes.Black;
  284. currentLine.Points = new PointCollection(linepoints);
  285. programView.DisplayCurrLine(currentLine);
  286. }
  287. */
  288. /// <summary>
  289. /// A function to update the displayed lines in the left canvas.
  290. /// </summary>
  291. public void UpdateLeftLines(List<InternalLine> lines)
  292. {
  293. programView.RemoveAllLeftLines();
  294. foreach (InternalLine line in lines)
  295. {
  296. Polyline newLine = new Polyline();
  297. newLine.Stroke = Brushes.Black;
  298. newLine.Points = line.GetPointCollection();
  299. programView.AddNewLineLeft(newLine);
  300. }
  301. programView.SetCanvasState("LeftCanvas", true);
  302. programView.SetCanvasState("RightCanvas", true);
  303. LeftLines = lines;
  304. }
  305. /// <summary>
  306. /// Called by the model when the state of the Program changes.
  307. /// Changes the look of the UI according to the current state of the model.
  308. /// </summary>
  309. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  310. /// <param name="canUndo">If actions in the model can be undone</param>
  311. /// <param name="canRedo">If actions in the model can be redone</param>
  312. /// <param name="canvasActive">If the right canvas is active</param>
  313. /// <param name="graphicLoaded">If an image is loaded in the model</param>
  314. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
  315. {
  316. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  317. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  318. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  319. if (canvasActive)
  320. {
  321. if (inDrawingMode)
  322. {
  323. dict["drawButton"] = MainWindow.ButtonState.Active;
  324. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  325. }
  326. else
  327. {
  328. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  329. dict["deleteButton"] = MainWindow.ButtonState.Active;
  330. }
  331. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  332. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  333. }
  334. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  335. {
  336. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  337. }
  338. programView.SetCanvasState("RightCanvas", canvasActive);
  339. programView.SetCanvasState("LeftCanvas", graphicLoaded);
  340. }
  341. /// <summary>
  342. /// Pass-trough function to display an info message in the view.
  343. /// </summary>
  344. /// <param name="msg">The message.</param>
  345. public void PassMessageToView(String msg)
  346. {
  347. programView.ShowInfoMessage(msg);
  348. }
  349. /// <summary>
  350. /// Pass-trough function to update the display of the last action taken.
  351. /// </summary>
  352. /// <param name="msg">The new last action taken.</param>
  353. public void PassLastActionTaken(String msg)
  354. {
  355. programView.SetLastActionTakenText(msg);
  356. }
  357. /// <summary>
  358. /// Passes whether or not the mouse is pressed.
  359. /// </summary>
  360. /// <returns>Whether or not the mouse is pressed</returns>
  361. public bool IsMousePressed()
  362. {
  363. return programView.IsMousePressed();
  364. }
  365. /// <summary>
  366. ///
  367. /// </summary>
  368. /// <param name="score">Score will be reset if NaN is passed,
  369. /// will be ignored if the score is not between 0 and 1</param>
  370. public void UpdateSimilarityScore(double score)
  371. {
  372. if (Double.IsNaN(score))
  373. {
  374. ImageSimilarity.Clear();
  375. programView.SetImageSimilarityText("");
  376. }
  377. else
  378. {
  379. if (score >= 0 && score <= 1) ImageSimilarity.Add(score);
  380. programView.SetImageSimilarityText((ImageSimilarity.Sum() / ImageSimilarity.Count).ToString());
  381. }
  382. }
  383. /*************************/
  384. /*** HELPING FUNCTIONS ***/
  385. /*************************/
  386. /// <summary>
  387. /// Sets the visibility of a polyline.
  388. /// </summary>
  389. /// <param name="line">The polyline</param>
  390. /// <param name="visible">Whether or not it should be visible.</param>
  391. private void SetVisibility(Shape line, bool visible)
  392. {
  393. if (!visible)
  394. {
  395. line.Opacity = 0.00001;
  396. }
  397. else
  398. {
  399. line.Opacity = 1;
  400. }
  401. }
  402. }
  403. }