namespace Yw.WinFrmUI.HydroL2d { /// /// 水泵 /// public class Pump : Link { /// /// 线色 /// public Color? LineColor { get; set; } /// /// 线宽 /// public float? LineWidth { get; set; } /// /// 悬停线色 /// public Color? HoveredLineColor { get; set; } /// /// 悬停线宽 /// public float? HoveredLineWidth { get; set; } /// /// 选择线色 /// public Color? SelectedLineColor { get; set; } /// /// 选择线宽 /// public float? SelectedLineWidth { get; set; } /// /// 图片 /// public Image Image { get; set; } /// /// 宽度 /// public float? Width { get; set; } /// /// 高度 /// public float? Height { get; set; } /// /// 悬停图片 /// public Image HoveredImage { get; set; } /// /// 悬停宽度 /// public float? HoveredWidth { get; set; } /// /// 悬停高度 /// public float? HoveredHeight { get; set; } /// /// 选择图片 /// public Image SelectedImage { get; set; } /// /// 选择宽度 /// public float? SelectedWidth { get; set; } /// /// 选择高度 /// public float? SelectedHeight { get; set; } private Coordinate _coordinate { get; set; }//坐标信息 //获取坐标位置 private Coordinate GetCoordinate(Graphics g) { var ws = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Pump.Size.Width; var hs = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Pump.Size.Height; var wz = ws / g.PageScale; var hz = hs / 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 * hz); return Coordinate.GetCoordinate(this.StartPosition, angle, size, StringAlignment.Near, StringAlignment.Center); } /// /// 绘制 /// public override void Draw(Graphics g) { _coordinate = GetCoordinate(g); #region 画线 if (this.Hovered) { var fromCachePen = true; var pen = CacheHelper.PumpHoveredLinePen; if (this.HoveredLineColor.HasValue && this.HoveredLineWidth.HasValue) { pen = new Pen(this.HoveredLineColor.Value, this.HoveredLineWidth.Value); fromCachePen = false; } var penWidth = pen.Width; pen.Width = pen.Width / g.PageScale; g.DrawLine(pen, this.StartPosition, this.EndPosition); pen.Width = penWidth; if (!fromCachePen) { pen.Dispose(); } } else if (this.Selected) { var fromCachePen = true; var pen = CacheHelper.PumpSelectedLinePen; if (this.SelectedLineColor.HasValue && this.SelectedLineWidth.HasValue) { pen = new Pen(this.SelectedLineColor.Value, this.SelectedLineWidth.Value); fromCachePen = false; } var penWidth = pen.Width; pen.Width = pen.Width / g.PageScale; g.DrawLine(pen, this.StartPosition, this.EndPosition); pen.Width = penWidth; if (!fromCachePen) { pen.Dispose(); } } else { var fromCachePen = true; var pen = CacheHelper.PumpLinePen; if (this.LineColor.HasValue && this.LineWidth.HasValue) { pen = new Pen(this.LineColor.Value, this.LineWidth.Value); fromCachePen = false; } var penWidth = pen.Width; pen.Width = pen.Width / g.PageScale; g.DrawLine(pen, this.StartPosition, this.EndPosition); pen.Width = penWidth; if (!fromCachePen) { pen.Dispose(); } } #endregion #region 画图片 if (this.Hovered) { var img = CacheHelper.PumpHoveredImage; if (this.HoveredImage != null) { img = this.HoveredImage; } var width = this.HoveredWidth.HasValue ? this.HoveredWidth.Value : CacheHelper.HydroL2d.Pump.HoveredSize.Width; var height = this.HoveredHeight.HasValue ? this.HoveredHeight.Value : CacheHelper.HydroL2d.Pump.HoveredSize.Height; var isNewImage = false; if (img.Width != (int)width || img.Height != (int)height) { img = img.CloneC(width, height); isNewImage = true; } var pt = this.StartPosition.GetCenter(this.EndPosition); var dx = width / g.PageScale / 2f; var dy = height / g.PageScale / 2f; var p0 = new PointF(pt.X - dx, pt.Y - dy); g.DrawImage(img, p0); if (isNewImage) { img.Dispose(); } } else if (this.Selected) { var img = CacheHelper.PumpSelectedImage; if (this.SelectedImage != null) { img = this.SelectedImage; } var width = this.SelectedWidth.HasValue ? this.SelectedWidth.Value : CacheHelper.HydroL2d.Pump.SelectedSize.Width; var height = this.SelectedHeight.HasValue ? this.SelectedHeight.Value : CacheHelper.HydroL2d.Pump.SelectedSize.Height; var isNewImage = false; if (img.Width != (int)width || img.Height != (int)height) { img = img.CloneC(width, height); isNewImage = true; } var pt = this.StartPosition.GetCenter(this.EndPosition); var dx = width / g.PageScale / 2f; var dy = height / g.PageScale / 2f; var p0 = new PointF(pt.X - dx, pt.Y - dy); g.DrawImage(img, p0); if (isNewImage) { img.Dispose(); } } else { var img = CacheHelper.PumpImage; if (this.Image != null) { img = this.Image; } var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Pump.Size.Width; var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Pump.Size.Height; var isNewImage = false; if (img.Width != (int)width || img.Height != (int)height) { img = img.CloneC(width, height); isNewImage = true; } var pt = this.StartPosition.GetCenter(this.EndPosition); var dx = width / g.PageScale / 2f; var dy = height / g.PageScale / 2f; var p0 = new PointF(pt.X - dx, pt.Y - dy); g.DrawImage(img, p0); if (isNewImage) { img.Dispose(); } } #endregion } /// /// 包含 /// 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); } /// /// 相交 /// public override bool Intersect(RectangleF rectf) { var positions = this.Positions; foreach (var position in positions) { if (rectf.Contains(position)) { return true; } } return false; } } }