MainWindow.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. /*************************/
  224. /*** PRESENTER -> VIEW ***/
  225. /*************************/
  226. /// <summary>
  227. /// Returns the cursor position.
  228. /// </summary>
  229. /// <returns>The cursor Position</returns>
  230. public Point GetCursorPosition()
  231. {
  232. return Mouse.GetPosition(RightCanvas);
  233. }
  234. /// <summary>
  235. /// If the mouse is pressed or not.
  236. /// </summary>
  237. /// <returns>Whether or not the mouse is pressed.</returns>
  238. public bool IsMousePressed()
  239. {
  240. if (!debugRunning) {
  241. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  242. else return true;
  243. }
  244. /// <summary>
  245. /// Remove the current line.
  246. /// </summary>
  247. public void RemoveCurrLine()
  248. {
  249. RightCanvas.Children.Remove(currentLine);
  250. }
  251. /// <summary>
  252. /// Display the current line.
  253. /// </summary>
  254. /// <param name="line">The current line to display</param>
  255. public void DisplayCurrLine(Polyline line)
  256. {
  257. if (RightCanvas.Children.Contains(currentLine))
  258. {
  259. RemoveCurrLine();
  260. }
  261. RightCanvas.Children.Add(line);
  262. currentLine = line;
  263. }
  264. /// <summary>
  265. /// Removes all Lines from the left canvas.
  266. /// </summary>
  267. public void RemoveAllLeftLines()
  268. {
  269. LeftCanvas.Children.Clear();
  270. }
  271. /// <summary>
  272. /// Removes all lines in the right canvas.
  273. /// </summary>
  274. public void RemoveAllRightLines()
  275. {
  276. RightCanvas.Children.Clear();
  277. }
  278. /// <summary>
  279. /// Adds another Line that will be displayed in the left display.
  280. /// </summary>
  281. /// <param name="newLine">The new Polyline to be added displayed.</param>
  282. public void AddNewLineLeft(Polyline newLine)
  283. {
  284. newLine.Stroke = Brushes.Black;
  285. newLine.StrokeThickness = 2;
  286. LeftCanvas.Children.Add(newLine);
  287. }
  288. /// <summary>
  289. /// Adds another Line that will be displayed in the right display.
  290. /// </summary>
  291. /// <param name="newLine">The new Polyline to be added displayed.</param>
  292. public void AddNewLineRight(Polyline newLine)
  293. {
  294. newLine.Stroke = Brushes.Black;
  295. newLine.StrokeThickness = 2;
  296. RightCanvas.Children.Add(newLine);
  297. }
  298. /// <summary>
  299. /// Adds a point to the right canvas
  300. /// </summary>
  301. /// <param name="newPoint">The point</param>
  302. public void AddNewPointRight(Ellipse newPoint)
  303. {
  304. newPoint.Height = 3; newPoint.Width = 3;
  305. newPoint.Fill = Brushes.Black;
  306. RightCanvas.Children.Add(newPoint);
  307. }
  308. /// <summary>
  309. /// Adds a point to the left canvas
  310. /// </summary>
  311. /// <param name="newPoint">The point</param>
  312. public void AddNewPointLeft(Ellipse newPoint)
  313. {
  314. newPoint.Height = 3; newPoint.Width = 3;
  315. newPoint.Fill = Brushes.Black;
  316. LeftCanvas.Children.Add(newPoint);
  317. }
  318. /// <summary>
  319. /// Enables the timer of the View, which will tick the Presenter.
  320. /// </summary>
  321. public void EnableTimer()
  322. {
  323. dispatcherTimer.Start();
  324. }
  325. /// <summary>
  326. /// A function that opens a file dialog and returns the filename.
  327. /// </summary>
  328. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  329. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  330. /// else returns a tuple with empty strigns</returns>
  331. public Tuple<string, string> openNewDialog(string Filter)
  332. {
  333. openFileDialog.Filter = Filter;
  334. if (openFileDialog.ShowDialog() == true)
  335. {
  336. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  337. }
  338. else
  339. {
  340. return new Tuple<string, string>("", "");
  341. }
  342. }
  343. /// <summary>
  344. /// Sets the contents of the last action taken indicator label.
  345. /// </summary>
  346. /// <param name="message">The new contents</param>
  347. public void SetLastActionTakenText(string message)
  348. {
  349. LastActionBox.Text = message;
  350. }
  351. /// <summary>
  352. /// Sets the contents of the last action taken indicator label.
  353. /// </summary>
  354. /// <param name="message">The new contents</param>
  355. public void SetOptiTrackText(string message)
  356. {
  357. OptiTrackBox.Text = message;
  358. }
  359. /// <summary>
  360. /// Changes the states of a tool strip button.
  361. /// </summary>
  362. /// <param name="buttonName">The name of the button.</param>
  363. /// <param name="state">The new state of the button.</param>
  364. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  365. {
  366. ButtonBase buttonToChange;
  367. bool isToggleable = false;
  368. switch (buttonName)
  369. {
  370. case "canvasButton":
  371. buttonToChange = CanvasButton;
  372. break;
  373. case "drawButton":
  374. buttonToChange = DrawButton;
  375. isToggleable = true;
  376. break;
  377. case "deleteButton":
  378. buttonToChange = DeleteButton;
  379. isToggleable = true;
  380. break;
  381. case "undoButton":
  382. buttonToChange = UndoButton;
  383. break;
  384. case "redoButton":
  385. buttonToChange = RedoButton;
  386. break;
  387. case "drawWithOptiButton":
  388. buttonToChange = DrawWithOptiButton;
  389. isToggleable = true;
  390. break;
  391. default:
  392. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  393. return;
  394. }
  395. if (isToggleable)
  396. {
  397. switch (state)
  398. {
  399. case ButtonState.Active:
  400. ((ToggleButton)buttonToChange).IsEnabled = true;
  401. ((ToggleButton)buttonToChange).IsChecked = true;
  402. ((ToggleButton)buttonToChange).Opacity = 1;
  403. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  404. break;
  405. case ButtonState.Disabled:
  406. ((ToggleButton)buttonToChange).IsEnabled = false;
  407. ((ToggleButton)buttonToChange).IsChecked = false;
  408. ((ToggleButton)buttonToChange).Opacity = 0.5;
  409. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  410. break;
  411. case ButtonState.Enabled:
  412. ((ToggleButton)buttonToChange).IsEnabled = true;
  413. ((ToggleButton)buttonToChange).IsChecked = false;
  414. ((ToggleButton)buttonToChange).Opacity = 1;
  415. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  416. break;
  417. }
  418. }
  419. else
  420. {
  421. switch (state)
  422. {
  423. case ButtonState.Disabled:
  424. ((Button)buttonToChange).IsEnabled = false;
  425. ((Button)buttonToChange).Opacity = 0.5;
  426. break;
  427. default:
  428. ((Button)buttonToChange).IsEnabled = true;
  429. ((Button)buttonToChange).Opacity = 1;
  430. break;
  431. }
  432. }
  433. }
  434. /// <summary>
  435. /// Sets the contents of the load status indicator label.
  436. /// </summary>
  437. /// <param name="message">The new contents</param>
  438. public void SetToolStripLoadStatus(string message)
  439. {
  440. LoadStatusBox.Text = message;
  441. }
  442. /// <summary>
  443. /// shows the given info message in a popup and asks the user to aknowledge it
  444. /// </summary>
  445. /// <param name="message">the message to show</param>
  446. public void ShowInfoMessage(string message)
  447. {
  448. MessageBox.Show(message);
  449. }
  450. /// <summary>
  451. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  452. /// </summary>
  453. /// <param name="message">The message of the warning.</param>
  454. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  455. public bool ShowWarning(string message)
  456. {
  457. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  458. return (result.Equals(MessageBoxResult.Yes));
  459. }
  460. /// <summary>
  461. /// Updates the colour of a canvas.
  462. /// </summary>
  463. /// <param name="canvasName">The name of the canvas to be updated.</param>
  464. /// <param name="active">Whether or not the canvas is active.</param>
  465. public void SetCanvasState(string canvasName, bool active)
  466. {
  467. Canvas canvas;
  468. switch (canvasName){
  469. case ("LeftCanvas"):
  470. canvas = LeftCanvas;
  471. break;
  472. case ("RightCanvas"):
  473. canvas = RightCanvas;
  474. break;
  475. default:
  476. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  477. }
  478. if (active)
  479. {
  480. canvas.Background = Brushes.White;
  481. }
  482. else
  483. {
  484. canvas.Background = Brushes.SlateGray;
  485. }
  486. }
  487. /************************/
  488. /*** HELPING FUNCTION ***/
  489. /************************/
  490. /// <summary>
  491. /// A function which simulates canvas input for debugging.
  492. /// </summary>
  493. /// <param name="option"></param>
  494. private async void Debug(int option)
  495. {
  496. Point[] points;
  497. switch (option)
  498. {
  499. case 1:
  500. points = debugDat.debugPoints1;
  501. break;
  502. case 2:
  503. points = debugDat.debugPoints2;
  504. break;
  505. default:
  506. return;
  507. }
  508. dispatcherTimer.Stop();
  509. debugRunning = true;
  510. ProgramPresenter.Tick(); await Task.Delay(10);
  511. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(50, 50));
  512. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  513. for (int x = 0; x < points.Length; x++)
  514. {
  515. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  516. await Task.Delay(1);
  517. if (x % 5 == 0)
  518. {
  519. ProgramPresenter.Tick();
  520. await Task.Delay(1);
  521. }
  522. }
  523. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  524. debugRunning = false;
  525. dispatcherTimer.Start();
  526. }
  527. }
  528. }