MVP_Presenter.cs 8.8 KB

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