MVP_Presenter.cs 13 KB

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