GasXmlView.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Key;
  3. using Aitex.Core.Util;
  4. using DocumentFormat.OpenXml.Drawing;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.Device.Bases;
  7. using OpenSEMI.ClientBase;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using System.Windows.Threading;
  23. using MECF.Framework.UI.Core.DxfScript;
  24. using MECF.Framework.Common;
  25. using System.IO;
  26. namespace FurnaceGasPanelUI.Views.Maintenances
  27. {
  28. /// <summary>
  29. /// TestView.xaml 的交互逻辑
  30. /// </summary>
  31. public partial class GasXmlView : UserControl
  32. {
  33. GasDxfDocument GasMap = null;
  34. private DispatcherTimer _refreshTimer;
  35. public GasXmlView()
  36. {
  37. InitializeComponent();
  38. GasMapProvider.GasMap = GasMap = new GasDxfDocument(draw, true, "GasViewXml.xml");
  39. draw.ClipToBounds = true;
  40. draw.Cursor = Cursors.Arrow;
  41. GasMap.LoadGas();
  42. GasMap.ParseConditions();
  43. }
  44. #region 画布事件
  45. private bool dragged = false;
  46. private System.Windows.Point mouseDownPoint = new System.Windows.Point(-1, -1);
  47. private Dictionary<UIElement, Rect> initialRect = new Dictionary<UIElement, Rect>();
  48. #region 鼠标操作:放大/缩小/拖拽/点击
  49. // 处理鼠标移动事件
  50. private void Canvas_MouseMove(object sender, MouseEventArgs e)
  51. {
  52. if (GasMap == null || GasMap.Lines == null) return;
  53. if (e.LeftButton == MouseButtonState.Pressed)
  54. {
  55. //拖动状态
  56. if (mouseDownPoint.X > 0 && mouseDownPoint.Y > 0)
  57. {
  58. dragged = true;
  59. }
  60. }
  61. }
  62. // 处理鼠标左键松开事件
  63. private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  64. {
  65. var endPoint = e.GetPosition(draw);
  66. if (dragged)
  67. {
  68. DraggedHandle(endPoint);
  69. return;
  70. }
  71. ClickHandle(endPoint);
  72. }
  73. // 处理鼠标左键按下事件
  74. private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  75. {
  76. var startPoint = e.GetPosition(draw);
  77. DownHandle(startPoint);
  78. CheckIfMouseIsOutOfCanvas(startPoint, draw);
  79. GasMap.ModifyDrag();
  80. }
  81. // 处理鼠标滚轮事件
  82. private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
  83. {
  84. if (e.Delta > 0)
  85. {
  86. ZoomInCmdClick(null, null);//放大
  87. }
  88. else
  89. {
  90. ZoomOutCmdClick(null, null);// 缩小
  91. }
  92. }
  93. #endregion
  94. #region 触摸操作:点击/拖拽
  95. private System.Windows.Point? touchStartPoint = null;
  96. private const double DragThreshold = 5.0;
  97. private void Canvas_TouchUp(object sender, TouchEventArgs e)
  98. {
  99. if (touchStartPoint.HasValue)
  100. {
  101. var endPoint = e.GetTouchPoint(draw).Position;
  102. double distance = Math.Sqrt(Math.Pow(endPoint.X - touchStartPoint.Value.X, 2) + Math.Pow(endPoint.Y - touchStartPoint.Value.Y, 2));
  103. if (distance < DragThreshold)
  104. {
  105. // 触发点击事件
  106. // ClickHandle(endPoint);
  107. }
  108. else
  109. {
  110. // 触发拖拽完成事件
  111. DraggedHandle(endPoint);
  112. }
  113. touchStartPoint = null;
  114. draw.ReleaseTouchCapture(e.TouchDevice);
  115. }
  116. }
  117. private void Canvas_TouchDown(object sender, TouchEventArgs e)
  118. {
  119. var startPoint = e.GetTouchPoint(draw).Position;
  120. DownHandle(startPoint);
  121. CheckIfMouseIsOutOfCanvas(startPoint, draw);
  122. draw.CaptureTouch(e.TouchDevice);
  123. GasMap.ModifyDrag();
  124. }
  125. #endregion
  126. #region 多指操作 :放大/缩小
  127. private void Canvas_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
  128. {
  129. ScriptVariables.IsFetchRT = false;
  130. e.ManipulationContainer = draw; // 设置操作容器为画布本身
  131. e.Mode = ManipulationModes.All; // 启用所有操作类型
  132. }
  133. private void Canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
  134. {
  135. if (e.DeltaManipulation.Scale.X > 1.0 && e.DeltaManipulation.Scale.Y > 1.0)
  136. {
  137. ZoomInCmdClick(null, null);//放大
  138. }
  139. else if (e.DeltaManipulation.Scale.X < 1.0 && e.DeltaManipulation.Scale.Y < 1.0)
  140. {
  141. ZoomOutCmdClick(null, null);// 缩小
  142. }
  143. e.Handled = true;
  144. }
  145. private void Canvas_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
  146. {
  147. ScriptVariables.IsFetchRT = true;
  148. }
  149. #endregion
  150. private void ZoomInCmdClick(object sender, RoutedEventArgs e)
  151. {
  152. double rate = 1;
  153. if (GasMap.Scale < 10)
  154. {
  155. GasMap.Scale += 0.5;
  156. rate = GasMap.Scale / (GasMap.Scale - 0.5);
  157. }
  158. double backupDragX = GasMap.dragX;
  159. double backupDragY = GasMap.dragY;
  160. GasMap.dragX = 0;
  161. GasMap.dragY = 0;
  162. GasMap.Draw();
  163. GasMap.dragX = backupDragX * rate;
  164. GasMap.dragY = backupDragY * rate;
  165. GasMap.DrawMovedImage();
  166. }
  167. private void ZoomOutCmdClick(object sender, RoutedEventArgs e)
  168. {
  169. double rate = 1;
  170. if (GasMap.Scale > 1)
  171. {
  172. GasMap.Scale -= 0.5;
  173. rate = GasMap.Scale / (GasMap.Scale + 0.5);
  174. }
  175. else
  176. {
  177. GasMap.Scale = 1;
  178. GasMap.dragX = 0;
  179. GasMap.dragY = 0;
  180. }
  181. double backupDragX = GasMap.dragX;
  182. double backupDragY = GasMap.dragY;
  183. GasMap.dragX = 0;
  184. GasMap.dragY = 0;
  185. GasMap.Draw();
  186. GasMap.dragX = backupDragX * rate;
  187. GasMap.dragY = backupDragY * rate;
  188. GasMap.DrawMovedImage();
  189. }
  190. private void CheckIfMouseIsOutOfCanvas(System.Windows.Point mousePosition, Canvas canvas)
  191. {
  192. double canvasWidth = canvas.ActualWidth;
  193. double canvasHeight = canvas.ActualHeight;
  194. if (mousePosition.X < 0 || mousePosition.Y < 0 || mousePosition.X > canvasWidth || mousePosition.Y > canvasHeight)
  195. {
  196. this.CaptureMouse();
  197. }
  198. }
  199. private void DraggedHandle(System.Windows.Point point)
  200. {
  201. dragged = false;
  202. GasMap.dragX += point.X - mouseDownPoint.X;
  203. GasMap.dragY += point.Y - mouseDownPoint.Y;
  204. GasMap.DrawMovedImage();
  205. mouseDownPoint.X = -1;
  206. mouseDownPoint.Y = -1;
  207. }
  208. private void ClickHandle(System.Windows.Point point)
  209. {
  210. var x = GasMap.ScreenToDxfX(point.X);
  211. var y = GasMap.ScreenToDxfY(point.Y);
  212. GasBaseShape shape = GasMap.GetShapeByPosition(x, y);
  213. if (shape != null)
  214. {
  215. GasMap.DrawShape(shape, true);
  216. GasMapProvider.GasMap.SelectedShape = GasMap.SelectedShape = shape;
  217. if (shape.ClickCondition != null)
  218. {
  219. shape.ClickCondition.Execute();
  220. GasMap.Draw();
  221. }
  222. }
  223. }
  224. private void DownHandle(System.Windows.Point point)
  225. {
  226. mouseDownPoint.X = point.X;
  227. mouseDownPoint.Y = point.Y;
  228. touchStartPoint = point;
  229. }
  230. #endregion
  231. }
  232. }