Form1.cs 11 KB

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