MainWindow.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. namespace SketchAssistantWPF
  22. {
  23. /// <summary>
  24. /// Interaction logic for MainWindow.xaml
  25. /// </summary>
  26. public partial class MainWindow : Window, MVP_View
  27. {
  28. public MainWindow()
  29. {
  30. bool InDebugMode = false;
  31. String[] commArgs = Environment.GetCommandLineArgs();
  32. InitializeComponent();
  33. if (commArgs.Length > 1)
  34. {
  35. if (commArgs[1].Equals("-debug"))
  36. {
  37. InDebugMode = true;
  38. }
  39. }
  40. if(!InDebugMode)
  41. {
  42. DebugMode.Visibility = Visibility.Collapsed;
  43. }
  44. ProgramPresenter = new MVP_Presenter(this);
  45. // DispatcherTimer setup
  46. dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
  47. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  48. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  49. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
  50. new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
  51. }
  52. public enum ButtonState
  53. {
  54. Enabled,
  55. Disabled,
  56. Active
  57. }
  58. DispatcherTimer dispatcherTimer;
  59. /// <summary>
  60. /// Dialog to select a file.
  61. /// </summary>
  62. OpenFileDialog openFileDialog = new OpenFileDialog();
  63. /// <summary>
  64. /// All Lines in the current session
  65. /// </summary>
  66. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  67. /// <summary>
  68. /// Queue for the cursorPositions
  69. /// </summary>
  70. Queue<Point> cursorPositions = new Queue<Point>();
  71. /// <summary>
  72. /// The Presenter Component of the MVP-Model
  73. /// </summary>
  74. MVP_Presenter ProgramPresenter;
  75. /// <summary>
  76. /// The line currently being drawn
  77. /// </summary>
  78. Polyline currentLine;
  79. /// <summary>
  80. /// If the debug function is running.
  81. /// </summary>
  82. bool debugRunning = false;
  83. /// <summary>
  84. /// Point collections for debugging.
  85. /// </summary>
  86. DebugData debugDat = new DebugData();
  87. /********************************************/
  88. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  89. /********************************************/
  90. /// <summary>
  91. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  92. /// </summary>
  93. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  94. {
  95. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
  96. new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
  97. }
  98. /// <summary>
  99. /// Redo an Action.
  100. /// </summary>
  101. private void RedoButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. ProgramPresenter.Redo();
  104. }
  105. /// <summary>
  106. /// Undo an Action.
  107. /// </summary>
  108. private void UndoButton_Click(object sender, RoutedEventArgs e)
  109. {
  110. ProgramPresenter.Undo();
  111. }
  112. /// <summary>
  113. /// Changes the state of the program to deletion
  114. /// </summary>
  115. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. ProgramPresenter.ChangeState(false);
  118. }
  119. /// <summary>
  120. /// Changes the state of the program to drawing
  121. /// </summary>
  122. private void DrawButton_Click(object sender, RoutedEventArgs e)
  123. {
  124. ProgramPresenter.ChangeState(true);
  125. }
  126. /// <summary>
  127. /// Hold left mouse button to start drawing.
  128. /// </summary>
  129. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  130. {
  131. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  132. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);");
  133. }
  134. /// <summary>
  135. /// Lift left mouse button to stop drawing and add a new Line.
  136. /// </summary>
  137. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  138. {
  139. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  140. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);");
  141. }
  142. /// <summary>
  143. /// If the mouse leaves the canvas, it is treated as if the mouse was released.
  144. /// </summary>
  145. private void RightCanvas_MouseLeave(object sender, MouseEventArgs e)
  146. {
  147. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  148. }
  149. /// <summary>
  150. /// If the cursor enters the canvas, it is treated as if the cursor was just pressed if the cursor is pressed.
  151. /// </summary>
  152. private void RightCanvas_MouseEnter(object sender, MouseEventArgs e)
  153. {
  154. if (IsMousePressed())
  155. {
  156. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, Mouse.GetPosition(RightCanvas));
  157. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  158. }
  159. }
  160. /// <summary>
  161. /// If the finger enters the canvas, it is treated as if the finger was just pressed if the finger is pressed.
  162. /// </summary>
  163. private void RightCanvas_TouchEnter(object sender, TouchEventArgs e)
  164. {
  165. if (IsMousePressed())
  166. {
  167. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, Mouse.GetPosition(RightCanvas));
  168. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  169. }
  170. }
  171. /// <summary>
  172. /// If the finger leaves the canvas, it is treated as if the finger was released.
  173. /// </summary>
  174. private void RightCanvas_TouchLeave(object sender, TouchEventArgs e)
  175. {
  176. 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. }
  195. /// <summary>
  196. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  197. /// Takes 7000ms
  198. /// </summary>
  199. private void DebugOne_Click(object sender, RoutedEventArgs e)
  200. {
  201. Debug(1);
  202. }
  203. /// <summary>
  204. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  205. /// Takes 24000ms
  206. /// </summary>
  207. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  208. {
  209. Debug(2);
  210. }
  211. /// <summary>
  212. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  213. /// Takes 7000ms
  214. /// </summary>
  215. private void DebugThree_Click(object sender, RoutedEventArgs e)
  216. {
  217. Debug(3);
  218. }
  219. /// <summary>
  220. /// Ticks the Presenter.
  221. /// </summary>
  222. private void dispatcherTimer_Tick(object sender, EventArgs e)
  223. {
  224. ProgramPresenter.Tick();
  225. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  226. }
  227. /// <summary>
  228. /// Import button for .isad drawing, will open an OpenFileDialog
  229. /// </summary>
  230. private void ISADMenuItem_Click(object sender, RoutedEventArgs e)
  231. {
  232. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  233. }
  234. /// <summary>
  235. /// Import button for .svg file, will open an OpenFileDialog
  236. /// </summary>
  237. private void SVGMenuItem_Click(object sender, RoutedEventArgs e)
  238. {
  239. ProgramPresenter.SVGToolStripMenuItemClick();
  240. }
  241. /*************************/
  242. /*** PRESENTER -> VIEW ***/
  243. /*************************/
  244. /// <summary>
  245. /// Returns the cursor position.
  246. /// </summary>
  247. /// <returns>The cursor Position</returns>
  248. public Point GetCursorPosition()
  249. {
  250. return Mouse.GetPosition(RightCanvas);
  251. }
  252. /// <summary>
  253. /// If the mouse is pressed or not.
  254. /// </summary>
  255. /// <returns>Whether or not the mouse is pressed.</returns>
  256. public bool IsMousePressed()
  257. {
  258. if (!debugRunning) {
  259. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  260. else return true;
  261. }
  262. /// <summary>
  263. /// Remove the current line.
  264. /// </summary>
  265. public void RemoveCurrLine()
  266. {
  267. RightCanvas.Children.Remove(currentLine);
  268. }
  269. /// <summary>
  270. /// Display the current line.
  271. /// </summary>
  272. /// <param name="line">The current line to display</param>
  273. public void DisplayCurrLine(Polyline line)
  274. {
  275. if (RightCanvas.Children.Contains(currentLine))
  276. {
  277. RemoveCurrLine();
  278. }
  279. RightCanvas.Children.Add(line);
  280. currentLine = line;
  281. }
  282. /// <summary>
  283. /// Removes all Lines from the left canvas.
  284. /// </summary>
  285. public void RemoveAllLeftLines()
  286. {
  287. LeftCanvas.Children.Clear();
  288. }
  289. /// <summary>
  290. /// Removes all lines in the right canvas.
  291. /// </summary>
  292. public void RemoveAllRightLines()
  293. {
  294. RightCanvas.Children.Clear();
  295. }
  296. /// <summary>
  297. /// Adds another Line that will be displayed in the left display.
  298. /// </summary>
  299. /// <param name="newLine">The new Polyline to be added displayed.</param>
  300. public void AddNewLineLeft(Polyline newLine)
  301. {
  302. newLine.Stroke = Brushes.Black;
  303. newLine.StrokeThickness = 2;
  304. LeftCanvas.Children.Add(newLine);
  305. }
  306. /// <summary>
  307. /// Adds another Line that will be displayed in the right display.
  308. /// </summary>
  309. /// <param name="newLine">The new Polyline to be added displayed.</param>
  310. public void AddNewLineRight(Polyline newLine)
  311. {
  312. newLine.Stroke = Brushes.Black;
  313. newLine.StrokeThickness = 2;
  314. RightCanvas.Children.Add(newLine);
  315. }
  316. /// <summary>
  317. /// Adds a point to the right canvas
  318. /// </summary>
  319. /// <param name="newPoint">The point</param>
  320. public void AddNewPointRight(Ellipse newPoint)
  321. {
  322. newPoint.Height = 3; newPoint.Width = 3;
  323. newPoint.Fill = Brushes.Black;
  324. RightCanvas.Children.Add(newPoint);
  325. }
  326. /// <summary>
  327. /// Adds a point to the left canvas
  328. /// </summary>
  329. /// <param name="newPoint">The point</param>
  330. public void AddNewPointLeft(Ellipse newPoint)
  331. {
  332. newPoint.Height = 3; newPoint.Width = 3;
  333. newPoint.Fill = Brushes.Black;
  334. LeftCanvas.Children.Add(newPoint);
  335. }
  336. /// <summary>
  337. /// Enables the timer of the View, which will tick the Presenter.
  338. /// </summary>
  339. public void EnableTimer()
  340. {
  341. dispatcherTimer.Start();
  342. }
  343. /// <summary>
  344. /// A function that opens a file dialog and returns the filename.
  345. /// </summary>
  346. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  347. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  348. /// else returns a tuple with empty strigns</returns>
  349. public Tuple<string, string> openNewDialog(string Filter)
  350. {
  351. openFileDialog.Filter = Filter;
  352. if (openFileDialog.ShowDialog() == true)
  353. {
  354. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  355. }
  356. else
  357. {
  358. return new Tuple<string, string>("", "");
  359. }
  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 SetLastActionTakenText(string message)
  366. {
  367. LastActionBox.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. default:
  398. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  399. return;
  400. }
  401. if (isToggleable)
  402. {
  403. switch (state)
  404. {
  405. case ButtonState.Active:
  406. ((ToggleButton)buttonToChange).IsEnabled = true;
  407. ((ToggleButton)buttonToChange).IsChecked = true;
  408. ((ToggleButton)buttonToChange).Opacity = 1;
  409. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  410. break;
  411. case ButtonState.Disabled:
  412. ((ToggleButton)buttonToChange).IsEnabled = false;
  413. ((ToggleButton)buttonToChange).IsChecked = false;
  414. ((ToggleButton)buttonToChange).Opacity = 0.5;
  415. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  416. break;
  417. case ButtonState.Enabled:
  418. ((ToggleButton)buttonToChange).IsEnabled = true;
  419. ((ToggleButton)buttonToChange).IsChecked = false;
  420. ((ToggleButton)buttonToChange).Opacity = 1;
  421. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  422. break;
  423. }
  424. }
  425. else
  426. {
  427. switch (state)
  428. {
  429. case ButtonState.Disabled:
  430. ((Button)buttonToChange).IsEnabled = false;
  431. ((Button)buttonToChange).Opacity = 0.5;
  432. break;
  433. default:
  434. ((Button)buttonToChange).IsEnabled = true;
  435. ((Button)buttonToChange).Opacity = 1;
  436. break;
  437. }
  438. }
  439. }
  440. /// <summary>
  441. /// Sets the contents of the load status indicator label.
  442. /// </summary>
  443. /// <param name="message">The new contents</param>
  444. public void SetToolStripLoadStatus(string message)
  445. {
  446. LoadStatusBox.Text = message;
  447. }
  448. /// <summary>
  449. /// shows the given info message in a popup and asks the user to aknowledge it
  450. /// </summary>
  451. /// <param name="message">the message to show</param>
  452. public void ShowInfoMessage(string message)
  453. {
  454. MessageBox.Show(message);
  455. }
  456. /// <summary>
  457. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  458. /// </summary>
  459. /// <param name="message">The message of the warning.</param>
  460. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  461. public bool ShowWarning(string message)
  462. {
  463. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  464. return (result.Equals(MessageBoxResult.Yes));
  465. }
  466. /// <summary>
  467. /// Updates the colour of a canvas.
  468. /// </summary>
  469. /// <param name="canvasName">The name of the canvas to be updated.</param>
  470. /// <param name="active">Whether or not the canvas is active.</param>
  471. public void SetCanvasState(string canvasName, bool active)
  472. {
  473. Canvas canvas;
  474. switch (canvasName){
  475. case ("LeftCanvas"):
  476. canvas = LeftCanvas;
  477. break;
  478. case ("RightCanvas"):
  479. canvas = RightCanvas;
  480. break;
  481. default:
  482. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  483. }
  484. if (active)
  485. {
  486. canvas.Background = Brushes.White;
  487. }
  488. else
  489. {
  490. canvas.Background = Brushes.SlateGray;
  491. }
  492. }
  493. /************************/
  494. /*** HELPING FUNCTION ***/
  495. /************************/
  496. /// <summary>
  497. /// A function which simulates canvas input for debugging.
  498. /// </summary>
  499. /// <param name="option"></param>
  500. private async void Debug(int option)
  501. {
  502. Point[] points;
  503. switch (option)
  504. {
  505. case 1:
  506. points = debugDat.debugPoints1;
  507. break;
  508. case 2:
  509. points = debugDat.debugPoints2;
  510. break;
  511. default:
  512. return;
  513. }
  514. dispatcherTimer.Stop();
  515. debugRunning = true;
  516. ProgramPresenter.Tick(); await Task.Delay(10);
  517. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(50, 50));
  518. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  519. for (int x = 0; x < points.Length; x++)
  520. {
  521. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  522. await Task.Delay(1);
  523. if (x % 5 == 0)
  524. {
  525. ProgramPresenter.Tick();
  526. await Task.Delay(1);
  527. }
  528. }
  529. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  530. debugRunning = false;
  531. dispatcherTimer.Start();
  532. }
  533. }
  534. }