MVP_Presenter.cs 18 KB

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