RecipeGasXmlView.xaml.cs 8.2 KB

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