ExtendableLine.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using MuchInfo.Chart.Infrastructure.Helpers;
  2. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Windows.Shapes;
  7. namespace MuchInfo.Chart.WPF.Controls.Drawing
  8. {
  9. /// <summary>
  10. /// ExtendableLine.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class ExtendableLine : UserControl
  13. {
  14. #region Fields
  15. private double _strokeThickness;
  16. #endregion Fields
  17. #region Constructors
  18. public ExtendableLine(IDrawingTool aTool, Chart aChart)
  19. {
  20. this._strokeThickness = 1.0;
  21. this.InitializeComponent();
  22. this.grabLeft.Setup(aTool, 0, aChart);
  23. this.grabRight.Setup(aTool, 1, aChart);
  24. this.grabSelection.Setup(aTool, aChart);
  25. bool flag = aTool is IChannelDrawing;
  26. if (flag)
  27. {
  28. this.grabTop.Setup((IChannelDrawing)aTool, aChart);
  29. this.grabBot.Setup((IChannelDrawing)aTool, aChart);
  30. }
  31. }
  32. #endregion Constructors
  33. #region Properties
  34. #region Public Properties
  35. public Brush ChannelStroke
  36. {
  37. get
  38. {
  39. return this.lineTopTopMain.Stroke;
  40. }
  41. set
  42. {
  43. this.lineTopTopMain.Stroke = (value);
  44. this.lineTopTopLeft.Stroke = (value);
  45. this.lineTopTopRight.Stroke = (value);
  46. this.lineTopMain.Stroke = (value);
  47. this.lineTopLeft.Stroke = (value);
  48. this.lineTopRight.Stroke = (value);
  49. this.lineBotMain.Stroke = (value);
  50. this.lineBotLeft.Stroke = (value);
  51. this.lineBotRight.Stroke = (value);
  52. this.lineBotBotMain.Stroke = (value);
  53. this.lineBotBotLeft.Stroke = (value);
  54. this.lineBotBotRight.Stroke = (value);
  55. }
  56. }
  57. public Brush ExtensionStroke
  58. {
  59. get
  60. {
  61. return this.lineLeft.Stroke;
  62. }
  63. set
  64. {
  65. this.lineLeft.Stroke = (value);
  66. this.lineRight.Stroke = (value);
  67. }
  68. }
  69. public Brush Stroke
  70. {
  71. get
  72. {
  73. return this.lineMain.Stroke;
  74. }
  75. set
  76. {
  77. this.lineMain.Stroke = (value);
  78. }
  79. }
  80. public double StrokeThickness
  81. {
  82. get
  83. {
  84. return this._strokeThickness;
  85. }
  86. set
  87. {
  88. this._strokeThickness = value;
  89. this.lineMain.StrokeThickness = (this._strokeThickness);
  90. this.lineLeft.StrokeThickness = (this._strokeThickness);
  91. this.lineRight.StrokeThickness = (this._strokeThickness);
  92. this.lineTopTopMain.StrokeThickness = (value);
  93. this.lineTopTopLeft.StrokeThickness = (value);
  94. this.lineTopTopRight.StrokeThickness = (value);
  95. this.lineTopMain.StrokeThickness = (value);
  96. this.lineTopLeft.StrokeThickness = (value);
  97. this.lineTopRight.StrokeThickness = (value);
  98. this.lineBotMain.StrokeThickness = (value);
  99. this.lineBotLeft.StrokeThickness = (value);
  100. this.lineBotRight.StrokeThickness = (value);
  101. this.lineBotBotMain.StrokeThickness = (value);
  102. this.lineBotBotLeft.StrokeThickness = (value);
  103. this.lineBotBotRight.StrokeThickness = (value);
  104. }
  105. }
  106. public bool TopBottomGrabsVisible
  107. {
  108. get
  109. {
  110. return this.grabTop.Visibility == Visibility.Visible;
  111. }
  112. set
  113. {
  114. if (value)
  115. {
  116. this.grabTop.Visibility = Visibility.Visible;
  117. this.grabBot.Visibility = Visibility.Visible;
  118. }
  119. else
  120. {
  121. this.grabTop.Visibility = Visibility.Collapsed;
  122. this.grabBot.Visibility = Visibility.Collapsed;
  123. }
  124. }
  125. }
  126. #endregion Public Properties
  127. #endregion Properties
  128. #region Methods
  129. #region Public Methods
  130. public void HideBotBotLine()
  131. {
  132. this.lineBotBotLeft.Visibility = Visibility.Collapsed;
  133. this.lineBotBotMain.Visibility = Visibility.Collapsed;
  134. this.lineBotBotRight.Visibility = Visibility.Collapsed;
  135. }
  136. public void HideBotLine()
  137. {
  138. this.lineBotLeft.Visibility = Visibility.Collapsed;
  139. this.lineBotMain.Visibility = Visibility.Collapsed;
  140. this.lineBotRight.Visibility = Visibility.Collapsed;
  141. }
  142. public void HideTopLine()
  143. {
  144. this.lineTopLeft.Visibility = Visibility.Collapsed;
  145. this.lineTopMain.Visibility = Visibility.Collapsed;
  146. this.lineTopRight.Visibility = Visibility.Collapsed;
  147. }
  148. public void HideTopTopLine()
  149. {
  150. this.lineTopTopLeft.Visibility = Visibility.Collapsed;
  151. this.lineTopTopMain.Visibility = Visibility.Collapsed;
  152. this.lineTopTopRight.Visibility = Visibility.Collapsed;
  153. }
  154. public void SetBotBotPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds)
  155. {
  156. this.lineBotBotMain.Visibility = Visibility.Visible;
  157. this.lineBotBotMain.X1 = (p1.X);
  158. this.lineBotBotMain.Y1 = (p1.Y);
  159. this.lineBotBotMain.X2 = (p2.X);
  160. this.lineBotBotMain.Y2 = (p2.Y);
  161. this.MoveLeftLine(this.lineBotBotLeft, ExtendLeft, p1, p2, bounds);
  162. this.MoveRightLine(this.lineBotBotRight, ExtendRight, p1, p2, bounds);
  163. }
  164. public void SetBotPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds)
  165. {
  166. this.lineBotMain.Visibility = Visibility.Visible;
  167. this.lineBotMain.X1 = (p1.X);
  168. this.lineBotMain.Y1 = (p1.Y);
  169. this.lineBotMain.X2 = (p2.X);
  170. this.lineBotMain.Y2 = (p2.Y);
  171. this.MoveLeftLine(this.lineBotLeft, ExtendLeft, p1, p2, bounds);
  172. this.MoveRightLine(this.lineBotRight, ExtendRight, p1, p2, bounds);
  173. }
  174. public void SetChannelStrokeDash(DoubleCollection pattern)
  175. {
  176. this.lineTopTopLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  177. this.lineTopTopMain.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  178. this.lineTopTopRight.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  179. this.lineTopLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  180. this.lineTopMain.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  181. this.lineTopRight.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  182. this.lineBotLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  183. this.lineBotMain.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  184. this.lineBotRight.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  185. this.lineBotBotLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  186. this.lineBotBotMain.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  187. this.lineBotBotRight.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  188. }
  189. public void SetExtensionStrokeDash(DoubleCollection pattern)
  190. {
  191. this.lineLeft.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  192. this.lineRight.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  193. }
  194. public void SetMainStrokeDash(DoubleCollection pattern)
  195. {
  196. this.lineMain.StrokeDashArray = (this.CloneDoubleCollection(pattern));
  197. }
  198. public void SetPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect aRect)
  199. {
  200. Point pointA = p1;
  201. Point pointB = p2;
  202. //TODO:更改结束点
  203. //计算斜率
  204. double slope = GeometryHelper.LineSlope(ref p1, ref p2);
  205. if (pointB.Y < aRect.Top)
  206. {
  207. pointB = new Point(GeometryHelper.XFromLine(p1, slope, aRect.Top), aRect.Top);
  208. }
  209. if (pointB.Y > aRect.Bottom)
  210. {
  211. pointB = new Point(GeometryHelper.XFromLine(p1, slope, aRect.Bottom), aRect.Bottom);
  212. }
  213. if (pointB.X < aRect.Left)
  214. {
  215. pointB = new Point(aRect.Left, GeometryHelper.YFromLine(p1, slope, aRect.Left));
  216. }
  217. if (pointB.X > aRect.Width)
  218. {
  219. pointB = new Point(aRect.Width, GeometryHelper.YFromLine(p1, slope, aRect.Width));
  220. }
  221. //TODO:更改开始点
  222. if (pointA.Y < aRect.Top)
  223. {
  224. pointA = new Point(GeometryHelper.XFromLine(p2, slope, aRect.Top), aRect.Top);
  225. }
  226. if (pointA.Y > aRect.Bottom)
  227. {
  228. pointA = new Point(GeometryHelper.XFromLine(p2, slope, aRect.Bottom), aRect.Bottom);
  229. }
  230. if (pointA.X < aRect.Left)
  231. {
  232. pointA = new Point(aRect.Left, GeometryHelper.YFromLine(p2, slope, aRect.Left));
  233. }
  234. if (pointA.X > aRect.Width)
  235. {
  236. pointA = new Point(aRect.Width, GeometryHelper.YFromLine(p2, slope, aRect.Width));
  237. }
  238. this.lineMain.X1 = pointA.X;
  239. this.lineMain.Y1 = pointA.Y;
  240. this.lineMain.X2 = pointB.X;
  241. this.lineMain.Y2 = pointB.Y;
  242. ////拖动时间滑块,隐藏线
  243. this.Visibility = Visibility.Visible;
  244. if (this.lineMain.X1.Equals(this.lineMain.X2) && (this.lineMain.X1 >= aRect.Width || this.lineMain.X1 <= 0))
  245. {
  246. this.Visibility = Visibility.Collapsed;
  247. }
  248. else
  249. {
  250. this.grabSelection.SetPosition(pointA, pointB);
  251. this.MoveLeftLine(this.lineLeft, ExtendLeft, pointA, pointB, aRect);
  252. this.MoveRightLine(this.lineRight, ExtendRight, pointA, pointB, aRect);
  253. this.grabLeft.ShowPointGrab();
  254. this.grabRight.ShowPointGrab();
  255. bool flag = this.grabTop.Visibility == Visibility.Visible;
  256. if (flag)
  257. {
  258. this.grabTop.DrawingChannelGrab_2829(true);
  259. this.grabBot.DrawingChannelGrab_2829(false);
  260. }
  261. }
  262. }
  263. public void SetTopPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds)
  264. {
  265. this.lineTopMain.Visibility = Visibility.Visible;
  266. this.lineTopMain.X1 = p1.X;
  267. this.lineTopMain.Y1 = p1.Y;
  268. this.lineTopMain.X2 = p2.X;
  269. this.lineTopMain.Y2 = p2.Y;
  270. this.MoveLeftLine(this.lineTopLeft, ExtendLeft, p1, p2, bounds);
  271. this.MoveRightLine(this.lineTopRight, ExtendRight, p1, p2, bounds);
  272. }
  273. public void SetTopTopPosition(Point p1, Point p2, bool ExtendLeft, bool ExtendRight, Rect bounds)
  274. {
  275. this.lineTopTopMain.Visibility = Visibility.Visible;
  276. this.lineTopTopMain.X1 = p1.X;
  277. this.lineTopTopMain.Y1 = p1.Y;
  278. this.lineTopTopMain.X2 = p2.X;
  279. this.lineTopTopMain.Y2 = p2.Y;
  280. this.MoveLeftLine(this.lineTopTopLeft, ExtendLeft, p1, p2, bounds);
  281. this.MoveRightLine(this.lineTopTopRight, ExtendRight, p1, p2, bounds);
  282. }
  283. #endregion Public Methods
  284. #region Private Methods
  285. private DoubleCollection CloneDoubleCollection(DoubleCollection pattern)
  286. {
  287. var doubleCollection = new DoubleCollection();
  288. if (pattern == null || pattern.Count == 0) return doubleCollection;
  289. foreach (var current in pattern)
  290. {
  291. doubleCollection.Add(current);
  292. }
  293. return doubleCollection;
  294. }
  295. private void MoveLeftLine(Line aTarget, bool visible, Point p1, Point p2, Rect bounds)
  296. {
  297. bool flag = !visible;
  298. if (flag)
  299. {
  300. aTarget.Visibility = Visibility.Collapsed;
  301. }
  302. else
  303. {
  304. Point @base = p2;
  305. Point point = p1;
  306. flag = (@base.X > point.X);
  307. if (flag)
  308. {
  309. @base = p1;
  310. point = p2;
  311. }
  312. //X相等,Y小的设为left base
  313. if (@base.X.Equals(point.X) && @base.Y > point.Y)
  314. {
  315. @base = p2;
  316. point = p1;
  317. }
  318. flag = (@base.X < bounds.Left);
  319. if (flag)
  320. {
  321. aTarget.Visibility = Visibility.Collapsed;
  322. }
  323. else
  324. {
  325. double num = GeometryHelper.LineSlope(ref @base, ref point);
  326. flag = num.Equals(1.7976931348623157E+308);
  327. if (flag)
  328. {
  329. aTarget.X1 = @base.X;
  330. aTarget.X2 = @base.X;
  331. aTarget.Y1 = bounds.Top;
  332. aTarget.Y2 = p1.Y;
  333. flag = (p2.Y < p1.Y);
  334. if (flag)
  335. {
  336. aTarget.Y2 = p2.Y;
  337. }
  338. }
  339. else
  340. {
  341. flag = num.Equals(0.0);
  342. if (flag)
  343. {
  344. aTarget.X1 = bounds.Left;
  345. aTarget.X2 = @base.X;
  346. aTarget.Y1 = @base.Y;
  347. aTarget.Y2 = @base.Y;
  348. }
  349. else
  350. {
  351. aTarget.X2 = @base.X;
  352. aTarget.Y2 = @base.Y;
  353. //计算X1, Y1
  354. aTarget.Y1 = @base.Y > point.Y ? bounds.Height : 0;
  355. aTarget.X1 = GeometryHelper.XFromLine(@base, num, aTarget.Y1);
  356. if (aTarget.X1 < 0)
  357. {
  358. aTarget.X1 = 0;
  359. aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1);
  360. }
  361. if (aTarget.X1 > bounds.Width)
  362. {
  363. aTarget.X1 = bounds.Width;
  364. aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1);
  365. }
  366. //aTarget.X1 = bounds.Height;
  367. //aTarget.Y1 = GeometryHelper.YFromLine(@base, num, bounds.Height);
  368. }
  369. }
  370. aTarget.Visibility = Visibility.Visible;
  371. }
  372. }
  373. }
  374. private void MoveRightLine(Line aTarget, bool visible, Point p1, Point p2, Rect bounds)
  375. {
  376. bool flag = !visible;
  377. if (flag)
  378. {
  379. aTarget.Visibility = Visibility.Collapsed;
  380. }
  381. else
  382. {
  383. Point @base = p1;
  384. Point point = p2;
  385. flag = (@base.X < point.X);
  386. if (flag)
  387. {
  388. @base = p2;
  389. point = p1;
  390. }
  391. //X相等,Y大的设为right base
  392. if (@base.X.Equals(point.X) && @base.Y < point.Y)
  393. {
  394. @base = p2;
  395. point = p1;
  396. }
  397. flag = (@base.X > bounds.Right);
  398. if (flag)
  399. {
  400. aTarget.Visibility = Visibility.Collapsed;
  401. }
  402. else
  403. {
  404. double num = GeometryHelper.LineSlope(ref point, ref @base);
  405. flag = num.Equals(1.7976931348623157E+308);
  406. if (flag)
  407. {
  408. aTarget.X1 = (@base.X);
  409. aTarget.X2 = (@base.X);
  410. aTarget.Y1 = (bounds.Bottom);
  411. aTarget.Y2 = (p1.Y);
  412. flag = (p2.Y > p1.Y);
  413. if (flag)
  414. {
  415. this.lineLeft.Y2 = (p2.Y);
  416. }
  417. }
  418. else
  419. {
  420. flag = num.Equals(0.0);
  421. if (flag)
  422. {
  423. aTarget.X1 = (bounds.Right);
  424. aTarget.X2 = (@base.X);
  425. aTarget.Y1 = (@base.Y);
  426. aTarget.Y2 = (@base.Y);
  427. }
  428. else
  429. {
  430. aTarget.X2 = (@base.X);
  431. aTarget.Y2 = (@base.Y);
  432. //计算X1, Y1
  433. aTarget.Y1 = @base.Y > point.Y ? bounds.Height : 0;
  434. aTarget.X1 = GeometryHelper.XFromLine(@base, num, aTarget.Y1);
  435. if (aTarget.X1 < 0)
  436. {
  437. aTarget.X1 = 0;
  438. aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1);
  439. }
  440. if (aTarget.X1 > bounds.Width)
  441. {
  442. aTarget.X1 = bounds.Width;
  443. aTarget.Y1 = GeometryHelper.YFromLine(@base, num, aTarget.X1);
  444. }
  445. //aTarget.X1 = (bounds.Right);
  446. //aTarget.Y1 = GeometryHelper.YFromLine(@base, num, bounds.Right);
  447. }
  448. }
  449. aTarget.Visibility = Visibility.Visible;
  450. }
  451. }
  452. }
  453. #endregion Private Methods
  454. #endregion Methods
  455. }
  456. }