MVP_Model.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. namespace SketchAssistantWPF
  11. {
  12. public class MVP_Model
  13. {
  14. /// <summary>
  15. /// The Presenter of the MVP-Model.
  16. /// </summary>
  17. MVP_Presenter programPresenter;
  18. /// <summary>
  19. /// History of Actions
  20. /// </summary>
  21. ActionHistory historyOfActions;
  22. /// <summary>
  23. /// The assistant responsible for the redraw mode
  24. /// </summary>
  25. //RedrawAssistant redrawAss;
  26. /***********************/
  27. /*** CLASS VARIABLES ***/
  28. /***********************/
  29. /// <summary>
  30. /// If the program is in drawing mode.
  31. /// </summary>
  32. bool inDrawingMode;
  33. /// <summary>
  34. /// Size of deletion area
  35. /// </summary>
  36. int deletionRadius = 5;
  37. /// <summary>
  38. /// Size of areas marking endpoints of lines in the redraw mode.
  39. /// </summary>
  40. int markerRadius = 10;
  41. /// <summary>
  42. /// The Position of the Cursor in the right picture box
  43. /// </summary>
  44. Point currentCursorPosition;
  45. /// <summary>
  46. /// The Previous Cursor Position in the right picture box
  47. /// </summary>
  48. Point previousCursorPosition;
  49. /// <summary>
  50. /// Queue for the cursorPositions
  51. /// </summary>
  52. Queue<Point> cursorPositions = new Queue<Point>();
  53. /// <summary>
  54. /// Lookup Matrix for checking postions of lines in the image
  55. /// </summary>
  56. bool[,] isFilledMatrix;
  57. /// <summary>
  58. /// Lookup Matrix for getting line ids at a certain postions of the image
  59. /// </summary>
  60. HashSet<int>[,] linesMatrix;
  61. /// <summary>
  62. /// List of items which will be overlayed over the right canvas.
  63. /// </summary>
  64. List<Tuple<bool, HashSet<Point>>> overlayItems;
  65. /// <summary>
  66. /// Width of the LeftImageBox.
  67. /// </summary>
  68. public int leftImageBoxWidth;
  69. /// <summary>
  70. /// Height of the LeftImageBox.
  71. /// </summary>
  72. public int leftImageBoxHeight;
  73. /// <summary>
  74. /// Width of the RightImageBox.
  75. /// </summary>
  76. public int rightImageBoxWidth;
  77. /// <summary>
  78. /// Height of the RightImageBox.
  79. /// </summary>
  80. public int rightImageBoxHeight;
  81. public ImageDimension leftImageSize { get; private set; }
  82. public ImageDimension rightImageSize { get; private set; }
  83. /// <summary>
  84. /// Indicates whether or not the canvas on the right side is active.
  85. /// </summary>
  86. public bool canvasActive {get; set;}
  87. /// <summary>
  88. /// Indicates if there is a graphic loaded in the left canvas.
  89. /// </summary>
  90. public bool graphicLoaded { get; set; }
  91. Image rightImageWithoutOverlay;
  92. List<InternalLine> leftLineList;
  93. List<Tuple<bool, InternalLine>> rightLineList;
  94. List<Point> currentLine = new List<Point>();
  95. public MVP_Model(MVP_Presenter presenter)
  96. {
  97. programPresenter = presenter;
  98. historyOfActions = new ActionHistory();
  99. //redrawAss = new RedrawAssistant();
  100. //overlayItems = new List<Tuple<bool, HashSet<Point>>>();
  101. rightLineList = new List<Tuple<bool, InternalLine>>();
  102. canvasActive = false;
  103. UpdateUI();
  104. rightImageSize = new ImageDimension(0, 0);
  105. leftImageSize = new ImageDimension(0, 0);
  106. }
  107. /**************************/
  108. /*** INTERNAL FUNCTIONS ***/
  109. /**************************/
  110. /// <summary>
  111. /// Change the status of whether or not the lines are shown.
  112. /// </summary>
  113. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  114. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  115. private void ChangeLines(HashSet<int> lines, bool shown)
  116. {
  117. foreach (int lineId in lines)
  118. {
  119. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  120. {
  121. rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// A function that populates the matrixes needed for deletion detection with line data.
  127. /// </summary>
  128. private void RepopulateDeletionMatrixes()
  129. {
  130. if (canvasActive)
  131. {
  132. isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
  133. linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
  134. foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
  135. {
  136. if (lineTuple.Item1)
  137. {
  138. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  139. }
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Tells the Presenter to Update the UI
  145. /// </summary>
  146. private void UpdateUI()
  147. {
  148. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded);
  149. }
  150. /// <summary>
  151. /// A function that checks the deletion matrixes at a certain point
  152. /// and returns all Line ids at that point and in a square around it in a certain range.
  153. /// </summary>
  154. /// <param name="p">The point around which to check.</param>
  155. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  156. /// <returns>A List of all lines.</returns>
  157. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  158. {
  159. HashSet<int> returnSet = new HashSet<int>();
  160. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  161. {
  162. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
  163. {
  164. if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
  165. {
  166. returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
  167. }
  168. }
  169. }
  170. return returnSet;
  171. }
  172. /********************************************/
  173. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  174. /********************************************/
  175. /// <summary>
  176. /// A function to update the dimensions of the left and right canvas when the window is resized.
  177. /// </summary>
  178. /// <param name="LeftCanvas">The size of the left canvas.</param>
  179. /// <param name="RightCanvas">The size of the right canvas.</param>
  180. public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
  181. {
  182. if(LeftCanvas.Height >= 0 && LeftCanvas.Width>= 0) { leftImageSize = LeftCanvas; }
  183. if(RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
  184. RepopulateDeletionMatrixes();
  185. }
  186. /// <summary>
  187. /// A function to reset the right image.
  188. /// </summary>
  189. public void ResetRightImage()
  190. {
  191. rightLineList.Clear();
  192. programPresenter.PassLastActionTaken(historyOfActions.Reset());
  193. programPresenter.ClearRightLines();
  194. }
  195. /// <summary>
  196. /// The function to set the left image.
  197. /// </summary>
  198. /// <param name="width">The width of the left image.</param>
  199. /// <param name="height">The height of the left image.</param>
  200. /// <param name="listOfLines">The List of Lines to be displayed in the left image.</param>
  201. public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
  202. {
  203. leftImageSize = new ImageDimension(width, height);
  204. rightImageSize = new ImageDimension(width, height);
  205. leftLineList = listOfLines;
  206. graphicLoaded = true;
  207. programPresenter.UpdateLeftLines(leftLineList);
  208. CanvasActivated();
  209. /*
  210. var workingCanvas = GetEmptyCanvas(width, height);
  211. var workingGraph = Graphics.FromImage(workingCanvas);
  212. leftLineList = listOfLines;
  213. //redrawAss = new RedrawAssistant(leftLineList);
  214. //overlayItems = redrawAss.Initialize(markerRadius);
  215. //Lines
  216. foreach (InternalLine line in leftLineList)
  217. {
  218. line.DrawLine(workingGraph);
  219. }
  220. leftImage = workingCanvas;
  221. programPresenter.UpdateLeftImage(leftImage);
  222. //Set right image to same size as left image and delete linelist
  223. DrawEmptyCanvasRight();
  224. rightLineList = new List<Tuple<bool, InternalLine>>();
  225. */
  226. }
  227. /// <summary>
  228. /// A function to tell the model a new canvas was activated.
  229. /// </summary>
  230. public void CanvasActivated()
  231. {
  232. canvasActive = true;
  233. RepopulateDeletionMatrixes();
  234. UpdateUI();
  235. }
  236. /// <summary>
  237. /// Will undo the last action taken, if the action history allows it.
  238. /// </summary>
  239. public void Undo()
  240. {
  241. if (historyOfActions.CanUndo())
  242. {
  243. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  244. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  245. switch (undoAction)
  246. {
  247. case SketchAction.ActionType.Delete:
  248. //Deleted Lines need to be shown
  249. ChangeLines(affectedLines, true);
  250. break;
  251. case SketchAction.ActionType.Draw:
  252. //Drawn lines need to be hidden
  253. ChangeLines(affectedLines, false);
  254. break;
  255. default:
  256. break;
  257. }
  258. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  259. programPresenter.UpdateRightLines(rightLineList);
  260. }
  261. RepopulateDeletionMatrixes();
  262. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
  263. UpdateUI();
  264. }
  265. /// <summary>
  266. /// Will redo the last action undone, if the action history allows it.
  267. /// </summary>
  268. public void Redo()
  269. {
  270. if (historyOfActions.CanRedo())
  271. {
  272. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
  273. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  274. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  275. switch (redoAction)
  276. {
  277. case SketchAction.ActionType.Delete:
  278. //Deleted Lines need to be redeleted
  279. ChangeLines(affectedLines, false);
  280. break;
  281. case SketchAction.ActionType.Draw:
  282. //Drawn lines need to be redrawn
  283. ChangeLines(affectedLines, true);
  284. break;
  285. default:
  286. break;
  287. }
  288. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  289. programPresenter.UpdateRightLines(rightLineList);
  290. RepopulateDeletionMatrixes();
  291. }
  292. UpdateUI();
  293. }
  294. /// <summary>
  295. /// The function called by the Presenter to change the drawing state of the program.
  296. /// </summary>
  297. /// <param name="nowDrawing">The new drawingstate of the program</param>
  298. public void ChangeState(bool nowDrawing)
  299. {
  300. inDrawingMode = nowDrawing;
  301. UpdateUI();
  302. }
  303. /// <summary>
  304. /// Updates the current cursor position of the model.
  305. /// </summary>
  306. /// <param name="p">The new cursor position</param>
  307. public void SetCurrentCursorPosition(Point p)
  308. {
  309. currentCursorPosition = p;
  310. }
  311. /// <summary>
  312. /// Start a new Line, when the Mouse is pressed down.
  313. /// </summary>
  314. public void MouseDown()
  315. {
  316. if (inDrawingMode)
  317. {
  318. currentLine.Clear();
  319. currentLine.Add(currentCursorPosition);
  320. }
  321. }
  322. /// <summary>
  323. /// Finish the current Line, when the pressed Mouse is released.
  324. /// </summary>
  325. public void MouseUp()
  326. {
  327. if (inDrawingMode && currentLine.Count > 0)
  328. {
  329. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  330. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  331. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  332. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  333. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  334. programPresenter.UpdateRightLines(rightLineList);
  335. currentLine.Clear();
  336. programPresenter.UpdateCurrentLine(currentLine);
  337. }
  338. UpdateUI();
  339. }
  340. /// <summary>
  341. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  342. /// </summary>
  343. public void Tick()
  344. {
  345. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  346. else { previousCursorPosition = currentCursorPosition; }
  347. cursorPositions.Enqueue(currentCursorPosition);
  348. //Drawing
  349. if (inDrawingMode && programPresenter.IsMousePressed())
  350. {
  351. currentLine.Add(currentCursorPosition);
  352. programPresenter.UpdateCurrentLine(currentLine);
  353. }
  354. //Deleting
  355. if (!inDrawingMode && programPresenter.IsMousePressed())
  356. {
  357. List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  358. foreach (Point currPoint in uncheckedPoints)
  359. {
  360. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  361. if (linesToDelete.Count > 0)
  362. {
  363. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  364. foreach (int lineID in linesToDelete)
  365. {
  366. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  367. }
  368. RepopulateDeletionMatrixes();
  369. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  370. programPresenter.UpdateRightLines(rightLineList);
  371. }
  372. }
  373. }
  374. }
  375. /// <summary>
  376. /// A helper Function that updates the markerRadius & deletionRadius, considering the size of the canvas.
  377. /// </summary>
  378. /// <param name="CanvasSize">The size of the canvas</param>
  379. public void UpdateSizes(ImageDimension CanvasSize)
  380. {
  381. if (rightImageWithoutOverlay != null)
  382. {
  383. int widthImage = rightImageSize.Width;
  384. int heightImage = rightImageSize.Height;
  385. int widthBox = CanvasSize.Width;
  386. int heightBox = CanvasSize.Height;
  387. float imageRatio = (float)widthImage / (float)heightImage;
  388. float containerRatio = (float)widthBox / (float)heightBox;
  389. float zoomFactor = 0;
  390. if (imageRatio >= containerRatio)
  391. {
  392. //Image is wider than it is high
  393. zoomFactor = (float)widthImage / (float)widthBox;
  394. }
  395. else
  396. {
  397. //Image is higher than it is wide
  398. zoomFactor = (float)heightImage / (float)heightBox;
  399. }
  400. markerRadius = (int)(10 * zoomFactor);
  401. deletionRadius = (int)(5 * zoomFactor);
  402. }
  403. }
  404. /// <summary>
  405. /// If there is unsaved progress.
  406. /// </summary>
  407. /// <returns>True if there is progress that has not been saved.</returns>
  408. public bool HasUnsavedProgress()
  409. {
  410. return !historyOfActions.IsEmpty();
  411. }
  412. }
  413. }