MainWindow.xaml.cs 23 KB

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