MVP_Presenter.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /// Instance of FileImporter to handle drawing imports.
  35. /// </summary>
  36. private FileImporter fileImporter;
  37. public MVP_Presenter(Form1 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. public void Resize(Tuple<int, int> leftPBS,Tuple<int, int> rightPBS)
  48. {
  49. programView.Refresh();
  50. programModel.leftImageBoxWidth = leftPBS.Item1;
  51. programModel.leftImageBoxHeight = leftPBS.Item2;
  52. programModel.rightImageBoxWidth = rightPBS.Item1;
  53. programModel.rightImageBoxHeight = rightPBS.Item2;
  54. programModel.UpdateSizes();
  55. }
  56. /// <summary>
  57. /// Display a new FileDialog to load an image of type jpg or png.
  58. /// </summary>
  59. public void LoadToolStripMenuItemClick()
  60. {
  61. var fileNameTup = programView.openNewDialog("Image|*.jpg;*.png;*.jpeg");
  62. if(!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  63. {
  64. }
  65. }
  66. /// <summary>
  67. /// Display a new FileDialog to load a collection of lines.
  68. /// </summary>
  69. public void ExamplePictureToolStripMenuItemClick()
  70. {
  71. var okToContinue = true;
  72. if (programModel.HasUnsavedProgress())
  73. {
  74. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  75. }
  76. if (okToContinue)
  77. {
  78. var fileNameTup = programView.openNewDialog("Interactive Sketch-Assistant Drawing|*.isad");
  79. if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
  80. {
  81. programView.SetToolStripLoadStatus(fileNameTup.Item2);
  82. (int, int, List<Line>) values = fileImporter.ParseISADInputFile(fileNameTup.Item1);
  83. programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
  84. programModel.ChangeState(true);
  85. programView.EnableTimer();
  86. }
  87. }
  88. }
  89. public void ChangeState(bool NowDrawing)
  90. {
  91. programModel.ChangeState(NowDrawing);
  92. }
  93. public void Undo()
  94. {
  95. programModel.Undo();
  96. }
  97. public void Redo()
  98. {
  99. programModel.Redo();
  100. }
  101. public void NewCanvas()
  102. {
  103. var okToContinue = true;
  104. if (programModel.HasUnsavedProgress())
  105. {
  106. okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
  107. }
  108. if (okToContinue)
  109. {
  110. programModel.DrawEmptyCanvasRight();
  111. programModel.ChangeState(true);
  112. programView.EnableTimer();
  113. }
  114. }
  115. public void Tick()
  116. {
  117. programModel.Tick();
  118. }
  119. public void MouseEvent(MouseAction mouseAction, MouseEventArgs e)
  120. {
  121. switch (mouseAction)
  122. {
  123. case MouseAction.Click:
  124. break;
  125. case MouseAction.Down:
  126. programModel.MouseDown();
  127. break;
  128. case MouseAction.Up:
  129. programModel.MouseUp();
  130. break;
  131. case MouseAction.Move:
  132. programModel.SetCurrentCursorPosition(ConvertCoordinates(new Point(e.X, e.Y)));
  133. break;
  134. }
  135. }
  136. /************************************/
  137. /*** FUNCTIONS MODEL -> PRESENTER ***/
  138. /************************************/
  139. /// <summary>
  140. /// Called by the model when the state of the Program changes.
  141. /// Changes the look of the UI according to the current state of the model.
  142. /// </summary>
  143. /// <param name="inDrawingMode">If the model is in Drawing Mode</param>
  144. /// <param name="canUndo">If actions in the model can be undone</param>
  145. /// <param name="canRedo">If actions in the model can be redone</param>
  146. /// <param name="imageLoaded">If an image is loaded in the model</param>
  147. public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool imageLoaded)
  148. {
  149. Dictionary<String, Form1.ButtonState> dict = new Dictionary<String, Form1.ButtonState> {
  150. {"canvasButton", Form1.ButtonState.Enabled }, {"drawButton", Form1.ButtonState.Disabled}, {"deleteButton",Form1.ButtonState.Disabled },
  151. {"undoButton", Form1.ButtonState.Disabled },{"redoButton", Form1.ButtonState.Disabled}};
  152. if (imageLoaded)
  153. {
  154. if (inDrawingMode)
  155. {
  156. dict["drawButton"] = Form1.ButtonState.Active;
  157. dict["deleteButton"] = Form1.ButtonState.Enabled;
  158. }
  159. else
  160. {
  161. dict["drawButton"] = Form1.ButtonState.Enabled;
  162. dict["deleteButton"] = Form1.ButtonState.Active;
  163. }
  164. if (canUndo){dict["undoButton"] = Form1.ButtonState.Enabled;}
  165. if (canRedo){dict["redoButton"] = Form1.ButtonState.Enabled;}
  166. }
  167. foreach(KeyValuePair<String, Form1.ButtonState> entry in dict)
  168. {
  169. programView.SetToolStripButtonStatus(entry.Key, entry.Value);
  170. }
  171. }
  172. /// <summary>
  173. /// Is called by the model when the left image is changed.
  174. /// </summary>
  175. /// <param name="img">The new image.</param>
  176. public void UpdateLeftImage(Image img)
  177. {
  178. programView.DisplayInLeftPictureBox(img);
  179. }
  180. /// <summary>
  181. /// Is called by the model when the right image is changed.
  182. /// </summary>
  183. /// <param name="img">The new image.</param>
  184. public void UpdateRightImage(Image img)
  185. {
  186. programView.DisplayInRightPictureBox(img);
  187. }
  188. public void PassMessageToView(String msg)
  189. {
  190. programView.ShowInfoMessage(msg);
  191. }
  192. public void PassLastActionTaken(String msg)
  193. {
  194. programView.SetLastActionTakenText(msg);
  195. }
  196. /*************************/
  197. /*** HELPING FUNCTIONS ***/
  198. /*************************/
  199. /// <summary>
  200. /// A function that calculates the coordinates of a point on a zoomed in image.
  201. /// </summary>
  202. /// <param name="">The position of the mouse cursor</param>
  203. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  204. private Point ConvertCoordinates(Point cursorPosition)
  205. {
  206. var rightImageDimensions = programModel.GetRightImageDimensions();
  207. Point realCoordinates = new Point(0, 0);
  208. int widthImage = rightImageDimensions.Item1;
  209. int heightImage = rightImageDimensions.Item2;
  210. int widthBox = programModel.rightImageBoxWidth;
  211. int heightBox = programModel.rightImageBoxHeight;
  212. if (heightImage == 0 && widthImage == 0)
  213. {
  214. return cursorPosition;
  215. }
  216. float imageRatio = (float)widthImage / (float)heightImage;
  217. float containerRatio = (float)widthBox / (float)heightBox;
  218. if (imageRatio >= containerRatio)
  219. {
  220. //Image is wider than it is high
  221. float zoomFactor = (float)widthImage / (float)widthBox;
  222. float scaledHeight = heightImage / zoomFactor;
  223. float filler = (heightBox - scaledHeight) / 2;
  224. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  225. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  226. }
  227. else
  228. {
  229. //Image is higher than it is wide
  230. float zoomFactor = (float)heightImage / (float)heightBox;
  231. float scaledWidth = widthImage / zoomFactor;
  232. float filler = (widthBox - scaledWidth) / 2;
  233. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  234. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  235. }
  236. return realCoordinates;
  237. }
  238. }
  239. }