MVP_Presenter.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. Form1 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. /// A dialog responsible for opening files.
  35. /// </summary>
  36. OpenFileDialog openFileDialog;
  37. /// <summary>
  38. /// Instance of FileImporter to handle drawing imports.
  39. /// </summary>
  40. private FileImporter fileImporter;
  41. public MVP_Presenter(Form1 form)
  42. {
  43. programView = form;
  44. programModel = new MVP_Model(this);
  45. //Initialize Class Variables
  46. openFileDialog = new OpenFileDialog();
  47. fileImporter = new FileImporter();
  48. }
  49. /***********************************/
  50. /*** FUNCTIONS VIEW -> PRESENTER ***/
  51. /***********************************/
  52. public void Resize(Tuple<int, int> leftPBS,Tuple<int, int> rightPBS)
  53. {
  54. programView.Refresh();
  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 an image.
  63. /// </summary>
  64. public void LoadToolStripMenuItemClick()
  65. {
  66. openFileDialog.Filter = "Image|*.jpg;*.png;*.jpeg";
  67. if (openFileDialog.ShowDialog() == DialogResult.OK)
  68. {
  69. ProgramView.SetToolStripLoadStatus(openFileDialog.SafeFileName);
  70. ProgramModel
  71. leftImage = Image.FromFile(openFileDialog.FileName);
  72. pictureBoxLeft.Image = leftImage;
  73. //Refresh the left image box when the content is changed
  74. this.Refresh();
  75. }
  76. UpdateButtonStatus();
  77. }
  78. /// <summary>
  79. /// Display a new FileDialog to load a collection of lines.
  80. /// </summary>
  81. public void ExamplePictureToolStripMenuItemClick()
  82. {
  83. if (ProgramModel.CheckSavedStatus())
  84. {
  85. openFileDialog.Filter = "Interactive Sketch-Assistant Drawing|*.isad";
  86. if (openFileDialog.ShowDialog() == DialogResult.OK)
  87. {
  88. ProgramView.SetToolStripLoadStatus(openFileDialog.SafeFileName);
  89. try
  90. {
  91. (int, int, List<Line>) values = fileImporter.ParseISADInputFile(openFileDialog.FileName);
  92. DrawEmptyCanvasLeft(values.Item1, values.Item2);
  93. BindAndDrawLeftImage(values.Item3);
  94. //Match The right canvas to the left
  95. historyOfActions = new ActionHistory(lastActionTakenLabel);
  96. DrawEmptyCanvasRight();
  97. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  98. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  99. rightLineList = new List<Tuple<bool, Line>>();
  100. //Start the redraw mode
  101. redrawAss = new RedrawAssistant(leftLineList);
  102. UpdateSizes();
  103. overlayItems = redrawAss.Tick(currentCursorPosition, rightLineList, -1, false);
  104. RedrawRightImage();
  105. this.Refresh();
  106. }
  107. catch (FileImporterException ex)
  108. {
  109. ShowInfoMessage(ex.ToString());
  110. }
  111. }
  112. }
  113. UpdateButtonStatus();
  114. }
  115. public void ChangeState(bool NowDrawing)
  116. {
  117. programModel.ChangeState(NowDrawing);
  118. }
  119. public void Undo()
  120. {
  121. }
  122. public void Redo()
  123. {
  124. }
  125. public void NewCanvas()
  126. {
  127. }
  128. public void Tick()
  129. {
  130. }
  131. public void MouseEvent(MouseAction mouseAction, MouseEventArgs e)
  132. {
  133. switch (mouseAction)
  134. {
  135. case MouseAction.Click:
  136. break;
  137. case MouseAction.Down:
  138. break;
  139. case MouseAction.Up:
  140. break;
  141. case MouseAction.Move:
  142. programModel.SetCurrentCursorPosition(ConvertCoordinates(new Point(e.X, e.Y)));
  143. break;
  144. }
  145. }
  146. /************************************/
  147. /*** FUNCTIONS MODEL -> PRESENTER ***/
  148. /************************************/
  149. /// <summary>
  150. /// Called by the model when the state of the Program changes.
  151. /// Changes the look of the UI according to the current state of the model.
  152. /// </summary>
  153. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  154. /// <param name="canUndo">If actions in the model can be undone</param>
  155. /// <param name="canRedo">If actions in the model can be redone</param>
  156. /// <param name="imageLoaded">If an image is loaded in the model</param>
  157. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
  158. {
  159. Dictionary<String, Form1.ButtonState> dict = new Dictionary<String, Form1.ButtonState> {
  160. {"canvasButton", Form1.ButtonState.Disabled }, {"drawButton", Form1.ButtonState.Disabled}, {"deleteButton",Form1.ButtonState.Disabled }
  161. {"undoButton", Form1.ButtonState.Disabled },{"redoButton", Form1.ButtonState.Disabled}};
  162. if (imageLoaded)
  163. {
  164. if (inDrawingMode)
  165. {
  166. dict["drawButton"] = Form1.ButtonState.Active;
  167. dict["deleteButton"] = Form1.ButtonState.Enabled;
  168. }
  169. else
  170. {
  171. dict["drawButton"] = Form1.ButtonState.Enabled;
  172. dict["deleteButton"] = Form1.ButtonState.Active;
  173. }
  174. if (canUndo){dict["undoButton"] = Form1.ButtonState.Enabled;}
  175. if (canRedo){dict["redoButton"] = Form1.ButtonState.Enabled;}
  176. }
  177. foreach(KeyValuePair<String, Form1.ButtonState> entry in dict)
  178. {
  179. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  180. }
  181. }
  182. /// <summary>
  183. /// Is called by the model when the left image is changed.
  184. /// </summary>
  185. /// <param name="img">The new image.</param>
  186. public void UpdateLeftImage(Image img)
  187. {
  188. programView.DisplayInLeftPictureBox(img);
  189. }
  190. /// <summary>
  191. /// Is called by the model when the right image is changed.
  192. /// </summary>
  193. /// <param name="img">The new image.</param>
  194. public void UpdateRightImage(Image img)
  195. {
  196. programView.DisplayInRightPictureBox(img);
  197. }
  198. public void PassMessageToView(String msg)
  199. {
  200. programView.ShowInfoMessage(msg);
  201. }
  202. /*************************/
  203. /*** HELPING FUNCTIONS ***/
  204. /*************************/
  205. /// <summary>
  206. /// A function that calculates the coordinates of a point on a zoomed in image.
  207. /// </summary>
  208. /// <param name="">The position of the mouse cursor</param>
  209. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  210. private Point ConvertCoordinates(Point cursorPosition)
  211. {
  212. var rightImageDimensions = programModel.GetRightImageDimensions();
  213. Point realCoordinates = new Point(0, 0);
  214. int widthImage = rightImageDimensions.Item1;
  215. int heightImage = rightImageDimensions.Item2;
  216. int widthBox = programModel.rightImageBoxWidth;
  217. int heightBox = programModel.rightImageBoxHeight;
  218. if (heightImage == 0 && widthImage == 0)
  219. {
  220. return cursorPosition;
  221. }
  222. float imageRatio = (float)widthImage / (float)heightImage;
  223. float containerRatio = (float)widthBox / (float)heightBox;
  224. if (imageRatio >= containerRatio)
  225. {
  226. //Image is wider than it is high
  227. float zoomFactor = (float)widthImage / (float)widthBox;
  228. float scaledHeight = heightImage / zoomFactor;
  229. float filler = (heightBox - scaledHeight) / 2;
  230. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  231. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  232. }
  233. else
  234. {
  235. //Image is higher than it is wide
  236. float zoomFactor = (float)heightImage / (float)heightBox;
  237. float scaledWidth = widthImage / zoomFactor;
  238. float filler = (widthBox - scaledWidth) / 2;
  239. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  240. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  241. }
  242. return realCoordinates;
  243. }
  244. }
  245. }