namespace Yw.WinFrmUI.HydroL2d
{
///
/// 管道
///
public class Pipe : 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; }
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);
}
///
/// 绘制
///
public override void Draw(Graphics g)
{
_coordinate = GetCoordinate(g);
if (this.Hovered)
{
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();
}
}
else if (this.Selected)
{
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();
}
}
}
///
/// 包含
///
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;
}
}
}