MVP_Presenter.cs 18 KB

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