cloudflight
2025-01-13 02c14c9c57dbde72d9c771bd8b0fab1cd023047d
WinFrmUI/Yw.WinFrmUI.Hydro.L2d.Core/02-panel/NetworkPanel.cs
@@ -1,5 +1,4 @@

namespace Yw.WinFrmUI.HydroL2d
namespace Yw.WinFrmUI.HydroL2d
{
    /// <summary>
    /// 管网面板
@@ -22,22 +21,11 @@
            this.MinimumSize = new Size(10, 10);
        }
        #region 初始化
        protected Network _network = null;//管网
        protected RectangleF _networkBounds;//管网边界
        private Bitmap _cache = null;//缓存<依据工作区域创建的空图片>
        private object _cacheLocker = new();//缓存锁
        private Bitmap _disp = null;//显示<在cache基础上进行绘制,最终的显示图片>
        private object _dispLocker = new();//显示锁
        private float _zoomMin = 0.01f;//最小缩放层级
        private float _zoomMax = 10000f;//最大缩放层级
        private float _zoom = 1f;//当前缩放层级
        private float _dxo = 0f;//x偏移量
        private float _dyo = 0f;//y偏移量
        private RectangleF _clientRectf;//工作区域矩形
        #region 初始化
        protected RectangleF _clientRectf;//工作区域矩形
        /// <summary>
        /// 是否初始化
@@ -60,6 +48,9 @@
        #region 缓存与显示
        private Bitmap _cache = null;//缓存<依据工作区域创建的空图片>
        private object _cacheLocker = new();//缓存锁
        /// <summary>
        /// 创建缓存
        /// </summary>
@@ -70,7 +61,6 @@
                return;
            }
            var img = new Bitmap((int)_clientRectf.Width, (int)_clientRectf.Height);
            img.MakeTransparent(Color.White);
            lock (_cacheLocker)
            {
                if (_cache != null)
@@ -94,14 +84,10 @@
            {
                return;
            }
            var disp = _cache.CloneC();
            if (disp == null)
            {
                return;
            }
            var dispRect = GetDispRectangleF();
            using (var g = Graphics.FromImage(disp))
            using (var g = Graphics.FromImage(_cache))
            {
                g.Clear(Color.White);
                g.PageUnit = GraphicsUnit.Pixel;
                g.InterpolationMode = InterpolationMode.High;
                g.SmoothingMode = SmoothingMode.AntiAlias;
@@ -111,15 +97,6 @@
                g.ScaleTransform(1, -1);
                _network.Draw(g, dispRect);
                // _network.Draw(g);
            }
            lock (_dispLocker)
            {
                if (_disp != null)
                {
                    _disp.Dispose();
                }
                _disp = disp;
            }
        }
@@ -132,11 +109,11 @@
            {
                return;
            }
            lock (_dispLocker)
            lock (_cacheLocker)
            {
                if (_disp != null)
                if (_cache != null)
                {
                    g.DrawImage(_disp, _clientRectf);
                    g.DrawImage(_cache, _clientRectf);
                }
            }
@@ -144,7 +121,13 @@
        #endregion
        #region 缩放
        #region 缩放与偏移
        private float _zoomMin = 0.001f;//最小缩放层级
        private float _zoomMax = 30000f;//最大缩放层级
        private float _zoom = 1f;//当前缩放层级
        private float _dxo = 0f;//x偏移量
        private float _dyo = 0f;//y偏移量
        /// <summary>
        /// 缩放等级
@@ -288,7 +271,7 @@
        #endregion
        #region 获取矩形
        #region 获取矩形区域
        /// <summary>
        /// 获取工作区域
@@ -345,17 +328,11 @@
        {
            if (e.Button == MouseButtons.Left)
            {
                if (Initialized)
                {
                    if (this.AllowMoveWhenMouseLeftDown)
                    {
                        _hasMouseLeftDown = true;
                        _mouseLeftDownMovePoint = e.Location;
                        return true;
                    }
                }
                _hasMouseLeftDown = true;
                _mouseLeftDownMovePoint = e.Location;
                return true;
            }
            return true;
            return false;
        }
        /// <summary>
@@ -364,20 +341,17 @@
        /// <param name="e"></param>
        protected virtual bool MouseLeftDownMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            if (_hasMouseLeftDown)
            {
                if (this.AllowMoveWhenMouseLeftDown)
                {
                    if (_hasMouseLeftDown)
                    if (this.Initialized)
                    {
                        if (Initialized)
                        {
                            var pt = new PointF(e.X - _mouseLeftDownMovePoint.X, e.Y - _mouseLeftDownMovePoint.Y);
                            _dxo += pt.X / _zoom;
                            _dyo += pt.Y / _zoom;
                            _mouseLeftDownMovePoint = e.Location;
                            return true;
                        }
                        var pt = new PointF(e.X - _mouseLeftDownMovePoint.X, e.Y - _mouseLeftDownMovePoint.Y);
                        _dxo += pt.X / _zoom;
                        _dyo += pt.Y / _zoom;
                        _mouseLeftDownMovePoint = e.Location;
                        return true;
                    }
                }
            }
@@ -389,13 +363,86 @@
        /// </summary>
        protected virtual bool HasMouseLeftUp(MouseEventArgs e)
        {
            _hasMouseLeftDown = false;
            return true;
            if (_hasMouseLeftDown)
            {
                _hasMouseLeftDown = false;
                return true;
            }
            return false;
        }
        #endregion
        #region 鼠标双击恢复自适应
        #region 鼠标右键按下拖动
        /// <summary>
        /// 当鼠标右键按下时允许拖动
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标右键按下时允许拖动")]
        [DefaultValue(true)]
        public bool AllowMoveWhenMouseRightDown
        {
            get => _allowMoveWhenMouseRightDown;
            set => _allowMoveWhenMouseRightDown = value;
        }
        private bool _allowMoveWhenMouseRightDown = true;
        protected bool _hasMouseRightDown = false;//鼠标右键是否按下
        protected Point _mouseRightDownMovePoint;//鼠标右键按下移动点
        /// <summary>
        /// 判断鼠标右键是否按下
        /// </summary>
        protected virtual bool HasMouseRightDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                _hasMouseRightDown = true;
                _mouseRightDownMovePoint = e.Location;
                return true;
            }
            return false;
        }
        /// <summary>
        /// 鼠标右键按下移动
        /// </summary>
        protected virtual bool MouseRightDownMove(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                if (this.AllowMoveWhenMouseRightDown)
                {
                    if (this.Initialized)
                    {
                        var pt = new PointF(e.X - _mouseRightDownMovePoint.X, e.Y - _mouseRightDownMovePoint.Y);
                        _dxo += pt.X / _zoom;
                        _dyo += pt.Y / _zoom;
                        _mouseRightDownMovePoint = e.Location;
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 判断鼠标左键是否弹起
        /// </summary>
        protected virtual bool HasMouseRightUp(MouseEventArgs e)
        {
            if (_hasMouseRightDown)
            {
                _hasMouseRightDown = false;
                return true;
            }
            return false;
        }
        #endregion
        #region 鼠标左键双击恢复自适应
        /// <summary>
        /// 当鼠标双击时允许自适应
@@ -403,27 +450,168 @@
        [Browsable(true)]
        [Description("当鼠标双击时允许自适应")]
        [DefaultValue(true)]
        public bool AllowZoomAllWhenMouseDoubleClick
        public bool AllowZoomAllWhenMouseLeftDoubleClick
        {
            get => _allowZoomAllWhenMouseDoubleClick;
            set => _allowZoomAllWhenMouseDoubleClick = value;
            get => _allowZoomAllWhenMouseLeftDoubleClick;
            set => _allowZoomAllWhenMouseLeftDoubleClick = value;
        }
        private bool _allowZoomAllWhenMouseDoubleClick = true;
        private bool _allowZoomAllWhenMouseLeftDoubleClick = true;
        /// <summary>
        /// 鼠标双击时自适应
        /// </summary>
        protected virtual bool ZoomAllWhenMouseDoubleClick(MouseEventArgs e)
        protected virtual bool ZoomAllWhenMouseLeftDoubleClick(MouseEventArgs e)
        {
            if (e.Clicks > 1)
            if (e.Button == MouseButtons.Left)
            {
                if (AllowZoomAllWhenMouseDoubleClick)
                if (e.Clicks > 1)
                {
                    ZoomAll();
                    if (this.AllowZoomAllWhenMouseLeftDoubleClick)
                    {
                        if (this.Initialized)
                        {
                            ZoomAll();
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        #endregion
        #region 鼠标悬停
        /// <summary>
        /// 允许鼠标悬停
        /// </summary>
        [Browsable(true)]
        [Description("允许鼠标悬停")]
        [DefaultValue(true)]
        public bool AllowMouseHover
        {
            get => _allowMouseHover;
            set => _allowMouseHover = value;
        }
        private bool _allowMouseHover = true;
        private List<Parter> _lastHoverList = null;//最后一次悬停列表
        /// <summary>
        /// 悬停
        /// 如果发生改变就返回true
        /// </summary>
        protected virtual bool Hover(MouseEventArgs e)
        {
            if (this.AllowMouseHover)
            {
                if (this.Initialized)
                {
                    var pt = ClientToDispPoint(e.Location);
                    var hoverList = _network.Hover(pt);
                    if (hoverList == null || hoverList.Count < 1)
                    {
                        if (_lastHoverList == null || _lastHoverList.Count < 1)
                        {
                            return false;
                        }
                    }
                    _lastHoverList = hoverList;
                    return true;
                }
            }
            if (_lastHoverList == null || _lastHoverList.Count < 1)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region 鼠标左键点击选择
        /// <summary>
        /// 允许鼠标左键单击选择
        /// </summary>
        [Browsable(true)]
        [Description("允许鼠标左键单击选择")]
        [DefaultValue(true)]
        public bool AllowMouseLeftSingleClickSelect
        {
            get => _allowMouseLeftSingleClickSelect;
            set => _allowMouseLeftSingleClickSelect = value;
        }
        private bool _allowMouseLeftSingleClickSelect = true;
        private List<Parter> _lastMouseLeftClickSelectList = null;//最后一次鼠标左键单击选择列表
        /// <summary>
        /// 当鼠标左键单击时选择
        /// </summary>
        protected virtual bool SelectWhenMouseLeftSingleClick(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Clicks == 1)
                {
                    if (this.Initialized)
                    {
                        var pt = ClientToDispPoint(e.Location);
                        var selectList = _network.Select(pt);
                        if (selectList == null || selectList.Count < 1)
                        {
                            if (_lastMouseLeftClickSelectList == null || _lastMouseLeftClickSelectList.Count < 1)
                            {
                                return false;
                            }
                        }
                        _lastMouseLeftClickSelectList = selectList;
                        return true;
                    }
                }
            }
            if (_lastMouseLeftClickSelectList == null || _lastMouseLeftClickSelectList.Count < 1)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region 鼠标滚轮滚动缩放
        /// <summary>
        /// 当鼠标滚轮滚动时允许缩放
        /// </summary>
        [Browsable(true)]
        [Description("当鼠标滚轮滚动时允许缩放")]
        [DefaultValue(true)]
        public bool AllowZoomWhenMouseWheelRoll
        {
            get => _allowZoomWhenMouseWheelRoll;
            set => _allowZoomWhenMouseWheelRoll = value;
        }
        private bool _allowZoomWhenMouseWheelRoll = true;
        /// <summary>
        /// 当鼠标滚轮滚动时缩放
        /// </summary>
        protected virtual bool ZoomWhenMouseWheelRoll(MouseEventArgs e)
        {
            if (e.Delta != 0)
            {
                if (this.AllowZoomWhenMouseWheelRoll)
                {
                    if (this.Initialized)
                    {
                        ZoomToPoint(e);
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion
@@ -444,29 +632,7 @@
        #endregion
        #region 选择
        /// <summary>
        /// 选择(通过点选择)
        /// </summary>
        protected virtual List<Parter> Select(MouseEventArgs e)
        {
            var pt = ClientToDispPoint(e.Location);
            var list = _network.Select(pt);
            return list;
        }
        /// <summary>
        /// 悬停
        /// </summary>
        protected virtual List<Parter> Hover(MouseEventArgs e)
        {
            var pt = ClientToDispPoint(e.Location);
            var list = _network.Hover(pt);
            return list;
        }
        #endregion
        /// <summary>
        /// 重绘
@@ -500,7 +666,7 @@
        {
            base.OnMouseDown(e);
            HasMouseLeftDown(e);
            HasMouseRightDown(e);
        }
        /// <summary>
@@ -509,9 +675,13 @@
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Hover(e);
            MouseLeftDownMove(e);
            this.Invalidate();
            var hoverResult = Hover(e);
            var hasMouseLeftDownMove = MouseLeftDownMove(e);
            var hasMouseRightDownMove = MouseRightDownMove(e);
            if (hoverResult || hasMouseLeftDownMove || hasMouseRightDownMove)
            {
                this.Invalidate();
            }
        }
        /// <summary>
@@ -520,17 +690,24 @@
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            HasMouseLeftUp(e);
            Select(e);
            this.Invalidate();
            var hasMouseLeftUp = HasMouseLeftUp(e);
            var hasMouseRightUp = HasMouseRightUp(e);
            var mouseLeftSingleClickSelectResult = SelectWhenMouseLeftSingleClick(e);
            if (hasMouseLeftUp || hasMouseRightUp || mouseLeftSingleClickSelectResult)
            {
                this.Invalidate();
            }
        }
        //鼠标滚轮滚动
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            ZoomToPoint(e);
            this.Refresh();
            var zoomWhenMouseWheelRollResult = ZoomWhenMouseWheelRoll(e);
            if (zoomWhenMouseWheelRollResult)
            {
                this.Refresh();
            }
        }
        /// <summary>
@@ -539,11 +716,11 @@
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (ZoomAllWhenMouseDoubleClick(e))
            var zoomAllWhenMouseLeftDoubleClickResult = ZoomAllWhenMouseLeftDoubleClick(e);
            if (zoomAllWhenMouseLeftDoubleClickResult)
            {
                this.Invalidate();
            }
        }
    }