MVP_Presenter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. namespace SketchAssistantWPF
  10. {
  11. public class MVP_Presenter
  12. {
  13. /// <summary>
  14. /// The View of the MVP-Model, in this case Form1.
  15. /// </summary>
  16. MVP_View programView;
  17. /// <summary>
  18. /// The Model of the MVP-Model.
  19. /// </summary>
  20. MVP_Model programModel;
  21. /*******************/
  22. /*** ENUMERATORS ***/
  23. /*******************/
  24. public enum MouseAction
  25. {
  26. Click,
  27. Down,
  28. Up,
  29. Move
  30. }
  31. /***********************/
  32. /*** CLASS VARIABLES ***/
  33. /***********************/
  34. /// <summary>
  35. /// Instance of FileImporter to handle drawing imports.
  36. /// </summary>
  37. private FileImporter fileImporter;
  38. public MVP_Presenter(MVP_View form)
  39. {
  40. programView = form;
  41. programModel = new MVP_Model(this);
  42. //Initialize Class Variables
  43. fileImporter = new FileImporter();
  44. }
  45. /***********************************/
  46. /*** FUNCTIONS VIEW -> PRESENTER ***/
  47. /***********************************/
  48. /// <summary>
  49. /// Pass-trough function to update the appropriate information of the model, when the window is resized.
  50. /// </summary>
  51. /// <param name="leftPBS">The new size of the left picture box.</param>
  52. /// <param name="rightPBS">The new size of the left picture box.</param>
  53. public void Resize(Tuple<int, int> leftPBS, Tuple<int, int> rightPBS)
  54. {
  55. programModel.leftImageBoxWidth = leftPBS.Item1;
  56. programModel.leftImageBoxHeight = leftPBS.Item2;
  57. programModel.rightImageBoxWidth = rightPBS.Item1;
  58. programModel.rightImageBoxHeight = rightPBS.Item2;
  59. programModel.UpdateSizes();
  60. }
  61. /// <summary>
  62. /// Display a new FileDialog to load a collection of lines.
  63. /// </summary>
  64. public void ExamplePictureToolStripMenuItemClick()
  65. {
  66. var okToContinue = true;
  67. if (programModel.HasUnsavedProgress())
  68. {
  69. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  70. }
  71. if (okToContinue)
  72. {
  73. var fileNameTup = programView.openNewDialog("Interactive Sketch-Assistant Drawing|*.isad");
  74. if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  75. {
  76. programView.SetToolStripLoadStatus(fileNameTup.Item2);
  77. Tuple<int, int, List<Line>> values = fileImporter.ParseISADInputFile(fileNameTup.Item1);
  78. programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
  79. programModel.ChangeState(true);
  80. programView.EnableTimer();
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// Pass-trough function to change the drawing state of the model.
  86. /// </summary>
  87. /// <param name="NowDrawing">Indicates if the program is in drawing (true) or deletion (false) mode.</param>
  88. public void ChangeState(bool NowDrawing)
  89. {
  90. programModel.ChangeState(NowDrawing);
  91. }
  92. /// <summary>
  93. /// Pass-trough function to undo an action.
  94. /// </summary>
  95. public void Undo()
  96. {
  97. programModel.Undo();
  98. }
  99. /// <summary>
  100. /// Pass-trough function to redo an action.
  101. /// </summary>
  102. public void Redo()
  103. {
  104. programModel.Redo();
  105. }
  106. /// <summary>
  107. /// Checks if there is unsaved progress, and promts the model to generate a new canvas if not.
  108. /// </summary>
  109. public void NewCanvas()
  110. {
  111. var okToContinue = true;
  112. if (programModel.HasUnsavedProgress())
  113. {
  114. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  115. }
  116. if (okToContinue)
  117. {
  118. programModel.DrawEmptyCanvasRight();
  119. programModel.ChangeState(true);
  120. programView.EnableTimer();
  121. }
  122. }
  123. /// <summary>
  124. /// Pass-trough function for ticking the model.
  125. /// </summary>
  126. public void Tick()
  127. {
  128. programModel.Tick();
  129. }
  130. /// <summary>
  131. /// Pass-trough when the mouse is moved.
  132. /// </summary>
  133. /// <param name="mouseAction">The action which is sent by the View.</param>
  134. /// <param name="e">The Mouse event arguments.</param>
  135. public void MouseEvent(MouseAction mouseAction, MouseEventArgs e)
  136. {
  137. switch (mouseAction)
  138. {
  139. case MouseAction.Move:
  140. programModel.SetCurrentCursorPosition(ConvertCoordinates(e.GetPosition());
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. /// <summary>
  147. /// Pass-trough function that calls the correct Mouse event of the model, when the mouse is clicked.
  148. /// </summary>
  149. /// <param name="mouseAction">The action which is sent by the View.</param>
  150. /// <param name="e">The Mouse event arguments.</param>
  151. public void MouseEvent(MouseAction mouseAction)
  152. {
  153. switch (mouseAction)
  154. {
  155. case MouseAction.Click:
  156. programModel.MouseDown();
  157. programModel.Tick();
  158. programModel.MouseUp();
  159. break;
  160. case MouseAction.Down:
  161. programModel.MouseDown();
  162. break;
  163. case MouseAction.Up:
  164. programModel.MouseUp();
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. /************************************/
  171. /*** FUNCTIONS MODEL -> PRESENTER ***/
  172. /************************************/
  173. /// <summary>
  174. /// Called by the model when the state of the Program changes.
  175. /// Changes the look of the UI according to the current state of the model.
  176. /// </summary>
  177. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  178. /// <param name="canUndo">If actions in the model can be undone</param>
  179. /// <param name="canRedo">If actions in the model can be redone</param>
  180. /// <param name="imageLoaded">If an image is loaded in the model</param>
  181. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
  182. {
  183. Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
  184. {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
  185. {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}};
  186. if (imageLoaded)
  187. {
  188. if (inDrawingMode)
  189. {
  190. dict["drawButton"] = MainWindow.ButtonState.Active;
  191. dict["deleteButton"] = MainWindow.ButtonState.Enabled;
  192. }
  193. else
  194. {
  195. dict["drawButton"] = MainWindow.ButtonState.Enabled;
  196. dict["deleteButton"] = MainWindow.ButtonState.Active;
  197. }
  198. if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
  199. if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
  200. }
  201. foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
  202. {
  203. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  204. }
  205. }
  206. /// <summary>
  207. /// Is called by the model when the left image is changed.
  208. /// </summary>
  209. /// <param name="img">The new image.</param>
  210. public void UpdateLeftImage(Image img)
  211. {
  212. programView.DisplayInLeftPictureBox(img);
  213. }
  214. /// <summary>
  215. /// Is called by the model when the right image is changed.
  216. /// </summary>
  217. /// <param name="img">The new image.</param>
  218. public void UpdateRightImage(Image img)
  219. {
  220. programView.DisplayInRightPictureBox(img);
  221. }
  222. /// <summary>
  223. /// Pass-trough function to display an info message in the view.
  224. /// </summary>
  225. /// <param name="msg">The message.</param>
  226. public void PassMessageToView(String msg)
  227. {
  228. programView.ShowInfoMessage(msg);
  229. }
  230. /// <summary>
  231. /// Pass-trough function to update the display of the last action taken.
  232. /// </summary>
  233. /// <param name="msg">The new last action taken.</param>
  234. public void PassLastActionTaken(String msg)
  235. {
  236. programView.SetLastActionTakenText(msg);
  237. }
  238. /*************************/
  239. /*** HELPING FUNCTIONS ***/
  240. /*************************/
  241. /// <summary>
  242. /// A function that calculates the coordinates of a point on a zoomed in image.
  243. /// </summary>
  244. /// <param name="">The position of the mouse cursor</param>
  245. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  246. private Point ConvertCoordinates(Point cursorPosition)
  247. {
  248. var rightImageDimensions = programModel.GetRightImageDimensions();
  249. Point realCoordinates = new Point(0, 0);
  250. int widthImage = rightImageDimensions.Item1;
  251. int heightImage = rightImageDimensions.Item2;
  252. int widthBox = programModel.rightImageBoxWidth;
  253. int heightBox = programModel.rightImageBoxHeight;
  254. if (heightImage == 0 && widthImage == 0)
  255. {
  256. return cursorPosition;
  257. }
  258. float imageRatio = (float)widthImage / (float)heightImage;
  259. float containerRatio = (float)widthBox / (float)heightBox;
  260. if (imageRatio >= containerRatio)
  261. {
  262. //Image is wider than it is high
  263. float zoomFactor = (float)widthImage / (float)widthBox;
  264. float scaledHeight = heightImage / zoomFactor;
  265. float filler = (heightBox - scaledHeight) / 2;
  266. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  267. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  268. }
  269. else
  270. {
  271. //Image is higher than it is wide
  272. float zoomFactor = (float)heightImage / (float)heightBox;
  273. float scaledWidth = widthImage / zoomFactor;
  274. float filler = (widthBox - scaledWidth) / 2;
  275. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  276. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  277. }
  278. return realCoordinates;
  279. }
  280. }
  281. }