MVP_Presenter.cs 17 KB

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