MVP_Presenter.cs 17 KB

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