MVP_Model.cs 18 KB

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