MVP_Model.cs 16 KB

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