MVP_Model.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.Drawing;
  7. namespace SketchAssistant
  8. {
  9. public class MVP_Model
  10. {
  11. /// <summary>
  12. /// The Presenter of the MVP-Model.
  13. /// </summary>
  14. MVP_Presenter programPresenter;
  15. /// <summary>
  16. /// History of Actions
  17. /// </summary>
  18. ActionHistory historyOfActions;
  19. /// <summary>
  20. /// The assistant responsible for the redraw mode
  21. /// </summary>
  22. RedrawAssistant redrawAss;
  23. /*******************/
  24. /*** ENUMERATORS ***/
  25. /*******************/
  26. /***********************/
  27. /*** CLASS VARIABLES ***/
  28. /***********************/
  29. bool inDrawingMode;
  30. bool mousePressed;
  31. /// <summary>
  32. /// Size of deletion area
  33. /// </summary>
  34. int deletionRadius = 5;
  35. /// <summary>
  36. /// Size of areas marking endpoints of lines in the redraw mode.
  37. /// </summary>
  38. int markerRadius = 10;
  39. /// <summary>
  40. /// The Position of the Cursor in the right picture box
  41. /// </summary>
  42. Point currentCursorPosition;
  43. /// <summary>
  44. /// The Previous Cursor Position in the right picture box
  45. /// </summary>
  46. Point previousCursorPosition;
  47. /// <summary>
  48. /// Queue for the cursorPositions
  49. /// </summary>
  50. Queue<Point> cursorPositions = new Queue<Point>();
  51. /// <summary>
  52. /// Lookup Matrix for checking postions of lines in the image
  53. /// </summary>
  54. bool[,] isFilledMatrix;
  55. /// <summary>
  56. /// Lookup Matrix for getting line ids at a certain postions of the image
  57. /// </summary>
  58. HashSet<int>[,] linesMatrix;
  59. /// <summary>
  60. /// List of items which will be overlayed over the right canvas.
  61. /// </summary>
  62. List<Tuple<bool, HashSet<Point>>> overlayItems;
  63. /// <summary>
  64. /// Width of the LeftImageBox.
  65. /// </summary>
  66. public int leftImageBoxWidth;
  67. /// <summary>
  68. /// Height of the LeftImageBox.
  69. /// </summary>
  70. public int leftImageBoxHeight;
  71. /// <summary>
  72. /// Width of the RightImageBox.
  73. /// </summary>
  74. public int rightImageBoxWidth;
  75. /// <summary>
  76. /// Height of the RightImageBox.
  77. /// </summary>
  78. public int rightImageBoxHeight;
  79. //Images
  80. Image leftImage;
  81. List<Line> leftLineList;
  82. Image rightImageWithoutOverlay;
  83. Image rightImageWithOverlay;
  84. List<Tuple<bool, Line>> rightLineList;
  85. List<Point> currentLine;
  86. public MVP_Model(MVP_Presenter presenter)
  87. {
  88. programPresenter = presenter;
  89. historyOfActions = new ActionHistory(null);
  90. redrawAss = new RedrawAssistant();
  91. }
  92. public void Undo()
  93. {
  94. }
  95. /// <summary>
  96. /// A function that returns a white canvas for a given width and height.
  97. /// </summary>
  98. /// <param name="width">The width of the canvas in pixels</param>
  99. /// <param name="height">The height of the canvas in pixels</param>
  100. /// <returns>The new canvas</returns>
  101. private Image GetEmptyCanvas(int width, int height)
  102. {
  103. Image image;
  104. try
  105. {
  106. image = new Bitmap(width, height);
  107. }
  108. catch (ArgumentException e)
  109. {
  110. programPresenter.PassMessageToView("The requested canvas size caused an error: \n"
  111. + e.ToString() + "\n The Canvas will be set to match your window.");
  112. image = new Bitmap(leftImageBoxWidth, leftImageBoxHeight);
  113. }
  114. Graphics graph = Graphics.FromImage(image);
  115. graph.FillRectangle(Brushes.White, 0, 0, width + 10, height + 10);
  116. return image;
  117. }
  118. /// <summary>
  119. /// Creates an empty Canvas
  120. /// </summary>
  121. private void DrawEmptyCanvasRight()
  122. {
  123. if (leftImage == null)
  124. {
  125. rightImageWithoutOverlay = GetEmptyCanvas(leftImageBoxWidth, leftImageBoxHeight);
  126. }
  127. else
  128. {
  129. rightImageWithoutOverlay = GetEmptyCanvas(leftImage.Width, leftImage.Height);
  130. }
  131. RefreshRightImage();
  132. }
  133. /// <summary>
  134. /// Creates an empty Canvas on the left
  135. /// </summary>
  136. /// <param name="width"> width of the new canvas in pixels </param>
  137. /// <param name="height"> height of the new canvas in pixels </param>
  138. private void DrawEmptyCanvasLeft(int width, int height)
  139. {
  140. if (width == 0)
  141. {
  142. leftImage = GetEmptyCanvas(leftImageBoxWidth, leftImageBoxHeight);
  143. }
  144. else
  145. {
  146. leftImage = GetEmptyCanvas(width, height);
  147. }
  148. RefreshLeftImage();
  149. }
  150. /// <summary>
  151. /// A function to refresh the image being displayed in the right picture box, with the current rightImageWithOverlay.
  152. /// </summary>
  153. /// <param name="image">The new Image</param>
  154. private void RefreshRightImage()
  155. {
  156. programPresenter.UpdateRightImage(rightImageWithOverlay);
  157. }
  158. /// <summary>
  159. /// A function to refresh the image being displayed in the left picture box, with the current leftImage.
  160. /// </summary>
  161. /// <param name="image">The new Image</param>
  162. private void RefreshLeftImage()
  163. {
  164. programPresenter.UpdateLeftImage(leftImage);
  165. }
  166. /// <summary>
  167. /// Redraws all lines in lineList, for which their associated boolean value equals true and calls RedrawRightOverlay.
  168. /// </summary>
  169. private void RedrawRightImage()
  170. {
  171. var workingCanvas = GetEmptyCanvas(rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height);
  172. var workingGraph = Graphics.FromImage(workingCanvas);
  173. //Lines
  174. foreach (Tuple<bool, Line> lineBoolTuple in rightLineList)
  175. {
  176. if (lineBoolTuple.Item1)
  177. {
  178. lineBoolTuple.Item2.DrawLine(workingGraph);
  179. }
  180. }
  181. //The Line being currently drawn
  182. if (currentLine != null && currentLine.Count > 0 && inDrawingMode && mousePressed)
  183. {
  184. var currLine = new Line(currentLine);
  185. currLine.DrawLine(workingGraph);
  186. }
  187. rightImageWithoutOverlay = workingCanvas;
  188. //Redraw the Overlay
  189. RedrawRightOverlay();
  190. RefreshRightImage();
  191. }
  192. /// <summary>
  193. /// Redraws all elements in the overlay items for which the respective boolean value is true.
  194. /// </summary>
  195. private void RedrawRightOverlay()
  196. {
  197. var workingCanvas = rightImageWithoutOverlay;
  198. var workingGraph = Graphics.FromImage(workingCanvas);
  199. foreach (Tuple<bool, HashSet<Point>> tup in overlayItems)
  200. {
  201. if (tup.Item1)
  202. {
  203. foreach (Point p in tup.Item2)
  204. {
  205. workingGraph.FillRectangle(Brushes.Green, p.X, p.Y, 1, 1);
  206. }
  207. }
  208. }
  209. rightImageWithOverlay = workingCanvas;
  210. RefreshRightImage();
  211. }
  212. /// <summary>
  213. /// The function called by the Presenter to change the drawing state of the program.
  214. /// </summary>
  215. /// <param name="nowDrawing">The new drawingstate of the program</param>
  216. public void ChangeState(bool nowDrawing)
  217. {
  218. inDrawingMode = nowDrawing;
  219. UpdateUI();
  220. }
  221. /// <summary>
  222. /// Change the status of whether or not the lines are shown.
  223. /// </summary>
  224. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  225. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  226. private void ChangeLines(HashSet<int> lines, bool shown)
  227. {
  228. var changed = false;
  229. foreach (int lineId in lines)
  230. {
  231. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  232. {
  233. rightLineList[lineId] = new Tuple<bool, Line>(shown, rightLineList[lineId].Item2);
  234. changed = true;
  235. }
  236. }
  237. if (changed) { RedrawRightImage(); }
  238. }
  239. /// <summary>
  240. /// A function that populates the matrixes needed for deletion detection with line data.
  241. /// </summary>
  242. private void RepopulateDeletionMatrixes()
  243. {
  244. if (rightImageWithoutOverlay != null)
  245. {
  246. isFilledMatrix = new bool[rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height];
  247. linesMatrix = new HashSet<int>[rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height];
  248. foreach (Tuple<bool, Line> lineTuple in rightLineList)
  249. {
  250. if (lineTuple.Item1)
  251. {
  252. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  253. }
  254. }
  255. }
  256. }
  257. /*
  258. /// <summary>
  259. /// A function that checks the deletion matrixes at a certain point
  260. /// and returns all Line ids at that point and in a square around it in a certain range.
  261. /// </summary>
  262. /// <param name="p">The point around which to check.</param>
  263. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  264. /// <returns>A List of all lines.</returns>
  265. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  266. {
  267. HashSet<int> returnSet = new HashSet<int>();
  268. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  269. {
  270. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImage.Width && pnt.Y < rightImage.Height)
  271. {
  272. if (isFilledMatrix[pnt.X, pnt.Y])
  273. {
  274. returnSet.UnionWith(linesMatrix[pnt.X, pnt.Y]);
  275. }
  276. }
  277. }
  278. return returnSet;
  279. }
  280. /// <summary>
  281. /// binds the given picture to templatePicture and draws it
  282. /// </summary>
  283. /// <param name="newTemplatePicture"> the new template picture, represented as a list of polylines </param>
  284. /// <returns></returns>
  285. private void BindAndDrawLeftImage(List<Line> newTemplatePicture)
  286. {
  287. leftLineList = newTemplatePicture;
  288. foreach (Line l in leftLineList)
  289. {
  290. l.DrawLine(Graphics.FromImage(leftImage));
  291. }
  292. }
  293. /// <summary>
  294. /// Will calculate the start and endpoints of the given line on the right canvas.
  295. /// </summary>
  296. /// <param name="line">The line.</param>
  297. /// <param name="size">The size of the circle with which the endpoints of the line are marked.</param>
  298. private Tuple<HashSet<Point>, HashSet<Point>> CalculateStartAndEnd(Line line, int size)
  299. {
  300. var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), size);
  301. var circle1 = GeometryCalculator.FilledCircleAlgorithm(line.GetEndPoint(), size);
  302. var currentLineEndings = new Tuple<HashSet<Point>, HashSet<Point>>(circle0, circle1);
  303. return currentLineEndings;
  304. }
  305. */
  306. /// <summary>
  307. /// A helper Function that updates the markerRadius & deletionRadius, considering the size of the canvas.
  308. /// </summary>
  309. public void UpdateSizes()
  310. {
  311. if (rightImageWithoutOverlay != null)
  312. {
  313. int widthImage = rightImageWithoutOverlay.Width;
  314. int heightImage = rightImageWithoutOverlay.Height;
  315. int widthBox = rightImageBoxWidth;
  316. int heightBox = rightImageBoxHeight;
  317. float imageRatio = (float)widthImage / (float)heightImage;
  318. float containerRatio = (float)widthBox / (float)heightBox;
  319. float zoomFactor = 0;
  320. if (imageRatio >= containerRatio)
  321. {
  322. //Image is wider than it is high
  323. zoomFactor = (float)widthImage / (float)widthBox;
  324. }
  325. else
  326. {
  327. //Image is higher than it is wide
  328. zoomFactor = (float)heightImage / (float)heightBox;
  329. }
  330. markerRadius = (int)(10 * zoomFactor);
  331. redrawAss.SetMarkerRadius(markerRadius);
  332. deletionRadius = (int)(5 * zoomFactor);
  333. }
  334. }
  335. /// <summary>
  336. /// Tells the Presenter to Update the UI
  337. /// </summary>
  338. private void UpdateUI()
  339. {
  340. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), (rightImageWithoutOverlay != null));
  341. }
  342. /// <summary>
  343. /// A method to get the dimensions of the right image.
  344. /// </summary>
  345. /// <returns>A tuple containing the width and height of the right image.</returns>
  346. public Tuple<int, int> GetRightImageDimensions()
  347. {
  348. if(rightImageWithoutOverlay != null)
  349. {
  350. return new Tuple<int, int>(rightImageWithoutOverlay.Width, rightImageWithoutOverlay.Height);
  351. }
  352. else
  353. {
  354. return new Tuple<int, int>(0, 0);
  355. }
  356. }
  357. public void SetCurrentCursorPosition(Point p)
  358. {
  359. }
  360. /*
  361. /// <summary>
  362. /// Checks if there is unsaved progess, and warns the user. Returns True if it safe to continue.
  363. /// </summary>
  364. /// <returns>true if there is none, or the user wishes to continue without saving.
  365. /// false if there is progress, and the user doesn't wish to continue.</returns>
  366. private bool CheckSavedStatus()
  367. {
  368. if (!historyOfActions.IsEmpty())
  369. {
  370. return (MessageBox.Show("You have unsaved changes, do you wish to continue?",
  371. "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes);
  372. }
  373. return true;
  374. }
  375. */
  376. }
  377. }