MainWindow.xaml.cs 22 KB

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