终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SiloPanel.xaml.cs 8.9 KiB

2 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  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. namespace WPFDemo
  20. {
  21. /// <summary>
  22. /// SiloPanel.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class SiloPanel : UserControl, INotifyPropertyChanged
  25. {
  26. //料仓模块左键按下标识
  27. bool siloIsMouseDown = false;
  28. //画线标识
  29. bool lineIsDraw = false;
  30. //当前线条
  31. private Line currentLine;
  32. //线条集合
  33. private List<Line> lines = new List<Line>();
  34. //料仓集合
  35. private List<Silo> silos = new List<Silo>();
  36. //用于序列化
  37. private Segment segment = new Segment();
  38. //提示
  39. private string tips = "常规模式";
  40. /// <summary>
  41. /// 提示
  42. /// </summary>
  43. public string Tips
  44. {
  45. get { return tips; }
  46. set
  47. {
  48. tips = value;
  49. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Tips"));
  50. }
  51. }
  52. /// <summary>
  53. /// 当前选中的料仓
  54. /// </summary>
  55. public Silo CurrentSilo { get; private set; }
  56. /// <summary>
  57. /// 画布宽度
  58. /// </summary>
  59. public double PanelWidth
  60. {
  61. get { return (double)GetValue(PanelWidthProperty); }
  62. set { SetValue(PanelWidthProperty, value); }
  63. }
  64. // Using a DependencyProperty as the backing store for PanelWidth. This enables animation, styling, binding, etc...
  65. public static readonly DependencyProperty PanelWidthProperty =
  66. DependencyProperty.Register("PanelWidth", typeof(double), typeof(SiloPanel), new PropertyMetadata(1920d));
  67. /// <summary>
  68. /// 画布高度
  69. /// </summary>
  70. public double PanelHeight
  71. {
  72. get { return (double)GetValue(PanelHeightProperty); }
  73. set { SetValue(PanelHeightProperty, value); }
  74. }
  75. // Using a DependencyProperty as the backing store for PanelHeight. This enables animation, styling, binding, etc...
  76. public static readonly DependencyProperty PanelHeightProperty =
  77. DependencyProperty.Register("PanelHeight", typeof(double), typeof(SiloPanel), new PropertyMetadata(1080d));
  78. public event PropertyChangedEventHandler? PropertyChanged;
  79. public SiloPanel()
  80. {
  81. InitializeComponent();
  82. cvMain.PreviewMouseLeftButtonDown += CvMain_PreviewMouseLeftButtonDown;
  83. cvMain.PreviewMouseMove += CvMain_PreviewMouseMove;
  84. cvMain.PreviewMouseLeftButtonUp += CvMain_PreviewMouseLeftButtonUp;
  85. }
  86. /// <summary>
  87. /// 开始画线
  88. /// </summary>
  89. public void AddLine()
  90. {
  91. lineIsDraw = true;
  92. Tips = "线条绘制";
  93. }
  94. /// <summary>
  95. /// 添加料仓模块
  96. /// </summary>
  97. public void AddSilo()
  98. {
  99. if (lineIsDraw)
  100. lineIsDraw = false;
  101. newSilo(String.Empty, String.Empty);
  102. }
  103. /// <summary>
  104. /// 保存文件
  105. /// </summary>
  106. public void Save()
  107. {
  108. segment.Silos.Clear();
  109. segment.Lines.Clear();
  110. lines.ForEach(line =>
  111. {
  112. segment.Lines.Add(new Segment.SimpleLine() { X1 = line.X1, X2 = line.X2, Y1 = line.Y1, Y2 = line.Y2 });
  113. });
  114. silos.ForEach(silo =>
  115. {
  116. segment.Silos.Add(new Segment.SimpleSilo()
  117. {
  118. Left = (double)silo.GetValue(Canvas.LeftProperty),
  119. Top = (double)silo.GetValue(Canvas.TopProperty),
  120. Caption = silo.Caption,
  121. Index = silo.Index
  122. });
  123. });
  124. string ResultJson = JsonConvert.SerializeObject(segment);
  125. SaveFileDialog dialog = new SaveFileDialog();
  126. dialog.Filter = "配置文件|*.json";
  127. if (dialog.ShowDialog() == true)
  128. {
  129. File.WriteAllText(dialog.FileName, ResultJson);
  130. }
  131. }
  132. /// <summary>
  133. /// 加载文件
  134. /// </summary>
  135. public void Load()
  136. {
  137. OpenFileDialog dialog = new OpenFileDialog();
  138. dialog.Filter = "配置文件|*.json";
  139. if (dialog.ShowDialog() == true)
  140. {
  141. string content = File.ReadAllText(dialog.FileName);
  142. try
  143. {
  144. segment = JsonConvert.DeserializeObject<Segment>(content);
  145. lines.Clear();
  146. silos.Clear();
  147. cvMain.Children.Clear();
  148. foreach (var line in segment.Lines)
  149. {
  150. newLine(line.X1, line.Y1, line.X2, line.Y2);
  151. }
  152. foreach (var temp in segment.Silos)
  153. {
  154. newSilo(temp.Caption, temp.Index,temp.Left,temp.Top);
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. MessageBox.Show(ex.Message);
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// 清空画布
  165. /// </summary>
  166. public void Clear()
  167. {
  168. lines.Clear();
  169. silos.Clear();
  170. cvMain.Children.Clear();
  171. }
  172. private void newSilo(string caption, string index, double left = -1, double top = -1)
  173. {
  174. Silo silo = new Silo() { Caption = caption, Index = index };
  175. if (left == -1)
  176. silo.SetValue(Canvas.LeftProperty, cvMain.ActualWidth / 2 - silo.Width / 2);
  177. else
  178. silo.SetValue(Canvas.LeftProperty, left);
  179. if (top == -1)
  180. silo.SetValue(Canvas.TopProperty, cvMain.ActualHeight / 2 - silo.Height / 2);
  181. else
  182. silo.SetValue(Canvas.TopProperty, top);
  183. silo.AllowDrop = true;
  184. cvMain.Children.Add(silo);
  185. silos.Add(silo);
  186. silo.PreviewMouseLeftButtonDown += Silo_PreviewMouseLeftButtonDown;
  187. silo.PreviewMouseMove += Silo_PreviewMouseMove;
  188. silo.PreviewMouseLeftButtonUp += Silo_PreviewMouseLeftButtonUp;
  189. CurrentSilo = silo;
  190. }
  191. private void newLine(double x1, double y1, double x2, double y2)
  192. {
  193. var temp = new Line();
  194. temp.Stroke = Brushes.Green;
  195. temp.StrokeThickness = 1;
  196. temp.X1 = x1;
  197. temp.Y1 = y1;
  198. temp.X2 = x2;
  199. temp.Y2 = y2;
  200. cvMain.Children.Add(temp);
  201. lines.Add(temp);
  202. currentLine = temp;
  203. }
  204. private void CvMain_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  205. {
  206. lineIsDraw = false;
  207. Tips = "常规模式";
  208. }
  209. private void CvMain_PreviewMouseMove(object sender, MouseEventArgs e)
  210. {
  211. if (e.LeftButton == MouseButtonState.Pressed && lineIsDraw)
  212. {
  213. Point point = e.GetPosition(cvMain);
  214. currentLine.X2 = point.X;
  215. currentLine.Y2 = point.Y;
  216. }
  217. }
  218. private void CvMain_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  219. {
  220. if (!lineIsDraw)
  221. return;
  222. var point = e.GetPosition(cvMain);
  223. newLine(point.X, point.Y, point.X, point.Y);
  224. }
  225. private void Silo_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  226. {
  227. var c = sender as UIElement;
  228. siloIsMouseDown = false;
  229. c.ReleaseMouseCapture();
  230. CurrentSilo = sender as Silo;
  231. }
  232. private void Silo_PreviewMouseMove(object sender, MouseEventArgs e)
  233. {
  234. if (siloIsMouseDown)
  235. {
  236. var c = sender as UIElement;
  237. var pos = e.GetPosition(this);
  238. c.SetValue(Canvas.LeftProperty, pos.X);
  239. c.SetValue(Canvas.TopProperty, pos.Y);
  240. }
  241. }
  242. private void Silo_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  243. {
  244. var c = sender as UIElement;
  245. siloIsMouseDown = true;
  246. var transform = c.RenderTransform as TranslateTransform;
  247. if (transform == null)
  248. {
  249. transform = new TranslateTransform();
  250. c.RenderTransform = transform;
  251. }
  252. c.CaptureMouse();
  253. }
  254. }
  255. }