MVP_Presenter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. /// <summary>
  276. /// Updates the currentline
  277. /// </summary>
  278. /// <param name="linepoints">The points of the current line.</param>
  279. public void UpdateCurrentLine(List<Point> linepoints)
  280. {
  281. Polyline currentLine = new Polyline();
  282. currentLine.Stroke = Brushes.Black;
  283. currentLine.Points = new PointCollection(linepoints);
  284. programView.DisplayCurrLine(currentLine);
  285. }
  286. /// <summary>
  287. /// A function to update the displayed lines in the left canvas.
  288. /// </summary>
  289. public void UpdateLeftLines(List<InternalLine> lines)
  290. {
  291. programView.RemoveAllLeftLines();
  292. foreach (InternalLine line in lines)
  293. {
  294. Polyline newLine = new Polyline();
  295. newLine.Stroke = Brushes.Black;
  296. newLine.Points = line.GetPointCollection();
  297. programView.AddNewLineLeft(newLine);
  298. }
  299. programView.SetCanvasState("LeftCanvas", true);
  300. programView.SetCanvasState("RightCanvas", true);
  301. LeftLines = lines;
  302. }
  303. /// <summary>
  304. /// Called by the model when the state of the Program changes.
  305. /// Changes the look of the UI according to the current state of the model.
  306. /// </summary>
  307. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  308. /// <param name="canUndo">If actions in the model can be undone</param>
  309. /// <param name="canRedo">If actions in the model can be redone</param>
  310. /// <param name="canvasActive">If the right canvas is active</param>
  311. /// <param name="graphicLoaded">If an image is loaded in the model</param>
  312. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
  313. {
  314. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  315. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  316. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  317. if (canvasActive)
  318. {
  319. if (inDrawingMode)
  320. {
  321. dict["drawButton"] = MainWindow.ButtonState.Active;
  322. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  323. }
  324. else
  325. {
  326. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  327. dict["deleteButton"] = MainWindow.ButtonState.Active;
  328. }
  329. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  330. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  331. }
  332. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  333. {
  334. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  335. }
  336. programView.SetCanvasState("RightCanvas", canvasActive);
  337. programView.SetCanvasState("LeftCanvas", graphicLoaded);
  338. }
  339. /// <summary>
  340. /// Pass-trough function to display an info message in the view.
  341. /// </summary>
  342. /// <param name="msg">The message.</param>
  343. public void PassMessageToView(String msg)
  344. {
  345. programView.ShowInfoMessage(msg);
  346. }
  347. /// <summary>
  348. /// Pass-trough function to update the display of the last action taken.
  349. /// </summary>
  350. /// <param name="msg">The new last action taken.</param>
  351. public void PassLastActionTaken(String msg)
  352. {
  353. programView.SetLastActionTakenText(msg);
  354. }
  355. /// <summary>
  356. /// Passes whether or not the mouse is pressed.
  357. /// </summary>
  358. /// <returns>Whether or not the mouse is pressed</returns>
  359. public bool IsMousePressed()
  360. {
  361. return programView.IsMousePressed();
  362. }
  363. /// <summary>
  364. /// Update the similarity score displayed in the UI.
  365. /// </summary>
  366. /// <param name="score">Score will be reset if NaN is passed,
  367. /// will be ignored if the score is not between 0 and 1</param>
  368. public void UpdateSimilarityScore(double score)
  369. {
  370. if (Double.IsNaN(score))
  371. {
  372. ImageSimilarity.Clear();
  373. programView.SetImageSimilarityText("");
  374. }
  375. else
  376. {
  377. if (score >= 0 && score <= 1) ImageSimilarity.Add(score);
  378. programView.SetImageSimilarityText((ImageSimilarity.Sum() / ImageSimilarity.Count).ToString());
  379. }
  380. }
  381. /// <summary>
  382. /// Change the properties of an overlay item.
  383. /// </summary>
  384. /// <param name="name">The name of the item in the overlay dictionary</param>
  385. /// <param name="visible">If the element should be visible</param>
  386. /// <param name="position">The new position of the element</param>
  387. public void SetOverlayStatus(String name, bool visible, Point position)
  388. {
  389. Shape shape = new Ellipse(); double visibility = 1; double xDif = 0; double yDif = 0;
  390. switch (name)
  391. {
  392. case "optipoint":
  393. shape = ((MainWindow)programView).OverlayDictionary["optipoint"];
  394. visibility = 0.5; xDif = 2.5; yDif = 2.5;
  395. break;
  396. case "startpoint":
  397. shape = ((MainWindow)programView).OverlayDictionary["startpoint"];
  398. xDif = ((MainWindow)programView).markerRadius; yDif = xDif;
  399. break;
  400. default:
  401. Console.WriteLine("Unknown Overlay Item. Please check in MVP_Presenter if this item exists.");
  402. return;
  403. }
  404. if (visible) shape.Opacity = visibility;
  405. else shape.Opacity = 0.00001;
  406. Point point = ConvertRightCanvasCoordinateToOverlay(position);
  407. shape.Margin = new Thickness(point.X - xDif, point.Y - yDif, 0, 0);
  408. }
  409. /// <summary>
  410. /// Change the properties of an overlay item.
  411. /// </summary>
  412. /// <param name="name">The name of the item in the overlay dictionary</param>
  413. /// <param name="visible">If the element should be visible</param>
  414. /// <param name="pos1">The new position of the element</param>
  415. /// <param name="pos2">The new position of the element</param>
  416. public void SetOverlayStatus(String name, bool visible, Point pos1, Point pos2)
  417. {
  418. Shape shape = new Ellipse();
  419. switch (name.Substring(0,7))
  420. {
  421. case "dotLine":
  422. if (((MainWindow)programView).OverlayDictionary.ContainsKey(name))
  423. { shape = ((MainWindow)programView).OverlayDictionary[name];
  424. break;
  425. }
  426. goto default;
  427. default:
  428. SetOverlayStatus(name, visible, pos1);
  429. return;
  430. }
  431. if (visible) shape.Opacity = 1;
  432. else shape.Opacity = 0.00001;
  433. Point p1 = ConvertRightCanvasCoordinateToOverlay(pos1); Point p2 = ConvertRightCanvasCoordinateToOverlay(pos2);
  434. ((Line)shape).X1 = p1.X; ((Line)shape).X2 = p2.X; ((Line)shape).Y1 = p1.Y; ((Line)shape).Y2 = p2.Y;
  435. }
  436. /// <summary>
  437. /// Move the optitrack pointer to a new position.
  438. /// </summary>
  439. /// <param name="position">Position relative to the Rightcanvas</param>
  440. public void MoveOptiPoint(Point position)
  441. {
  442. Point point = ConvertRightCanvasCoordinateToOverlay(position);
  443. Shape shape = ((MainWindow)programView).OverlayDictionary["optipoint"];
  444. shape.Margin = new Thickness(point.X - 2.5, point.Y - 2.5, 0, 0);
  445. }
  446. /*************************/
  447. /*** HELPING FUNCTIONS ***/
  448. /*************************/
  449. /// <summary>
  450. /// Convert point relative to the right canvas to a point relative to the overlay canvas.
  451. /// </summary>
  452. /// <param name="p">The point to convert.</param>
  453. /// <returns>The respective overlay canvas.</returns>
  454. private Point ConvertRightCanvasCoordinateToOverlay(Point p)
  455. {
  456. MainWindow main = (MainWindow)programView;
  457. double xDif = (main.CanvasLeftEdge.ActualWidth + main.LeftCanvas.ActualWidth + main.CanvasSeperator.ActualWidth);
  458. double yDif = (main.ButtonToolbar.ActualHeight);
  459. return new Point(p.X + xDif,p.Y + yDif);
  460. }
  461. /// <summary>
  462. /// Sets the visibility of a polyline.
  463. /// </summary>
  464. /// <param name="line">The polyline</param>
  465. /// <param name="visible">Whether or not it should be visible.</param>
  466. private void SetVisibility(Shape line, bool visible)
  467. {
  468. if (!visible)
  469. {
  470. line.Opacity = 0.00001;
  471. }
  472. else
  473. {
  474. line.Opacity = 1;
  475. }
  476. }
  477. }
  478. }