Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.RegularExpressions;
  11. // This is the code for your desktop app.
  12. // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
  13. namespace SketchAssistant
  14. {
  15. public partial class Form1 : Form, MVP_View
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. ProgramPresenter = new MVP_Presenter(this);
  21. }
  22. /**********************************/
  23. /*** CLASS VARIABLES START HERE ***/
  24. /**********************************/
  25. //important: add new variables only at the end of the list to keep the order of definition consistent with the order in which they are returned by GetAllVariables()
  26. public enum ButtonState
  27. {
  28. Enabled,
  29. Disabled,
  30. Active
  31. }
  32. /// <summary>
  33. /// Different Program States
  34. /// </summary>
  35. public enum ProgramState
  36. {
  37. Idle,
  38. Draw,
  39. Delete
  40. }
  41. /// <summary>
  42. /// Dialog to select a file.
  43. /// </summary>
  44. OpenFileDialog openFileDialog = new OpenFileDialog();
  45. /// <summary>
  46. /// All Lines in the current session
  47. /// </summary>
  48. List<Tuple<bool,Line>> rightLineList = new List<Tuple<bool, Line>>();
  49. /// <summary>
  50. /// Queue for the cursorPositions
  51. /// </summary>
  52. Queue<Point> cursorPositions = new Queue<Point>();
  53. /// <summary>
  54. /// The Presenter Component of the MVP-Model
  55. /// </summary>
  56. MVP_Presenter ProgramPresenter;
  57. /******************************************/
  58. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  59. /******************************************/
  60. private void Form1_Load(object sender, EventArgs e)
  61. {
  62. this.DoubleBuffered = true;
  63. }
  64. /// <summary>
  65. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  66. /// </summary>
  67. private void Form1_Resize(object sender, System.EventArgs e)
  68. {
  69. ProgramPresenter.Resize(new Tuple<int, int>(pictureBoxLeft.Width, pictureBoxLeft.Height),
  70. new Tuple<int, int>(pictureBoxRight.Width, pictureBoxRight.Height));
  71. this.Refresh();
  72. }
  73. /// <summary>
  74. /// Import button, will open an OpenFileDialog
  75. /// </summary>
  76. private void examplePictureToolStripMenuItem_Click(object sender, EventArgs e)
  77. {
  78. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  79. }
  80. /// <summary>
  81. /// Changes the state of the program to drawing
  82. /// </summary>
  83. private void drawButton_Click(object sender, EventArgs e)
  84. {
  85. ProgramPresenter.ChangeState(true);
  86. }
  87. /// <summary>
  88. /// Changes the state of the program to deletion
  89. /// </summary>
  90. private void deleteButton_Click(object sender, EventArgs e)
  91. {
  92. ProgramPresenter.ChangeState(false);
  93. }
  94. /// <summary>
  95. /// Undo an Action.
  96. /// </summary>
  97. private void undoButton_Click(object sender, EventArgs e)
  98. {
  99. ProgramPresenter.Undo();
  100. }
  101. /// <summary>
  102. /// Redo an Action.
  103. /// </summary>
  104. private void redoButton_Click(object sender, EventArgs e)
  105. {
  106. ProgramPresenter.Redo();
  107. }
  108. /// <summary>
  109. /// Detect Keyboard Shortcuts.
  110. /// </summary>
  111. private void Form1_KeyDown(object sender, KeyEventArgs e)
  112. {
  113. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z)
  114. {
  115. ProgramPresenter.Undo();
  116. }
  117. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Y)
  118. {
  119. ProgramPresenter.Redo();
  120. }
  121. }
  122. /// <summary>
  123. /// The Picture box is clicked.
  124. /// </summary>
  125. private void pictureBoxRight_Click(object sender, EventArgs e)
  126. {
  127. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Click);
  128. }
  129. /// <summary>
  130. /// Get current Mouse positon within the right picture box.
  131. /// </summary>
  132. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  133. {
  134. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e);
  135. }
  136. /// <summary>
  137. /// Hold left mouse button to start drawing.
  138. /// </summary>
  139. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  140. {
  141. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  142. }
  143. /// <summary>
  144. /// Lift left mouse button to stop drawing and add a new Line.
  145. /// </summary>
  146. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  147. {
  148. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  149. }
  150. /// <summary>
  151. /// Button to create a new Canvas. Will create an empty image
  152. /// which is the size of the left image, if there is one.
  153. /// If there is no image loaded the canvas will be the size of the right picture box
  154. /// </summary>
  155. private void canvasButton_Click(object sender, EventArgs e)
  156. {
  157. ProgramPresenter.NewCanvas();
  158. }
  159. /// <summary>
  160. /// Add a Point on every tick to the Drawpath.
  161. /// Or detect lines for deletion on every tick
  162. /// </summary>
  163. private void mouseTimer_Tick(object sender, EventArgs e)
  164. {
  165. ProgramPresenter.Tick();
  166. }
  167. /*************************/
  168. /*** PRESENTER -> VIEW ***/
  169. /*************************/
  170. /// <summary>
  171. /// Enables the timer of the View, which will tick the Presenter.
  172. /// </summary>
  173. public void EnableTimer()
  174. {
  175. mouseTimer.Enabled = true;
  176. }
  177. /// <summary>
  178. /// A function that opens a file dialog and returns the filename.
  179. /// </summary>
  180. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  181. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  182. /// else returns a tuple with empty strigns</returns>
  183. public Tuple<String, String> openNewDialog(String Filter)
  184. {
  185. openFileDialog.Filter = Filter;
  186. if (openFileDialog.ShowDialog() == DialogResult.OK)
  187. {
  188. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  189. }
  190. else
  191. {
  192. return new Tuple<string, string>("", "");
  193. }
  194. }
  195. /// <summary>
  196. /// Sets the contents of the load status indicator label.
  197. /// </summary>
  198. /// <param name="message">The new contents</param>
  199. public void SetToolStripLoadStatus(String message)
  200. {
  201. toolStripLoadStatus.Text = message;
  202. }
  203. /// <summary>
  204. /// Sets the contents of the last action taken indicator label.
  205. /// </summary>
  206. /// <param name="message">The new contents</param>
  207. public void SetLastActionTakenText(String message)
  208. {
  209. lastActionTakenLabel.Text = message;
  210. }
  211. /// <summary>
  212. /// Changes the states of a tool strip button.
  213. /// </summary>
  214. /// <param name="buttonName">The name of the button.</param>
  215. /// <param name="state">The new state of the button.</param>
  216. public void SetToolStripButtonStatus(String buttonName, ButtonState state)
  217. {
  218. ToolStripButton buttonToChange;
  219. switch (buttonName)
  220. {
  221. case "canvasButton":
  222. buttonToChange = canvasButton;
  223. break;
  224. case "drawButton":
  225. buttonToChange = drawButton;
  226. break;
  227. case "deleteButton":
  228. buttonToChange = deleteButton;
  229. break;
  230. case "undoButton":
  231. buttonToChange = undoButton;
  232. break;
  233. case "redoButton":
  234. buttonToChange = redoButton;
  235. break;
  236. default:
  237. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  238. return;
  239. }
  240. switch (state)
  241. {
  242. case ButtonState.Active:
  243. buttonToChange.Checked = true;
  244. break;
  245. case ButtonState.Disabled:
  246. buttonToChange.Checked = false;
  247. buttonToChange.Enabled = false;
  248. break;
  249. case ButtonState.Enabled:
  250. buttonToChange.Checked = false;
  251. buttonToChange.Enabled = true;
  252. break;
  253. }
  254. }
  255. /// <summary>
  256. /// Displays an image in the left Picture box.
  257. /// </summary>
  258. /// <param name="img">The new image.</param>
  259. public void DisplayInLeftPictureBox(Image img)
  260. {
  261. pictureBoxLeft.Image = img;
  262. pictureBoxLeft.Refresh();
  263. }
  264. /// <summary>
  265. /// Displays an image in the right Picture box.
  266. /// </summary>
  267. /// <param name="img">The new image.</param>
  268. public void DisplayInRightPictureBox(Image img)
  269. {
  270. pictureBoxRight.Image = img;
  271. pictureBoxRight.Refresh();
  272. }
  273. /// <summary>
  274. /// shows the given info message in a popup and asks the user to aknowledge it
  275. /// </summary>
  276. /// <param name="message">the message to show</param>
  277. public void ShowInfoMessage(String message)
  278. {
  279. MessageBox.Show(message);
  280. }
  281. /// <summary>
  282. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  283. /// </summary>
  284. /// <param name="message">The message of the warning.</param>
  285. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  286. public bool ShowWarning(String message)
  287. {
  288. return (MessageBox.Show(message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes);
  289. }
  290. }
  291. }