MVP_Presenter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Shapes;
  11. namespace SketchAssistantWPF
  12. {
  13. public class MVP_Presenter
  14. {
  15. /// <summary>
  16. /// The View of the MVP-Model, in this case Form1.
  17. /// </summary>
  18. MVP_View programView;
  19. /// <summary>
  20. /// The Model of the MVP-Model.
  21. /// </summary>
  22. MVP_Model programModel;
  23. /// <summary>
  24. /// A dictionary connecting the id of an InternalLine with the respective Polyline in the right canvas.
  25. /// </summary>
  26. Dictionary<int, Shape> rightPolyLines;
  27. ImageDimension CanvasSizeLeft = new ImageDimension(0,0);
  28. ImageDimension CanvasSizeRight = new ImageDimension(0, 0);
  29. ImageDimension ImageSizeLeft = new ImageDimension(0, 0);
  30. ImageDimension ImageSizeRight = new ImageDimension(0, 0);
  31. /*******************/
  32. /*** ENUMERATORS ***/
  33. /*******************/
  34. public enum MouseAction
  35. {
  36. Click,
  37. Down,
  38. Up,
  39. Up_Invalid,
  40. Move
  41. }
  42. /***********************/
  43. /*** CLASS VARIABLES ***/
  44. /***********************/
  45. /// <summary>
  46. /// Instance of FileImporter to handle drawing imports.
  47. /// </summary>
  48. private FileImporter fileImporter;
  49. public MVP_Presenter(MVP_View form)
  50. {
  51. programView = form;
  52. programModel = new MVP_Model(this);
  53. //Initialize Class Variables
  54. fileImporter = new FileImporter();
  55. }
  56. /***********************************/
  57. /*** FUNCTIONS VIEW -> PRESENTER ***/
  58. /***********************************/
  59. /// <summary>
  60. /// Pass-trough function to update the appropriate information of the model, when the window is resized.
  61. /// </summary>
  62. /// <param name="leftPBS">The new size of the left picture box.</param>
  63. /// <param name="rightPBS">The new size of the left picture box.</param>
  64. public void Resize(Tuple<int, int> leftPBS, Tuple<int, int> rightPBS)
  65. {
  66. CanvasSizeLeft.ChangeDimension(leftPBS.Item1, leftPBS.Item2);
  67. CanvasSizeRight.ChangeDimension(rightPBS.Item1, rightPBS.Item2);
  68. //programModel.UpdateSizes(CanvasSizeRight);
  69. programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
  70. }
  71. /// <summary>
  72. /// Display a new FileDialog to load a collection of lines.
  73. /// </summary>
  74. public void ExamplePictureToolStripMenuItemClick()
  75. {
  76. var okToContinue = true;
  77. if (programModel.HasUnsavedProgress())
  78. {
  79. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  80. }
  81. if (okToContinue)
  82. {
  83. var fileNameTup = programView.openNewDialog("Interactive Sketch-Assistant Drawing|*.isad");
  84. if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  85. {
  86. programView.SetToolStripLoadStatus(fileNameTup.Item2);
  87. try
  88. {
  89. Tuple<int, int, List<InternalLine>> values = fileImporter.ParseISADInputFile(fileNameTup.Item1);
  90. programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
  91. programModel.ResetRightImage();
  92. programModel.CanvasActivated();
  93. programModel.ChangeState(true);
  94. programView.EnableTimer();
  95. ClearRightLines();
  96. }
  97. catch (FileImporterException ex)
  98. {
  99. programView.ShowInfoMessage(ex.ToString());
  100. }
  101. catch (Exception ex)
  102. {
  103. programView.ShowInfoMessage("exception occured while trying to load interactive sketch-assistant drawing file:\n\n" + ex.ToString() + "\n\n" + ex.StackTrace);
  104. }
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Display a new FileDialog to a svg drawing.
  110. /// </summary>
  111. public void SVGToolStripMenuItemClick()
  112. {
  113. var okToContinue = true;
  114. if (programModel.HasUnsavedProgress())
  115. {
  116. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  117. }
  118. if (okToContinue)
  119. {
  120. var fileNameTup = programView.openNewDialog("Scalable Vector Graphics|*.svg");
  121. if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  122. {
  123. programView.SetToolStripLoadStatus(fileNameTup.Item2);
  124. try
  125. {
  126. Tuple<int, int, List<InternalLine>> values = fileImporter.ParseSVGInputFile(fileNameTup.Item1, programModel.leftImageBoxWidth, programModel.leftImageBoxHeight);
  127. programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
  128. programModel.ResetRightImage();
  129. programModel.CanvasActivated();
  130. programModel.ChangeState(true);
  131. programView.EnableTimer();
  132. ClearRightLines();
  133. }
  134. catch (FileImporterException ex)
  135. {
  136. programView.ShowInfoMessage(ex.ToString());
  137. }
  138. catch (Exception ex)
  139. {
  140. programView.ShowInfoMessage("exception occured while trying to parse svg file:\n\n" + ex.ToString() + "\n\n" + ex.StackTrace);
  141. }
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// Pass-trough function to change the drawing state of the model.
  147. /// </summary>
  148. /// <param name="NowDrawing">Indicates if the program is in drawing (true) or deletion (false) mode.</param>
  149. public void ChangeState(bool NowDrawing)
  150. {
  151. programModel.ChangeState(NowDrawing);
  152. }
  153. /// <summary>
  154. /// Pass-trough function to undo an action.
  155. /// </summary>
  156. public void Undo()
  157. {
  158. programModel.Undo();
  159. }
  160. /// <summary>
  161. /// Pass-trough function to redo an action.
  162. /// </summary>
  163. public void Redo()
  164. {
  165. programModel.Redo();
  166. }
  167. /// <summary>
  168. /// Pass-trough function for ticking the model.
  169. /// </summary>
  170. public void Tick()
  171. {
  172. programModel.Tick();
  173. }
  174. /// <summary>
  175. /// Checks if there is unsaved progress, and promts the model to generate a new canvas if not.
  176. /// </summary>
  177. public void NewCanvas()
  178. {
  179. var okToContinue = true;
  180. if (programModel.HasUnsavedProgress())
  181. {
  182. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  183. }
  184. if (okToContinue)
  185. {
  186. programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
  187. programModel.ResetRightImage();
  188. programModel.CanvasActivated();
  189. programModel.ChangeState(true);
  190. programView.EnableTimer();
  191. ClearRightLines();
  192. }
  193. }
  194. /// <summary>
  195. /// Pass-trough when the mouse is moved.
  196. /// </summary>
  197. /// <param name="mouseAction">The action which is sent by the View.</param>
  198. /// <param name="e">The Mouse event arguments.</param>
  199. public void MouseEvent(MouseAction mouseAction, Point position)
  200. {
  201. switch (mouseAction)
  202. {
  203. case MouseAction.Move:
  204. programModel.SetCurrentCursorPosition(position);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. /// <summary>
  211. /// Pass-trough function that calls the correct Mouse event of the model, when the mouse is clicked.
  212. /// </summary>
  213. /// <param name="mouseAction">The action which is sent by the View.</param>
  214. /// <param name="e">The Mouse event arguments.</param>
  215. public void MouseEvent(MouseAction mouseAction)
  216. {
  217. switch (mouseAction)
  218. {
  219. case MouseAction.Click:
  220. programModel.MouseDown();
  221. programModel.Tick();
  222. programModel.MouseUp(true);
  223. break;
  224. case MouseAction.Down:
  225. programModel.MouseDown();
  226. break;
  227. case MouseAction.Up:
  228. programModel.MouseUp(true);
  229. break;
  230. case MouseAction.Up_Invalid:
  231. programModel.MouseUp(false);
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. /************************************/
  238. /*** FUNCTIONS MODEL -> PRESENTER ***/
  239. /************************************/
  240. /// <summary>
  241. /// Return the position of the cursor
  242. /// </summary>
  243. /// <returns>The position of the cursor</returns>
  244. public Point GetCursorPosition()
  245. {
  246. return programView.GetCursorPosition();
  247. }
  248. /// <summary>
  249. /// Clears all Lines in the right canvas.
  250. /// </summary>
  251. public void ClearRightLines()
  252. {
  253. programView.RemoveAllRightLines();
  254. rightPolyLines = new Dictionary<int, Shape>();
  255. }
  256. /// <summary>
  257. /// A function to update the displayed lines in the right canvas.
  258. /// </summary>
  259. public void UpdateRightLines(List<Tuple<bool, InternalLine>> lines)
  260. {
  261. foreach(Tuple<bool, InternalLine> tup in lines)
  262. {
  263. var status = tup.Item1;
  264. var line = tup.Item2;
  265. if (!rightPolyLines.ContainsKey(line.GetID()))
  266. {
  267. if (!line.isPoint)
  268. {
  269. Polyline newLine = new Polyline();
  270. newLine.Points = line.GetPointCollection();
  271. rightPolyLines.Add(line.GetID(), newLine);
  272. programView.AddNewLineRight(newLine);
  273. }
  274. else
  275. {
  276. Ellipse newPoint = new Ellipse();
  277. rightPolyLines.Add(line.GetID(), newPoint);
  278. programView.AddNewPointRight(newPoint, line);
  279. }
  280. }
  281. SetVisibility(rightPolyLines[line.GetID()], status);
  282. }
  283. }
  284. /*
  285. /// <summary>
  286. /// Updates the currentline
  287. /// </summary>
  288. /// <param name="linepoints">The points of the current line.</param>
  289. public void UpdateCurrentLine(List<Point> linepoints)
  290. {
  291. Polyline currentLine = new Polyline();
  292. currentLine.Stroke = Brushes.Black;
  293. currentLine.Points = new PointCollection(linepoints);
  294. programView.DisplayCurrLine(currentLine);
  295. }
  296. */
  297. /// <summary>
  298. /// A function to update the displayed lines in the left canvas.
  299. /// </summary>
  300. public void UpdateLeftLines(List<InternalLine> lines)
  301. {
  302. programView.RemoveAllLeftLines();
  303. foreach (InternalLine line in lines)
  304. {
  305. Polyline newLine = new Polyline();
  306. newLine.Stroke = Brushes.Black;
  307. newLine.Points = line.GetPointCollection();
  308. programView.AddNewLineLeft(newLine);
  309. }
  310. programView.SetCanvasState("LeftCanvas", true);
  311. programView.SetCanvasState("RightCanvas", true);
  312. }
  313. /// <summary>
  314. /// Called by the model when the state of the Program changes.
  315. /// Changes the look of the UI according to the current state of the model.
  316. /// </summary>
  317. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  318. /// <param name="canUndo">If actions in the model can be undone</param>
  319. /// <param name="canRedo">If actions in the model can be redone</param>
  320. /// <param name="canvasActive">If the right canvas is active</param>
  321. /// <param name="graphicLoaded">If an image is loaded in the model</param>
  322. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive, bool graphicLoaded)
  323. {
  324. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  325. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  326. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  327. if (canvasActive)
  328. {
  329. if (inDrawingMode)
  330. {
  331. dict["drawButton"] = MainWindow.ButtonState.Active;
  332. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  333. }
  334. else
  335. {
  336. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  337. dict["deleteButton"] = MainWindow.ButtonState.Active;
  338. }
  339. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  340. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  341. }
  342. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  343. {
  344. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  345. }
  346. programView.SetCanvasState("RightCanvas", canvasActive);
  347. programView.SetCanvasState("LeftCanvas", graphicLoaded);
  348. }
  349. /// <summary>
  350. /// Pass-trough function to display an info message in the view.
  351. /// </summary>
  352. /// <param name="msg">The message.</param>
  353. public void PassMessageToView(String msg)
  354. {
  355. programView.ShowInfoMessage(msg);
  356. }
  357. /// <summary>
  358. /// Pass-trough function to update the display of the last action taken.
  359. /// </summary>
  360. /// <param name="msg">The new last action taken.</param>
  361. public void PassLastActionTaken(String msg)
  362. {
  363. programView.SetLastActionTakenText(msg);
  364. }
  365. /// <summary>
  366. /// Passes whether or not the mouse is pressed.
  367. /// </summary>
  368. /// <returns>Whether or not the mouse is pressed</returns>
  369. public bool IsMousePressed()
  370. {
  371. return programView.IsMousePressed();
  372. }
  373. /*************************/
  374. /*** HELPING FUNCTIONS ***/
  375. /*************************/
  376. /// <summary>
  377. /// Sets the visibility of a polyline.
  378. /// </summary>
  379. /// <param name="line">The polyline</param>
  380. /// <param name="visible">Whether or not it should be visible.</param>
  381. private void SetVisibility(Shape line, bool visible)
  382. {
  383. if (!visible)
  384. {
  385. line.Opacity = 0.00001;
  386. }
  387. else
  388. {
  389. line.Opacity = 1;
  390. }
  391. }
  392. }
  393. }