MVP_Presenter.cs 18 KB

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