From 96b6869bb20677f9b945f67c014a9b992ee05ac4 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期三, 28 四月 2021 11:40:01 +0800
Subject: [PATCH] 1. 添加离线测试功能 2. 单键分开测量修改赋值和释放时判断

---
 src/Bro.UI.Model.Winform/UI/CanvasImage.cs |  245 +++++++++++++++++++++++++++---------------------
 1 files changed, 136 insertions(+), 109 deletions(-)

diff --git a/src/Bro.UI.Model.Winform/UI/CanvasImage.cs b/src/Bro.UI.Model.Winform/UI/CanvasImage.cs
index e31ebeb..582061d 100644
--- a/src/Bro.UI.Model.Winform/UI/CanvasImage.cs
+++ b/src/Bro.UI.Model.Winform/UI/CanvasImage.cs
@@ -30,12 +30,15 @@
             MouseDown += Canvas_MouseDown;
             MouseMove += Canvas_MouseMove;
             MouseUp += Canvas_MouseUp;
+
+            EventRouter.ChangeElementsMouseState -= OnElementChangeMouseState;
+            EventRouter.ChangeElementsMouseState += OnElementChangeMouseState;
         }
 
         #region Event
         public Action<MouseState> OnMouseStateChanged;
         public Action<IShapeElement> DrawTemplateChanged = null;
-        public Action<Point, Point> OnMouseLocationUpdated;
+        public Action<Point, Point, string> OnMouseLocationUpdated;
         #endregion
 
         private MouseState mouseState = MouseState.Normal;
@@ -64,8 +67,21 @@
         #endregion
 
         #region 閲嶇粯
+        volatile bool _isInRepaint = false;
+        object _isInRepaintLock = new object();
         protected override void OnPaint(PaintEventArgs e)
         {
+            //if (_isInRepaint)
+            //    return;
+
+            //lock (_isInRepaintLock)
+            //{
+            //    if (_isInRepaint)
+            //        return;
+            //}
+
+            //_isInRepaint = true;
+
             try
             {
                 Rectangle rect = ClientRectangle;
@@ -75,6 +91,9 @@
                 Graphics g = myBuffer.Graphics;
                 g.SmoothingMode = SmoothingMode.HighSpeed;
                 g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
+                g.InterpolationMode = InterpolationMode.Low;
+                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
+                g.CompositingQuality = CompositingQuality.HighSpeed;
                 g.Clear(BackColor);
 
                 g.MultiplyTransform(Matrix);
@@ -170,6 +189,8 @@
             catch (Exception)
             {
             }
+
+            _isInRepaint = false;
         }
 
         private void halfTransparent()
@@ -261,10 +282,12 @@
                         MouseState = MouseState.New;
                         break;
                     case ElementState.Selected:
-                        MouseState = MouseState.SelectedElement;
+                        CurrentElementId = ele.ID;
+                        Cursor = Cursors.Default;
                         break;
                     case ElementState.Moving:
                         MouseState = MouseState.MoveElement;
+                        Cursor = Cursors.NoMove2D;
                         break;
                     case ElementState.MouseInSide:
                         MouseState = MouseState.InSideElement;
@@ -272,14 +295,67 @@
                     case ElementState.MouseHover:
                         MouseState = MouseState.HoverElement;
                         break;
+
+                    case ElementState.CanStretchLeft:
+                        Cursor = Cursors.SizeWE;
+                        break;
+                    case ElementState.StretchingLeft:
+                        MouseState = MouseState.StretchingLeft;
+                        Cursor = Cursors.SizeWE;
+                        break;
+                    case ElementState.CanStretchBottom:
+                        Cursor = Cursors.SizeNS;
+                        break;
+                    case ElementState.StretchingBottom:
+                        MouseState = MouseState.StretchingBottom;
+                        Cursor = Cursors.SizeNS;
+                        break;
+                    case ElementState.CanStretchRight:
+                        Cursor = Cursors.SizeWE;
+                        break;
+                    case ElementState.StretchingRight:
+                        MouseState = MouseState.StretchingRight;
+                        Cursor = Cursors.SizeWE;
+                        break;
+                    case ElementState.CanStretchTop:
+                        Cursor = Cursors.SizeNS;
+                        break;
+                    case ElementState.StretchingTop:
+                        MouseState = MouseState.StretchingTop;
+                        Cursor = Cursors.SizeNS;
+                        break;
+
+                    case ElementState.CanStretchLeftLowerCorner:
+                        Cursor = Cursors.SizeNESW;
+                        break;
+                    case ElementState.StretchingLeftLowerCorner:
+                        MouseState = MouseState.StretchingLeftLowerCorner;
+                        Cursor = Cursors.SizeNESW;
+                        break;
+                    case ElementState.CanStretchLeftUpperCorner:
+                        Cursor = Cursors.SizeNWSE;
+                        break;
+                    case ElementState.StretchingLeftUpperCorner:
+                        MouseState = MouseState.StretchingLeftUpperCorner;
+                        Cursor = Cursors.SizeNWSE;
+                        break;
+                    case ElementState.CanStretchRightLowerCorner:
+                        Cursor = Cursors.SizeNWSE;
+                        break;
+                    case ElementState.StretchingRightLowerCorner:
+                        MouseState = MouseState.StretchingRightLowerCorner;
+                        Cursor = Cursors.SizeNWSE;
+                        break;
+                    case ElementState.CanStretchRightUpperCorner:
+                        Cursor = Cursors.SizeNESW;
+                        break;
+                    case ElementState.StretchingRightUpperCorner:
+                        MouseState = MouseState.StretchingRightUpperCorner;
+                        Cursor = Cursors.SizeNESW;
+                        break;
                     default:
                         MouseState = MouseState.Normal;
                         break;
-                }
-
-                if (MouseState == MouseState.SelectedElement)
-                {
-                    CurrentElementId = ele.ID;
                 }
             }
             else
@@ -301,6 +377,8 @@
                     MouseState = MouseState.Normal;
                 }
             }
+
+            this.Invalidate();
         }
         #endregion
 
@@ -328,6 +406,7 @@
         }
 
         Point startPoint, currentPoint;
+        bool _isMouseBtnPressing = false;
 
         private void Canvas_MouseDown(object sender, MouseEventArgs e)
         {
@@ -335,49 +414,22 @@
 
             if (e.Button == MouseButtons.Left)
             {
+                _isMouseBtnPressing = true;
+
                 switch (MouseState)
                 {
                     case MouseState.Normal:
-                        //if (DrawTemplate == null && string.IsNullOrWhiteSpace(CurrentElementId))
-                        //{
-                        //    this.Cursor = Cursors.Hand;
-                        //    startPoint = e.Location;
-                        //}
-                        //else
-                        //{
-                        //    MouseState = MouseState.New;
-                        //}
-                        MouseState = MouseState.MovingAll;
                         startPoint = e.Location;
-
-                        //Elements.ForEach(ele =>
-                        foreach (IShapeElement ele in Elements)
-                        {
-                            ele.State = ElementState.Normal;
-                            ele.OnMouseDown(p);
-                        }
-                        //);
                         break;
                     case MouseState.StretchingLeft:
                         break;
                     case MouseState.StretchingRight:
                         break;
-                    case MouseState.StretchingUp:
+                    case MouseState.StretchingTop:
                         break;
-                    case MouseState.StretchingDown:
+                    case MouseState.StretchingBottom:
                         break;
                     case MouseState.MoveElement:
-                        break;
-                    case MouseState.SelectedElement:
-                        //MouseState = MouseState.MoveElement;
-                        startPoint = e.Location;
-
-                        //Elements.ForEach(ele =>
-                        foreach (IShapeElement ele in Elements)
-                        {
-                            ele.OnMouseDown(p);
-                        }
-                        //);
                         break;
                     case MouseState.HoverElement:
                     case MouseState.InSideElement:
@@ -392,26 +444,16 @@
                         break;
                 }
 
-                //if (DrawTemplate == null)
-                //{
-                //    this.Cursor = Cursors.Hand;
-                //    startPoint = e.Location;
-                //}
-
-                //Point p = ToMapPoint(e.Location);
-
-                //Elements.ForEach(ele =>
-                //{
-                //    ele.State = ElementState.Normal;
-                //    ele.OnMouseDown(p);
-                //});
-
-                //DrawTemplate?.OnMouseDown(p);
+                foreach (IShapeElement ele in Elements)
+                {
+                    ele.OnMouseDown(p);
+                }
             }
         }
 
         private void Canvas_MouseUp(object sender, MouseEventArgs e)
         {
+            _isMouseBtnPressing = false;
             switch (MouseState)
             {
                 case MouseState.Normal:
@@ -424,9 +466,9 @@
                     break;
                 case MouseState.StretchingRight:
                     break;
-                case MouseState.StretchingUp:
+                case MouseState.StretchingTop:
                     break;
-                case MouseState.StretchingDown:
+                case MouseState.StretchingBottom:
                     break;
                 case MouseState.MoveElement:
                     //MouseState = MouseState.SelectedElement;
@@ -443,7 +485,6 @@
                 case MouseState.SelectionZoneDoing:
                     MouseState = MouseState.SelectionZone;
 
-                    //Elements.ForEach(ele =>
                     foreach (IShapeElement ele in Elements)
                     {
                         if (ele.IsIntersect(_selectionRect))
@@ -451,7 +492,6 @@
                             ele.State = ElementState.Selected;
                         }
                     }
-                    //);
 
                     break;
             }
@@ -460,12 +500,10 @@
 
             Point p = ToMapPoint(e.Location);
             DrawTemplate?.OnMouseUp(p);
-            //Elements.ForEach(ele =>
             foreach (IShapeElement ele in Elements)
             {
                 ele.OnMouseUp(p);
             }
-            //);
         }
 
         private void Canvas_MouseMove(object sender, MouseEventArgs e)
@@ -475,26 +513,26 @@
             switch (MouseState)
             {
                 case MouseState.Normal:
-                    //Elements.ForEach(u =>
-                    //{
-                    //    u.OnMouseMove(p);
-                    //});
+                    {
+                        if (_isMouseBtnPressing)
+                        {
+                            currentPoint = e.Location;
+                            Point p1 = ToMapPoint(startPoint);
+                            Point p2 = ToMapPoint(currentPoint);
+                            Matrix.Translate(p2.X - p1.X, p2.Y - p1.Y);
+                            startPoint = e.Location;
+                        }
+                    }
                     break;
                 case MouseState.StretchingLeft:
                     break;
                 case MouseState.StretchingRight:
                     break;
-                case MouseState.StretchingUp:
+                case MouseState.StretchingTop:
                     break;
-                case MouseState.StretchingDown:
+                case MouseState.StretchingBottom:
                     break;
                 case MouseState.MoveElement:
-                    //Elements.ForEach(ele =>
-                    foreach (IShapeElement ele in Elements)
-                    {
-                        ele.OnMouseMove(p);
-                    }
-                    //);
                     break;
                 case MouseState.HoverElement:
                 case MouseState.InSideElement:
@@ -504,13 +542,6 @@
                 case MouseState.Editing:
                     break;
                 case MouseState.MovingAll:
-                    {
-                        currentPoint = e.Location;
-                        Point p1 = ToMapPoint(startPoint);
-                        Point p2 = ToMapPoint(currentPoint);
-                        Matrix.Translate(p2.X - p1.X, p2.Y - p1.Y);
-                        startPoint = e.Location;
-                    }
                     break;
                 case MouseState.SelectionZoneDoing:
                     {
@@ -529,27 +560,20 @@
                     break;
             }
 
-            //if (this.Cursor == Cursors.Hand)
-            //{
-            //    currentPoint = e.Location;
-
-            //    Point p1 = ToMapPoint(startPoint);
-            //    Point p2 = ToMapPoint(currentPoint);
-            //    Matrix.Translate(p2.X - p1.X, p2.Y - p1.Y);
-            //    startPoint = e.Location;
-            //}
-
-            //DisplayMouseLocation(e.Location);
-            OnMouseLocationUpdated?.BeginInvoke(e.Location, ToMapPoint(e.Location), null, null);
+            Point mapPoint = ToMapPoint(e.Location);
+            Color color = Color.Transparent;
+            if (MAP != null && mapPoint.X > 0 && mapPoint.X < MAP.Width && mapPoint.Y > 0 && mapPoint.Y < MAP.Height)
+            {
+                color = MAP.GetPixel(mapPoint.X, mapPoint.Y);
+            }
+            OnMouseLocationUpdated?.BeginInvoke(e.Location, mapPoint, color.Name, null, null);
 
             if (MouseState != MouseState.SelectionZoneDoing)
             {
-                //Elements.ForEach(u =>
                 foreach (IShapeElement ele in Elements)
                 {
                     ele.OnMouseMove(p);
                 }
-                //);
             }
 
             Invalidate();
@@ -563,7 +587,7 @@
             {
                 switch (MouseState)
                 {
-                    case MouseState.SelectedElement:
+                    //case MouseState.SelectedElement:
                     case MouseState.HoverElement:
                     case MouseState.InSideElement:
                     case MouseState.MoveElement:
@@ -581,7 +605,7 @@
             }
             else
             {
-                if (MouseState == MouseState.SelectedElement)
+                //if (MouseState == MouseState.SelectedElement)
                 {
                     MouseState = MouseState.Normal;
 
@@ -607,6 +631,9 @@
         {
             if (map == null)
                 return;
+
+            MAP?.Dispose();
+            MAP = null;
 
             MAP = map;
             //MAP = map;
@@ -696,22 +723,22 @@
         #region 鎸夐敭鎿嶄綔
         public void OnCanvasKeyPressed(object sender, KeyPressEventArgs e)
         {
-            if (e.KeyChar == 27)  //Esc
-            {
-                if (MouseState == MouseState.SelectedElement)
-                {
-                    MouseState = MouseState.Normal;
+            //if (e.KeyChar == 27)  //Esc
+            //{
+            //    //if (MouseState == MouseState.SelectedElement)
+            //    {
+            //        MouseState = MouseState.Normal;
 
-                    //Elements.ForEach(ele =>
-                    foreach (IShapeElement ele in Elements)
-                    {
-                        ele.State = ElementState.Normal;
-                    }
-                    //);
-                }
-            }
+            //        //Elements.ForEach(ele =>
+            //        foreach (IShapeElement ele in Elements)
+            //        {
+            //            ele.State = ElementState.Normal;
+            //        }
+            //        //);
+            //    }
+            //}
 
-            Invalidate();
+            //Invalidate();
         }
 
         public void OnCanvasKeyDown(object sender, KeyEventArgs e)
@@ -735,7 +762,7 @@
                     Elements.Remove(Elements.FirstOrDefault(u => u.ID == CurrentElementId));
                     break;
                 case Keys.Escape:
-                    if (MouseState == MouseState.SelectedElement)
+                    //if (MouseState == MouseState.SelectedElement)
                     {
                         MouseState = MouseState.Normal;
 

--
Gitblit v1.8.0