MainWindow.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using System.Windows.Threading;
  21. using System.Windows.Ink;
  22. namespace SketchAssistantWPF
  23. {
  24. /// <summary>
  25. /// Interaction logic for MainWindow.xaml
  26. /// </summary>
  27. public partial class MainWindow : Window, MVP_View
  28. {
  29. public MainWindow()
  30. {
  31. bool InDebugMode = false;
  32. String[] commArgs = Environment.GetCommandLineArgs();
  33. InitializeComponent();
  34. if (commArgs.Length > 1)
  35. {
  36. if (commArgs[1].Equals("-debug"))
  37. {
  38. InDebugMode = true;
  39. }
  40. }
  41. if(!InDebugMode)
  42. {
  43. DebugMode.Visibility = Visibility.Collapsed;
  44. }
  45. ProgramPresenter = new MVP_Presenter(this);
  46. // DispatcherTimer setup
  47. dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
  48. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  49. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  50. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
  51. new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
  52. }
  53. public enum ButtonState
  54. {
  55. Enabled,
  56. Disabled,
  57. Active
  58. }
  59. DispatcherTimer dispatcherTimer;
  60. /// <summary>
  61. /// Dialog to select a file.
  62. /// </summary>
  63. OpenFileDialog openFileDialog = new OpenFileDialog();
  64. /// <summary>
  65. /// All Lines in the current session
  66. /// </summary>
  67. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  68. /// <summary>
  69. /// Queue for the cursorPositions
  70. /// </summary>
  71. Queue<Point> cursorPositions = new Queue<Point>();
  72. /// <summary>
  73. /// The Presenter Component of the MVP-Model
  74. /// </summary>
  75. MVP_Presenter ProgramPresenter;
  76. /// <summary>
  77. /// The line currently being drawn
  78. /// </summary>
  79. Polyline currentLine;
  80. /// <summary>
  81. /// If the debug function is running.
  82. /// </summary>
  83. bool debugRunning = false;
  84. /// <summary>
  85. /// Point collections for debugging.
  86. /// </summary>
  87. DebugData debugDat = new DebugData();
  88. /// <summary>
  89. /// Stores Lines drawn on RightCanvas.
  90. /// </summary>
  91. StrokeCollection strokeCollection = new StrokeCollection();
  92. /********************************************/
  93. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  94. /********************************************/
  95. /// <summary>
  96. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  97. /// </summary>
  98. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  99. {
  100. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
  101. new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
  102. }
  103. /// <summary>
  104. /// Collects all Strokes on RightCanvas
  105. /// </summary>
  106. public void RightCanvas_StrokeCollection(object sender, InkCanvasStrokeCollectedEventArgs e)
  107. {
  108. strokeCollection.Add(e.Stroke);
  109. //System.Diagnostics.Debug.WriteLine(strokeCollection.Count);
  110. }
  111. /// <summary>
  112. /// Redo an Action.
  113. /// </summary>
  114. private void RedoButton_Click(object sender, RoutedEventArgs e)
  115. {
  116. ProgramPresenter.Redo();
  117. }
  118. /// <summary>
  119. /// Undo an Action.
  120. /// </summary>
  121. private void UndoButton_Click(object sender, RoutedEventArgs e)
  122. {
  123. ProgramPresenter.Undo();
  124. }
  125. /// <summary>
  126. /// Changes the state of the program to deletion
  127. /// </summary>
  128. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  129. {
  130. ProgramPresenter.ChangeState(false);
  131. RightCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  132. }
  133. /// <summary>
  134. /// Changes the state of the program to drawing
  135. /// </summary>
  136. private void DrawButton_Click(object sender, RoutedEventArgs e)
  137. {
  138. ProgramPresenter.ChangeState(true);
  139. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  140. }
  141. /// <summary>
  142. /// Hold left mouse button to start drawing.
  143. /// </summary>
  144. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  145. {
  146. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  147. //System.Diagnostics.Debug.WriteLine("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 RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  153. {
  154. if(strokeCollection.Count == 0)
  155. {
  156. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up_Invalid);
  157. }
  158. else
  159. {
  160. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  161. RightCanvas.Strokes.RemoveAt(0);
  162. strokeCollection.RemoveAt(0);
  163. //System.Diagnostics.Debug.WriteLine(strokeCollection.Count);
  164. }
  165. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);");
  166. }
  167. /// <summary>
  168. /// Get current Mouse positon within the right picture box.
  169. /// </summary>
  170. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  171. {
  172. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  173. //System.Diagnostics.Debug.WriteLine("new Point(" + ((int)e.GetPosition(RightCanvas).X).ToString() + "," + ((int)e.GetPosition(RightCanvas).Y).ToString() + "), ");
  174. }
  175. /// <summary>
  176. /// Button to create a new Canvas. Will create an empty image
  177. /// which is the size of the left image, if there is one.
  178. /// If there is no image loaded the canvas will be the size of the right picture box
  179. /// </summary>
  180. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  181. {
  182. ProgramPresenter.NewCanvas();
  183. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  184. RightCanvas.Strokes.Clear();
  185. }
  186. /// <summary>
  187. /// Ticks the Presenter.
  188. /// </summary>
  189. private void dispatcherTimer_Tick(object sender, EventArgs e)
  190. {
  191. ProgramPresenter.Tick();
  192. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  193. }
  194. /// <summary>
  195. /// Import button for .isad drawing, will open an OpenFileDialog
  196. /// </summary>
  197. private void ISADMenuItem_Click(object sender, RoutedEventArgs e)
  198. {
  199. if(ProgramPresenter.ExamplePictureToolStripMenuItemClick())
  200. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  201. }
  202. /// <summary>
  203. /// Import button for .svg file, will open an OpenFileDialog
  204. /// </summary>
  205. private void SVGMenuItem_Click(object sender, RoutedEventArgs e)
  206. {
  207. if(ProgramPresenter.SVGToolStripMenuItemClick())
  208. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  209. }
  210. /*************************/
  211. /*** PRESENTER -> VIEW ***/
  212. /*************************/
  213. /// <summary>
  214. /// Returns the cursor position.
  215. /// </summary>
  216. /// <returns>The cursor Position</returns>
  217. public Point GetCursorPosition()
  218. {
  219. return Mouse.GetPosition(RightCanvas);
  220. }
  221. /// <summary>
  222. /// If the mouse is pressed or not.
  223. /// </summary>
  224. /// <returns>Whether or not the mouse is pressed.</returns>
  225. public bool IsMousePressed()
  226. {
  227. if (!debugRunning) {
  228. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  229. else return true;
  230. }
  231. /// <summary>
  232. /// Remove the current line.
  233. /// </summary>
  234. public void RemoveCurrLine()
  235. {
  236. RightCanvas.Children.Remove(currentLine);
  237. }
  238. /// <summary>
  239. /// Display the current line.
  240. /// </summary>
  241. /// <param name="line">The current line to display</param>
  242. public void DisplayCurrLine(Polyline line)
  243. {
  244. if (RightCanvas.Children.Contains(currentLine))
  245. {
  246. RemoveCurrLine();
  247. }
  248. RightCanvas.Children.Add(line);
  249. currentLine = line;
  250. }
  251. /// <summary>
  252. /// Removes all Lines from the left canvas.
  253. /// </summary>
  254. public void RemoveAllLeftLines()
  255. {
  256. LeftCanvas.Children.Clear();
  257. }
  258. /// <summary>
  259. /// Removes all lines in the right canvas.
  260. /// </summary>
  261. public void RemoveAllRightLines()
  262. {
  263. RightCanvas.Children.Clear();
  264. }
  265. /// <summary>
  266. /// Adds another Line that will be displayed in the left display.
  267. /// </summary>
  268. /// <param name="newLine">The new Polyline to be added displayed.</param>
  269. public void AddNewLineLeft(Polyline newLine)
  270. {
  271. newLine.Stroke = Brushes.Black;
  272. newLine.StrokeThickness = 2;
  273. LeftCanvas.Children.Add(newLine);
  274. }
  275. /// <summary>
  276. /// Adds another Line that will be displayed in the right display.
  277. /// </summary>
  278. /// <param name="newLine">The new Polyline to be added displayed.</param>
  279. public void AddNewLineRight(Polyline newLine)
  280. {
  281. newLine.Stroke = Brushes.Black;
  282. newLine.StrokeThickness = 2;
  283. RightCanvas.Children.Add(newLine);
  284. }
  285. /// <summary>
  286. /// Adds a point to the right canvas
  287. /// </summary>
  288. /// <param name="newPoint">The point</param>
  289. public void AddNewPointRight(Ellipse newPoint, InternalLine line)
  290. {
  291. newPoint.Height = 3; newPoint.Width = 3;
  292. newPoint.Fill = Brushes.Black;
  293. RightCanvas.Children.Add(newPoint);
  294. newPoint.Margin = new Thickness(line.point.X - 1.5, line.point.Y - 1.5, 0,0);
  295. }
  296. /// <summary>
  297. /// Adds a point to the left canvas
  298. /// </summary>
  299. /// <param name="newPoint">The point</param>
  300. public void AddNewPointLeft(Ellipse newPoint)
  301. {
  302. newPoint.Height = 3; newPoint.Width = 3;
  303. newPoint.Fill = Brushes.Black;
  304. LeftCanvas.Children.Add(newPoint);
  305. }
  306. /// <summary>
  307. /// Enables the timer of the View, which will tick the Presenter.
  308. /// </summary>
  309. public void EnableTimer()
  310. {
  311. dispatcherTimer.Start();
  312. }
  313. /// <summary>
  314. /// A function that opens a file dialog and returns the filename.
  315. /// </summary>
  316. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  317. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  318. /// else returns a tuple with empty strigns</returns>
  319. public Tuple<string, string> openNewDialog(string Filter)
  320. {
  321. openFileDialog.Filter = Filter;
  322. if (openFileDialog.ShowDialog() == true)
  323. {
  324. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  325. }
  326. else
  327. {
  328. return new Tuple<string, string>("", "");
  329. }
  330. }
  331. /// <summary>
  332. /// Sets the contents of the last action taken indicator label.
  333. /// </summary>
  334. /// <param name="message">The new contents</param>
  335. public void SetLastActionTakenText(string message)
  336. {
  337. LastActionBox.Text = message;
  338. }
  339. /// <summary>
  340. /// Sets the contents of the status bar label containing
  341. /// the similarity score of the left and right image.
  342. /// </summary>
  343. /// <param name="message">The message to be set,
  344. /// will be set to the default value if left empty.</param>
  345. public void SetImageSimilarityText(string message)
  346. {
  347. if (message.Count() > 0) LineSimilarityBox.Text = message;
  348. else LineSimilarityBox.Text = "-";
  349. }
  350. /// <summary>
  351. /// Changes the states of a tool strip button.
  352. /// </summary>
  353. /// <param name="buttonName">The name of the button.</param>
  354. /// <param name="state">The new state of the button.</param>
  355. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  356. {
  357. ButtonBase buttonToChange;
  358. bool isToggleable = false;
  359. switch (buttonName)
  360. {
  361. case "canvasButton":
  362. buttonToChange = CanvasButton;
  363. break;
  364. case "drawButton":
  365. buttonToChange = DrawButton;
  366. isToggleable = true;
  367. break;
  368. case "deleteButton":
  369. buttonToChange = DeleteButton;
  370. isToggleable = true;
  371. break;
  372. case "undoButton":
  373. buttonToChange = UndoButton;
  374. break;
  375. case "redoButton":
  376. buttonToChange = RedoButton;
  377. break;
  378. default:
  379. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  380. return;
  381. }
  382. if (isToggleable)
  383. {
  384. switch (state)
  385. {
  386. case ButtonState.Active:
  387. ((ToggleButton)buttonToChange).IsEnabled = true;
  388. ((ToggleButton)buttonToChange).IsChecked = true;
  389. ((ToggleButton)buttonToChange).Opacity = 1;
  390. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  391. break;
  392. case ButtonState.Disabled:
  393. ((ToggleButton)buttonToChange).IsEnabled = false;
  394. ((ToggleButton)buttonToChange).IsChecked = false;
  395. ((ToggleButton)buttonToChange).Opacity = 0.5;
  396. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  397. break;
  398. case ButtonState.Enabled:
  399. ((ToggleButton)buttonToChange).IsEnabled = true;
  400. ((ToggleButton)buttonToChange).IsChecked = false;
  401. ((ToggleButton)buttonToChange).Opacity = 1;
  402. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  403. break;
  404. }
  405. }
  406. else
  407. {
  408. switch (state)
  409. {
  410. case ButtonState.Disabled:
  411. ((Button)buttonToChange).IsEnabled = false;
  412. ((Button)buttonToChange).Opacity = 0.5;
  413. break;
  414. default:
  415. ((Button)buttonToChange).IsEnabled = true;
  416. ((Button)buttonToChange).Opacity = 1;
  417. break;
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Sets the contents of the load status indicator label.
  423. /// </summary>
  424. /// <param name="message">The new contents</param>
  425. public void SetToolStripLoadStatus(string message)
  426. {
  427. LoadStatusBox.Text = message;
  428. }
  429. /// <summary>
  430. /// shows the given info message in a popup and asks the user to aknowledge it
  431. /// </summary>
  432. /// <param name="message">the message to show</param>
  433. public void ShowInfoMessage(string message)
  434. {
  435. MessageBox.Show(message);
  436. }
  437. /// <summary>
  438. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  439. /// </summary>
  440. /// <param name="message">The message of the warning.</param>
  441. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  442. public bool ShowWarning(string message)
  443. {
  444. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  445. return (result.Equals(MessageBoxResult.Yes));
  446. }
  447. /// <summary>
  448. /// Updates the colour of a canvas.
  449. /// </summary>
  450. /// <param name="canvasName">The name of the canvas to be updated.</param>
  451. /// <param name="active">Whether or not the canvas is active.</param>
  452. public void SetCanvasState(string canvasName, bool active)
  453. {
  454. switch (canvasName)
  455. {
  456. case ("LeftCanvas"):
  457. if (active)
  458. {
  459. LeftCanvas.Background = Brushes.White;
  460. }
  461. else
  462. {
  463. LeftCanvas.Background = Brushes.SlateGray;
  464. }
  465. break;
  466. case ("RightCanvas"):
  467. if (active)
  468. {
  469. RightCanvas.Background = Brushes.White;
  470. }
  471. else
  472. {
  473. RightCanvas.Background = Brushes.SlateGray;
  474. }
  475. break;
  476. default:
  477. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  478. }
  479. }
  480. /************************/
  481. /*** HELPING FUNCTION ***/
  482. /************************/
  483. /// <summary>
  484. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  485. /// Takes 7000ms
  486. /// </summary>
  487. private void DebugOne_Click(object sender, RoutedEventArgs e)
  488. {
  489. Debug(1);
  490. }
  491. /// <summary>
  492. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  493. /// Takes 24000ms
  494. /// </summary>
  495. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  496. {
  497. Debug(2);
  498. }
  499. /// <summary>
  500. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  501. /// Takes 4000ms
  502. /// </summary>
  503. private void DebugThree_Click(object sender, RoutedEventArgs e)
  504. {
  505. Debug(3);
  506. }
  507. /// <summary>
  508. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  509. /// Takes
  510. /// </summary>
  511. private void DebugFour_Click(object sender, RoutedEventArgs e)
  512. {
  513. Debug(4);
  514. }
  515. /// <summary>
  516. /// A function which simulates canvas input for debugging.
  517. /// </summary>
  518. /// <param name="option"></param>
  519. private async void Debug(int option)
  520. {
  521. Point[] points;
  522. Point start = new Point(50, 50);
  523. switch (option)
  524. {
  525. case 1:
  526. points = debugDat.debugPoints1;
  527. break;
  528. case 2:
  529. points = debugDat.debugPoints2;
  530. break;
  531. case 3:
  532. points = debugDat.debugPoints3;
  533. break;
  534. case 4:
  535. points = debugDat.debugPoints4;
  536. start = new Point(284, 148);
  537. break;
  538. default:
  539. return;
  540. }
  541. dispatcherTimer.Stop();
  542. debugRunning = true;
  543. ProgramPresenter.Tick(); await Task.Delay(10);
  544. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, start);
  545. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  546. for (int x = 0; x < points.Length; x++)
  547. {
  548. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  549. await Task.Delay(1);
  550. if (x % 5 == 0)
  551. {
  552. ProgramPresenter.Tick();
  553. await Task.Delay(1);
  554. }
  555. }
  556. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  557. debugRunning = false;
  558. dispatcherTimer.Start();
  559. }
  560. }
  561. }