MVP_Model.cs 18 KB

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