MVP_Model.cs 16 KB

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