MainWindow.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. OptiTrackConnector connector = new OptiTrackConnector();
  53. if (connector.Init(@"C:\Users\etri\Desktop\bppp2\optitrack_setup.ttp"))
  54. {
  55. connector.StartTracking(handleTrackingResult);
  56. }
  57. }
  58. private void handleTrackingResult(OptiTrack.Frame frame)
  59. {
  60. Console.WriteLine(frame.Markers.Length);
  61. }
  62. public enum ButtonState
  63. {
  64. Enabled,
  65. Disabled,
  66. Active
  67. }
  68. DispatcherTimer dispatcherTimer;
  69. /// <summary>
  70. /// Dialog to select a file.
  71. /// </summary>
  72. OpenFileDialog openFileDialog = new OpenFileDialog();
  73. /// <summary>
  74. /// All Lines in the current session
  75. /// </summary>
  76. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  77. /// <summary>
  78. /// Queue for the cursorPositions
  79. /// </summary>
  80. Queue<Point> cursorPositions = new Queue<Point>();
  81. /// <summary>
  82. /// The Presenter Component of the MVP-Model
  83. /// </summary>
  84. MVP_Presenter ProgramPresenter;
  85. /// <summary>
  86. /// The line currently being drawn
  87. /// </summary>
  88. Polyline currentLine;
  89. /// <summary>
  90. /// If the debug function is running.
  91. /// </summary>
  92. bool debugRunning = false;
  93. /// <summary>
  94. /// Point collections for debugging.
  95. /// </summary>
  96. DebugData debugDat = new DebugData();
  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. /// Redo an Action.
  110. /// </summary>
  111. private void RedoButton_Click(object sender, RoutedEventArgs e)
  112. {
  113. ProgramPresenter.Redo();
  114. }
  115. /// <summary>
  116. /// Undo an Action.
  117. /// </summary>
  118. private void UndoButton_Click(object sender, RoutedEventArgs e)
  119. {
  120. ProgramPresenter.Undo();
  121. }
  122. /// <summary>
  123. /// Changes the state of the program to deletion
  124. /// </summary>
  125. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  126. {
  127. ProgramPresenter.ChangeState(false);
  128. }
  129. /// <summary>
  130. /// Changes the state of the program to drawing
  131. /// </summary>
  132. private void DrawButton_Click(object sender, RoutedEventArgs e)
  133. {
  134. ProgramPresenter.ChangeState(true);
  135. }
  136. /// <summary>
  137. /// Hold left mouse button to start drawing.
  138. /// </summary>
  139. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  140. {
  141. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  142. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);");
  143. }
  144. /// <summary>
  145. /// Lift left mouse button to stop drawing and add a new Line.
  146. /// </summary>
  147. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  148. {
  149. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  150. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);");
  151. }
  152. /// <summary>
  153. /// If the mouse leaves the canvas, it is treated as if the mouse was released.
  154. /// </summary>
  155. private void RightCanvas_MouseLeave(object sender, MouseEventArgs e)
  156. {
  157. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  158. }
  159. /// <summary>
  160. /// If the finger leaves the canvas, it is treated as if the finger was released.
  161. /// </summary>
  162. private void RightCanvas_TouchLeave(object sender, TouchEventArgs e)
  163. {
  164. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  165. }
  166. /// <summary>
  167. /// Get current Mouse positon within the right picture box.
  168. /// </summary>
  169. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  170. {
  171. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  172. //System.Diagnostics.Debug.WriteLine("new Point(" + ((int)e.GetPosition(RightCanvas).X).ToString() + "," + ((int)e.GetPosition(RightCanvas).Y).ToString() + "), ");
  173. }
  174. /// <summary>
  175. /// Button to create a new Canvas. Will create an empty image
  176. /// which is the size of the left image, if there is one.
  177. /// If there is no image loaded the canvas will be the size of the right picture box
  178. /// </summary>
  179. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  180. {
  181. ProgramPresenter.NewCanvas();
  182. }
  183. /// <summary>
  184. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  185. /// Takes 7000ms
  186. /// </summary>
  187. private void DebugOne_Click(object sender, RoutedEventArgs e)
  188. {
  189. Debug(1);
  190. }
  191. /// <summary>
  192. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  193. /// Takes 24000ms
  194. /// </summary>
  195. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  196. {
  197. Debug(2);
  198. }
  199. /// <summary>
  200. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  201. /// Takes 7000ms
  202. /// </summary>
  203. private void DebugThree_Click(object sender, RoutedEventArgs e)
  204. {
  205. Debug(3);
  206. }
  207. /// <summary>
  208. /// Ticks the Presenter.
  209. /// </summary>
  210. private void dispatcherTimer_Tick(object sender, EventArgs e)
  211. {
  212. ProgramPresenter.Tick();
  213. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  214. }
  215. /// <summary>
  216. /// Import button, will open an OpenFileDialog
  217. /// </summary>
  218. private void MenuItem_Click(object sender, RoutedEventArgs e)
  219. {
  220. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  221. }
  222. /*************************/
  223. /*** PRESENTER -> VIEW ***/
  224. /*************************/
  225. /// <summary>
  226. /// Returns the cursor position.
  227. /// </summary>
  228. /// <returns>The cursor Position</returns>
  229. public Point GetCursorPosition()
  230. {
  231. return Mouse.GetPosition(RightCanvas);
  232. }
  233. /// <summary>
  234. /// If the mouse is pressed or not.
  235. /// </summary>
  236. /// <returns>Whether or not the mouse is pressed.</returns>
  237. public bool IsMousePressed()
  238. {
  239. if (!debugRunning) {
  240. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  241. else return true;
  242. }
  243. /// <summary>
  244. /// Remove the current line.
  245. /// </summary>
  246. public void RemoveCurrLine()
  247. {
  248. RightCanvas.Children.Remove(currentLine);
  249. }
  250. /// <summary>
  251. /// Display the current line.
  252. /// </summary>
  253. /// <param name="line">The current line to display</param>
  254. public void DisplayCurrLine(Polyline line)
  255. {
  256. if (RightCanvas.Children.Contains(currentLine))
  257. {
  258. RemoveCurrLine();
  259. }
  260. RightCanvas.Children.Add(line);
  261. currentLine = line;
  262. }
  263. /// <summary>
  264. /// Removes all Lines from the left canvas.
  265. /// </summary>
  266. public void RemoveAllLeftLines()
  267. {
  268. LeftCanvas.Children.Clear();
  269. }
  270. /// <summary>
  271. /// Removes all lines in the right canvas.
  272. /// </summary>
  273. public void RemoveAllRightLines()
  274. {
  275. RightCanvas.Children.Clear();
  276. }
  277. /// <summary>
  278. /// Adds another Line that will be displayed in the left display.
  279. /// </summary>
  280. /// <param name="newLine">The new Polyline to be added displayed.</param>
  281. public void AddNewLineLeft(Polyline newLine)
  282. {
  283. newLine.Stroke = Brushes.Black;
  284. newLine.StrokeThickness = 2;
  285. LeftCanvas.Children.Add(newLine);
  286. }
  287. /// <summary>
  288. /// Adds another Line that will be displayed in the right display.
  289. /// </summary>
  290. /// <param name="newLine">The new Polyline to be added displayed.</param>
  291. public void AddNewLineRight(Polyline newLine)
  292. {
  293. newLine.Stroke = Brushes.Black;
  294. newLine.StrokeThickness = 2;
  295. RightCanvas.Children.Add(newLine);
  296. }
  297. /// <summary>
  298. /// Adds a point to the right canvas
  299. /// </summary>
  300. /// <param name="newPoint">The point</param>
  301. public void AddNewPointRight(Ellipse newPoint)
  302. {
  303. newPoint.Height = 3; newPoint.Width = 3;
  304. newPoint.Fill = Brushes.Black;
  305. RightCanvas.Children.Add(newPoint);
  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 = "test";
  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. default:
  387. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  388. return;
  389. }
  390. if (isToggleable)
  391. {
  392. switch (state)
  393. {
  394. case ButtonState.Active:
  395. ((ToggleButton)buttonToChange).IsEnabled = true;
  396. ((ToggleButton)buttonToChange).IsChecked = true;
  397. ((ToggleButton)buttonToChange).Opacity = 1;
  398. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  399. break;
  400. case ButtonState.Disabled:
  401. ((ToggleButton)buttonToChange).IsEnabled = false;
  402. ((ToggleButton)buttonToChange).IsChecked = false;
  403. ((ToggleButton)buttonToChange).Opacity = 0.5;
  404. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  405. break;
  406. case ButtonState.Enabled:
  407. ((ToggleButton)buttonToChange).IsEnabled = true;
  408. ((ToggleButton)buttonToChange).IsChecked = false;
  409. ((ToggleButton)buttonToChange).Opacity = 1;
  410. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  411. break;
  412. }
  413. }
  414. else
  415. {
  416. switch (state)
  417. {
  418. case ButtonState.Disabled:
  419. ((Button)buttonToChange).IsEnabled = false;
  420. ((Button)buttonToChange).Opacity = 0.5;
  421. break;
  422. default:
  423. ((Button)buttonToChange).IsEnabled = true;
  424. ((Button)buttonToChange).Opacity = 1;
  425. break;
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// Sets the contents of the load status indicator label.
  431. /// </summary>
  432. /// <param name="message">The new contents</param>
  433. public void SetToolStripLoadStatus(string message)
  434. {
  435. LoadStatusBox.Text = message;
  436. }
  437. /// <summary>
  438. /// shows the given info message in a popup and asks the user to aknowledge it
  439. /// </summary>
  440. /// <param name="message">the message to show</param>
  441. public void ShowInfoMessage(string message)
  442. {
  443. MessageBox.Show(message);
  444. }
  445. /// <summary>
  446. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  447. /// </summary>
  448. /// <param name="message">The message of the warning.</param>
  449. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  450. public bool ShowWarning(string message)
  451. {
  452. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  453. return (result.Equals(MessageBoxResult.Yes));
  454. }
  455. /// <summary>
  456. /// Updates the colour of a canvas.
  457. /// </summary>
  458. /// <param name="canvasName">The name of the canvas to be updated.</param>
  459. /// <param name="active">Whether or not the canvas is active.</param>
  460. public void SetCanvasState(string canvasName, bool active)
  461. {
  462. Canvas canvas;
  463. switch (canvasName){
  464. case ("LeftCanvas"):
  465. canvas = LeftCanvas;
  466. break;
  467. case ("RightCanvas"):
  468. canvas = RightCanvas;
  469. break;
  470. default:
  471. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  472. }
  473. if (active)
  474. {
  475. canvas.Background = Brushes.White;
  476. }
  477. else
  478. {
  479. canvas.Background = Brushes.SlateGray;
  480. }
  481. }
  482. /************************/
  483. /*** HELPING FUNCTION ***/
  484. /************************/
  485. /// <summary>
  486. /// A function which simulates canvas input for debugging.
  487. /// </summary>
  488. /// <param name="option"></param>
  489. private async void Debug(int option)
  490. {
  491. Point[] points;
  492. switch (option)
  493. {
  494. case 1:
  495. points = debugDat.debugPoints1;
  496. break;
  497. case 2:
  498. points = debugDat.debugPoints2;
  499. break;
  500. default:
  501. return;
  502. }
  503. dispatcherTimer.Stop();
  504. debugRunning = true;
  505. ProgramPresenter.Tick(); await Task.Delay(10);
  506. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(50, 50));
  507. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  508. for (int x = 0; x < points.Length; x++)
  509. {
  510. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  511. await Task.Delay(1);
  512. if (x % 5 == 0)
  513. {
  514. ProgramPresenter.Tick();
  515. await Task.Delay(1);
  516. }
  517. }
  518. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  519. debugRunning = false;
  520. dispatcherTimer.Start();
  521. }
  522. }
  523. }