MVP_Presenter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /// <summary>
  84. /// Pass-trough function to change the drawing state of the model.
  85. /// </summary>
  86. /// <param name="NowDrawing">Indicates if the program is in drawing (true) or deletion (false) mode.</param>
  87. public void ChangeState(bool NowDrawing)
  88. {
  89. programModel.ChangeState(NowDrawing);
  90. }
  91. /// <summary>
  92. /// Pass-trough function to undo an action.
  93. /// </summary>
  94. public void Undo()
  95. {
  96. programModel.Undo();
  97. }
  98. /// <summary>
  99. /// Pass-trough function to redo an action.
  100. /// </summary>
  101. public void Redo()
  102. {
  103. programModel.Redo();
  104. }
  105. /// <summary>
  106. /// Checks if there is unsaved progress, and promts the model to generate a new canvas if not.
  107. /// </summary>
  108. public void NewCanvas()
  109. {
  110. var okToContinue = true;
  111. if (programModel.HasUnsavedProgress())
  112. {
  113. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  114. }
  115. if (okToContinue)
  116. {
  117. programModel.DrawEmptyCanvasRight();
  118. programModel.ChangeState(true);
  119. programView.EnableTimer();
  120. }
  121. }
  122. /// <summary>
  123. /// Pass-trough function for ticking the model.
  124. /// </summary>
  125. public void Tick()
  126. {
  127. programModel.Tick();
  128. }
  129. /// <summary>
  130. /// Pass-trough when the mouse is moved.
  131. /// </summary>
  132. /// <param name="mouseAction">The action which is sent by the View.</param>
  133. /// <param name="e">The Mouse event arguments.</param>
  134. public void MouseEvent(MouseAction mouseAction, MouseEventArgs e)
  135. {
  136. switch (mouseAction)
  137. {
  138. case MouseAction.Move:
  139. programModel.SetCurrentCursorPosition(ConvertCoordinates(new Point(e.X, e.Y)));
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. /// <summary>
  146. /// Pass-trough function that calls the correct Mouse event of the model, when the mouse is clicked.
  147. /// </summary>
  148. /// <param name="mouseAction">The action which is sent by the View.</param>
  149. /// <param name="e">The Mouse event arguments.</param>
  150. public void MouseEvent(MouseAction mouseAction)
  151. {
  152. switch (mouseAction)
  153. {
  154. case MouseAction.Click:
  155. programModel.MouseDown();
  156. programModel.Tick();
  157. programModel.MouseUp();
  158. break;
  159. case MouseAction.Down:
  160. programModel.MouseDown();
  161. break;
  162. case MouseAction.Up:
  163. programModel.MouseUp();
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. /************************************/
  170. /*** FUNCTIONS MODEL -> PRESENTER ***/
  171. /************************************/
  172. /// <summary>
  173. /// Called by the model when the state of the Program changes.
  174. /// Changes the look of the UI according to the current state of the model.
  175. /// </summary>
  176. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  177. /// <param name="canUndo">If actions in the model can be undone</param>
  178. /// <param name="canRedo">If actions in the model can be redone</param>
  179. /// <param name="imageLoaded">If an image is loaded in the model</param>
  180. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
  181. {
  182. Dictionary<String, Form1.ButtonState> dict = new Dictionary<String, Form1.ButtonState> {
  183. {"canvasButton", Form1.ButtonState.Enabled }, {"drawButton", Form1.ButtonState.Disabled}, {"deleteButton",Form1.ButtonState.Disabled },
  184. {"undoButton", Form1.ButtonState.Disabled },{"redoButton", Form1.ButtonState.Disabled}};
  185. if (imageLoaded)
  186. {
  187. if (inDrawingMode)
  188. {
  189. dict["drawButton"] = Form1.ButtonState.Active;
  190. dict["deleteButton"] = Form1.ButtonState.Enabled;
  191. }
  192. else
  193. {
  194. dict["drawButton"] = Form1.ButtonState.Enabled;
  195. dict["deleteButton"] = Form1.ButtonState.Active;
  196. }
  197. if (canUndo){dict["undoButton"] = Form1.ButtonState.Enabled;}
  198. if (canRedo){dict["redoButton"] = Form1.ButtonState.Enabled;}
  199. }
  200. foreach(KeyValuePair<String, Form1.ButtonState> entry in dict)
  201. {
  202. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  203. }
  204. }
  205. /// <summary>
  206. /// Is called by the model when the left image is changed.
  207. /// </summary>
  208. /// <param name="img">The new image.</param>
  209. public void UpdateLeftImage(Image img)
  210. {
  211. programView.DisplayInLeftPictureBox(img);
  212. }
  213. /// <summary>
  214. /// Is called by the model when the right image is changed.
  215. /// </summary>
  216. /// <param name="img">The new image.</param>
  217. public void UpdateRightImage(Image img)
  218. {
  219. programView.DisplayInRightPictureBox(img);
  220. }
  221. /// <summary>
  222. /// Pass-trough function to display an info message in the view.
  223. /// </summary>
  224. /// <param name="msg">The message.</param>
  225. public void PassMessageToView(String msg)
  226. {
  227. programView.ShowInfoMessage(msg);
  228. }
  229. /// <summary>
  230. /// Pass-trough function to update the display of the last action taken.
  231. /// </summary>
  232. /// <param name="msg">The new last action taken.</param>
  233. public void PassLastActionTaken(String msg)
  234. {
  235. programView.SetLastActionTakenText(msg);
  236. }
  237. /*************************/
  238. /*** HELPING FUNCTIONS ***/
  239. /*************************/
  240. /// <summary>
  241. /// A function that calculates the coordinates of a point on a zoomed in image.
  242. /// </summary>
  243. /// <param name="">The position of the mouse cursor</param>
  244. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  245. private Point ConvertCoordinates(Point cursorPosition)
  246. {
  247. var rightImageDimensions = programModel.GetRightImageDimensions();
  248. Point realCoordinates = new Point(0, 0);
  249. int widthImage = rightImageDimensions.Item1;
  250. int heightImage = rightImageDimensions.Item2;
  251. int widthBox = programModel.rightImageBoxWidth;
  252. int heightBox = programModel.rightImageBoxHeight;
  253. if (heightImage == 0 && widthImage == 0)
  254. {
  255. return cursorPosition;
  256. }
  257. float imageRatio = (float)widthImage / (float)heightImage;
  258. float containerRatio = (float)widthBox / (float)heightBox;
  259. if (imageRatio >= containerRatio)
  260. {
  261. //Image is wider than it is high
  262. float zoomFactor = (float)widthImage / (float)widthBox;
  263. float scaledHeight = heightImage / zoomFactor;
  264. float filler = (heightBox - scaledHeight) / 2;
  265. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  266. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  267. }
  268. else
  269. {
  270. //Image is higher than it is wide
  271. float zoomFactor = (float)heightImage / (float)heightBox;
  272. float scaledWidth = widthImage / zoomFactor;
  273. float filler = (widthBox - scaledWidth) / 2;
  274. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  275. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  276. }
  277. return realCoordinates;
  278. }
  279. }
  280. }