MVP_Presenter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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-trough function to undo an action.
  107. /// </summary>
  108. public void Undo()
  109. {
  110. programModel.Undo();
  111. }
  112. /// <summary>
  113. /// Pass-trough function to redo an action.
  114. /// </summary>
  115. public void Redo()
  116. {
  117. programModel.Redo();
  118. }
  119. /// <summary>
  120. /// Pass-trough function for ticking the model.
  121. /// </summary>
  122. public void Tick()
  123. {
  124. programModel.Tick();
  125. }
  126. /// <summary>
  127. /// Checks if there is unsaved progress, and promts the model to generate a new canvas if not.
  128. /// </summary>
  129. public void NewCanvas()
  130. {
  131. var okToContinue = true;
  132. if (programModel.HasUnsavedProgress())
  133. {
  134. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  135. }
  136. if (okToContinue)
  137. {
  138. programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
  139. programModel.ResetRightImage();
  140. programModel.CanvasActivated();
  141. programModel.ChangeState(true);
  142. programView.EnableTimer();
  143. ClearRightLines();
  144. }
  145. }
  146. /// <summary>
  147. /// Pass-trough when the mouse is moved.
  148. /// </summary>
  149. /// <param name="mouseAction">The action which is sent by the View.</param>
  150. /// <param name="e">The Mouse event arguments.</param>
  151. public void MouseEvent(MouseAction mouseAction, Point position)
  152. {
  153. switch (mouseAction)
  154. {
  155. case MouseAction.Move:
  156. programModel.SetCurrentCursorPosition(position);
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. /// <summary>
  163. /// Pass-trough function that calls the correct Mouse event of the model, when the mouse is clicked.
  164. /// </summary>
  165. /// <param name="mouseAction">The action which is sent by the View.</param>
  166. /// <param name="e">The Mouse event arguments.</param>
  167. public void MouseEvent(MouseAction mouseAction)
  168. {
  169. switch (mouseAction)
  170. {
  171. case MouseAction.Click:
  172. programModel.MouseDown();
  173. programModel.Tick();
  174. programModel.MouseUp();
  175. break;
  176. case MouseAction.Down:
  177. programModel.MouseDown();
  178. break;
  179. case MouseAction.Up:
  180. programModel.MouseUp();
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. /************************************/
  187. /*** FUNCTIONS MODEL -> PRESENTER ***/
  188. /************************************/
  189. /// <summary>
  190. /// Return the position of the cursor
  191. /// </summary>
  192. /// <returns>The position of the cursor</returns>
  193. public Point GetCursorPosition()
  194. {
  195. return programView.GetCursorPosition();
  196. }
  197. /// <summary>
  198. /// Updates the currentline
  199. /// </summary>
  200. /// <param name="linepoints">The points of the current line.</param>
  201. public void UpdateCurrentLine(List<Point> linepoints)
  202. {
  203. Polyline currentLine = new Polyline();
  204. currentLine.Stroke = Brushes.Black;
  205. currentLine.Points = new PointCollection(linepoints);
  206. programView.DisplayCurrLine(currentLine);
  207. }
  208. /// <summary>
  209. /// Clears all Lines in the right canvas.
  210. /// </summary>
  211. public void ClearRightLines()
  212. {
  213. programView.RemoveAllRightLines();
  214. rightPolyLines = new Dictionary<int, Shape>();
  215. }
  216. /// <summary>
  217. /// A function to update the displayed lines in the right canvas.
  218. /// </summary>
  219. public void UpdateRightLines(List<Tuple<bool, InternalLine>> lines)
  220. {
  221. foreach(Tuple<bool, InternalLine> tup in lines)
  222. {
  223. var status = tup.Item1;
  224. var line = tup.Item2;
  225. if (!rightPolyLines.ContainsKey(line.GetID()))
  226. {
  227. if (!line.isPoint)
  228. {
  229. Polyline newLine = new Polyline();
  230. newLine.Points = line.GetPointCollection();
  231. rightPolyLines.Add(line.GetID(), newLine);
  232. programView.AddNewLineRight(newLine);
  233. }
  234. else
  235. {
  236. Ellipse newPoint = new Ellipse();
  237. newPoint.SetValue(Canvas.LeftProperty, line.point.X);
  238. newPoint.SetValue(Canvas.TopProperty, line.point.Y);
  239. rightPolyLines.Add(line.GetID(), newPoint);
  240. programView.AddNewPointRight(newPoint);
  241. }
  242. }
  243. SetVisibility(rightPolyLines[line.GetID()], status);
  244. }
  245. }
  246. /// <summary>
  247. /// A function to update the displayed lines in the left canvas.
  248. /// </summary>
  249. public void UpdateLeftLines(List<InternalLine> lines)
  250. {
  251. programView.RemoveAllLeftLines();
  252. foreach (InternalLine line in lines)
  253. {
  254. Polyline newLine = new Polyline();
  255. newLine.Stroke = Brushes.Black;
  256. newLine.Points = line.GetPointCollection();
  257. programView.AddNewLineLeft(newLine);
  258. }
  259. programView.SetCanvasState("LeftCanvas", true);
  260. programView.SetCanvasState("RightCanvas", true);
  261. }
  262. /// <summary>
  263. /// Called by the model when the state of the Program changes.
  264. /// Changes the look of the UI according to the current state of the model.
  265. /// </summary>
  266. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  267. /// <param name="canUndo">If actions in the model can be undone</param>
  268. /// <param name="canRedo">If actions in the model can be redone</param>
  269. /// <param name="canvasActive">If the right canvas is active</param>
  270. /// <param name="graphicLoaded">If an image is loaded in the model</param>
  271. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
  272. {
  273. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  274. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  275. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  276. if (canvasActive)
  277. {
  278. if (inDrawingMode)
  279. {
  280. dict["drawButton"] = MainWindow.ButtonState.Active;
  281. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  282. }
  283. else
  284. {
  285. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  286. dict["deleteButton"] = MainWindow.ButtonState.Active;
  287. }
  288. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  289. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  290. }
  291. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  292. {
  293. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  294. }
  295. programView.SetCanvasState("RightCanvas", canvasActive);
  296. programView.SetCanvasState("LeftCanvas", graphicLoaded);
  297. }
  298. /// <summary>
  299. /// Pass-trough function to display an info message in the view.
  300. /// </summary>
  301. /// <param name="msg">The message.</param>
  302. public void PassMessageToView(String msg)
  303. {
  304. programView.ShowInfoMessage(msg);
  305. }
  306. /// <summary>
  307. /// Pass-trough function to update the display of the last action taken.
  308. /// </summary>
  309. /// <param name="msg">The new last action taken.</param>
  310. public void PassLastActionTaken(String msg)
  311. {
  312. programView.SetLastActionTakenText(msg);
  313. }
  314. /// <summary>
  315. /// Passes whether or not the mouse is pressed.
  316. /// </summary>
  317. /// <returns>Whether or not the mouse is pressed</returns>
  318. public bool IsMousePressed()
  319. {
  320. return programView.IsMousePressed();
  321. }
  322. public void PassOptiTrackMessage(OptiTrack.Frame frame)
  323. {
  324. float x = frame.Trackables[0].X;
  325. float y = frame.Trackables[0].Y;
  326. float z = frame.Trackables[0].Z;
  327. MessageBox.Show(x.ToString());
  328. //programView.SetOptiTrackText("X: ");// + x + "Y: " + y + "Z: " + z);
  329. }
  330. /*************************/
  331. /*** HELPING FUNCTIONS ***/
  332. /*************************/
  333. /// <summary>
  334. /// Sets the visibility of a polyline.
  335. /// </summary>
  336. /// <param name="line">The polyline</param>
  337. /// <param name="visible">Whether or not it should be visible.</param>
  338. private void SetVisibility(Shape line, bool visible)
  339. {
  340. if (!visible)
  341. {
  342. line.Opacity = 0.00001;
  343. }
  344. else
  345. {
  346. line.Opacity = 1;
  347. }
  348. }
  349. /// <summary>
  350. /// A function that calculates the coordinates of a point on a zoomed in image.
  351. /// </summary>
  352. /// <param name="">The position of the mouse cursor</param>
  353. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  354. private Point ConvertCoordinates(Point cursorPosition)
  355. {
  356. if (!programModel.canvasActive) { return cursorPosition; }
  357. if (programModel.canvasActive && !programModel.graphicLoaded) { return cursorPosition; }
  358. ImageDimension rightImageDimensions = programModel.rightImageSize;
  359. Point realCoordinates = new Point(0, 0);
  360. int widthImage = rightImageDimensions.Width;
  361. int heightImage = rightImageDimensions.Height;
  362. int widthBox = programModel.rightImageBoxWidth;
  363. int heightBox = programModel.rightImageBoxHeight;
  364. if (heightImage == 0 && widthImage == 0)
  365. {
  366. return cursorPosition;
  367. }
  368. float imageRatio = (float)widthImage / (float)heightImage;
  369. float containerRatio = (float)widthBox / (float)heightBox;
  370. if (imageRatio >= containerRatio)
  371. {
  372. //Image is wider than it is high
  373. float zoomFactor = (float)widthImage / (float)widthBox;
  374. float scaledHeight = heightImage / zoomFactor;
  375. float filler = (heightBox - scaledHeight) / 2;
  376. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  377. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  378. }
  379. else
  380. {
  381. //Image is higher than it is wide
  382. float zoomFactor = (float)heightImage / (float)heightBox;
  383. float scaledWidth = widthImage / zoomFactor;
  384. float filler = (widthBox - scaledWidth) / 2;
  385. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  386. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  387. }
  388. return realCoordinates;
  389. }
  390. }
  391. }