MVP_Presenter.cs 15 KB

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