MVP_Presenter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. 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. /// Updates the currentline
  190. /// </summary>
  191. /// <param name="linepoints">The points of the current line.</param>
  192. public void UpdateCurrentLine(List<Point> linepoints)
  193. {
  194. Polyline currentLine = new Polyline();
  195. currentLine.Stroke = Brushes.Black;
  196. currentLine.Points = new PointCollection(linepoints);
  197. programView.DisplayCurrLine(currentLine);
  198. }
  199. /// <summary>
  200. /// Clears all Lines in the right canvas.
  201. /// </summary>
  202. public void ClearRightLines()
  203. {
  204. programView.RemoveAllRightLines();
  205. rightPolyLines = new Dictionary<int, Polyline>();
  206. }
  207. /// <summary>
  208. /// A function to update the displayed lines in the right canvas.
  209. /// </summary>
  210. public void UpdateRightLines(List<Tuple<bool, InternalLine>> lines)
  211. {
  212. foreach(Tuple<bool, InternalLine> tup in lines)
  213. {
  214. var status = tup.Item1;
  215. var line = tup.Item2;
  216. if (!rightPolyLines.ContainsKey(line.GetID()))
  217. {
  218. Polyline newLine = new Polyline();
  219. newLine.Points = line.GetPointCollection();
  220. rightPolyLines.Add(line.GetID(), newLine);
  221. programView.AddNewLineRight(newLine);
  222. }
  223. SetVisibility(rightPolyLines[line.GetID()], status);
  224. }
  225. }
  226. /// <summary>
  227. /// A function to update the displayed lines in the left canvas.
  228. /// </summary>
  229. public void UpdateLeftLines(List<InternalLine> lines)
  230. {
  231. programView.RemoveAllLeftLines();
  232. foreach (InternalLine line in lines)
  233. {
  234. Polyline newLine = new Polyline();
  235. newLine.Stroke = Brushes.Black;
  236. newLine.Points = line.GetPointCollection();
  237. programView.AddNewLineLeft(newLine);
  238. }
  239. programView.SetCanvasState("LeftCanvas", true);
  240. programView.SetCanvasState("RightCanvas", true);
  241. }
  242. /// <summary>
  243. /// Called by the model when the state of the Program changes.
  244. /// Changes the look of the UI according to the current state of the model.
  245. /// </summary>
  246. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  247. /// <param name="canUndo">If actions in the model can be undone</param>
  248. /// <param name="canRedo">If actions in the model can be redone</param>
  249. /// <param name="canvasActive">If the right canvas is active</param>
  250. /// <param name="graphicLoaded">If an image is loaded in the model</param>
  251. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
  252. {
  253. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  254. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  255. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  256. if (canvasActive)
  257. {
  258. if (inDrawingMode)
  259. {
  260. dict["drawButton"] = MainWindow.ButtonState.Active;
  261. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  262. }
  263. else
  264. {
  265. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  266. dict["deleteButton"] = MainWindow.ButtonState.Active;
  267. }
  268. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  269. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  270. }
  271. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  272. {
  273. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  274. }
  275. programView.SetCanvasState("RightCanvas", canvasActive);
  276. programView.SetCanvasState("LeftCanvas", graphicLoaded);
  277. }
  278. /// <summary>
  279. /// Pass-trough function to display an info message in the view.
  280. /// </summary>
  281. /// <param name="msg">The message.</param>
  282. public void PassMessageToView(String msg)
  283. {
  284. programView.ShowInfoMessage(msg);
  285. }
  286. /// <summary>
  287. /// Pass-trough function to update the display of the last action taken.
  288. /// </summary>
  289. /// <param name="msg">The new last action taken.</param>
  290. public void PassLastActionTaken(String msg)
  291. {
  292. programView.SetLastActionTakenText(msg);
  293. }
  294. /*************************/
  295. /*** HELPING FUNCTIONS ***/
  296. /*************************/
  297. /// <summary>
  298. /// Sets the visibility of a polyline.
  299. /// </summary>
  300. /// <param name="line">The polyline</param>
  301. /// <param name="visible">Whether or not it should be visible.</param>
  302. private void SetVisibility(Polyline line, bool visible)
  303. {
  304. if (!visible)
  305. {
  306. line.Opacity = 0.00001;
  307. }
  308. else
  309. {
  310. line.Opacity = 1;
  311. }
  312. }
  313. /// <summary>
  314. /// A function that calculates the coordinates of a point on a zoomed in image.
  315. /// </summary>
  316. /// <param name="">The position of the mouse cursor</param>
  317. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  318. private Point ConvertCoordinates(Point cursorPosition)
  319. {
  320. if (!programModel.canvasActive) { return cursorPosition; }
  321. if (programModel.canvasActive && !programModel.graphicLoaded) { return cursorPosition; }
  322. ImageDimension rightImageDimensions = programModel.rightImageSize;
  323. Point realCoordinates = new Point(0, 0);
  324. int widthImage = rightImageDimensions.Width;
  325. int heightImage = rightImageDimensions.Height;
  326. int widthBox = programModel.rightImageBoxWidth;
  327. int heightBox = programModel.rightImageBoxHeight;
  328. if (heightImage == 0 && widthImage == 0)
  329. {
  330. return cursorPosition;
  331. }
  332. float imageRatio = (float)widthImage / (float)heightImage;
  333. float containerRatio = (float)widthBox / (float)heightBox;
  334. if (imageRatio >= containerRatio)
  335. {
  336. //Image is wider than it is high
  337. float zoomFactor = (float)widthImage / (float)widthBox;
  338. float scaledHeight = heightImage / zoomFactor;
  339. float filler = (heightBox - scaledHeight) / 2;
  340. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  341. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  342. }
  343. else
  344. {
  345. //Image is higher than it is wide
  346. float zoomFactor = (float)heightImage / (float)heightBox;
  347. float scaledWidth = widthImage / zoomFactor;
  348. float filler = (widthBox - scaledWidth) / 2;
  349. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  350. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  351. }
  352. return realCoordinates;
  353. }
  354. }
  355. }