Form1.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. fileImporter = new FileImporter(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. /// <summary>
  27. /// Different Program States
  28. /// </summary>
  29. public enum ProgramState
  30. {
  31. Idle,
  32. Draw,
  33. Delete
  34. }
  35. /// <summary>
  36. /// Current Program State
  37. /// </summary>
  38. private ProgramState currentState;
  39. /// <summary>
  40. /// instance of FileImporter to handle drawing imports
  41. /// </summary>
  42. private FileImporter fileImporter;
  43. /// <summary>
  44. /// Dialog to select a file.
  45. /// </summary>
  46. OpenFileDialog openFileDialog = new OpenFileDialog();
  47. /// <summary>
  48. /// Image loaded on the left
  49. /// </summary>
  50. private Image leftImage = null;
  51. /// <summary>
  52. /// the graphic shown in the left window, represented as a list of polylines
  53. /// </summary>
  54. private List<Line> leftLineList;
  55. /// <summary>
  56. /// Image on the right
  57. /// </summary>
  58. Image rightImage = null;
  59. /// <summary>
  60. /// Current Line being Drawn
  61. /// </summary>
  62. List<Point> currentLine;
  63. /// <summary>
  64. /// All Lines in the current session
  65. /// </summary>
  66. List<Tuple<bool,Line>> rightLineList = new List<Tuple<bool, Line>>();
  67. /// <summary>
  68. /// Whether the Mouse is currently pressed in the rightPictureBox
  69. /// </summary>
  70. bool mousePressed = false;
  71. /// <summary>
  72. /// The Position of the Cursor in the right picture box
  73. /// </summary>
  74. Point currentCursorPosition;
  75. /// <summary>
  76. /// The Previous Cursor Position in the right picture box
  77. /// </summary>
  78. Point previousCursorPosition;
  79. /// <summary>
  80. /// Queue for the cursorPositions
  81. /// </summary>
  82. Queue<Point> cursorPositions = new Queue<Point>();
  83. /// <summary>
  84. /// The graphic representation of the right image
  85. /// </summary>
  86. Graphics rightGraph = null;
  87. /// <summary>
  88. /// Deletion Matrixes for checking postions of lines in the image
  89. /// </summary>
  90. bool[,] isFilledMatrix;
  91. HashSet<int>[,] linesMatrix;
  92. /// <summary>
  93. /// Size of deletion area
  94. /// </summary>
  95. uint deletionSize = 2;
  96. /// <summary>
  97. /// History of Actions
  98. /// </summary>
  99. ActionHistory historyOfActions;
  100. /******************************************/
  101. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  102. /******************************************/
  103. private void Form1_Load(object sender, EventArgs e)
  104. {
  105. currentState = ProgramState.Idle;
  106. this.DoubleBuffered = true;
  107. historyOfActions = new ActionHistory(null);
  108. UpdateButtonStatus();
  109. }
  110. //Resize Function connected to the form resize event, will refresh the form when it is resized
  111. private void Form1_Resize(object sender, System.EventArgs e)
  112. {
  113. this.Refresh();
  114. }
  115. //Load button, will open an OpenFileDialog
  116. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  117. {
  118. openFileDialog.Filter = "Image|*.jpg;*.png;*.jpeg";
  119. if(openFileDialog.ShowDialog() == DialogResult.OK)
  120. {
  121. toolStripLoadStatus.Text = openFileDialog.SafeFileName;
  122. leftImage = Image.FromFile(openFileDialog.FileName);
  123. pictureBoxLeft.Image = leftImage;
  124. //Refresh the left image box when the content is changed
  125. this.Refresh();
  126. }
  127. UpdateButtonStatus();
  128. }
  129. /// <summary>
  130. /// Import button, will open an OpenFileDialog
  131. /// </summary>
  132. private void examplePictureToolStripMenuItem_Click(object sender, EventArgs e)
  133. {
  134. openFileDialog.Filter = "Interactive Sketch-Assistant Drawing|*.isad";
  135. if (openFileDialog.ShowDialog() == DialogResult.OK)
  136. {
  137. toolStripLoadStatus.Text = openFileDialog.SafeFileName;
  138. try
  139. {
  140. (int, int, List<Line>) values = fileImporter.ParseISADInputFile(openFileDialog.FileName);
  141. DrawEmptyCanvasLeft(values.Item1, values.Item2);
  142. BindAndDrawLeftImage(values.Item3);
  143. this.Refresh();
  144. }
  145. catch(FileImporterException ex)
  146. {
  147. ShowInfoMessage(ex.ToString());
  148. }
  149. }
  150. }
  151. //Changes the state of the program to drawing
  152. private void drawButton_Click(object sender, EventArgs e)
  153. {
  154. if(rightImage != null)
  155. {
  156. if (currentState.Equals(ProgramState.Draw))
  157. {
  158. ChangeState(ProgramState.Idle);
  159. }
  160. else
  161. {
  162. ChangeState(ProgramState.Draw);
  163. }
  164. }
  165. UpdateButtonStatus();
  166. }
  167. //Changes the state of the program to deletion
  168. private void deleteButton_Click(object sender, EventArgs e)
  169. {
  170. if (rightImage != null)
  171. {
  172. if (currentState.Equals(ProgramState.Delete))
  173. {
  174. ChangeState(ProgramState.Idle);
  175. }
  176. else
  177. {
  178. ChangeState(ProgramState.Delete);
  179. }
  180. }
  181. UpdateButtonStatus();
  182. }
  183. //Undo an action
  184. private void undoButton_Click(object sender, EventArgs e)
  185. {
  186. if (historyOfActions.CanUndo())
  187. {
  188. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  189. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  190. switch (undoAction)
  191. {
  192. case SketchAction.ActionType.Delete:
  193. //Deleted Lines need to be shown
  194. ChangeLines(affectedLines, true);
  195. break;
  196. case SketchAction.ActionType.Draw:
  197. //Drawn lines need to be hidden
  198. ChangeLines(affectedLines, false);
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. historyOfActions.MoveAction(true);
  205. UpdateButtonStatus();
  206. }
  207. //Redo an action
  208. private void redoButton_Click(object sender, EventArgs e)
  209. {
  210. if (historyOfActions.CanRedo())
  211. {
  212. historyOfActions.MoveAction(false);
  213. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  214. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  215. switch (redoAction)
  216. {
  217. case SketchAction.ActionType.Delete:
  218. //Deleted Lines need to be redeleted
  219. ChangeLines(affectedLines, false);
  220. break;
  221. case SketchAction.ActionType.Draw:
  222. //Drawn lines need to be redrawn
  223. ChangeLines(affectedLines, true);
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. UpdateButtonStatus();
  230. }
  231. //Detect Keyboard Shortcuts
  232. private void Form1_KeyDown(object sender, KeyEventArgs e)
  233. {
  234. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z)
  235. {
  236. undoButton_Click(sender, e);
  237. }
  238. if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Y)
  239. {
  240. redoButton_Click(sender, e);
  241. }
  242. }
  243. //get current Mouse positon within the right picture box
  244. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  245. {
  246. currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
  247. }
  248. //hold left mouse button to draw.
  249. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  250. {
  251. mousePressed = true;
  252. if (currentState.Equals(ProgramState.Draw))
  253. {
  254. currentLine = new List<Point>();
  255. }
  256. }
  257. //Lift left mouse button to stop drawing and add a new Line.
  258. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  259. {
  260. mousePressed = false;
  261. if (currentState.Equals(ProgramState.Draw) && currentLine.Count > 0)
  262. {
  263. Line newLine = new Line(currentLine, rightLineList.Count);
  264. rightLineList.Add(new Tuple<bool, Line>(true, newLine));
  265. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  266. historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID()));
  267. }
  268. UpdateButtonStatus();
  269. }
  270. //Button to create a new Canvas. Will create an empty image
  271. //which is the size of the left image, if there is one.
  272. //If there is no image loaded the canvas will be the size of the right picture box
  273. private void canvasButton_Click(object sender, EventArgs e)
  274. {
  275. if (!historyOfActions.IsEmpty())
  276. {
  277. if (MessageBox.Show("You have unsaved changes, creating a new canvas will discard these.",
  278. "Attention", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
  279. {
  280. historyOfActions = new ActionHistory(lastActionTakenLabel);
  281. DrawEmptyCanvasRight();
  282. //The following lines cannot be in DrawEmptyCanvas()
  283. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  284. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  285. rightLineList = new List<Tuple<bool, Line>>();
  286. }
  287. }
  288. else
  289. {
  290. historyOfActions = new ActionHistory(lastActionTakenLabel);
  291. DrawEmptyCanvasRight();
  292. //The following lines cannot be in DrawEmptyCanvas()
  293. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  294. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  295. rightLineList = new List<Tuple<bool, Line>>();
  296. }
  297. UpdateButtonStatus();
  298. }
  299. //add a Point on every tick to the Drawpath
  300. private void mouseTimer_Tick(object sender, EventArgs e)
  301. {
  302. cursorPositions.Enqueue(currentCursorPosition);
  303. previousCursorPosition = cursorPositions.Dequeue();
  304. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  305. {
  306. currentLine.Add(currentCursorPosition);
  307. Line drawline = new Line(currentLine);
  308. drawline.DrawLine(rightGraph);
  309. pictureBoxRight.Image = rightImage;
  310. }
  311. if (currentState.Equals(ProgramState.Delete) && mousePressed)
  312. {
  313. List<Point> uncheckedPoints = Line.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  314. foreach (Point currPoint in uncheckedPoints)
  315. {
  316. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionSize);
  317. if (linesToDelete.Count > 0)
  318. {
  319. historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete));
  320. foreach (int lineID in linesToDelete)
  321. {
  322. rightLineList[lineID] = new Tuple<bool, Line>(false, rightLineList[lineID].Item2);
  323. }
  324. RepopulateDeletionMatrixes();
  325. RedrawRightImage();
  326. }
  327. }
  328. }
  329. }
  330. /***********************************/
  331. /*** HELPER FUNCTIONS START HERE ***/
  332. /***********************************/
  333. /// <summary>
  334. /// Creates an empty Canvas
  335. /// </summary>
  336. private void DrawEmptyCanvasRight()
  337. {
  338. if (leftImage == null)
  339. {
  340. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  341. rightGraph = Graphics.FromImage(rightImage);
  342. rightGraph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  343. pictureBoxRight.Image = rightImage;
  344. }
  345. else
  346. {
  347. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  348. rightGraph = Graphics.FromImage(rightImage);
  349. rightGraph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  350. pictureBoxRight.Image = rightImage;
  351. }
  352. this.Refresh();
  353. pictureBoxRight.Refresh();
  354. }
  355. /// <summary>
  356. /// Creates an empty Canvas on the left
  357. /// </summary>
  358. /// <param name="width"> width of the new canvas in pixels </param>
  359. /// <param name="height"> height of the new canvas in pixels </param>
  360. private void DrawEmptyCanvasLeft(int width, int height)
  361. {
  362. if (width == 0)
  363. {
  364. leftImage = new Bitmap(pictureBoxLeft.Width, pictureBoxLeft.Height);
  365. }
  366. else
  367. {
  368. leftImage = new Bitmap(width, height);
  369. }
  370. Graphics.FromImage(leftImage).FillRectangle(Brushes.White, 0, 0, pictureBoxLeft.Width + 10, pictureBoxLeft.Height + 10);
  371. pictureBoxLeft.Image = leftImage;
  372. this.Refresh();
  373. pictureBoxLeft.Refresh();
  374. }
  375. /// <summary>
  376. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  377. /// </summary>
  378. private void RedrawRightImage()
  379. {
  380. DrawEmptyCanvasRight();
  381. foreach (Tuple<bool, Line> lineBoolTuple in rightLineList)
  382. {
  383. if (lineBoolTuple.Item1)
  384. {
  385. lineBoolTuple.Item2.DrawLine(rightGraph);
  386. }
  387. }
  388. pictureBoxRight.Refresh();
  389. }
  390. /// <summary>
  391. /// Change the status of whether or not the lines are shown.
  392. /// </summary>
  393. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  394. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  395. private void ChangeLines(HashSet<int> lines, bool shown)
  396. {
  397. foreach (int lineId in lines)
  398. {
  399. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  400. {
  401. rightLineList[lineId] = new Tuple<bool, Line>(shown, rightLineList[lineId].Item2);
  402. }
  403. }
  404. RedrawRightImage();
  405. }
  406. /// <summary>
  407. /// Updates the active status of buttons. Currently draw, delete, undo and redo button.
  408. /// </summary>
  409. private void UpdateButtonStatus()
  410. {
  411. undoButton.Enabled = historyOfActions.CanUndo();
  412. redoButton.Enabled = historyOfActions.CanRedo();
  413. drawButton.Enabled = (rightImage != null);
  414. deleteButton.Enabled = (rightImage != null);
  415. }
  416. /// <summary>
  417. /// A helper function which handles tasks associated witch changing states,
  418. /// such as checking and unchecking buttons and changing the state.
  419. /// </summary>
  420. /// <param name="newState">The new state of the program</param>
  421. private void ChangeState(ProgramState newState)
  422. {
  423. switch (currentState)
  424. {
  425. case ProgramState.Draw:
  426. drawButton.CheckState = CheckState.Unchecked;
  427. mouseTimer.Enabled = false;
  428. break;
  429. case ProgramState.Delete:
  430. deleteButton.CheckState = CheckState.Unchecked;
  431. mouseTimer.Enabled = false;
  432. break;
  433. default:
  434. break;
  435. }
  436. switch (newState)
  437. {
  438. case ProgramState.Draw:
  439. drawButton.CheckState = CheckState.Checked;
  440. mouseTimer.Enabled = true;
  441. break;
  442. case ProgramState.Delete:
  443. deleteButton.CheckState = CheckState.Checked;
  444. mouseTimer.Enabled = true;
  445. break;
  446. default:
  447. break;
  448. }
  449. currentState = newState;
  450. pictureBoxRight.Refresh();
  451. }
  452. /// <summary>
  453. /// A function that calculates the coordinates of a point on a zoomed in image.
  454. /// </summary>
  455. /// <param name="">The position of the mouse cursor</param>
  456. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  457. private Point ConvertCoordinates(Point cursorPosition)
  458. {
  459. Point realCoordinates = new Point(5,3);
  460. if(pictureBoxRight.Image == null)
  461. {
  462. return cursorPosition;
  463. }
  464. int widthImage = pictureBoxRight.Image.Width;
  465. int heightImage = pictureBoxRight.Image.Height;
  466. int widthBox = pictureBoxRight.Width;
  467. int heightBox = pictureBoxRight.Height;
  468. float imageRatio = (float)widthImage / (float)heightImage;
  469. float containerRatio = (float)widthBox / (float)heightBox;
  470. if (imageRatio >= containerRatio)
  471. {
  472. //Image is wider than it is high
  473. float zoomFactor = (float)widthImage / (float)widthBox;
  474. float scaledHeight = heightImage / zoomFactor;
  475. float filler = (heightBox - scaledHeight) / 2;
  476. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  477. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  478. }
  479. else
  480. {
  481. //Image is higher than it is wide
  482. float zoomFactor = (float)heightImage / (float)heightBox;
  483. float scaledWidth = widthImage / zoomFactor;
  484. float filler = (widthBox - scaledWidth) / 2;
  485. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  486. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  487. }
  488. return realCoordinates;
  489. }
  490. /// <summary>
  491. /// A function that populates the matrixes needed for deletion detection with line data.
  492. /// </summary>
  493. private void RepopulateDeletionMatrixes()
  494. {
  495. if(rightImage != null)
  496. {
  497. isFilledMatrix = new bool[rightImage.Width,rightImage.Height];
  498. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  499. foreach(Tuple<bool,Line> lineTuple in rightLineList)
  500. {
  501. if (lineTuple.Item1)
  502. {
  503. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  504. }
  505. }
  506. }
  507. }
  508. /// <summary>
  509. /// A function that checks the deletion matrixes at a certain point
  510. /// and returns all Line ids at that point and in a square around it in a certain range.
  511. /// </summary>
  512. /// <param name="p">The point around which to check.</param>
  513. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  514. /// <returns>A List of all lines.</returns>
  515. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, uint range)
  516. {
  517. HashSet<int> returnSet = new HashSet<int>();
  518. if (p.X >= 0 && p.Y >= 0 && p.X < rightImage.Width && p.Y < rightImage.Height)
  519. {
  520. if (isFilledMatrix[p.X, p.Y])
  521. {
  522. returnSet.UnionWith(linesMatrix[p.X, p.Y]);
  523. }
  524. }
  525. for (int x_mod = (int)range*(-1); x_mod < range; x_mod++)
  526. {
  527. for (int y_mod = (int)range * (-1); y_mod < range; y_mod++)
  528. {
  529. if (p.X + x_mod >= 0 && p.Y + y_mod >= 0 && p.X + x_mod < rightImage.Width && p.Y + y_mod < rightImage.Height)
  530. {
  531. if (isFilledMatrix[p.X + x_mod, p.Y + y_mod])
  532. {
  533. returnSet.UnionWith(linesMatrix[p.X + x_mod, p.Y + y_mod]);
  534. }
  535. }
  536. }
  537. }
  538. return returnSet;
  539. }
  540. /// <summary>
  541. /// binds the given picture to templatePicture and draws it
  542. /// </summary>
  543. /// <param name="newTemplatePicture"> the new template picture, represented as a list of polylines </param>
  544. /// <returns></returns>
  545. private void BindAndDrawLeftImage(List<Line> newTemplatePicture)
  546. {
  547. leftLineList = newTemplatePicture;
  548. foreach(Line l in leftLineList)
  549. {
  550. l.DrawLine(Graphics.FromImage(leftImage));
  551. }
  552. }
  553. /// <summary>
  554. /// shows the given info message in a popup and asks the user to aknowledge it
  555. /// </summary>
  556. /// <param name="message">the message to show</param>
  557. private void ShowInfoMessage(String message)
  558. {
  559. MessageBox.Show(message);
  560. }
  561. /// <summary>
  562. /// returns all instance variables in the order of their definition for testing
  563. /// </summary>
  564. /// <returns>all instance variables in the order of their definition</returns>
  565. public Object[]/*(ProgramState, FileImporter, OpenFileDialog, Image, List<Line>, Image, List<Point>, List<Tuple<bool, Line>>, bool, Point, Point, Queue<Point>, Graphics, bool[,], HashSet<int>[,], uint, ActionHistory)*/ GetAllVariables()
  566. {
  567. return new Object[] { currentState, fileImporter, openFileDialog, leftImage, leftLineList, rightImage, currentLine, rightLineList, mousePressed, currentCursorPosition, previousCursorPosition, cursorPositions, rightGraph, isFilledMatrix, linesMatrix, deletionSize, historyOfActions };
  568. }
  569. /// <summary>
  570. /// public method wrapper for testing purposes, invoking DrawEmptyCanvas(...) and BindAndDrawLeftImage(...)
  571. /// </summary>
  572. /// <param name="width">width of the parsed image</param>
  573. /// <param name="height">height of the parsed image</param>
  574. /// <param name="newImage">the parsed image</param>
  575. public void CreateCanvasAndSetPictureForTesting(int width, int height, List<Line> newImage)
  576. {
  577. DrawEmptyCanvasLeft(width, height);
  578. BindAndDrawLeftImage(newImage);
  579. }
  580. }
  581. }