MVP_Presenter.cs 15 KB

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