using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.Key; using Aitex.Core.Util; using DocumentFormat.OpenXml.Drawing; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Device.Bases; using OpenSEMI.ClientBase; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; using MECF.Framework.UI.Core.DxfScript; using MECF.Framework.Common; using System.IO; namespace FurnaceGasPanelUI.Views.Maintenances { /// /// TestView.xaml 的交互逻辑 /// public partial class GasXmlView : UserControl { GasDxfDocument GasMap = null; private DispatcherTimer _refreshTimer; public GasXmlView() { InitializeComponent(); GasMapProvider.GasMap = GasMap = new GasDxfDocument(draw, true, "GasViewXml.xml"); draw.ClipToBounds = true; draw.Cursor = Cursors.Arrow; GasMap.LoadGas(); GasMap.ParseConditions(); } #region 画布事件 private bool dragged = false; private System.Windows.Point mouseDownPoint = new System.Windows.Point(-1, -1); private Dictionary initialRect = new Dictionary(); #region 鼠标操作:放大/缩小/拖拽/点击 // 处理鼠标移动事件 private void Canvas_MouseMove(object sender, MouseEventArgs e) { if (GasMap == null || GasMap.Lines == null) return; if (e.LeftButton == MouseButtonState.Pressed) { //拖动状态 if (mouseDownPoint.X > 0 && mouseDownPoint.Y > 0) { dragged = true; } } } // 处理鼠标左键松开事件 private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var endPoint = e.GetPosition(draw); if (dragged) { DraggedHandle(endPoint); return; } ClickHandle(endPoint); } // 处理鼠标左键按下事件 private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var startPoint = e.GetPosition(draw); DownHandle(startPoint); CheckIfMouseIsOutOfCanvas(startPoint, draw); GasMap.ModifyDrag(); } // 处理鼠标滚轮事件 private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e) { if (e.Delta > 0) { ZoomInCmdClick(null, null);//放大 } else { ZoomOutCmdClick(null, null);// 缩小 } } #endregion #region 触摸操作:点击/拖拽 private System.Windows.Point? touchStartPoint = null; private const double DragThreshold = 5.0; private void Canvas_TouchUp(object sender, TouchEventArgs e) { if (touchStartPoint.HasValue) { var endPoint = e.GetTouchPoint(draw).Position; double distance = Math.Sqrt(Math.Pow(endPoint.X - touchStartPoint.Value.X, 2) + Math.Pow(endPoint.Y - touchStartPoint.Value.Y, 2)); if (distance < DragThreshold) { // 触发点击事件 // ClickHandle(endPoint); } else { // 触发拖拽完成事件 DraggedHandle(endPoint); } touchStartPoint = null; draw.ReleaseTouchCapture(e.TouchDevice); } } private void Canvas_TouchDown(object sender, TouchEventArgs e) { var startPoint = e.GetTouchPoint(draw).Position; DownHandle(startPoint); CheckIfMouseIsOutOfCanvas(startPoint, draw); draw.CaptureTouch(e.TouchDevice); GasMap.ModifyDrag(); } #endregion #region 多指操作 :放大/缩小 private void Canvas_ManipulationStarting(object sender, ManipulationStartingEventArgs e) { ScriptVariables.IsFetchRT = false; e.ManipulationContainer = draw; // 设置操作容器为画布本身 e.Mode = ManipulationModes.All; // 启用所有操作类型 } private void Canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { if (e.DeltaManipulation.Scale.X > 1.0 && e.DeltaManipulation.Scale.Y > 1.0) { ZoomInCmdClick(null, null);//放大 } else if (e.DeltaManipulation.Scale.X < 1.0 && e.DeltaManipulation.Scale.Y < 1.0) { ZoomOutCmdClick(null, null);// 缩小 } e.Handled = true; } private void Canvas_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e) { ScriptVariables.IsFetchRT = true; } #endregion private void ZoomInCmdClick(object sender, RoutedEventArgs e) { double rate = 1; if (GasMap.Scale < 10) { GasMap.Scale += 0.5; rate = GasMap.Scale / (GasMap.Scale - 0.5); } double backupDragX = GasMap.dragX; double backupDragY = GasMap.dragY; GasMap.dragX = 0; GasMap.dragY = 0; GasMap.Draw(); GasMap.dragX = backupDragX * rate; GasMap.dragY = backupDragY * rate; GasMap.DrawMovedImage(); } private void ZoomOutCmdClick(object sender, RoutedEventArgs e) { double rate = 1; if (GasMap.Scale > 1) { GasMap.Scale -= 0.5; rate = GasMap.Scale / (GasMap.Scale + 0.5); } else { GasMap.Scale = 1; GasMap.dragX = 0; GasMap.dragY = 0; } double backupDragX = GasMap.dragX; double backupDragY = GasMap.dragY; GasMap.dragX = 0; GasMap.dragY = 0; GasMap.Draw(); GasMap.dragX = backupDragX * rate; GasMap.dragY = backupDragY * rate; GasMap.DrawMovedImage(); } private void CheckIfMouseIsOutOfCanvas(System.Windows.Point mousePosition, Canvas canvas) { double canvasWidth = canvas.ActualWidth; double canvasHeight = canvas.ActualHeight; if (mousePosition.X < 0 || mousePosition.Y < 0 || mousePosition.X > canvasWidth || mousePosition.Y > canvasHeight) { this.CaptureMouse(); } } private void DraggedHandle(System.Windows.Point point) { dragged = false; GasMap.dragX += point.X - mouseDownPoint.X; GasMap.dragY += point.Y - mouseDownPoint.Y; GasMap.DrawMovedImage(); mouseDownPoint.X = -1; mouseDownPoint.Y = -1; } private void ClickHandle(System.Windows.Point point) { var x = GasMap.ScreenToDxfX(point.X); var y = GasMap.ScreenToDxfY(point.Y); GasBaseShape shape = GasMap.GetShapeByPosition(x, y); if (shape != null) { GasMap.DrawShape(shape, true); GasMapProvider.GasMap.SelectedShape = GasMap.SelectedShape = shape; if (shape.ClickCondition != null) { shape.ClickCondition.Execute(); GasMap.Draw(); } } } private void DownHandle(System.Windows.Point point) { mouseDownPoint.X = point.X; mouseDownPoint.Y = point.Y; touchStartPoint = point; } #endregion } }