MVP_Presenter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 function that calls the correct Mouse event of the model.
  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.Click:
  139. break;
  140. case MouseAction.Down:
  141. programModel.MouseDown();
  142. break;
  143. case MouseAction.Up:
  144. programModel.MouseUp();
  145. break;
  146. case MouseAction.Move:
  147. programModel.SetCurrentCursorPosition(ConvertCoordinates(new Point(e.X, e.Y)));
  148. break;
  149. }
  150. }
  151. /************************************/
  152. /*** FUNCTIONS MODEL -> PRESENTER ***/
  153. /************************************/
  154. /// <summary>
  155. /// Called by the model when the state of the Program changes.
  156. /// Changes the look of the UI according to the current state of the model.
  157. /// </summary>
  158. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  159. /// <param name="canUndo">If actions in the model can be undone</param>
  160. /// <param name="canRedo">If actions in the model can be redone</param>
  161. /// <param name="imageLoaded">If an image is loaded in the model</param>
  162. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
  163. {
  164. Dictionary<String, Form1.ButtonState> dict = new Dictionary<String, Form1.ButtonState> {
  165. {"canvasButton", Form1.ButtonState.Enabled }, {"drawButton", Form1.ButtonState.Disabled}, {"deleteButton",Form1.ButtonState.Disabled },
  166. {"undoButton", Form1.ButtonState.Disabled },{"redoButton", Form1.ButtonState.Disabled}};
  167. if (imageLoaded)
  168. {
  169. if (inDrawingMode)
  170. {
  171. dict["drawButton"] = Form1.ButtonState.Active;
  172. dict["deleteButton"] = Form1.ButtonState.Enabled;
  173. }
  174. else
  175. {
  176. dict["drawButton"] = Form1.ButtonState.Enabled;
  177. dict["deleteButton"] = Form1.ButtonState.Active;
  178. }
  179. if (canUndo){dict["undoButton"] = Form1.ButtonState.Enabled;}
  180. if (canRedo){dict["redoButton"] = Form1.ButtonState.Enabled;}
  181. }
  182. foreach(KeyValuePair<String, Form1.ButtonState> entry in dict)
  183. {
  184. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  185. }
  186. }
  187. /// <summary>
  188. /// Is called by the model when the left image is changed.
  189. /// </summary>
  190. /// <param name="img">The new image.</param>
  191. public void UpdateLeftImage(Image img)
  192. {
  193. programView.DisplayInLeftPictureBox(img);
  194. }
  195. /// <summary>
  196. /// Is called by the model when the right image is changed.
  197. /// </summary>
  198. /// <param name="img">The new image.</param>
  199. public void UpdateRightImage(Image img)
  200. {
  201. programView.DisplayInRightPictureBox(img);
  202. }
  203. /// <summary>
  204. /// Pass-trough function to display an info message in the view.
  205. /// </summary>
  206. /// <param name="msg">The message.</param>
  207. public void PassMessageToView(String msg)
  208. {
  209. programView.ShowInfoMessage(msg);
  210. }
  211. /// <summary>
  212. /// Pass-trough function to update the display of the last action taken.
  213. /// </summary>
  214. /// <param name="msg">The new last action taken.</param>
  215. public void PassLastActionTaken(String msg)
  216. {
  217. programView.SetLastActionTakenText(msg);
  218. }
  219. /*************************/
  220. /*** HELPING FUNCTIONS ***/
  221. /*************************/
  222. /// <summary>
  223. /// A function that calculates the coordinates of a point on a zoomed in image.
  224. /// </summary>
  225. /// <param name="">The position of the mouse cursor</param>
  226. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  227. private Point ConvertCoordinates(Point cursorPosition)
  228. {
  229. var rightImageDimensions = programModel.GetRightImageDimensions();
  230. Point realCoordinates = new Point(0, 0);
  231. int widthImage = rightImageDimensions.Item1;
  232. int heightImage = rightImageDimensions.Item2;
  233. int widthBox = programModel.rightImageBoxWidth;
  234. int heightBox = programModel.rightImageBoxHeight;
  235. if (heightImage == 0 && widthImage == 0)
  236. {
  237. return cursorPosition;
  238. }
  239. float imageRatio = (float)widthImage / (float)heightImage;
  240. float containerRatio = (float)widthBox / (float)heightBox;
  241. if (imageRatio >= containerRatio)
  242. {
  243. //Image is wider than it is high
  244. float zoomFactor = (float)widthImage / (float)widthBox;
  245. float scaledHeight = heightImage / zoomFactor;
  246. float filler = (heightBox - scaledHeight) / 2;
  247. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  248. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  249. }
  250. else
  251. {
  252. //Image is higher than it is wide
  253. float zoomFactor = (float)heightImage / (float)heightBox;
  254. float scaledWidth = widthImage / zoomFactor;
  255. float filler = (widthBox - scaledWidth) / 2;
  256. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  257. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  258. }
  259. return realCoordinates;
  260. }
  261. }
  262. }