MainWindow.xaml.cs 14 KB

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