MainWindow.xaml.cs 21 KB

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