MVP_Presenter.cs 18 KB

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