using MuchInfo.Chart.Infrastructure.Helpers; using MuchInfo.Chart.WPF.Primitives.Interfaces; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace MuchInfo.Chart.WPF.Controls.Drawing { /// /// ExtendableLine.xaml 的交互逻辑 /// public partial class ExtendableLine : UserControl { #region Fields private double _strokeThickness; #endregion Fields #region Constructors public ExtendableLine(IDrawingTool aTool, Chart aChart) { this._strokeThickness = 1.0; this.InitializeComponent(); this.grabLeft.Setup(aTool, 0, aChart); this.grabRight.Setup(aTool, 1, aChart); this.grabSelection.Setup(aTool, aChart); bool flag = aTool is IChannelDrawing; if (flag) { this.grabTop.Setup((IChannelDrawing)aTool, aChart); this.grabBot.Setup((IChannelDrawing)aTool, aChart); } } #endregion Constructors #region Properties #region Public Properties public Brush ChannelStroke { get { return this.lineTopTopMain.Stroke; } set { this.lineTopTopMain.Stroke = (value); this.lineTopTopLeft.Stroke = (value); this.lineTopTopRight.Stroke = (value); this.lineTopMain.Stroke = (value); this.lineTopLeft.Stroke = (value); this.lineTopRight.Stroke = (value); this.lineBotMain.Stroke = (value); this.lineBotLeft.Stroke = (value); this.lineBotRight.Stroke = (value); this.lineBotBotMain.Stroke = (value); this.lineBotBotLeft.Stroke = (value); this.lineBotBotRight.Stroke = (value); } } public Brush ExtensionStroke { get { return this.lineLeft.Stroke; } set { this.lineLeft.Stroke = (value); this.lineRight.Stroke = (value); } } public Brush Stroke { get { return this.lineMain.Stroke; } set { this.lineMain.Stroke = (value); } } public double StrokeThickness { get { return this._strokeThickness; } set { this._strokeThickness = value; this.lineMain.StrokeThickness = (this._strokeThickness); this.lineLeft.StrokeThickness = (this._strokeThickness); this.lineRight.StrokeThickness = (this._strokeThickness); this.lineTopTopMain.StrokeThickness = (value); this.lineTopTopLeft.StrokeThickness = (value); this.lineTopTopRight.StrokeThickness = (value); this.lineTopMain.StrokeThickness = (value); this.lineTopLeft.StrokeThickness = (value); this.lineTopRight.StrokeThickness = (value); this.lineBotMain.StrokeThickness = (value); this.lineBotLeft.StrokeThickness = (value); this.lineBotRight.StrokeThickness = (value); this.lineBotBotMain.StrokeThickness = (value); this.lineBotBotLeft.StrokeThickness = (value); this.lineBotBotRight.StrokeThickness = (value); } } public bool TopBottomGrabsVisible { get { return this.grabTop.Visibility == Visibility.Visible; } set { if (value) { this.grabTop.Visibility = Visibility.Visible; this.grabBot.Visibility = Visibility.Visible; } else { this.grabTop.Visibility = Visibility.Collapsed; this.grabBot.Visibility = Visibility.Collapsed; } } } #endregion Public Properties #endregion Properties #region Methods #region Public Methods public void HideBotBotLine() { this.lineBotBotLeft.Visibility = Visibility.Collapsed; this.lineBotBotMain.Visibility = Visibility.Collapsed; this.lineBotBotRight.Visibility = Visibility.Collapsed; } public void HideBotLine() { this.lineBotLeft.Visibility = Visibility.Collapsed; this.lineBotMain.Visibility = Visibility.Collapsed; this.lineBotRight.Visibility = Visibility.Collapsed; } public void HideTopLine() { this.lineTopLeft.Visibility = Visibility.Collapsed; this.lineTopMain.Visibility = Visibility.Collapsed; this.lineTopRight.Visibility = Visibility.Collapsed; } public void HideTopTopLine() { this.lineTopTopLeft.Visibility = Visibility.Collapsed; this.lineTopTopMain.Visibility = Visibility.Collapsed; this.lineTopTopRight.Visibility = Visibility.Collapsed; } public void SetBotBotPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds) { this.lineBotBotMain.Visibility = Visibility.Visible; this.lineBotBotMain.X1 = (p1.X); this.lineBotBotMain.Y1 = (p1.Y); this.lineBotBotMain.X2 = (p2.X); this.lineBotBotMain.Y2 = (p2.Y); this.MoveLeftLine(this.lineBotBotLeft, ExtendLeft, p1, p2, bounds); this.MoveRightLine(this.lineBotBotRight, ExtendRight, p1, p2, bounds); } public void SetBotPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds) { this.lineBotMain.Visibility = Visibility.Visible; this.lineBotMain.X1 = (p1.X); this.lineBotMain.Y1 = (p1.Y); this.lineBotMain.X2 = (p2.X); this.lineBotMain.Y2 = (p2.Y); this.MoveLeftLine(this.lineBotLeft, ExtendLeft, p1, p2, bounds); this.MoveRightLine(this.lineBotRight, ExtendRight, p1, p2, bounds); } public void SetChannelStrokeDash(DoubleCollection pattern) { this.lineTopTopLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineTopTopMain.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineTopTopRight.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineTopLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineTopMain.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineTopRight.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotMain.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotRight.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotBotLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotBotMain.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineBotBotRight.StrokeDashArray = (this.CloneDoubleCollection(pattern)); } public void SetExtensionStrokeDash(DoubleCollection pattern) { this.lineLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern)); this.lineRight.StrokeDashArray = (this.CloneDoubleCollection(pattern)); } public void SetMainStrokeDash(DoubleCollection pattern) { this.lineMain.StrokeDashArray = (this.CloneDoubleCollection(pattern)); } public void SetPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect aRect) { Point pointA = p1; Point pointB = p2; //TODO:更改结束点 //计算斜率 double slope = GeometryHelper.LineSlope(ref p1, ref p2); if (pointB.Y < aRect.Top) { pointB = new Point(GeometryHelper.XFromLine(p1, slope, aRect.Top), aRect.Top); } if (pointB.Y > aRect.Bottom) { pointB = new Point(GeometryHelper.XFromLine(p1, slope, aRect.Bottom), aRect.Bottom); } if (pointB.X < aRect.Left) { pointB = new Point(aRect.Left, GeometryHelper.YFromLine(p1, slope, aRect.Left)); } if (pointB.X > aRect.Width) { pointB = new Point(aRect.Width, GeometryHelper.YFromLine(p1, slope, aRect.Width)); } //TODO:更改开始点 if (pointA.Y < aRect.Top) { pointA = new Point(GeometryHelper.XFromLine(p2, slope, aRect.Top), aRect.Top); } if (pointA.Y > aRect.Bottom) { pointA = new Point(GeometryHelper.XFromLine(p2, slope, aRect.Bottom), aRect.Bottom); } if (pointA.X < aRect.Left) { pointA = new Point(aRect.Left, GeometryHelper.YFromLine(p2, slope, aRect.Left)); } if (pointA.X > aRect.Width) { pointA = new Point(aRect.Width, GeometryHelper.YFromLine(p2, slope, aRect.Width)); } this.lineMain.X1 = pointA.X; this.lineMain.Y1 = pointA.Y; this.lineMain.X2 = pointB.X; this.lineMain.Y2 = pointB.Y; ////拖动时间滑块,隐藏线 this.Visibility = Visibility.Visible; if (this.lineMain.X1.Equals(this.lineMain.X2) && (this.lineMain.X1 >= aRect.Width || this.lineMain.X1 <= 0)) { this.Visibility = Visibility.Collapsed; } else { this.grabSelection.SetPosition(pointA, pointB); this.MoveLeftLine(this.lineLeft, ExtendLeft, pointA, pointB, aRect); this.MoveRightLine(this.lineRight, ExtendRight, pointA, pointB, aRect); this.grabLeft.ShowPointGrab(); this.grabRight.ShowPointGrab(); bool flag = this.grabTop.Visibility == Visibility.Visible; if (flag) { this.grabTop.DrawingChannelGrab_2829(true); this.grabBot.DrawingChannelGrab_2829(false); } } } public void SetTopPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds) { this.lineTopMain.Visibility = Visibility.Visible; this.lineTopMain.X1 = p1.X; this.lineTopMain.Y1 = p1.Y; this.lineTopMain.X2 = p2.X; this.lineTopMain.Y2 = p2.Y; this.MoveLeftLine(this.lineTopLeft, ExtendLeft, p1, p2, bounds); this.MoveRightLine(this.lineTopRight, ExtendRight, p1, p2, bounds); } public void SetTopTopPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds) { this.lineTopTopMain.Visibility = Visibility.Visible; this.lineTopTopMain.X1 = p1.X; this.lineTopTopMain.Y1 = p1.Y; this.lineTopTopMain.X2 = p2.X; this.lineTopTopMain.Y2 = p2.Y; this.MoveLeftLine(this.lineTopTopLeft, ExtendLeft, p1, p2, bounds); this.MoveRightLine(this.lineTopTopRight, ExtendRight, p1, p2, bounds); } #endregion Public Methods #region Private Methods private DoubleCollection CloneDoubleCollection(DoubleCollection pattern) { var doubleCollection = new DoubleCollection(); if (pattern == null || pattern.Count == 0) return doubleCollection; foreach (var current in pattern) { doubleCollection.Add(current); } return doubleCollection; } private void MoveLeftLine(Line aTarget, bool visible, Point p1, Point p2, Rect bounds) { bool flag = !visible; if (flag) { aTarget.Visibility = Visibility.Collapsed; } else { Point @base = p2; Point point = p1; flag = (@base.X > point.X); if (flag) { @base = p1; point = p2; } //X相等,Y小的设为left base if (@base.X.Equals(point.X) && @base.Y > point.Y) { @base = p2; point = p1; } flag = (@base.X < bounds.Left); if (flag) { aTarget.Visibility = Visibility.Collapsed; } else { double num = GeometryHelper.LineSlope(ref @base, ref point); flag = num.Equals(1.7976931348623157E+308); if (flag) { aTarget.X1 = @base.X; aTarget.X2 = @base.X; aTarget.Y1 = bounds.Top; aTarget.Y2 = p1.Y; flag = (p2.Y < p1.Y); if (flag) { aTarget.Y2 = p2.Y; } } else { flag = num.Equals(0.0); if (flag) { aTarget.X1 = bounds.Left; aTarget.X2 = @base.X; aTarget.Y1 = @base.Y; aTarget.Y2 = @base.Y; } else { aTarget.X2 = @base.X; aTarget.Y2 = @base.Y; //计算X1, Y1 aTarget.Y1 = @base.Y > point.Y ? bounds.Height : 0; aTarget.X1 = GeometryHelper.XFromLine(@base, num, aTarget.Y1); if (aTarget.X1 < 0) { aTarget.X1 = 0; aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1); } if (aTarget.X1 > bounds.Width) { aTarget.X1 = bounds.Width; aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1); } //aTarget.X1 = bounds.Height; //aTarget.Y1 = GeometryHelper.YFromLine(@base, num, bounds.Height); } } aTarget.Visibility = Visibility.Visible; } } } private void MoveRightLine(Line aTarget, bool visible, Point p1, Point p2, Rect bounds) { bool flag = !visible; if (flag) { aTarget.Visibility = Visibility.Collapsed; } else { Point @base = p1; Point point = p2; flag = (@base.X < point.X); if (flag) { @base = p2; point = p1; } //X相等,Y大的设为right base if (@base.X.Equals(point.X) && @base.Y < point.Y) { @base = p2; point = p1; } flag = (@base.X > bounds.Right); if (flag) { aTarget.Visibility = Visibility.Collapsed; } else { double num = GeometryHelper.LineSlope(ref point, ref @base); flag = num.Equals(1.7976931348623157E+308); if (flag) { aTarget.X1 = (@base.X); aTarget.X2 = (@base.X); aTarget.Y1 = (bounds.Bottom); aTarget.Y2 = (p1.Y); flag = (p2.Y > p1.Y); if (flag) { this.lineLeft.Y2 = (p2.Y); } } else { flag = num.Equals(0.0); if (flag) { aTarget.X1 = (bounds.Right); aTarget.X2 = (@base.X); aTarget.Y1 = (@base.Y); aTarget.Y2 = (@base.Y); } else { aTarget.X2 = (@base.X); aTarget.Y2 = (@base.Y); //计算X1, Y1 aTarget.Y1 = @base.Y > point.Y ? bounds.Height : 0; aTarget.X1 = GeometryHelper.XFromLine(@base, num, aTarget.Y1); if (aTarget.X1 < 0) { aTarget.X1 = 0; aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1); } if (aTarget.X1 > bounds.Width) { aTarget.X1 = bounds.Width; aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1); } //aTarget.X1 = (bounds.Right); //aTarget.Y1 = GeometryHelper.YFromLine(@base, num, bounds.Right); } } aTarget.Visibility = Visibility.Visible; } } } #endregion Private Methods #endregion Methods } }