lixiaojun
2024-11-08 571fb22bfd7ca4b0ca49328be3a3e91c0e3fed0d
WinFrmUI/Yw.WinFrmUI.Hydro.L2d.Core/01-network/02-node/Junction.cs
@@ -17,56 +17,155 @@
        public float? Height { get; set; }
        /// <summary>
        /// 边框颜色
        /// </summary>
        public Color? BorderColor { get; set; }
        /// <summary>
        /// 边框宽度
        /// </summary>
        public float? BorderWidth { get; set; }
        /// <summary>
        /// 填充颜色
        /// </summary>
        public Color? FillColor { get; set; }
        /// <summary>
        /// 悬停宽度
        /// </summary>
        public float? HoveredWidth { get; set; }
        /// <summary>
        /// 悬停高度
        /// </summary>
        public float? HoveredHeight { get; set; }
        /// <summary>
        /// 悬停填充颜色
        /// </summary>
        public Color? HoveredFillColor { get; set; }
        /// <summary>
        /// 选择宽度
        /// </summary>
        public float? SelectedWidth { get; set; }
        /// <summary>
        /// 选择高度
        /// </summary>
        public float? SelectedHeight { get; set; }
        /// <summary>
        /// 选择填充颜色
        /// </summary>
        public Color? SelectedFillColor { get; set; }
        private Coordinate _coordinate { get; set; }//坐标信息
        //获取坐标位置
        private Coordinate GetCoordinate(Graphics g)
        {
            var ps = this.Position;//原始点
            var ws = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Junction.Size.Width;//原始宽度
            var hs = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Junction.Size.Height;//原始高度
            var wz = ws / g.PageScale; //缩放宽度
            var hz = hs / g.PageScale;//缩放高度
            return Coordinate.GetCoordinate(ps, 0, new SizeF(wz, hz), StringAlignment.Center, StringAlignment.Center);
        }
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(Graphics g)
        {
            var fromCachePen = true;
            var pen = CacheHelper.JunctionBorderPen;
            if (this.BorderColor.HasValue && this.BorderWidth.HasValue)
            {
                pen = new Pen(this.BorderColor.Value, this.BorderWidth.Value);
                fromCachePen = false;
            }
            var fromCacheBrush = true;
            var brush = CacheHelper.JunctionFillBrush;
            if (this.FillColor.HasValue)
            {
                brush = new SolidBrush(this.FillColor.Value);
                fromCacheBrush = false;
            }
            _coordinate = GetCoordinate(g);
            var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Junction.Width;
            var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Junction.Height;
            var x = this.Position.X - width / 2f;
            var y = this.Position.Y - height / 2f;
            var rect = new RectangleF(x, y, width, height);
            g.DrawEllipse(pen, rect);
            g.FillEllipse(brush, rect);
            if (this.Hovered)
            {
                var fromCacheBrush = true;
                var brush = CacheHelper.JunctionHoveredFillBrush;
                if (this.HoveredFillColor.HasValue)
                {
                    brush = new SolidBrush(this.HoveredFillColor.Value);
                    fromCacheBrush = false;
                }
            if (!fromCachePen)
            {
                pen.Dispose();
                var width = this.HoveredWidth.HasValue ? this.HoveredWidth.Value : CacheHelper.HydroL2d.Junction.HoveredSize.Width;
                var height = this.HoveredHeight.HasValue ? this.HoveredHeight.Value : CacheHelper.HydroL2d.Junction.HoveredSize.Height;
                var x = this.Position.X - width / g.PageScale / 2f;
                var y = this.Position.Y - height / g.PageScale / 2f;
                var rect = new RectangleF(x, y, width / g.PageScale, height / g.PageScale);
                g.FillEllipse(brush, rect);
                if (!fromCacheBrush)
                {
                    brush.Dispose();
                }
            }
            if (!fromCacheBrush)
            else if (this.Selected)
            {
                brush.Dispose();
                var fromCacheBrush = true;
                var brush = CacheHelper.JunctionSelectedFillBrush;
                if (this.SelectedFillColor.HasValue)
                {
                    brush = new SolidBrush(this.SelectedFillColor.Value);
                    fromCacheBrush = false;
                }
                var width = this.SelectedWidth.HasValue ? this.SelectedWidth.Value : CacheHelper.HydroL2d.Junction.SelectedSize.Width;
                var height = this.SelectedHeight.HasValue ? this.SelectedHeight.Value : CacheHelper.HydroL2d.Junction.SelectedSize.Height;
                var x = this.Position.X - width / g.PageScale / 2f;
                var y = this.Position.Y - height / g.PageScale / 2f;
                var rect = new RectangleF(x, y, width / g.PageScale, height / g.PageScale);
                g.FillEllipse(brush, rect);
                if (!fromCacheBrush)
                {
                    brush.Dispose();
                }
            }
            else
            {
                var fromCacheBrush = true;
                var brush = CacheHelper.JunctionFillBrush;
                if (this.FillColor.HasValue)
                {
                    brush = new SolidBrush(this.FillColor.Value);
                    fromCacheBrush = false;
                }
                var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Junction.Size.Width;
                var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Junction.Size.Height;
                var x = this.Position.X - width / g.PageScale / 2f;
                var y = this.Position.Y - height / g.PageScale / 2f;
                var rect = new RectangleF(x, y, width / g.PageScale, height / g.PageScale);
                g.FillEllipse(brush, rect);
                if (!fromCacheBrush)
                {
                    brush.Dispose();
                }
            }
        }
        /// <summary>
        /// 包含
        /// </summary>
        public override bool Contains(PointF pt)
        {
            if (_coordinate == null)
            {
                return false;
            }
            return _coordinate.BeforeRectf.Contains(pt);
        }
        /// <summary>
        /// 相交
        /// </summary>
        public override bool Intersect(RectangleF rectf)
        {
            if (rectf.Contains(this.Position))
            {
                return true;
            }
            return false;
        }
    }
}