MainWindow.xaml.cs 20 KB

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