MainWindow.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. StrokeCollection strokeCollection = new StrokeCollection();
  93. /********************************************/
  94. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  95. /********************************************/
  96. /// <summary>
  97. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  98. /// </summary>
  99. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  100. {
  101. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
  102. new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
  103. }
  104. /// <summary>
  105. /// Collects all Strokes on RightCanvas
  106. /// </summary>
  107. public void RightCanvas_StrokeCollection(object sender, InkCanvasStrokeCollectedEventArgs e)
  108. {
  109. strokeCollection.Add(e.Stroke);
  110. System.Diagnostics.Debug.WriteLine(strokeCollection.Count);
  111. }
  112. /// <summary>
  113. /// Redo an Action.
  114. /// </summary>
  115. private void RedoButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. ProgramPresenter.Redo();
  118. }
  119. /// <summary>
  120. /// Undo an Action.
  121. /// </summary>
  122. private void UndoButton_Click(object sender, RoutedEventArgs e)
  123. {
  124. ProgramPresenter.Undo();
  125. }
  126. /// <summary>
  127. /// Changes the state of the program to deletion
  128. /// </summary>
  129. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  130. {
  131. ProgramPresenter.ChangeState(false);
  132. RightCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  133. }
  134. /// <summary>
  135. /// Changes the state of the program to drawing
  136. /// </summary>
  137. private void DrawButton_Click(object sender, RoutedEventArgs e)
  138. {
  139. ProgramPresenter.ChangeOptiTrack(false);
  140. ProgramPresenter.ChangeState(true);
  141. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  142. }
  143. /// <summary>
  144. /// Changes the state of the program to drawing with OptiTrack
  145. /// </summary>
  146. private void DrawWithOptiButton_Click(object sender, RoutedEventArgs e)
  147. {
  148. ProgramPresenter.ChangeOptiTrack(true);
  149. ProgramPresenter.ChangeState(true);
  150. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  151. }
  152. /// <summary>
  153. /// Hold left mouse button to start drawing.
  154. /// </summary>
  155. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  156. {
  157. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  158. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);");
  159. }
  160. /// <summary>
  161. /// Lift left mouse button to stop drawing and add a new Line.
  162. /// </summary>
  163. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  164. {
  165. if (strokeCollection.Count == 0)
  166. {
  167. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up_Invalid);
  168. }
  169. else
  170. {
  171. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  172. RightCanvas.Strokes.RemoveAt(0);
  173. strokeCollection.RemoveAt(0);
  174. System.Diagnostics.Debug.WriteLine(strokeCollection.Count);
  175. }
  176. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);");
  177. }
  178. /// <summary>
  179. /// Get current Mouse positon within the right picture box.
  180. /// </summary>
  181. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  182. {
  183. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  184. //System.Diagnostics.Debug.WriteLine("new Point(" + ((int)e.GetPosition(RightCanvas).X).ToString() + "," + ((int)e.GetPosition(RightCanvas).Y).ToString() + "), ");
  185. }
  186. /// <summary>
  187. /// Button to create a new Canvas. Will create an empty image
  188. /// which is the size of the left image, if there is one.
  189. /// If there is no image loaded the canvas will be the size of the right picture box
  190. /// </summary>
  191. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  192. {
  193. ProgramPresenter.NewCanvas();
  194. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  195. RightCanvas.Strokes.Clear();
  196. }
  197. /// <summary>
  198. /// Ticks the Presenter.
  199. /// </summary>
  200. private void dispatcherTimer_Tick(object sender, EventArgs e)
  201. {
  202. ProgramPresenter.Tick();
  203. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  204. }
  205. /// <summary>
  206. /// Import button for .isad drawing, will open an OpenFileDialog
  207. /// </summary>
  208. private void ISADMenuItem_Click(object sender, RoutedEventArgs e)
  209. {
  210. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  211. }
  212. /// <summary>
  213. /// Import button for .svg file, will open an OpenFileDialog
  214. /// </summary>
  215. private void SVGMenuItem_Click(object sender, RoutedEventArgs e)
  216. {
  217. ProgramPresenter.SVGToolStripMenuItemClick();
  218. }
  219. /*************************/
  220. /*** PRESENTER -> VIEW ***/
  221. /*************************/
  222. /// <summary>
  223. /// Returns the cursor position.
  224. /// </summary>
  225. /// <returns>The cursor Position</returns>
  226. public Point GetCursorPosition()
  227. {
  228. return Mouse.GetPosition(RightCanvas);
  229. }
  230. /// <summary>
  231. /// If the mouse is pressed or not.
  232. /// </summary>
  233. /// <returns>Whether or not the mouse is pressed.</returns>
  234. public bool IsMousePressed()
  235. {
  236. if (!debugRunning)
  237. {
  238. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed));
  239. }
  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 last action taken indicator label.
  352. /// </summary>
  353. /// <param name="message">The new contents</param>
  354. public void SetOptiTrackText(string message)
  355. {
  356. OptiTrackBox.Text = message;
  357. }
  358. /// <summary>
  359. /// Changes the states of a tool strip button.
  360. /// </summary>
  361. /// <param name="buttonName">The name of the button.</param>
  362. /// <param name="state">The new state of the button.</param>
  363. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  364. {
  365. ButtonBase buttonToChange;
  366. bool isToggleable = false;
  367. switch (buttonName)
  368. {
  369. case "canvasButton":
  370. buttonToChange = CanvasButton;
  371. break;
  372. case "drawButton":
  373. buttonToChange = DrawButton;
  374. isToggleable = true;
  375. break;
  376. case "deleteButton":
  377. buttonToChange = DeleteButton;
  378. isToggleable = true;
  379. break;
  380. case "undoButton":
  381. buttonToChange = UndoButton;
  382. break;
  383. case "redoButton":
  384. buttonToChange = RedoButton;
  385. break;
  386. case "drawWithOptiButton":
  387. buttonToChange = DrawWithOptiButton;
  388. isToggleable = true;
  389. break;
  390. default:
  391. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  392. return;
  393. }
  394. if (isToggleable)
  395. {
  396. switch (state)
  397. {
  398. case ButtonState.Active:
  399. ((ToggleButton)buttonToChange).IsEnabled = true;
  400. ((ToggleButton)buttonToChange).IsChecked = true;
  401. ((ToggleButton)buttonToChange).Opacity = 1;
  402. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  403. break;
  404. case ButtonState.Disabled:
  405. ((ToggleButton)buttonToChange).IsEnabled = false;
  406. ((ToggleButton)buttonToChange).IsChecked = false;
  407. ((ToggleButton)buttonToChange).Opacity = 0.5;
  408. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  409. break;
  410. case ButtonState.Enabled:
  411. ((ToggleButton)buttonToChange).IsEnabled = true;
  412. ((ToggleButton)buttonToChange).IsChecked = false;
  413. ((ToggleButton)buttonToChange).Opacity = 1;
  414. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  415. break;
  416. }
  417. }
  418. else
  419. {
  420. switch (state)
  421. {
  422. case ButtonState.Disabled:
  423. ((Button)buttonToChange).IsEnabled = false;
  424. ((Button)buttonToChange).Opacity = 0.5;
  425. break;
  426. default:
  427. ((Button)buttonToChange).IsEnabled = true;
  428. ((Button)buttonToChange).Opacity = 1;
  429. break;
  430. }
  431. }
  432. }
  433. /// <summary>
  434. /// Sets the contents of the load status indicator label.
  435. /// </summary>
  436. /// <param name="message">The new contents</param>
  437. public void SetToolStripLoadStatus(string message)
  438. {
  439. LoadStatusBox.Text = message;
  440. }
  441. /// <summary>
  442. /// shows the given info message in a popup and asks the user to aknowledge it
  443. /// if caption is "Warning" add MessageBoxImage.Warning to the pop up window
  444. /// </summary>
  445. /// <param name="message">the message to show</param>
  446. public void ShowInfoMessage(string message, string caption)
  447. {
  448. if(caption == "Warning")
  449. {
  450. MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Warning);
  451. }
  452. else MessageBox.Show(message, caption);
  453. }
  454. /// <summary>
  455. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  456. /// </summary>
  457. /// <param name="message">The message of the warning.</param>
  458. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  459. public bool ShowWarning(string message)
  460. {
  461. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  462. return (result.Equals(MessageBoxResult.Yes));
  463. }
  464. /// <summary>
  465. /// Updates the colour of a canvas.
  466. /// </summary>
  467. /// <param name="canvasName">The name of the canvas to be updated.</param>
  468. /// <param name="active">Whether or not the canvas is active.</param>
  469. public void SetCanvasState(string canvasName, bool active)
  470. {
  471. switch (canvasName)
  472. {
  473. case ("LeftCanvas"):
  474. if (active)
  475. {
  476. LeftCanvas.Background = Brushes.White;
  477. }
  478. else
  479. {
  480. LeftCanvas.Background = Brushes.SlateGray;
  481. }
  482. break;
  483. case ("RightCanvas"):
  484. if (active)
  485. {
  486. RightCanvas.Background = Brushes.White;
  487. }
  488. else
  489. {
  490. RightCanvas.Background = Brushes.SlateGray;
  491. }
  492. break;
  493. default:
  494. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  495. }
  496. }
  497. /************************/
  498. /*** HELPING FUNCTION ***/
  499. /************************/
  500. /// <summary>
  501. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  502. /// Takes 7000ms
  503. /// </summary>
  504. private void DebugOne_Click(object sender, RoutedEventArgs e)
  505. {
  506. Debug(1);
  507. }
  508. /// <summary>
  509. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  510. /// Takes 24000ms
  511. /// </summary>
  512. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  513. {
  514. Debug(2);
  515. }
  516. /// <summary>
  517. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  518. /// Takes 4000ms
  519. /// </summary>
  520. private void DebugThree_Click(object sender, RoutedEventArgs e)
  521. {
  522. Debug(3);
  523. }
  524. /// <summary>
  525. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  526. /// Takes
  527. /// </summary>
  528. private void DebugFour_Click(object sender, RoutedEventArgs e)
  529. {
  530. Debug(4);
  531. }
  532. /// <summary>
  533. /// A function which simulates canvas input for debugging.
  534. /// </summary>
  535. /// <param name="option"></param>
  536. private async void Debug(int option)
  537. {
  538. Point[] points;
  539. Point start = new Point(50, 50);
  540. switch (option)
  541. {
  542. case 1:
  543. points = debugDat.debugPoints1;
  544. break;
  545. case 2:
  546. points = debugDat.debugPoints2;
  547. break;
  548. case 3:
  549. points = debugDat.debugPoints3;
  550. break;
  551. case 4:
  552. points = debugDat.debugPoints4;
  553. start = new Point(284, 148);
  554. break;
  555. default:
  556. return;
  557. }
  558. dispatcherTimer.Stop();
  559. debugRunning = true;
  560. ProgramPresenter.Tick(); await Task.Delay(10);
  561. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, start);
  562. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  563. for (int x = 0; x < points.Length; x++)
  564. {
  565. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  566. await Task.Delay(1);
  567. if (x % 5 == 0)
  568. {
  569. ProgramPresenter.Tick();
  570. await Task.Delay(1);
  571. }
  572. }
  573. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  574. debugRunning = false;
  575. dispatcherTimer.Start();
  576. }
  577. }
  578. }