MainWindow.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. public 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. if (!IsMousePressed()) ProgramPresenter.Redo();
  117. }
  118. /// <summary>
  119. /// Undo an Action.
  120. /// </summary>
  121. private void UndoButton_Click(object sender, RoutedEventArgs e)
  122. {
  123. if(!IsMousePressed()) 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, strokeCollection);
  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, strokeCollection);
  157. }
  158. else
  159. {
  160. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up, strokeCollection);
  161. RightCanvas.Strokes.RemoveAt(0);
  162. strokeCollection.RemoveAt(0);
  163. }
  164. }
  165. /// <summary>
  166. /// Is called when a stylus is lifted, which has the same effect as releasing the mouse.
  167. /// </summary>
  168. private void RightCanvas_IsStylusCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
  169. {
  170. System.Diagnostics.Debug.WriteLine("Stylus Capture is now: {0}", RightCanvas.IsStylusCaptured);
  171. if (!RightCanvas.IsStylusCaptured)
  172. {
  173. if (strokeCollection.Count == 0)
  174. {
  175. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up_Invalid,strokeCollection);
  176. }
  177. else
  178. {
  179. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up, strokeCollection);
  180. RightCanvas.Strokes.RemoveAt(0);
  181. strokeCollection.RemoveAt(0);
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Is called when touch is lifted, which has the same effect as releasing the mouse.
  187. /// </summary>
  188. private void RightCanvas_TouchUp(object sender, TouchEventArgs e)
  189. {
  190. if (strokeCollection.Count == 0)
  191. {
  192. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up_Invalid, strokeCollection);
  193. }
  194. else
  195. {
  196. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up, strokeCollection);
  197. RightCanvas.Strokes.RemoveAt(0);
  198. strokeCollection.RemoveAt(0);
  199. }
  200. }
  201. /// <summary>
  202. /// Get current Mouse positon within the right picture box.
  203. /// </summary>
  204. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  205. {
  206. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  207. //System.Diagnostics.Debug.WriteLine("new Point(" + ((int)e.GetPosition(RightCanvas).X).ToString() + "," + ((int)e.GetPosition(RightCanvas).Y).ToString() + "), ");
  208. }
  209. /// <summary>
  210. /// Button to create a new Canvas. Will create an empty image
  211. /// which is the size of the left image, if there is one.
  212. /// If there is no image loaded the canvas will be the size of the right picture box
  213. /// </summary>
  214. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  215. {
  216. ProgramPresenter.NewCanvas();
  217. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  218. RightCanvas.Strokes.Clear();
  219. }
  220. /// <summary>
  221. /// Ticks the Presenter.
  222. /// </summary>
  223. private void dispatcherTimer_Tick(object sender, EventArgs e)
  224. {
  225. ProgramPresenter.Tick();
  226. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  227. }
  228. /// <summary>
  229. /// Import button for .isad drawing, will open an OpenFileDialog
  230. /// </summary>
  231. private void ISADMenuItem_Click(object sender, RoutedEventArgs e)
  232. {
  233. if(ProgramPresenter.ExamplePictureToolStripMenuItemClick())
  234. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  235. }
  236. /// <summary>
  237. /// Import button for .svg file, will open an OpenFileDialog
  238. /// </summary>
  239. private void SVGMenuItem_Click(object sender, RoutedEventArgs e)
  240. {
  241. if(ProgramPresenter.SVGToolStripMenuItemClick())
  242. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  243. }
  244. /*************************/
  245. /*** PRESENTER -> VIEW ***/
  246. /*************************/
  247. /// <summary>
  248. /// Returns the cursor position.
  249. /// </summary>
  250. /// <returns>The cursor Position</returns>
  251. public Point GetCursorPosition()
  252. {
  253. return Mouse.GetPosition(RightCanvas);
  254. }
  255. /// <summary>
  256. /// If the mouse is pressed or not.
  257. /// </summary>
  258. /// <returns>Whether or not the mouse is pressed.</returns>
  259. public bool IsMousePressed()
  260. {
  261. if (!debugRunning) {
  262. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  263. else return true;
  264. }
  265. /// <summary>
  266. /// Remove the current line.
  267. /// </summary>
  268. public void RemoveCurrLine()
  269. {
  270. RightCanvas.Children.Remove(currentLine);
  271. }
  272. /// <summary>
  273. /// Display the current line.
  274. /// </summary>
  275. /// <param name="line">The current line to display</param>
  276. public void DisplayCurrLine(Polyline line)
  277. {
  278. if (RightCanvas.Children.Contains(currentLine))
  279. {
  280. RemoveCurrLine();
  281. }
  282. RightCanvas.Children.Add(line);
  283. currentLine = line;
  284. }
  285. /// <summary>
  286. /// Removes all Lines from the left canvas.
  287. /// </summary>
  288. public void RemoveAllLeftLines()
  289. {
  290. LeftCanvas.Children.Clear();
  291. }
  292. /// <summary>
  293. /// Removes all lines in the right canvas.
  294. /// </summary>
  295. public void RemoveAllRightLines()
  296. {
  297. RightCanvas.Children.Clear();
  298. }
  299. /// <summary>
  300. /// Adds another Line that will be displayed in the left display.
  301. /// </summary>
  302. /// <param name="newLine">The new Polyline to be added displayed.</param>
  303. public void AddNewLineLeft(Polyline newLine)
  304. {
  305. newLine.Stroke = Brushes.Black;
  306. newLine.StrokeThickness = 2;
  307. LeftCanvas.Children.Add(newLine);
  308. }
  309. /// <summary>
  310. /// Adds another Line that will be displayed in the right display.
  311. /// </summary>
  312. /// <param name="newLine">The new Polyline to be added displayed.</param>
  313. public void AddNewLineRight(Polyline newLine)
  314. {
  315. newLine.Stroke = Brushes.Black;
  316. newLine.StrokeThickness = 2;
  317. RightCanvas.Children.Add(newLine);
  318. }
  319. /// <summary>
  320. /// Adds a point to the right canvas
  321. /// </summary>
  322. /// <param name="newPoint">The point</param>
  323. public void AddNewPointRight(Ellipse newPoint, InternalLine line)
  324. {
  325. newPoint.Height = 3; newPoint.Width = 3;
  326. newPoint.Fill = Brushes.Black;
  327. RightCanvas.Children.Add(newPoint);
  328. newPoint.Margin = new Thickness(line.point.X - 1.5, line.point.Y - 1.5, 0,0);
  329. }
  330. /// <summary>
  331. /// Adds a point to the left canvas
  332. /// </summary>
  333. /// <param name="newPoint">The point</param>
  334. public void AddNewPointLeft(Ellipse newPoint)
  335. {
  336. newPoint.Height = 3; newPoint.Width = 3;
  337. newPoint.Fill = Brushes.Black;
  338. LeftCanvas.Children.Add(newPoint);
  339. }
  340. /// <summary>
  341. /// Enables the timer of the View, which will tick the Presenter.
  342. /// </summary>
  343. public void EnableTimer()
  344. {
  345. dispatcherTimer.Start();
  346. }
  347. /// <summary>
  348. /// A function that opens a file dialog and returns the filename.
  349. /// </summary>
  350. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  351. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  352. /// else returns a tuple with empty strigns</returns>
  353. public Tuple<string, string> openNewDialog(string Filter)
  354. {
  355. openFileDialog.Filter = Filter;
  356. if (openFileDialog.ShowDialog() == true)
  357. {
  358. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  359. }
  360. else
  361. {
  362. return new Tuple<string, string>("", "");
  363. }
  364. }
  365. /// <summary>
  366. /// Sets the contents of the last action taken indicator label.
  367. /// </summary>
  368. /// <param name="message">The new contents</param>
  369. public void SetLastActionTakenText(string message)
  370. {
  371. LastActionBox.Text = message;
  372. }
  373. /// <summary>
  374. /// Sets the contents of the status bar label containing
  375. /// the similarity score of the left and right image.
  376. /// </summary>
  377. /// <param name="message">The message to be set,
  378. /// will be set to the default value if left empty.</param>
  379. public void SetImageSimilarityText(string message)
  380. {
  381. if (message.Count() > 0) LineSimilarityBox.Text = message;
  382. else LineSimilarityBox.Text = "-";
  383. }
  384. /// <summary>
  385. /// Changes the states of a tool strip button.
  386. /// </summary>
  387. /// <param name="buttonName">The name of the button.</param>
  388. /// <param name="state">The new state of the button.</param>
  389. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  390. {
  391. ButtonBase buttonToChange;
  392. bool isToggleable = false;
  393. switch (buttonName)
  394. {
  395. case "canvasButton":
  396. buttonToChange = CanvasButton;
  397. break;
  398. case "drawButton":
  399. buttonToChange = DrawButton;
  400. isToggleable = true;
  401. break;
  402. case "deleteButton":
  403. buttonToChange = DeleteButton;
  404. isToggleable = true;
  405. break;
  406. case "undoButton":
  407. buttonToChange = UndoButton;
  408. break;
  409. case "redoButton":
  410. buttonToChange = RedoButton;
  411. break;
  412. default:
  413. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  414. return;
  415. }
  416. if (isToggleable)
  417. {
  418. switch (state)
  419. {
  420. case ButtonState.Active:
  421. ((ToggleButton)buttonToChange).IsEnabled = true;
  422. ((ToggleButton)buttonToChange).IsChecked = true;
  423. ((ToggleButton)buttonToChange).Opacity = 1;
  424. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  425. break;
  426. case ButtonState.Disabled:
  427. ((ToggleButton)buttonToChange).IsEnabled = false;
  428. ((ToggleButton)buttonToChange).IsChecked = false;
  429. ((ToggleButton)buttonToChange).Opacity = 0.5;
  430. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  431. break;
  432. case ButtonState.Enabled:
  433. ((ToggleButton)buttonToChange).IsEnabled = true;
  434. ((ToggleButton)buttonToChange).IsChecked = false;
  435. ((ToggleButton)buttonToChange).Opacity = 1;
  436. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  437. break;
  438. }
  439. }
  440. else
  441. {
  442. switch (state)
  443. {
  444. case ButtonState.Disabled:
  445. ((Button)buttonToChange).IsEnabled = false;
  446. ((Button)buttonToChange).Opacity = 0.5;
  447. break;
  448. default:
  449. ((Button)buttonToChange).IsEnabled = true;
  450. ((Button)buttonToChange).Opacity = 1;
  451. break;
  452. }
  453. }
  454. }
  455. /// <summary>
  456. /// Sets the contents of the load status indicator label.
  457. /// </summary>
  458. /// <param name="message">The new contents</param>
  459. public void SetToolStripLoadStatus(string message)
  460. {
  461. LoadStatusBox.Text = message;
  462. }
  463. /// <summary>
  464. /// shows the given info message in a popup and asks the user to aknowledge it
  465. /// </summary>
  466. /// <param name="message">the message to show</param>
  467. public void ShowInfoMessage(string message)
  468. {
  469. MessageBox.Show(message);
  470. }
  471. /// <summary>
  472. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  473. /// </summary>
  474. /// <param name="message">The message of the warning.</param>
  475. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  476. public bool ShowWarning(string message)
  477. {
  478. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  479. return (result.Equals(MessageBoxResult.Yes));
  480. }
  481. /// <summary>
  482. /// Updates the colour of a canvas.
  483. /// </summary>
  484. /// <param name="canvasName">The name of the canvas to be updated.</param>
  485. /// <param name="active">Whether or not the canvas is active.</param>
  486. public void SetCanvasState(string canvasName, bool active)
  487. {
  488. switch (canvasName)
  489. {
  490. case ("LeftCanvas"):
  491. if (active)
  492. {
  493. LeftCanvas.Background = Brushes.White;
  494. }
  495. else
  496. {
  497. LeftCanvas.Background = Brushes.SlateGray;
  498. }
  499. break;
  500. case ("RightCanvas"):
  501. if (active)
  502. {
  503. RightCanvas.Background = Brushes.White;
  504. }
  505. else
  506. {
  507. RightCanvas.Background = Brushes.SlateGray;
  508. }
  509. break;
  510. default:
  511. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  512. }
  513. }
  514. /************************/
  515. /*** HELPING FUNCTION ***/
  516. /************************/
  517. /// <summary>
  518. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  519. /// Takes 7000ms
  520. /// </summary>
  521. private void DebugOne_Click(object sender, RoutedEventArgs e)
  522. {
  523. Debug(1);
  524. }
  525. /// <summary>
  526. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  527. /// Takes 24000ms
  528. /// </summary>
  529. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  530. {
  531. Debug(2);
  532. }
  533. /// <summary>
  534. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  535. /// Takes 4000ms
  536. /// </summary>
  537. private void DebugThree_Click(object sender, RoutedEventArgs e)
  538. {
  539. Debug(3);
  540. }
  541. /// <summary>
  542. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  543. /// Takes
  544. /// </summary>
  545. private void DebugFour_Click(object sender, RoutedEventArgs e)
  546. {
  547. Debug(4);
  548. }
  549. /// <summary>
  550. /// A function which simulates canvas input for debugging.
  551. /// </summary>
  552. /// <param name="option"></param>
  553. private async void Debug(int option)
  554. {
  555. Point[] points;
  556. Point start = new Point(50, 50);
  557. switch (option)
  558. {
  559. case 1:
  560. points = debugDat.debugPoints1;
  561. break;
  562. case 2:
  563. points = debugDat.debugPoints2;
  564. break;
  565. case 3:
  566. points = debugDat.debugPoints3;
  567. break;
  568. case 4:
  569. points = debugDat.debugPoints4;
  570. start = new Point(284, 148);
  571. break;
  572. default:
  573. return;
  574. }
  575. dispatcherTimer.Stop();
  576. debugRunning = true;
  577. ProgramPresenter.Tick(); await Task.Delay(10);
  578. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, start);
  579. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down, strokeCollection); await Task.Delay(10);
  580. for (int x = 0; x < points.Length; x++)
  581. {
  582. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  583. await Task.Delay(1);
  584. if (x % 5 == 0)
  585. {
  586. ProgramPresenter.Tick();
  587. await Task.Delay(1);
  588. }
  589. }
  590. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up, strokeCollection); await Task.Delay(1);
  591. debugRunning = false;
  592. dispatcherTimer.Start();
  593. }
  594. }
  595. }