Drawing2PointBase.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. using MuchInfo.Chart.Data;
  2. using MuchInfo.Chart.Data.EnumTypes;
  3. using MuchInfo.Chart.Infrastructure.Controls;
  4. using MuchInfo.Chart.Infrastructure.Helpers;
  5. using MuchInfo.Chart.Infrastructure.Utilities;
  6. using MuchInfo.Chart.WPF.Controls.Editors;
  7. using MuchInfo.Chart.WPF.Helpers;
  8. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Xml.Linq;
  16. namespace MuchInfo.Chart.WPF.Primitives.Drawing
  17. {
  18. public abstract class Drawing2PointBase : IDrawingTool
  19. {
  20. #region Fields
  21. protected Point mActualP1;
  22. protected Point mActualP2;
  23. protected Point mDrawP1;
  24. protected Point mDrawP2;
  25. protected Color mColor;
  26. protected Color mExtensionColor;
  27. protected DateTime P1Date;
  28. protected DateTime P2Date;
  29. protected bool mExtendLeft;
  30. protected bool mExtendRight;
  31. protected float P1Val;
  32. protected float P2Val;
  33. protected Rect mLastRect;
  34. public ScaleLayer mLastScale = new ScaleLayer();
  35. public Chart Chart { get; set; }
  36. private IDrawingToolSpec _drawingToolSpec;
  37. private bool _plotOnAllSymbols;
  38. private double _p1X;
  39. private double _p1Y;
  40. private double _p2X;
  41. private double _p2Y;
  42. #endregion Fields
  43. #region Constructors
  44. protected Drawing2PointBase()
  45. {
  46. this.mColor = Colors.Red;
  47. this.mExtensionColor = Colors.Yellow;
  48. this.mExtendRight = false;
  49. this.mExtendLeft = false;
  50. this._plotOnAllSymbols = false;
  51. }
  52. #endregion Constructors
  53. #region Properties
  54. #region Public Properties
  55. public Color Color
  56. {
  57. get
  58. {
  59. return this.mColor;
  60. }
  61. set
  62. {
  63. bool flag = !this.mColor.Equals(value);
  64. if (flag)
  65. {
  66. this.mColor = value;
  67. this.ColorChanged();
  68. this.PropertyChanged();
  69. }
  70. }
  71. }
  72. public DateTime DrawingDate1
  73. {
  74. get
  75. {
  76. return this.P1Date;
  77. }
  78. set
  79. {
  80. this.P1Date = value;
  81. this.PropertyChanged();
  82. }
  83. }
  84. public DateTime DrawingDate2
  85. {
  86. get
  87. {
  88. return this.P2Date;
  89. }
  90. set
  91. {
  92. this.P2Date = value;
  93. this.PropertyChanged();
  94. }
  95. }
  96. public float DrawingValue1
  97. {
  98. get
  99. {
  100. return this.P1Val;
  101. }
  102. set
  103. {
  104. this.P1Val = value;
  105. this.PropertyChanged();
  106. }
  107. }
  108. public float DrawingValue2
  109. {
  110. get
  111. {
  112. return this.P2Val;
  113. }
  114. set
  115. {
  116. this.P2Val = value;
  117. this.PropertyChanged();
  118. }
  119. }
  120. public bool ExtendLeft
  121. {
  122. get
  123. {
  124. return this.mExtendLeft;
  125. }
  126. set
  127. {
  128. bool flag = this.mExtendLeft != value;
  129. if (flag)
  130. {
  131. this.mExtendLeft = value;
  132. this.PropertyChanged();
  133. this.MoveDrawingToNewLocations(this.mLastRect);
  134. }
  135. }
  136. }
  137. public bool ExtendRight
  138. {
  139. get
  140. {
  141. return this.mExtendRight;
  142. }
  143. set
  144. {
  145. bool flag = this.mExtendRight != value;
  146. if (flag)
  147. {
  148. this.mExtendRight = value;
  149. this.PropertyChanged();
  150. this.MoveDrawingToNewLocations(this.mLastRect);
  151. }
  152. }
  153. }
  154. public Color ExtensionColor
  155. {
  156. get
  157. {
  158. return this.mExtensionColor;
  159. }
  160. set
  161. {
  162. bool flag = !this.mExtensionColor.Equals(value);
  163. if (flag)
  164. {
  165. this.mExtensionColor = value;
  166. this.ColorChanged();
  167. this.PropertyChanged();
  168. }
  169. }
  170. }
  171. public IDrawingToolSpec OwnerSpec
  172. {
  173. get
  174. {
  175. return this._drawingToolSpec;
  176. }
  177. set
  178. {
  179. this._drawingToolSpec = value;
  180. }
  181. }
  182. public bool PlotOnAllSymbols
  183. {
  184. get
  185. {
  186. return this._plotOnAllSymbols;
  187. }
  188. set
  189. {
  190. bool flag = value != this._plotOnAllSymbols;
  191. if (flag)
  192. {
  193. this._plotOnAllSymbols = value;
  194. this.PropertyChanged();
  195. }
  196. }
  197. }
  198. public abstract string RootName
  199. {
  200. get;
  201. }
  202. #endregion Public Properties
  203. #endregion Properties
  204. #region Indexers
  205. public Point this[int aIndex]
  206. {
  207. get
  208. {
  209. bool flag = aIndex == 0;
  210. Point result = new Point();
  211. if (flag)
  212. {
  213. Point point = new Point(this.mDrawP1.X, this.mDrawP1.Y);
  214. result = point;
  215. }
  216. else
  217. {
  218. flag = (aIndex == 1);
  219. if (flag)
  220. {
  221. Point point = new Point(this.mDrawP2.X, this.mDrawP2.Y);
  222. result = point;
  223. }
  224. }
  225. return result;
  226. }
  227. set
  228. {
  229. // Console.WriteLine("----------------------------------------------------------------------------------------");
  230. // Console.WriteLine("----------------------------------------------------------------------------------------");
  231. // Console.WriteLine("鼠标坐标:( " + (int)value.X + "," + (int)value.Y + " )");
  232. //加上左边纵坐标位移
  233. var offset = this.Chart.GetChartPanelOffset();
  234. //Console.WriteLine("偏移量: " + (int)offset);
  235. //获取偏后一个的时间
  236. DateTime dateTime = this.Chart.DateScaler.OriginalDateFromX((float)value.X + (float)offset);
  237. //Console.WriteLine("偏后时间: " + dateTime.ToString());
  238. //获取偏后时间 index
  239. var lastindex = this.Chart.DateScaler.IndexFromDate(dateTime);
  240. //Console.WriteLine("获取偏后时间 index: " + lastindex.ToString());
  241. //获取偏前时间
  242. var fristTime = this.Chart.DateScaler.DateValue(lastindex - 1);
  243. //Console.WriteLine("获取偏前时间 index: " + fristTime.ToString());
  244. var fristint = this.Chart.DateScaler.XforDate(fristTime);
  245. //Console.WriteLine("获取偏前长度: " + fristint);
  246. var lastint = this.Chart.DateScaler.XforDate(dateTime);
  247. //Console.WriteLine("获取偏后长度: " + lastint);
  248. //Console.WriteLine("鼠标坐标: " + (int)value.X);
  249. //Console.WriteLine("+++++++++++++++++++++++++++++++++++++++");
  250. //Console.WriteLine("dateTime: " + dateTime.ToString());
  251. //P1
  252. if ((value.X - fristint) >= ((lastint - fristint) / 2))
  253. {
  254. dateTime = this.Chart.DateScaler.OriginalDateFromX((float)value.X + (float)offset);
  255. }
  256. else if ((value.X - fristint) < ((lastint - fristint) / 2))
  257. {
  258. dateTime = this.Chart.DateScaler.OriginalDateFromX((float)fristint + (float)offset);
  259. }
  260. //Console.WriteLine("+++++++++++++++++++++++++++++++++++++++");
  261. //Console.WriteLine("dateTime: " + dateTime.ToString());
  262. float num = this.mLastScale.GetRightValueScaler().ValueFromPercent((float)(1.0 - value.Y / this.mLastRect.Height));
  263. if (aIndex == 0)
  264. {
  265. this.P1Date = dateTime;
  266. this.P1Val = num;
  267. this.mDrawP1.X = (value.X);
  268. this.mDrawP1.Y = (value.Y);
  269. this.CalculateValues();
  270. this.MoveDrawingToNewLocations(this.mLastRect);
  271. }
  272. else if (aIndex == 1)
  273. {
  274. this.P2Date = dateTime;
  275. this.P2Val = num;
  276. this.mDrawP2.X = (value.X);
  277. this.mDrawP2.Y = (value.Y);
  278. this.CalculateValues();
  279. this.MoveDrawingToNewLocations(this.mLastRect);
  280. }
  281. //Console.WriteLine("------------------------------------------------------------------------");
  282. //Console.WriteLine("P1Date:" + P1Date);
  283. //Console.WriteLine("P1Val:" + (int)P1Val);
  284. //Console.WriteLine("mDrawP1: (" + (int)mDrawP1.X + "," + (int)mDrawP1.Y + ")");
  285. //Console.WriteLine("mDrawP2: (" + (int)mDrawP2.X + "," + (int)mDrawP2.Y + ")");
  286. }
  287. }
  288. #endregion Indexers
  289. #region Methods
  290. #region Public Methods
  291. public double GetChartFontSize()
  292. {
  293. return (Chart == null) ? 12f : this.Chart.ChartFontSize;
  294. }
  295. public abstract string Abbreviation();
  296. public virtual List<ControlPair> AdditionalEditors()
  297. {
  298. List<ControlPair> result = null;
  299. return result;
  300. }
  301. public DateTime aP1Date()
  302. {
  303. return this.P1Date;
  304. }
  305. public float aP1Val()
  306. {
  307. return this.P1Val;
  308. }
  309. public DateTime aP2Date()
  310. {
  311. return this.P2Date;
  312. }
  313. public float aP2Val()
  314. {
  315. return this.P2Val;
  316. }
  317. public virtual void CalculateValues()
  318. {
  319. }
  320. public virtual bool CanPlotOnAllSymbols()
  321. {
  322. return false;
  323. }
  324. public abstract IDrawingTool Clone();
  325. public void Delete()
  326. {
  327. bool flag = this._drawingToolSpec != null;
  328. if (flag)
  329. {
  330. this._drawingToolSpec.DeleteTool();
  331. }
  332. }
  333. public abstract string Description();
  334. public void FromInfo(DrawingToolInfo info)
  335. {
  336. bool hasValue = info.P1Date.HasValue;
  337. if (hasValue)
  338. {
  339. this.P1Date = info.P1Date.Value;
  340. }
  341. hasValue = info.P1Val.HasValue;
  342. if (hasValue)
  343. {
  344. this.P1Val = info.P1Val.Value;
  345. }
  346. hasValue = info.P2Date.HasValue;
  347. if (hasValue)
  348. {
  349. this.P2Date = info.P2Date.Value;
  350. }
  351. hasValue = info.P2Val.HasValue;
  352. if (hasValue)
  353. {
  354. this.P2Val = info.P2Val.Value;
  355. }
  356. hasValue = info.Color1.HasValue;
  357. if (hasValue)
  358. {
  359. this.mColor = ColorHelper.ColorFromInt(info.Color1.Value);
  360. }
  361. hasValue = info.Color2.HasValue;
  362. if (hasValue)
  363. {
  364. this.mExtensionColor = ColorHelper.ColorFromInt(info.Color2.Value);
  365. }
  366. hasValue = info.Bool1.HasValue;
  367. if (hasValue)
  368. {
  369. this.mExtendRight = info.Bool1.Value;
  370. }
  371. hasValue = info.Bool2.HasValue;
  372. if (hasValue)
  373. {
  374. this.mExtendLeft = info.Bool2.Value;
  375. }
  376. this._plotOnAllSymbols = info.DrawOnAllSymbols;
  377. this.AddFromInfo(info);
  378. }
  379. public void FromXml(XElement node)
  380. {
  381. bool flag = node.Attribute("D1") != null;
  382. if (flag)
  383. {
  384. this.P1Date = new DateTime(long.Parse(node.Attribute("D1").Value));
  385. }
  386. flag = (node.Attribute("D2") != null);
  387. if (flag)
  388. {
  389. this.P2Date = new DateTime(long.Parse(node.Attribute("D2").Value));
  390. }
  391. flag = (node.Attribute("V1") != null);
  392. if (flag)
  393. {
  394. this.P1Val = XMLHelp.ParseSingle(node.Attribute("V1").Value);
  395. }
  396. flag = (node.Attribute("V2") != null);
  397. if (flag)
  398. {
  399. this.P2Val = XMLHelp.ParseSingle(node.Attribute("V2").Value);
  400. }
  401. flag = (node.Attribute("Clr") != null);
  402. if (flag)
  403. {
  404. this.mColor = ColorHelper.ColorFromString(node.Attribute("Clr").Value);
  405. }
  406. flag = (node.Attribute("ALS") != null);
  407. if (flag)
  408. {
  409. this._plotOnAllSymbols = bool.Parse(node.Attribute("ALS").Value);
  410. }
  411. flag = (node.Attribute("EClr") != null);
  412. if (flag)
  413. {
  414. this.mExtensionColor = ColorHelper.ColorFromString(node.Attribute("EClr").Value);
  415. }
  416. flag = (node.Attribute("ER") != null);
  417. if (flag)
  418. {
  419. this.mExtendRight = true;
  420. }
  421. flag = (node.Attribute("EL") != null);
  422. if (flag)
  423. {
  424. this.mExtendLeft = true;
  425. }
  426. this.AddFromXml(node);
  427. }
  428. public void GetDrawingInfo(DrawingToolInfo info)
  429. {
  430. info.P1Date = this.P1Date;
  431. info.P1Val = this.P1Val;
  432. info.P2Date = this.P2Date;
  433. info.P2Val = this.P2Val;
  434. info.Color1 = ColorHelper.ColorToInt(this.mColor);
  435. info.Color2 = ColorHelper.ColorToInt(this.mExtensionColor);
  436. info.Bool1 = this.mExtendRight;
  437. info.Bool2 = this.mExtendLeft;
  438. info.DrawOnAllSymbols = this._plotOnAllSymbols;
  439. this.AddToInfo(info);
  440. }
  441. public List<FrameworkElement> GetPlots(Chart owner, ScaleLayer aScale, Rect aRect, bool DrawHandles)
  442. {
  443. this.Chart = owner;
  444. this.mLastScale = aScale;
  445. this.mLastRect = aRect;
  446. bool flag = !ColorHelper.ColorIsContrasting(this.mColor, ColorHelper.GetBrushColor(owner.ChartBackground));
  447. if (flag)
  448. {
  449. this.mColor = ColorHelper.InverseColor(this.mColor);
  450. }
  451. var list = new List<FrameworkElement>();
  452. IDateScaler dateScaler = owner.DateScaler;
  453. flag = this.IsTooSmallToShow(owner, aScale);
  454. List<FrameworkElement> result;
  455. if (flag)
  456. {
  457. result = list;
  458. }
  459. else
  460. {
  461. this.mDrawP1.X = ((double)dateScaler.XforDate(this.P1Date));
  462. this.mDrawP1.Y = ((double)aScale.GetRightValueScaler().ScaledY(this.P1Val, aRect));
  463. this.mDrawP2.X = ((double)dateScaler.XforDate(this.P2Date));
  464. this.mDrawP2.Y = ((double)aScale.GetRightValueScaler().ScaledY(this.P2Val, aRect));
  465. this.mActualP1 = new Point(this.mDrawP1.X, this.mDrawP1.Y);
  466. this.mActualP2 = new Point(this.mDrawP2.X, this.mDrawP2.Y);
  467. GeometryHelper.CheckLineExtents(ref this.mDrawP1, ref this.mDrawP2);
  468. this.CalculateValues();
  469. list.AddRange(this.GetDrawingPlots(owner, aScale, aRect));
  470. result = list;
  471. }
  472. return result;
  473. }
  474. public virtual List<ControlPair> GetPropertyEditors()
  475. {
  476. var list = new List<ControlPair>
  477. {
  478. new ControlPair
  479. {
  480. ControlLeft = new TextBlock {Text = LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_Color)},
  481. ControlRight = new ColorOpacityEditor(this, "Color", "", "")
  482. }
  483. };
  484. List<ControlPair> list2 = this.AdditionalEditors();
  485. if (list2 != null)
  486. {
  487. list.AddRange(list2);
  488. }
  489. var flag = (this.CanExtendLeft() || this.CanExtendRight());
  490. bool flag2;
  491. if (flag)
  492. {
  493. flag2 = this.AllowExtensionColorEdit();
  494. if (flag2)
  495. {
  496. list.Add(new ControlPair()
  497. {
  498. ControlLeft = new TextBlock() { Text = LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_ExtensionColor) },
  499. ControlRight = new ColorOpacityEditor(this, "ExtensionColor", "", "")
  500. });
  501. }
  502. var controlPair = new ControlPair();
  503. flag2 = this.CanExtendLeft();
  504. if (flag2)
  505. {
  506. controlPair.ControlLeft = new BooleanEditor(this, "ExtendLeft", LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_ExtendLeft))
  507. {
  508. HorizontalAlignment = HorizontalAlignment.Left
  509. };
  510. }
  511. flag2 = this.CanExtendRight();
  512. if (flag2)
  513. {
  514. controlPair.ControlRight = new BooleanEditor(this, "ExtendRight", LanguageManager.FindResource(LanguageConst.DrawingTool_Edit_ExtendRight))
  515. {
  516. HorizontalAlignment = HorizontalAlignment.Left
  517. };
  518. }
  519. list.Add(controlPair);
  520. }
  521. //flag2 = this.CanPlotOnAllSymbols();
  522. //if (flag2)
  523. //{
  524. // list.Add(new ControlPair()
  525. // {
  526. // ControlRight = new BooleanEditor(this, "PlotOnAllSymbols", "Draw on all Symbols")
  527. // });
  528. //}
  529. return list;
  530. }
  531. public abstract ImageSource Icon();
  532. public void InitDone(MouseEventArgs e)
  533. {
  534. }
  535. public void InitPoint(DateTime aDate, float aVal, ScaleLayer aScale, Rect aRect, Point p)
  536. {
  537. this.mLastRect = aRect;
  538. this.mLastScale = aScale;
  539. this.P1Date = aDate;
  540. this.P1Val = aVal;
  541. this.CalculateValues();
  542. }
  543. public bool IsTooSmallToShow(Chart owner, ScaleLayer aScale, DrawingToolInfo Info)
  544. {
  545. bool flag = Info == null;
  546. bool result;
  547. if (flag)
  548. {
  549. result = false;
  550. }
  551. else
  552. {
  553. long ticks = owner.CurrentTimeFrame.Ticks;
  554. long? timeFrame = Info.TimeFrame;
  555. bool? arg_71_0;
  556. if (!timeFrame.HasValue)
  557. {
  558. arg_71_0 = default(bool?);
  559. }
  560. else
  561. {
  562. bool? flag2 = new bool?(timeFrame.GetValueOrDefault() >= ticks);
  563. arg_71_0 = flag2;
  564. }
  565. bool? flag3 = arg_71_0;
  566. flag = flag3.GetValueOrDefault();
  567. if (flag)
  568. {
  569. result = false;
  570. }
  571. else
  572. {
  573. flag = (owner.LimitDrawingsBySpan && aScale.GetChartPane() != null);
  574. if (flag)
  575. {
  576. IDateScaler dateScaler = owner.DateScaler;
  577. Rect aRect = aScale.GetChartPane().LastDrawnRect();
  578. float num = dateScaler.XforDate(this.P1Date);
  579. float num2 = dateScaler.XforDate(this.P2Date);
  580. flag = ((double)Math.Abs(num - num2) > aRect.Width * (double)(owner.DrawingMinChartSpanPercent / 100f));
  581. if (flag)
  582. {
  583. result = false;
  584. }
  585. else
  586. {
  587. float num3 = aScale.GetRightValueScaler().ScaledY(this.P1Val, aRect);
  588. float num4 = aScale.GetRightValueScaler().ScaledY(this.P2Val, aRect);
  589. flag = ((double)Math.Abs(num3 - num4) > aRect.Height * (double)(owner.DrawingMinChartSpanPercent / 100f));
  590. result = !flag;
  591. }
  592. }
  593. else
  594. {
  595. result = false;
  596. }
  597. }
  598. }
  599. return result;
  600. }
  601. public bool IsTooSmallToShow(Chart owner, ScaleLayer aScale)
  602. {
  603. bool flag = !(this._drawingToolSpec is SharedDrawingToolSpec);
  604. bool result;
  605. if (flag)
  606. {
  607. result = false;
  608. }
  609. else
  610. {
  611. SharedDrawingToolSpec sharedDrawingToolSpec = (SharedDrawingToolSpec)this._drawingToolSpec;
  612. result = this.IsTooSmallToShow(owner, aScale, sharedDrawingToolSpec.GetDrawingInfo());
  613. }
  614. return result;
  615. }
  616. public abstract string MenuDescription();
  617. public void MovePlot(Point initialPosition, Point currentPosition)
  618. {
  619. double num = currentPosition.X - initialPosition.X;
  620. double num2 = currentPosition.Y - initialPosition.Y;
  621. var value = new Point(this._p1X + num, this._p1Y + num2);
  622. var value2 = new Point(this._p2X + num, this._p2Y + num2);
  623. value.X = Math.Max(0, value.X);
  624. value.Y = Math.Max(0, value.Y);
  625. value2.X = Math.Max(0, value2.X);
  626. value2.Y = Math.Max(0, value2.Y);
  627. this[0] = value;
  628. this[1] = value2;
  629. }
  630. public void MovePlot(Point initialPosition, Point currentPosition, Point maxPoint)
  631. {
  632. double num = currentPosition.X - initialPosition.X;
  633. double num2 = currentPosition.Y - initialPosition.Y;
  634. var value = new Point(this._p1X + num, this._p1Y + num2);
  635. var value2 = new Point(this._p2X + num, this._p2Y + num2);
  636. value.X = Math.Max(0, value.X);
  637. value.X = Math.Min(maxPoint.X, value.X);
  638. value.Y = Math.Max(0, value.Y);
  639. value.Y = Math.Min(maxPoint.Y, value.Y);
  640. value2.X = Math.Max(0, value2.X);
  641. value2.X = Math.Min(maxPoint.X, value2.X);
  642. value2.Y = Math.Max(0, value2.Y);
  643. value2.Y = Math.Min(maxPoint.Y, value2.Y);
  644. this[0] = value;
  645. this[1] = value2;
  646. }
  647. public void MoveSecondPoint(DateTime aDate, float aVal, Point p)
  648. {
  649. this.P2Date = aDate;
  650. this.P2Val = aVal;
  651. this.CalculateValues();
  652. }
  653. public void PrepareToMove()
  654. {
  655. this._p1X = this.mActualP1.X;
  656. this._p1Y = this.mActualP1.Y;
  657. this._p2X = this.mActualP2.X;
  658. this._p2Y = this.mActualP2.Y;
  659. }
  660. public void PropertyChanged()
  661. {
  662. bool flag = this._drawingToolSpec != null;
  663. if (flag)
  664. {
  665. this._drawingToolSpec.ToolChanged(this);
  666. }
  667. }
  668. public void ShowEditor(MouseEventArgs Mouseargs)
  669. {
  670. bool flag = this.Chart == null;
  671. if (!flag)
  672. {
  673. var drawingToolEditor = new DrawingToolEditor(this);
  674. drawingToolEditor.Show(Mouseargs);
  675. }
  676. }
  677. public abstract DrawingToolType ToolType();
  678. /// <summary>
  679. /// 直线矩形范围内截取
  680. /// </summary>
  681. /// <param name="pointA">输出起始点</param>
  682. /// <param name="pointB">输出结束点</param>
  683. /// <param name="oldPointA">原先起始点</param>
  684. /// <param name="oldPointB">原先结束点</param>
  685. /// <param name="aRect">限定范围</param>
  686. public void PointOfLineAtRectangleCalculator(ref Point pointA, ref Point pointB, Point oldPointA, Point oldPointB, Rect aRect)
  687. {
  688. pointA = oldPointA;
  689. pointB = oldPointB;
  690. //TODO:更改结束点
  691. //计算斜率
  692. double slope = GeometryHelper.LineSlope(ref oldPointA, ref oldPointB);
  693. if (pointB.Y < aRect.Top)
  694. {
  695. pointB = new Point(GeometryHelper.XFromLine(oldPointA, slope, aRect.Top), aRect.Top);
  696. }
  697. if (pointB.Y > aRect.Bottom)
  698. {
  699. pointB = new Point(GeometryHelper.XFromLine(oldPointA, slope, aRect.Bottom), aRect.Bottom);
  700. }
  701. if (pointB.X < aRect.Left)
  702. {
  703. pointB = new Point(aRect.Left, GeometryHelper.YFromLine(oldPointA, slope, aRect.Left));
  704. }
  705. if (pointB.X > aRect.Width)
  706. {
  707. pointB = new Point(aRect.Width, GeometryHelper.YFromLine(oldPointA, slope, aRect.Width));
  708. }
  709. //TODO:更改开始点
  710. if (pointA.Y < aRect.Top)
  711. {
  712. pointA = new Point(GeometryHelper.XFromLine(oldPointB, slope, aRect.Top), aRect.Top);
  713. }
  714. if (pointA.Y > aRect.Bottom)
  715. {
  716. pointA = new Point(GeometryHelper.XFromLine(oldPointB, slope, aRect.Bottom), aRect.Bottom);
  717. }
  718. if (pointA.X < aRect.Left)
  719. {
  720. pointA = new Point(aRect.Left, GeometryHelper.YFromLine(oldPointB, slope, aRect.Left));
  721. }
  722. if (pointA.X > aRect.Width)
  723. {
  724. pointA = new Point(aRect.Width, GeometryHelper.YFromLine(oldPointB, slope, aRect.Width));
  725. }
  726. }
  727. /// <summary>
  728. /// 直线矩形范围内拓展直线
  729. /// </summary>
  730. /// <param name="pointA">输出起始点</param>
  731. /// <param name="pointB">输出结束点</param>
  732. /// <param name="oldPointA">原先起始点</param>
  733. /// <param name="oldPointB">原先结束点</param>
  734. /// <param name="aRect">限定范围</param>
  735. /// <param name="Direction">以第二象限为准,延长线方向,0为向下,1为向右</param>
  736. public void PointOfLineAtRectangleExtAllCalculator(ref Point pointA, ref Point pointB, Point oldPointA, Point DirectionPoint, Rect aRect, int Direction = 0)
  737. {
  738. var flag = default(int);
  739. pointA = oldPointA;
  740. pointB = DirectionPoint;
  741. //Reg:此处的象限是以P1为坐标原点
  742. //flag 延长方向:0为上边 1为下边 2为左边 3为右边
  743. if (oldPointA.X < DirectionPoint.X && oldPointA.Y < DirectionPoint.Y) //第一象限
  744. {
  745. if (Direction == 0)
  746. {
  747. pointB.X = aRect.Width + 1;
  748. }
  749. else if (Direction == 1)
  750. {
  751. pointB.Y = -1;
  752. }
  753. }
  754. else if (oldPointA.X < DirectionPoint.X && oldPointA.Y > DirectionPoint.Y) //第二象限
  755. {
  756. if (Direction == 0)
  757. {
  758. pointB.Y = aRect.Bottom + 1;
  759. }
  760. else if (Direction == 1)
  761. {
  762. pointB.X = aRect.Width + 1;
  763. }
  764. }
  765. else if (oldPointA.X < DirectionPoint.X && oldPointA.Y < DirectionPoint.Y) //第三象限
  766. {
  767. if (Direction == 0)
  768. {
  769. pointB.X = -1;
  770. }
  771. else if (Direction == 1)
  772. {
  773. pointB.Y = aRect.Bottom + 1;
  774. }
  775. }
  776. else if (oldPointA.X < DirectionPoint.X && oldPointA.Y > DirectionPoint.Y) //第四象限
  777. {
  778. if (Direction == 0)
  779. {
  780. pointB.X = -1;
  781. }
  782. else if (Direction == 1)
  783. {
  784. pointB.Y = -1;
  785. }
  786. }
  787. //TODO:更改结束点
  788. //计算斜率
  789. double slope = GeometryHelper.LineSlope(ref oldPointA, ref DirectionPoint);
  790. if (pointB.Y < aRect.Top)
  791. {
  792. pointB = new Point(GeometryHelper.XFromLine(oldPointA, slope, aRect.Top), aRect.Top);
  793. }
  794. if (pointB.Y > aRect.Bottom)
  795. {
  796. pointB = new Point(GeometryHelper.XFromLine(oldPointA, slope, aRect.Bottom), aRect.Bottom);
  797. }
  798. if (pointB.X < aRect.Left)
  799. {
  800. pointB = new Point(aRect.Left, GeometryHelper.YFromLine(oldPointA, slope, aRect.Left));
  801. }
  802. if (pointB.X > aRect.Width)
  803. {
  804. pointB = new Point(aRect.Width, GeometryHelper.YFromLine(oldPointA, slope, aRect.Width));
  805. }
  806. }
  807. public XElement ToXml(List<object> args)
  808. {
  809. var xElement = new XElement(this.RootName);
  810. bool flag = args == null || !args.Contains("SAVEDEF");
  811. if (flag)
  812. {
  813. xElement.Add(new XAttribute("D1", this.P1Date.Ticks.ToString()));
  814. xElement.Add(new XAttribute("D2", this.P2Date.Ticks.ToString()));
  815. xElement.Add(new XAttribute("V1", XMLHelp.NumToString(this.P1Val)));
  816. xElement.Add(new XAttribute("V2", XMLHelp.NumToString(this.P2Val)));
  817. xElement.Add(new XAttribute("ALS", this._plotOnAllSymbols.ToString()));
  818. }
  819. xElement.Add(new XAttribute("Clr", this.mColor.ToString()));
  820. xElement.Add(new XAttribute("EClr", this.mExtensionColor.ToString()));
  821. flag = this.mExtendRight;
  822. if (flag)
  823. {
  824. xElement.Add(new XAttribute("ER", "1"));
  825. }
  826. flag = this.mExtendLeft;
  827. if (flag)
  828. {
  829. xElement.Add(new XAttribute("EL", "1"));
  830. }
  831. this.AddToXml(xElement, args);
  832. return xElement;
  833. }
  834. #endregion Public Methods
  835. #region Protected Methods
  836. protected virtual void AddFromInfo(DrawingToolInfo info)
  837. {
  838. }
  839. protected virtual void AddFromXml(XElement xml)
  840. {
  841. }
  842. protected virtual void AddToInfo(DrawingToolInfo info)
  843. {
  844. }
  845. protected virtual void AddToXml(XElement xml, List<object> args)
  846. {
  847. }
  848. protected virtual bool AllowExtensionColorEdit()
  849. {
  850. return true;
  851. }
  852. protected abstract bool CanExtendLeft();
  853. protected abstract bool CanExtendRight();
  854. protected abstract void ColorChanged();
  855. protected abstract List<FrameworkElement> GetDrawingPlots(Chart owner, ScaleLayer aScale, Rect aRect);
  856. protected abstract void MoveDrawingToNewLocations(Rect aRect);
  857. #endregion Protected Methods
  858. public string GetXmlForDefault()
  859. {
  860. var list = new List<object>();
  861. list.Add("SAVEDEF");
  862. return this.ToXml(list).ToString();
  863. }
  864. #endregion Methods
  865. }
  866. }