duheng
2024-09-23 d9b8c4b8639e5239cea8fbb88fa2ae6fa1ac706b
WinFrmUI/Yw.WinFrmUI.Hydro.L2d.Core/01-network/04-link/Pipe.cs
@@ -16,22 +16,129 @@
        public float? LineWidth { get; set; }
        /// <summary>
        /// 悬停线色
        /// </summary>
        public Color? HoveredLineColor { get; set; }
        /// <summary>
        /// 悬停线宽
        /// </summary>
        public float? HoveredLineWidth { get; set; }
        /// <summary>
        /// 选择线色
        /// </summary>
        public Color? SelectedLineColor { get; set; }
        /// <summary>
        /// 选择线宽
        /// </summary>
        public float? SelectedLineWidth { get; set; }
        private Coordinate _coordinate { get; set; }//坐标信息
        //获取坐标位置
        private Coordinate GetCoordinate(Graphics g)
        {
            var ws = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL2d.Pipe.Line.Width;
            var wz = ws / g.PageScale;
            var angle = (float)this.StartPosition.AngleClockWise(this.EndPosition);
            var xp = this.StartPosition.GetXPoint(this.EndPosition);
            var size = new SizeF(xp.X - this.StartPosition.X, 2 * wz);
            return Coordinate.GetCoordinate(this.StartPosition, angle, size, StringAlignment.Near, StringAlignment.Center);
        }
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(Graphics g)
        {
            var fromCachePen = true;
            var pen = CacheHelper.PipeLinePen;
            if (this.LineColor.HasValue && this.LineWidth.HasValue)
            _coordinate = GetCoordinate(g);
            if (this.Hovered)
            {
                pen = new Pen(this.LineColor.Value, this.LineWidth.Value);
                fromCachePen = false;
                var fromCachePen = true;
                var pen = CacheHelper.PipeHoveredLinePen;
                if (this.HoveredLineColor.HasValue && this.HoveredLineWidth.HasValue)
                {
                    pen = new Pen(this.HoveredLineColor.Value, this.HoveredLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            g.DrawLines(pen, this.Positions.ToArray());
            if (!fromCachePen)
            else if (this.Selected)
            {
                pen.Dispose();
                var fromCachePen = true;
                var pen = CacheHelper.PipeSelectedLinePen;
                if (this.SelectedLineColor.HasValue && this.SelectedLineWidth.HasValue)
                {
                    pen = new Pen(this.SelectedLineColor.Value, this.SelectedLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            else
            {
                var fromCachePen = true;
                var pen = CacheHelper.PipeLinePen;
                if (this.LineColor.HasValue && this.LineWidth.HasValue)
                {
                    pen = new Pen(this.LineColor.Value, this.LineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
        }
        /// <summary>
        /// 包含
        /// </summary>
        public override bool Contains(PointF pt)
        {
            if (_coordinate == null)
            {
                return false;
            }
            var pa = _coordinate.Origin.RotatePointF(pt, _coordinate.Angle, false);
            return _coordinate.BeforeRectf.Contains(pa);
        }
        /// <summary>
        /// 相交
        /// </summary>
        public override bool Intersect(RectangleF rectf)
        {
            var positions = this.Positions;
            foreach (var position in positions)
            {
                if (rectf.Contains(position))
                {
                    return true;
                }
            }
            return false;
        }