MVP_Presenter.cs 15 KB

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