MainWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Timers;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Windows.Threading;
  19. namespace SketchAssistantWPF
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window, MVP_View
  25. {
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. ProgramPresenter = new MVP_Presenter(this);
  30. // DispatcherTimer setup
  31. dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
  32. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  33. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  34. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
  35. new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
  36. }
  37. public enum ButtonState
  38. {
  39. Enabled,
  40. Disabled,
  41. Active
  42. }
  43. DispatcherTimer dispatcherTimer;
  44. /// <summary>
  45. /// Dialog to select a file.
  46. /// </summary>
  47. OpenFileDialog openFileDialog = new OpenFileDialog();
  48. /// <summary>
  49. /// All Lines in the current session
  50. /// </summary>
  51. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  52. /// <summary>
  53. /// Queue for the cursorPositions
  54. /// </summary>
  55. Queue<Point> cursorPositions = new Queue<Point>();
  56. /// <summary>
  57. /// The Presenter Component of the MVP-Model
  58. /// </summary>
  59. MVP_Presenter ProgramPresenter;
  60. /// <summary>
  61. /// The line currently being drawn
  62. /// </summary>
  63. Polyline currentLine;
  64. /********************************************/
  65. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  66. /********************************************/
  67. /// <summary>
  68. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  69. /// </summary>
  70. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  71. {
  72. ProgramPresenter.Resize(new Tuple<int, int>((int) LeftCanvas.Width, (int) LeftCanvas.Height),
  73. new Tuple<int, int>((int) RightCanvas.Width, (int) RightCanvas.Height));
  74. }
  75. /// <summary>
  76. /// Redo an Action.
  77. /// </summary>
  78. private void RedoButton_Click(object sender, RoutedEventArgs e)
  79. {
  80. ProgramPresenter.Redo();
  81. }
  82. /// <summary>
  83. /// Undo an Action.
  84. /// </summary>
  85. private void UndoButton_Click(object sender, RoutedEventArgs e)
  86. {
  87. ProgramPresenter.Undo();
  88. }
  89. /// <summary>
  90. /// Changes the state of the program to deletion
  91. /// </summary>
  92. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. ProgramPresenter.ChangeState(false);
  95. }
  96. /// <summary>
  97. /// Changes the state of the program to drawing
  98. /// </summary>
  99. private void DrawButton_Click(object sender, RoutedEventArgs e)
  100. {
  101. ProgramPresenter.ChangeState(true);
  102. }
  103. /// <summary>
  104. /// Hold left mouse button to start drawing.
  105. /// </summary>
  106. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  107. {
  108. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  109. }
  110. /// <summary>
  111. /// Lift left mouse button to stop drawing and add a new Line.
  112. /// </summary>
  113. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  114. {
  115. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  116. }
  117. /// <summary>
  118. /// Get current Mouse positon within the right picture box.
  119. /// </summary>
  120. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  121. {
  122. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  123. }
  124. /// <summary>
  125. /// Button to create a new Canvas. Will create an empty image
  126. /// which is the size of the left image, if there is one.
  127. /// If there is no image loaded the canvas will be the size of the right picture box
  128. /// </summary>
  129. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  130. {
  131. ProgramPresenter.NewCanvas();
  132. }
  133. /// <summary>
  134. /// Ticks the Presenter.
  135. /// </summary>
  136. private void dispatcherTimer_Tick(object sender, EventArgs e)
  137. {
  138. ProgramPresenter.Tick();
  139. }
  140. /// <summary>
  141. /// Import button, will open an OpenFileDialog
  142. /// </summary>
  143. private void MenuItem_Click(object sender, RoutedEventArgs e)
  144. {
  145. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  146. }
  147. /*************************/
  148. /*** PRESENTER -> VIEW ***/
  149. /*************************/
  150. /*
  151. /// <summary>
  152. /// Displays a list of lines in the left Picture box.
  153. /// </summary>
  154. /// <param name="img">The new image.</param>
  155. public void DisplayInLeftPictureBox(List<InternalLine> lineList)
  156. {
  157. foreach (InternalLine line in lineList)
  158. {
  159. Polyline newPolyLine = new Polyline();
  160. newPolyLine.Stroke = Brushes.Black;
  161. newPolyLine.Points = line.GetPointCollection();
  162. LeftCanvas.Children.Add(newPolyLine);
  163. }
  164. }
  165. /// <summary>
  166. /// Displays a list of lines in the right Picture box.
  167. /// </summary>
  168. /// <param name="img">The new image.</param>
  169. public void DisplayInRightPictureBox(List<InternalLine> lineList)
  170. {
  171. foreach (InternalLine line in lineList)
  172. {
  173. Polyline newPolyLine = new Polyline();
  174. newPolyLine.Stroke = Brushes.Black;
  175. newPolyLine.Points = line.GetPointCollection();
  176. LeftCanvas.Children.Add(newPolyLine);
  177. }
  178. }*/
  179. /// <summary>
  180. /// Remove the current line.
  181. /// </summary>
  182. public void RemoveCurrLine()
  183. {
  184. RightCanvas.Children.Remove(currentLine);
  185. }
  186. /// <summary>
  187. /// Display the current line.
  188. /// </summary>
  189. /// <param name="line">The current line to display</param>
  190. public void DisplayCurrLine(Polyline line)
  191. {
  192. if (RightCanvas.Children.Contains(currentLine))
  193. {
  194. RemoveCurrLine();
  195. }
  196. RightCanvas.Children.Add(line);
  197. currentLine = line;
  198. }
  199. /// <summary>
  200. /// Removes all Lines from the left canvas.
  201. /// </summary>
  202. public void RemoveAllLeftLines()
  203. {
  204. LeftCanvas.Children.Clear();
  205. }
  206. /// <summary>
  207. /// Removes all lines in the right canvas.
  208. /// </summary>
  209. public void RemoveAllRightLines()
  210. {
  211. RightCanvas.Children.Clear();
  212. }
  213. /// <summary>
  214. /// Adds another Line that will be displayed in the left display.
  215. /// </summary>
  216. /// <param name="newLine">The new Polyline to be added displayed.</param>
  217. public void AddNewLineLeft(Polyline newLine)
  218. {
  219. newLine.Stroke = Brushes.Black;
  220. newLine.StrokeThickness = 2;
  221. LeftCanvas.Children.Add(newLine);
  222. }
  223. /// <summary>
  224. /// Adds another Line that will be displayed in the right display.
  225. /// </summary>
  226. /// <param name="newLine">The new Polyline to be added displayed.</param>
  227. public void AddNewLineRight(Polyline newLine)
  228. {
  229. newLine.Stroke = Brushes.Black;
  230. newLine.StrokeThickness = 2;
  231. RightCanvas.Children.Add(newLine);
  232. }
  233. /// <summary>
  234. /// Enables the timer of the View, which will tick the Presenter.
  235. /// </summary>
  236. public void EnableTimer()
  237. {
  238. dispatcherTimer.Start();
  239. }
  240. /// <summary>
  241. /// A function that opens a file dialog and returns the filename.
  242. /// </summary>
  243. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  244. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  245. /// else returns a tuple with empty strigns</returns>
  246. public Tuple<string, string> openNewDialog(string Filter)
  247. {
  248. openFileDialog.Filter = Filter;
  249. if (openFileDialog.ShowDialog() == true)
  250. {
  251. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  252. }
  253. else
  254. {
  255. return new Tuple<string, string>("", "");
  256. }
  257. }
  258. /// <summary>
  259. /// Sets the contents of the last action taken indicator label.
  260. /// </summary>
  261. /// <param name="message">The new contents</param>
  262. public void SetLastActionTakenText(string message)
  263. {
  264. LastActionBox.Text = message;
  265. }
  266. /// <summary>
  267. /// Changes the states of a tool strip button.
  268. /// </summary>
  269. /// <param name="buttonName">The name of the button.</param>
  270. /// <param name="state">The new state of the button.</param>
  271. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  272. {
  273. ButtonBase buttonToChange;
  274. bool isToggleable = false;
  275. switch (buttonName)
  276. {
  277. case "canvasButton":
  278. buttonToChange = CanvasButton;
  279. break;
  280. case "drawButton":
  281. buttonToChange = DrawButton;
  282. isToggleable = true;
  283. break;
  284. case "deleteButton":
  285. buttonToChange = DeleteButton;
  286. isToggleable = true;
  287. break;
  288. case "undoButton":
  289. buttonToChange = UndoButton;
  290. break;
  291. case "redoButton":
  292. buttonToChange = RedoButton;
  293. break;
  294. default:
  295. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  296. return;
  297. }
  298. if (isToggleable)
  299. {
  300. switch (state)
  301. {
  302. case ButtonState.Active:
  303. ((ToggleButton)buttonToChange).IsEnabled = true;
  304. ((ToggleButton)buttonToChange).IsChecked = true;
  305. ((ToggleButton)buttonToChange).Opacity = 1;
  306. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  307. break;
  308. case ButtonState.Disabled:
  309. ((ToggleButton)buttonToChange).IsEnabled = false;
  310. ((ToggleButton)buttonToChange).IsChecked = false;
  311. ((ToggleButton)buttonToChange).Opacity = 0.5;
  312. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  313. break;
  314. case ButtonState.Enabled:
  315. ((ToggleButton)buttonToChange).IsEnabled = true;
  316. ((ToggleButton)buttonToChange).IsChecked = false;
  317. ((ToggleButton)buttonToChange).Opacity = 1;
  318. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  319. break;
  320. }
  321. }
  322. else
  323. {
  324. switch (state)
  325. {
  326. case ButtonState.Disabled:
  327. ((Button)buttonToChange).IsEnabled = false;
  328. ((Button)buttonToChange).Opacity = 0.5;
  329. break;
  330. default:
  331. ((Button)buttonToChange).IsEnabled = true;
  332. ((Button)buttonToChange).Opacity = 1;
  333. break;
  334. }
  335. }
  336. }
  337. /// <summary>
  338. /// Sets the contents of the load status indicator label.
  339. /// </summary>
  340. /// <param name="message">The new contents</param>
  341. public void SetToolStripLoadStatus(string message)
  342. {
  343. LoadStatusBox.Text = message;
  344. }
  345. /// <summary>
  346. /// shows the given info message in a popup and asks the user to aknowledge it
  347. /// </summary>
  348. /// <param name="message">the message to show</param>
  349. public void ShowInfoMessage(string message)
  350. {
  351. MessageBox.Show(message);
  352. }
  353. /// <summary>
  354. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  355. /// </summary>
  356. /// <param name="message">The message of the warning.</param>
  357. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  358. public bool ShowWarning(string message)
  359. {
  360. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  361. return (result.Equals(MessageBoxResult.Yes));
  362. }
  363. /// <summary>
  364. /// Updates the colour of a canvas.
  365. /// </summary>
  366. /// <param name="canvasName">The name of the canvas to be updated.</param>
  367. /// <param name="active">Whether or not the canvas is active.</param>
  368. public void SetCanvasState(string canvasName, bool active)
  369. {
  370. Canvas canvas;
  371. switch (canvasName){
  372. case ("LeftCanvas"):
  373. canvas = LeftCanvas;
  374. break;
  375. case ("RightCanvas"):
  376. canvas = RightCanvas;
  377. break;
  378. default:
  379. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  380. }
  381. if (active)
  382. {
  383. canvas.Background = Brushes.White;
  384. }
  385. else
  386. {
  387. canvas.Background = Brushes.SlateGray;
  388. }
  389. }
  390. }
  391. }